SKIA在Windows上渲染(来自HDC的SkCanvas)

时间:2015-06-17 16:30:49

标签: chromium skia

我正在尝试使用SKIA在Windows上绘图。

无论如何从HDC发起SkCanvas?

1 个答案:

答案 0 :(得分:2)

有几种方法"使用" Windows上的滑雪板。这些方法在 skia 文件夹中skia\src\views\win的SkOSWindow_win.cpp中描述。

例如:

  1. 您可以将OpenGL视口附加到窗口,因为它已在函数 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. 您可以通过执行BitBlt操作将SkCanvas的内容复制到HDC,就像在 SkOSWindow :: doPaint 中一样,

    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();
    
相关问题