Chromium Code Reviews| Index: skia/ext/skia_utils_win.cc |
| diff --git a/skia/ext/skia_utils_win.cc b/skia/ext/skia_utils_win.cc |
| index 9873373725705fe6f8e4491a54ebd56916829276..a951a26974236952051c208f831a03785a5b569f 100644 |
| --- a/skia/ext/skia_utils_win.cc |
| +++ b/skia/ext/skia_utils_win.cc |
| @@ -7,7 +7,10 @@ |
| #include <stddef.h> |
| #include <windows.h> |
| +#include "base/debug/gdi_debug_util_win.h" |
| +#include "base/win/win_util.h" |
| #include "third_party/skia/include/core/SkRect.h" |
| +#include "third_party/skia/include/core/SkSurface.h" |
| #include "third_party/skia/include/core/SkTypes.h" |
| #include "third_party/skia/include/effects/SkGradientShader.h" |
| @@ -23,6 +26,48 @@ static_assert(sizeof(RECT().right) == sizeof(SkIRect().fRight), "o7"); |
| static_assert(sizeof(RECT().bottom) == sizeof(SkIRect().fBottom), "o8"); |
| static_assert(sizeof(RECT) == sizeof(SkIRect), "o9"); |
| +void CreateBitmapHeaderWithColorDepth(LONG width, |
| + LONG height, |
| + WORD color_depth, |
| + BITMAPINFOHEADER* hdr) { |
| + // These values are shared with gfx::PlatformDevice. |
| + hdr->biSize = sizeof(BITMAPINFOHEADER); |
| + hdr->biWidth = width; |
| + hdr->biHeight = -height; // Minus means top-down bitmap. |
| + hdr->biPlanes = 1; |
| + hdr->biBitCount = color_depth; |
| + hdr->biCompression = BI_RGB; // No compression. |
| + hdr->biSizeImage = 0; |
| + hdr->biXPelsPerMeter = 1; |
| + hdr->biYPelsPerMeter = 1; |
| + hdr->biClrUsed = 0; |
| + hdr->biClrImportant = 0; |
| +} |
| + |
| +HBITMAP CreateHBitmap(int width, int height) { |
| + // CreateDIBSection fails to allocate anything if we try to create an empty |
| + // bitmap, so just create a minimal bitmap. |
| + if ((width == 0) || (height == 0)) { |
| + width = 1; |
| + height = 1; |
| + } |
| + |
| + BITMAPINFOHEADER hdr = {0}; |
| + CreateBitmapHeaderWithColorDepth(width, height, 32, &hdr); |
| + |
| + HBITMAP hbitmap = CreateDIBSection(nullptr, |
| + reinterpret_cast<BITMAPINFO*>(&hdr), |
| + 0, nullptr, nullptr, 0); |
| + |
| + // If CreateDIBSection() failed, try to get some useful information out |
| + // before we crash for post-mortem analysis. |
| + if (!hbitmap) |
| + base::debug::GDIBitmapAllocFailure(&hdr, nullptr); |
| + |
| + return hbitmap; |
| +} |
| + |
| + |
| } // namespace |
| namespace skia { |
| @@ -73,7 +118,7 @@ void InitializeDC(HDC context) { |
| SkASSERT(res != 0); |
| // As per SetStretchBltMode() documentation, SetBrushOrgEx() must be called |
| // right after. |
| - res = SetBrushOrgEx(context, 0, 0, NULL); |
| + res = SetBrushOrgEx(context, 0, 0, nullptr); |
| SkASSERT(res != 0); |
| // Sets up default orientation. |
| @@ -97,5 +142,67 @@ void InitializeDC(HDC context) { |
| SkASSERT(res != 0); |
| } |
| +SkImageInfo PrepareAllocation(HDC context, BITMAP* backing) { |
| + HBITMAP backing_handle = |
| + static_cast<HBITMAP>(GetCurrentObject(context, OBJ_BITMAP)); |
| + const size_t backing_size = sizeof(*backing); |
| + return (GetObject(backing_handle, backing_size, backing) == backing_size) |
| + ? SkImageInfo::MakeN32Premul(backing->bmWidth, backing->bmHeight) |
| + : SkImageInfo(); |
| +} |
| + |
| +sk_sp<SkSurface> MapPlatformSurface(HDC context) { |
| + BITMAP backing; |
| + const SkImageInfo size(PrepareAllocation(context, &backing)); |
| + return size.isEmpty() ? nullptr |
| + : SkSurface::MakeRasterDirect(size, backing.bmBits, |
| + backing.bmWidthBytes); |
| +} |
| + |
| +SkBitmap MapPlatformBitmap(HDC context) { |
| + BITMAP backing; |
| + const SkImageInfo size(PrepareAllocation(context, &backing)); |
| + SkBitmap bitmap; |
| + if (!size.isEmpty()) |
| + bitmap.installPixels(size, backing.bmBits, size.minRowBytes()); |
| + return bitmap; |
| +} |
| + |
| +HDC CreateOffscreenSurface(int width, int height) { |
| + HBITMAP bitmap = nullptr; |
| + |
| + // If this process doesn't have access to GDI, we'll have to use a shared |
| + // memory segment instead. |
| + if (!base::win::IsUser32AndGdi32Available()) |
| + return nullptr; |
| + |
| + bitmap = CreateHBitmap(width, height); |
| + if (!bitmap) |
| + return nullptr; |
| + |
| + HDC hdc = CreateCompatibleDC(nullptr); |
|
Peter Kasting
2016/06/22 23:43:52
Seems like we could use ScopedCreateDC here to avo
|
| + if (!hdc) |
| + return nullptr; |
| + InitializeDC(hdc); |
| + HRGN clip = CreateRectRgn(0, 0, width, height); |
| + if (SelectClipRgn(hdc, clip) == ERROR) { |
| + DeleteObject(hdc); |
| + return nullptr; |
| + } |
| + if (!DeleteObject(clip)) { |
| + DeleteObject(hdc); |
| + return nullptr; |
| + } |
| + |
| + SelectObject(hdc, bitmap); |
| + |
| + // The caller must call DeleteDC(hdc) on this object once done with it. |
| + return hdc; |
| +} |
| + |
| +void CreateBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr) { |
| + CreateBitmapHeaderWithColorDepth(width, height, 32, hdr); |
| +} |
| + |
| } // namespace skia |