Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Implement CGDataConsumer - #2177

Merged
Allen Ballway (aballway) merged 5 commits into
microsoft:packagingfrom
aballway:develop
Mar 14, 2017
Merged

Allen Ballway (aballway) merged 5 commits into
microsoft:packagingfrom
aballway:develop

Conversation

@aballway

Copy link
Copy Markdown
Contributor

Fixes #2047

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

@DHowett-MSFT Dustin L. Howett (MSFT) (DHowett-MSFT) Mar 7, 2017 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to do this, please annotate them with what callback method they implement.

{
    /* Release */
    [](){ ... }
}

@aballway Allen Ballway (aballway) Mar 7, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove empty notes.

[esc]:g/@Notes$/d

Comment thread include/CoreGraphics/CGDataConsumer.h Outdated
#import <CoreFoundation/CFData.h>

typedef struct __CGDataConsumer* CGDataConsumerRef;
typedef struct CGDataConsumer* CGDataConsumerRef;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep these __ to match the entire rest of CoreGraphics.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird! We don't need to be idiosyncrasy-compatible, though. Hmm.

Comment thread include/CoreGraphics/CGDataConsumer.h Outdated
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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: callbacks

char compareData[100]{ 0 };
EXPECT_EQ(nullptr, CGDataConsumerCreate(compareData, nullptr));

CGDataConsumerCallbacks emptyCallbacks{ nullptr, [](void* info) {} };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aaaaaaaaa RAII!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you never free these things...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's freed in the callback

__CGDataConsumer(CFURLRef url) : __CGDataConsumer((void*)CFRetain(url), &sc_CGDataConsumerCFURLCallbacks) {
}

size_t WriteData(const void* data, size_t size) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?!?!?!
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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By george, you've done it!

@DHowett-MSFT

Copy link
Copy Markdown

By george, you've blown it up!

@aballway

Copy link
Copy Markdown
Contributor Author

Rebased on top of packaging to resolve merge conflict
ping ms-jihua Jeyaraj (@msft-Jeyaram) @rajsesh-msft

@msft-Jeyaram Jeyaraj (msft-Jeyaram) left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approbamus

@aballway
Allen Ballway (aballway) merged commit 2f10e0c into microsoft:packaging Mar 14, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants