Skip to content

Commit

Permalink
Do full copy of image data before loading in side thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Avaer Kazmer committed Jul 30, 2019
1 parent 5979383 commit b4ec03b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
1 change: 0 additions & 1 deletion deps/exokit-bindings/canvascontext/include/image-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class Image : public ObjectWrap {
sk_sp<SkImage> image;
Nan::Persistent<Uint8ClampedArray> dataArray;

Nan::Persistent<ArrayBuffer> arrayBuffer;
Nan::Persistent<Function> cbFn;
bool loading;
bool hasCbFn;
Expand Down
14 changes: 6 additions & 8 deletions deps/exokit-bindings/canvascontext/src/image-context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,25 @@ unsigned int Image::GetNumChannels() {
void Image::Load(Local<ArrayBuffer> arrayBuffer, size_t byteOffset, size_t byteLength, Local<Function> cbFn) {
if (!this->loading) {
unsigned char *buffer = (unsigned char *)arrayBuffer->GetContents().Data() + byteOffset;
sk_sp<SkData> extendedData = SkData::MakeUninitialized(byteLength + 1);
memcpy((unsigned char *)extendedData->data(), buffer, byteLength);
sk_sp<SkData> data = SkData::MakeWithoutCopy(buffer, byteLength);

this->arrayBuffer.Reset(arrayBuffer);
this->cbFn.Reset(cbFn);
this->loading = true;
this->hasCbFn = !cbFn.IsEmpty();

auto loadFn = [this, buffer, byteLength]() -> void {
sk_sp<SkData> data = SkData::MakeWithoutCopy(buffer, byteLength);
auto loadFn = [this, extendedData, data]() -> void {
SkBitmap bitmap;
bool ok = DecodeDataToBitmap(data, &bitmap);

if (ok) {
bitmap.setImmutable();
this->image = SkImage::MakeFromBitmap(bitmap);
} else {
unique_ptr<char[]> svgString(new char[byteLength + 1]);
memcpy(svgString.get(), buffer, byteLength);
svgString[byteLength] = 0;
((char *)extendedData->data())[extendedData->size() - 1] = 0; // NUL-terminate

NSVGimage *svgImage = nsvgParse(svgString.get(), "px", 96);
NSVGimage *svgImage = nsvgParse((char *)extendedData->data(), "px", 96);
if (svgImage != nullptr) {
if (svgImage->width > 0 && svgImage->height > 0 && svgImage->shapes != nullptr) {
int w = svgImage->width;
Expand Down Expand Up @@ -125,7 +124,6 @@ void Image::Load(Local<ArrayBuffer> arrayBuffer, size_t byteOffset, size_t byteL
asyncResource.MakeCallback(cbFn, sizeof(argv)/sizeof(argv[0]), argv);

this->cbFn.Reset();
this->arrayBuffer.Reset();
this->error = "";
});
} else {
Expand Down

0 comments on commit b4ec03b

Please sign in to comment.