0

I'm trying to embed skia canvas into NSView and HWND to do some cross platform drawing. I'm using the class SkView comes with the skia source code, and use SkOSWindow as the window. But the window got blanked when the window is resizing. As showing below enter image description here

Here's the code used by SkWindow when resizing,

void SkWindow::resize(int width, int height, SkColorType ct) {
    if (ct == kUnknown_SkColorType)
        ct = fColorType;

    if (width != fBitmap.width() || height != fBitmap.height() || ct != fColorType) {
        fColorType = ct;
        fBitmap.allocPixels(SkImageInfo::Make(width, height,
                                              ct, kPremul_SkAlphaType));

        this->setSize(SkIntToScalar(width), SkIntToScalar(height));
        this->inval(nullptr);
    }
}

I'm very new to skia, and I can't find any document about this problem. Does anyone ever running into this problem? Any suggestion will appreciated!

4

1 回答 1

0

您可以通过单独处理实时调整大小的情况来解决它。我的猜测是,当窗口处于实时调整大小时,skia 位图后端会重新创建,但不会重新绘制。所以只需要自己做一个后端,复制到drawRect方法中的CGContext中。请参阅下面的代码。

这个解决方案并不完美。如果您知道如何优雅地解决它,请告诉我。

- (void)drawRect:(NSRect)rect {
  [super drawRect:rect];

  CGContextRef ctx =
      (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];

  if ([self inLiveResize]) {
    SkBitmap bm;
    bm.allocN32Pixels(rect.size.width, rect.size.height);
    SkCanvas canvas(bm);

    _document.draw(&canvas); // replace your drawing code

    CGImageRef img = SkCreateCGImageRef(bm);
    if (img) {
      CGRect r = CGRectMake(0, 0, bm.width(), bm.height());
      CGContextRef cg = reinterpret_cast<CGContextRef>(ctx);
      CGContextSaveGState(cg);
      CGContextTranslateCTM(cg, 0, r.size.height);
      CGContextScaleCTM(cg, 1, -1);
      CGContextDrawImage(cg, r, img);
      CGContextRestoreGState(cg);
      CGImageRelease(img);
    }

  } else {
    SkCGDrawBitmap(ctx, fWind->getBitmap(), 0, 0);
  }
}
于 2016-08-13T05:04:50.380 回答