Repository navigation
Implement CGDataConsumer - #2177
Conversation
Dustin L. Howett (MSFT) (DHowett-MSFT)
left a comment
There was a problem hiding this comment.
i am being ornery
| [](void* info, const void* data, size_t size) { | ||
| auto dataWrapper = woc::MakeAutoCF<CFDataRef>(CFDataCreateWithBytesNoCopy(nullptr, (const UInt8*)data, size, kCFAllocatorNull)); | ||
| int errorCode = 0; | ||
| CFURLWriteDataAndPropertiesToResource(static_cast<CFURLRef>(info), dataWrapper, nullptr, &errorCode); |
There was a problem hiding this comment.
This does not seem like it would append; this callback is for appending, right?
I am not completely certain I like this inline lambda callback style ;P
There was a problem hiding this comment.
If you're going to do this, please annotate them with what callback method they implement.
{
/* Release */
[](){ ... }
}There was a problem hiding this comment.
Documentation is light as to what exactly it should be doing other than "When Core Graphics is ready to send data to the consumer, your function is called. It should copy the specified number of bytes from buffer into some resource under your control—for example, a file." I took this to mean writing to the resource is the expected behavior for URL. https://developer.apple.com/reference/coregraphics/cgdataconsumerputbytescallback?language=objc
Update: talked offline, will use CFWriteStream to get appending behavior
| /** | ||
| @Status Stub | ||
| @Status Interoperable | ||
| @Notes |
There was a problem hiding this comment.
please remove empty notes.
[esc]:g/@Notes$/d
| #import <CoreFoundation/CFData.h> | ||
|
|
||
| typedef struct __CGDataConsumer* CGDataConsumerRef; | ||
| typedef struct CGDataConsumer* CGDataConsumerRef; |
There was a problem hiding this comment.
Let's keep these __ to match the entire rest of CoreGraphics.
There was a problem hiding this comment.
I've seen other cases where people have used CG...* vs CG...Ref so I thought we should keep it equivalent to the reference platform, which interestingly breaks pattern for this https://developer.apple.com/reference/coregraphics/cgdataconsumerref?language=objc
There was a problem hiding this comment.
Weird! We don't need to be idiosyncrasy-compatible, though. Hmm.
| COREGRAPHICS_EXPORT CFTypeID CGDataConsumerGetTypeID() STUB_METHOD; | ||
| COREGRAPHICS_EXPORT void CGDataConsumerRelease(CGDataConsumerRef consumer) STUB_METHOD; | ||
| COREGRAPHICS_EXPORT CGDataConsumerRef CGDataConsumerRetain(CGDataConsumerRef consumer) STUB_METHOD; | ||
| COREGRAPHICS_EXPORT CGDataConsumerRef CGDataConsumerCreate(void* info, const CGDataConsumerCallbacks* cbks); |
There was a problem hiding this comment.
nit: callbacks
| char compareData[100]{ 0 }; | ||
| EXPECT_EQ(nullptr, CGDataConsumerCreate(compareData, nullptr)); | ||
|
|
||
| CGDataConsumerCallbacks emptyCallbacks{ nullptr, [](void* info) {} }; |
There was a problem hiding this comment.
this isn't empty, it has a release callback.
| EXPECT_EQ(49, compareData[49]); | ||
| EXPECT_EQ(0, compareData[50]); | ||
|
|
||
| uint8_t* largeCompareData = (uint8_t*)calloc(100000, sizeof(uint8_t)); |
There was a problem hiding this comment.
aaaaaaaaa RAII!
There was a problem hiding this comment.
you never free these things...
There was a problem hiding this comment.
It's freed in the callback
| __CGDataConsumer(CFURLRef url) : __CGDataConsumer((void*)CFRetain(url), &sc_CGDataConsumerCFURLCallbacks) { | ||
| } | ||
|
|
||
| size_t WriteData(const void* data, size_t size) { |
There was a problem hiding this comment.
confused - how is this class meant to be used? the putbytes callback is only used here, which is only used in a private function, which is only used in test
There was a problem hiding this comment.
It's not able to be written to by end-users, only internally. It seems to be a generic interface for a data sink, where CG wants to write some image data or something out to the user, where they can specify what URL or CFData or wherever the data will go. I added this as the simplest "Write to Consumer" method, which AFAICT will only really be used by CGPDFContext and CGImageDestination if at all
|
|
||
| static const CGDataConsumerCallbacks sc_CGDataConsumerCFURLCallbacks{ // CGDataConsumerPutBytesCallback | ||
| [](void* info, const void* data, size_t size) -> size_t { | ||
| auto writeStream = woc::MakeAutoCF<CFWriteStreamRef>( |
There was a problem hiding this comment.
?!?!?!
creating a new CFWriteStream is identical to writing the data to the new file again every time...
You have to make the write stream the shared context.
|
|
||
| const char* dataToWrite = "TEST123"; | ||
| EXPECT_NE(0, _CGDataConsumerWriteData(consumer, dataToWrite, 7)); | ||
| EXPECT_NE(0, _CGDataConsumerWriteData(consumer, dataToWrite, 4)); |
There was a problem hiding this comment.
You may have to release the consumer here to make utterly certain the buffer has been flushed. It is technically allowable for the I/O system to buffer these two writes and for them to not hit the disk.
Dustin L. Howett (MSFT) (DHowett-MSFT)
left a comment
There was a problem hiding this comment.
By george, you've done it!
|
By george, you've blown it up! |
a4f4e66 to
03e4739
Compare
|
Rebased on top of packaging to resolve merge conflict |
Fixes #2047