SKIA Rendering on Windows(SkCanvas from HDC)
Asked Answered
F

1

1

I'm trying to use SKIA for drawing on Windows.

Is there anyway to initiate SkCanvas from HDC ?

Folberth answered 17/6, 2015 at 16:30 Comment(0)
S
3

There are several approaches "to use" skia on Windows. These approaches described in the SkOSWindow_win.cpp at the skia\src\views\win in your skia folder.

For example:

  1. You can attach OpenGL viewport to your window as it has been done in function SkOSWindow::attachGL

    HDC dc = GetDC((HWND)fHWND);
    if (NULL == fHGLRC) {
        fHGLRC = SkCreateWGLContext(dc, msaaSampleCount,
            kGLPreferCompatibilityProfile_SkWGLContextRequest);
        if (NULL == fHGLRC) {
            return false;
        }
        glClearStencil(0);
        glClearColor(0, 0, 0, 0);
        glStencilMask(0xffffffff);
        glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    }
    if (wglMakeCurrent(dc, (HGLRC)fHGLRC)) {
        // use DescribePixelFormat to get the stencil bit depth.
        int pixelFormat = GetPixelFormat(dc);
        PIXELFORMATDESCRIPTOR pfd;
        DescribePixelFormat(dc, pixelFormat, sizeof(pfd), &pfd);
        info->fStencilBits = pfd.cStencilBits;
    
        // Get sample count if the MSAA WGL extension is present
        SkWGLExtensions extensions;
        if (extensions.hasExtension(dc, "WGL_ARB_multisample")) {
            static const int kSampleCountAttr = SK_WGL_SAMPLES;
            extensions.getPixelFormatAttribiv(dc,
                pixelFormat,
                0,
                1,
                &kSampleCountAttr,
                &info->fSampleCount);
        }
        else {
            info->fSampleCount = 0;
        }
    
        glViewport(0, 0,
            SkScalarRoundToInt(this->width()),
            SkScalarRoundToInt(this->height()));
        return true;
    }
    
  2. You can copy contents of your SkCanvas to HDC as is has been done in SkOSWindow::doPaint, by doing BitBlt operations

    HDC hdc = (HDC)ctx;
    const SkBitmap& bitmap = this->getBitmap();
    
    BITMAPINFO bmi;
    memset(&bmi, 0, sizeof(bmi));
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = bitmap.width();
    bmi.bmiHeader.biHeight = -bitmap.height(); // top-down image
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;
    bmi.bmiHeader.biSizeImage = 0;
    
    //
    // Do the SetDIBitsToDevice.
    //
    // TODO(wjmaclean):
    //       Fix this call to handle SkBitmaps that have rowBytes != width,
    //       i.e. may have padding at the end of lines. The SkASSERT below
    //       may be ignored by builds, and the only obviously safe option
    //       seems to be to copy the bitmap to a temporary (contiguous)
    //       buffer before passing to SetDIBitsToDevice().
    SkASSERT(bitmap.width() * bitmap.bytesPerPixel() == bitmap.rowBytes());
    bitmap.lockPixels();
    int ret = SetDIBitsToDevice(hdc,
        0, 0,
        bitmap.width(), bitmap.height(),
        0, 0,
        0, bitmap.height(),
        bitmap.getPixels(),
        &bmi,
        DIB_RGB_COLORS);
    (void)ret; // we're ignoring potential failures for now.
    bitmap.unlockPixels();
    
Skirl answered 7/7, 2015 at 21:4 Comment(2)
I think a solution 2 is best way.Renell
This depends on what you want to get. #2 produces predictable render result and more robust, but not using OpenGL and therefore significant slower.Skirl

© 2022 - 2024 — McMap. All rights reserved.