Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
278 changes: 46 additions & 232 deletions packages/canvas/ImageAsset/index.ts

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions packages/canvas/platforms/ios/src/cpp/AsyncCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ struct AsyncCallback {
if (isPrepared_) {
ALooper_removeFd(looper_, fd_[0]);
close(fd_[0]);
close(fd_[1]);
fd_[0] = -1;
fd_[1] = -1;
ALooper_release(looper_);
Expand Down Expand Up @@ -160,6 +161,7 @@ struct AsyncCallback {
}
ALooper_removeFd(looper_, fd_[0]);
close(fd_[0]);
close(fd_[1]);
ALooper_release(looper_);
callback_.Reset();
}
Expand Down
22 changes: 22 additions & 0 deletions packages/canvas/platforms/ios/src/cpp/ImageAssetImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ v8::Local<v8::FunctionTemplate> ImageAssetImpl::GetCtor(v8::Isolate *isolate) {
tmpl->Set(
ConvertToV8String(isolate, "__getRef"),
v8::FunctionTemplate::New(isolate, GetReference));

tmpl->Set(
ConvertToV8String(isolate, "__syncExternalMemory"),
v8::FunctionTemplate::New(isolate, SyncExternalMemory));



Expand Down Expand Up @@ -199,6 +203,20 @@ ImageAssetImpl::GetReference(const v8::FunctionCallbackInfo<v8::Value> &args) {
args.GetReturnValue().SetEmptyString();
}

void ImageAssetImpl::UpdateExternalMemory(v8::Isolate *isolate) {
auto asset = this->GetImageAsset();
auto bytes = (int64_t) canvas_native_image_asset_width(asset) *
(int64_t) canvas_native_image_asset_height(asset) * 4;
SetExternalMemory(isolate, bytes);
}

void ImageAssetImpl::SyncExternalMemory(const v8::FunctionCallbackInfo<v8::Value> &args) {
auto ptr = GetPointer(args.This());
if (ptr != nullptr) {
ptr->UpdateExternalMemory(args.GetIsolate());
}
}

void
ImageAssetImpl::GetError(v8::Local<v8::Name> name,
const v8::PropertyCallbackInfo<v8::Value> &info) {
Expand All @@ -223,6 +241,7 @@ void ImageAssetImpl::FromUrlSync(const v8::FunctionCallbackInfo<v8::Value> &args
auto url = ConvertFromV8String(isolate, args[0]);

auto done = canvas_native_image_asset_load_from_url(ptr->GetImageAsset(), url.c_str());
ptr->UpdateExternalMemory(isolate);

args.GetReturnValue().Set(done);
}
Expand Down Expand Up @@ -393,6 +412,7 @@ void ImageAssetImpl::FromFileSync(const v8::FunctionCallbackInfo<v8::Value> &arg
auto path = ConvertFromV8String(isolate, args[0]);

auto done = canvas_native_image_asset_load_from_path(ptr->GetImageAsset(), path.c_str());
ptr->UpdateExternalMemory(isolate);

args.GetReturnValue().Set(done);
}
Expand Down Expand Up @@ -526,6 +546,7 @@ void ImageAssetImpl::FromBytesSync(const v8::FunctionCallbackInfo<v8::Value> &ar
done = canvas_native_image_asset_load_from_raw(ptr->GetImageAsset(), width, height, bytes.data, bytes.size);
}
}
ptr->UpdateExternalMemory(args.GetIsolate());

args.GetReturnValue().Set(done);
return;
Expand Down Expand Up @@ -664,6 +685,7 @@ void ImageAssetImpl::FromEncodedBytesSync(const v8::FunctionCallbackInfo<v8::Val
if (bytes) {
auto done = canvas_native_image_asset_load_from_raw_encoded(ptr->GetImageAsset(), bytes.data,
bytes.size);
ptr->UpdateExternalMemory(args.GetIsolate());

args.GetReturnValue().Set(done);
return;
Expand Down
4 changes: 4 additions & 0 deletions packages/canvas/platforms/ios/src/cpp/ImageAssetImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class ImageAssetImpl : ObjectWrapperImpl {

static void GetReference(const v8::FunctionCallbackInfo<v8::Value> &args);

static void SyncExternalMemory(const v8::FunctionCallbackInfo<v8::Value> &args);

void UpdateExternalMemory(v8::Isolate *isolate);

static void GetError(v8::Local<v8::Name> name,
const v8::PropertyCallbackInfo<v8::Value> &info);

Expand Down
4 changes: 4 additions & 0 deletions packages/canvas/platforms/ios/src/cpp/ImageBitmapImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class ImageBitmapImpl : public ObjectWrapperImpl {

impl->BindFinalizer(isolate, object);

auto bitmap = static_cast<ImageBitmapImpl *>(impl)->GetImageAsset();
impl->SetExternalMemory(isolate, (int64_t) canvas_native_image_asset_width(bitmap) *
(int64_t) canvas_native_image_asset_height(bitmap) * 4);

return scope.Escape(object);
}

Expand Down
1 change: 1 addition & 0 deletions packages/canvas/platforms/ios/src/cpp/JSICallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ struct JSICallback {
~JSICallback() {
ALooper_removeFd(looper_, fd_[0]);
close(fd_[0]);
close(fd_[1]);
ALooper_release(looper_);
auto callback = callback_.get();
if(callback != nullptr){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ struct JSIReadFileCallback {
~JSIReadFileCallback() {
ALooper_removeFd(looper_, fd_[0]);
close(fd_[0]);
close(fd_[1]);
ALooper_release(looper_);
callback_.Reset();
canvas_native_u8_buffer_release(data_);
Expand Down
14 changes: 14 additions & 0 deletions packages/canvas/platforms/ios/src/cpp/ObjectWrapperImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "Common.h"
#include "NativeType.h"
#include "include/v8-external-memory-accounter.h"

class ObjectWrapperImpl {
public:
Expand All @@ -26,9 +27,20 @@ class ObjectWrapperImpl {
static void Finalizer(const v8::WeakCallbackInfo<ObjectWrapperImpl> &data) {
auto *pThis = data.GetParameter();
pThis->weakHandle_.Reset();
pThis->SetExternalMemory(data.GetIsolate(), 0);
delete pThis;
}

// Native bytes V8 should count against this wrapper.
void SetExternalMemory(v8::Isolate *isolate, int64_t bytes) {
auto delta = bytes - externalBytes_;
if (delta == 0) {
return;
}
externalMemory_.Update(isolate, delta);
externalBytes_ = bytes;
}

void BindFinalizer(v8::Isolate *isolate, const v8::Local<v8::Object> &object) {
v8::HandleScope scopedHandle(isolate);
weakHandle_.Reset(isolate, object);
Expand Down Expand Up @@ -85,6 +97,8 @@ class ObjectWrapperImpl {

private:
v8::Global<v8::Object> weakHandle_;
v8::ExternalMemoryAccounter externalMemory_;
int64_t externalBytes_ = 0;

};

Expand Down
2 changes: 2 additions & 0 deletions packages/canvas/platforms/ios/src/cpp/PromiseCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ struct PromiseCallback {
if (isPrepared_) {
ALooper_removeFd(looper_, fd_[0]);
close(fd_[0]);
close(fd_[1]);
fd_[0] = -1;
fd_[1] = -1;
ALooper_release(looper_);
Expand Down Expand Up @@ -178,6 +179,7 @@ struct PromiseCallback {
}
ALooper_removeFd(looper_, fd_[0]);
close(fd_[0]);
close(fd_[1]);
ALooper_release(looper_);
callback_.Reset();
}
Expand Down
Loading