Skip to content

Commit

Permalink
Merge pull request #601 from gree/fix/osx_takesnapshot
Browse files Browse the repository at this point in the history
fixed osx WebView.mm not for avoiding race condition about bitmap.
  • Loading branch information
KojiNakamaru authored Oct 14, 2020
2 parents ac144eb + 95319c2 commit a14c82a
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions plugins/Mac/Sources/WebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -614,37 +614,34 @@ - (void)update:(BOOL)refreshBitmap
bytesPerRow:(4 * rect.size.width)
bitsPerPixel:32];
// bitmap = [webView bitmapImageRepForCachingDisplayInRect:webView.frame];
needsDisplay = true;
}
inRendering = YES;
if (window != nil) {
memset([bitmap bitmapData], 128, [bitmap bytesPerRow] * [bitmap pixelsHigh]);
needsDisplay = true;
inRendering = NO;
} else {
NSBitmapImageRep *bmpRep = bitmap;
// memset([bitmap bitmapData], 0, [bitmap bytesPerRow] * [bitmap pixelsHigh]);
// [webView cacheDisplayInRect:webView.frame toBitmapImageRep:bitmap];
WKWebView *wkv = webView;
WKSnapshotConfiguration *cfg = [WKSnapshotConfiguration new];
//cfg.snapshotWidth = [NSNumber numberWithLong:[bitmap pixelsWide]];
//cfg.rect = webView.frame;
[wkv takeSnapshotWithConfiguration:cfg
[webView takeSnapshotWithConfiguration:[WKSnapshotConfiguration new]
completionHandler:^(NSImage *nsImg, NSError *err) {
if (err == nil) {
NSGraphicsContext *ctx = [NSGraphicsContext graphicsContextWithBitmapImageRep:bmpRep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:ctx];
[nsImg drawAtPoint:CGPointZero
fromRect:CGRectMake(0, 0, [bmpRep pixelsWide], [bmpRep pixelsHigh])
operation:NSCompositingOperationCopy
fraction:1.0];
[[NSGraphicsContext currentContext] flushGraphics];
[NSGraphicsContext restoreGraphicsState];
@synchronized(self) {
if (err == nil) {
NSGraphicsContext *ctx = [NSGraphicsContext graphicsContextWithBitmapImageRep:self->bitmap];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:ctx];
[nsImg drawAtPoint:CGPointZero
fromRect:CGRectMake(0, 0, [self->bitmap pixelsWide], [self->bitmap pixelsHigh])
operation:NSCompositingOperationCopy
fraction:1.0];
[[NSGraphicsContext currentContext] flushGraphics];
[NSGraphicsContext restoreGraphicsState];
}
self->needsDisplay = true;
self->inRendering = NO;
}
self->inRendering = NO;
}];
}
}
needsDisplay = refreshBitmap;
}
}

Expand Down Expand Up @@ -698,6 +695,7 @@ - (void)render
glPixelStorei(GL_UNPACK_ROW_LENGTH, rowLength);
glPixelStorei(GL_UNPACK_ALIGNMENT, unpackAlign);
}
needsDisplay = false;
}
}

Expand Down

0 comments on commit a14c82a

Please sign in to comment.