Skip to content

Commit 63084b7

Browse files
committed
Avoid extra copy of image data when decoding
1 parent 96fadbd commit 63084b7

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

packages/webgpu/apple/ApplePlatformContext.mm

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,15 @@ void checkIfUsingSimulatorWithAPIValidation() {
8484

8585
ImageData ApplePlatformContext::createImageBitmapFromData(
8686
std::span<const uint8_t> data) {
87-
NSData *nsData = [NSData dataWithBytes:data.data()
88-
length:data.size()];
87+
// This avoids a copy by assuming the UIImage/NSImage constructors
88+
// decode `nsData` eagerly before the memory for the wrapped `data`
89+
// is freed.
90+
//
91+
// Since we get the `CGImageRef` from `image` and then throw
92+
// it away, that's a fairly safe assumption.
93+
NSData *nsData = [NSData dataWithBytesNoCopy:const_cast<uint8_t *>(data.data())
94+
length:data.size()
95+
freeWhenDone:NO];
8996

9097
#if !TARGET_OS_OSX
9198
UIImage *image = [UIImage imageWithData:nsData];

0 commit comments

Comments
 (0)