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!

Zhiqiang Li
  • 401
  • 7
  • 16

1 Answers1

0

You may solve it by dealing with the case of live resize separately. My guess is that when the window is in live resize, the skia bitmap backend is re-created, but not re-drawn. So just make a backend by ourselves, and copy it to CGContext in the drawRect method. See the code below.

This solution is not perfect. If you know how to solve it elegantly, please let me know.

- (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);
  }
}
robin33n
  • 1
  • 1