Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Unified Diff: skia/ext/skia_utils_win.cc

Issue 1492353002: Consolidate Windows bitmap and DC code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@move-printing-dc-from-device
Patch Set: Fix bugs, extend comments, remove unused parameter Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..3db97dcd18b81754afe96939f8775e986a4123ac 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,8 +26,42 @@ 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");
-} // namespace
+HBITMAP CreateHBitmap(int width, int height) {
+ // CreateDIBSection appears to get unhappy if we create an empty bitmap, so
Peter Kasting 2016/02/11 00:19:25 What does "appears to get unhappy" mean? Be speci
tomhudson 2016/02/16 17:47:54 This comment comes verbatim out of skia/ext/bitmap
Peter Kasting 2016/02/16 21:12:09 Can you try a quick runtime test to see what happe
tomhudson 2016/06/21 22:20:22 Under 64b Windows 7, if I write HBITMAP hb = Cre
Peter Kasting 2016/06/22 02:23:33 Yes, please update the comment to be clearer about
+ // just create a minimal bitmap
Peter Kasting 2016/02/11 00:19:26 Nit: Trailing period
tomhudson 2016/02/16 17:47:54 Done.
+ if ((width == 0) || (height == 0)) {
+ width = 1;
+ height = 1;
+ }
Peter Kasting 2016/02/11 00:19:25 Nit: Do we really want to force e.g. (30, 0) to (1
tomhudson 2016/02/16 17:47:54 https://codereview.chromium.org/9459 changed it fr
+
+ BITMAPINFOHEADER hdr = {0};
+ hdr.biSize = sizeof(BITMAPINFOHEADER);
+ hdr.biWidth = width;
+ hdr.biHeight = -height; // minus means top-down bitmap
+ hdr.biPlanes = 1;
+ hdr.biBitCount = 32;
+ hdr.biCompression = BI_RGB; // no compression
+ hdr.biSizeImage = 0;
+ hdr.biXPelsPerMeter = 1;
+ hdr.biYPelsPerMeter = 1;
+ hdr.biClrUsed = 0;
+ hdr.biClrImportant = 0;
Peter Kasting 2016/02/11 00:19:26 Don't do all this manually; use gfx::CreateBitmapH
tomhudson 2016/02/16 17:47:54 Dependency is in the wrong direction? gfx depends
Peter Kasting 2016/02/16 21:12:09 Sounds like we should go ahead and move that funct
tomhudson 2016/06/21 22:20:22 Done.
+
+ HBITMAP hbitmap = CreateDIBSection(NULL, reinterpret_cast<BITMAPINFO*>(&hdr),
+ 0, NULL, NULL, 0);
+
+#if !defined(_WIN64)
Peter Kasting 2016/02/11 00:19:25 Why is this ifdefed?
tomhudson 2016/02/16 17:47:54 cpu@ added that to try to isolate reasons for beta
Peter Kasting 2016/02/16 21:12:09 Can you ping Carlos and try to find out why this i
cpu_(ooo_6.6-7.5) 2016/06/12 19:13:16 Looking at the code I use windows's toohelp which
+ // If this call fails, we're gonna crash hard. Try to get some useful
Peter Kasting 2016/02/11 00:19:26 Which call? The call above? And why/where will w
tomhudson 2016/02/16 17:47:54 Acknowledged. Another legacy comment. Changing to
+ // information out before we crash for post-mortem analysis.
+ if (!hbitmap)
+ base::debug::GDIBitmapAllocFailure(&hdr, NULL);
+#endif
+
+ return hbitmap;
+}
+
+} // namespace
namespace skia {
POINT SkPointToPOINT(const SkPoint& point) {
@@ -97,5 +134,61 @@ void InitializeDC(HDC context) {
SkASSERT(res != 0);
}
+SkSurface* MapPlatformSurface(HDC context) {
+ HBITMAP backing_handle = (HBITMAP)GetCurrentObject(context, OBJ_BITMAP);
Peter Kasting 2016/02/11 00:19:25 No C-style casts (2 places)
tomhudson 2016/02/16 17:47:53 Done.
+ BITMAP backing;
+ int byte_count = GetObject(backing_handle, sizeof(backing),
+ &backing);
+ if (byte_count != sizeof(backing))
Peter Kasting 2016/02/11 00:19:25 Nit: Just inline this test above to remove the tem
tomhudson 2016/02/16 17:47:54 Done. * Is there something I'm missing in the sty
+ return nullptr;
+ SkImageInfo size = SkImageInfo::MakeN32Premul(backing.bmWidth,
+ backing.bmHeight);
+ return SkSurface::NewRasterDirect(size, backing.bmBits,
+ backing.bmWidthBytes);
Peter Kasting 2016/02/11 00:19:25 Nit: Shorter: return SkSurface::NewRasterDirect
tomhudson 2016/02/16 17:47:54 Done.
+}
+
+SkBitmap MapPlatformBitmap(HDC context) {
+ HBITMAP backing_handle = (HBITMAP)GetCurrentObject(context, OBJ_BITMAP);
+ BITMAP backing;
+ SkBitmap bitmap;
+ int byte_count = GetObject(backing_handle, sizeof(backing),
+ &backing);
+ if (byte_count != sizeof(backing))
+ return bitmap;
+ SkImageInfo size = SkImageInfo::MakeN32Premul(backing.bmWidth,
+ backing.bmHeight);
+ bitmap.installPixels(size, backing.bmBits, size.minRowBytes());
Peter Kasting 2016/02/11 00:19:26 Everything about this function above this line is
tomhudson 2016/02/16 17:47:54 I'm not convinced that a helper function is right
Peter Kasting 2016/02/16 21:12:09 Assuming you don't need to be able to allocate emp
tomhudson 2016/06/21 22:20:22 ...
+ return bitmap;
+}
+
+HDC CreateOffscreenSurface(int width, int height) {
+
Peter Kasting 2016/02/11 00:19:26 Nit: Remove this blank line
tomhudson 2016/02/16 17:47:54 Done.
+ HBITMAP bitmap = nullptr;
Peter Kasting 2016/02/11 00:19:26 Your code seems to mix NULL and nullptr without ap
tomhudson 2016/02/16 17:47:54 Done.
+
+ // If this process doesn't have access to GDI, we'll have to use a shared
+ // memory segment instead.
+ if (!base::win::IsUser32AndGdi32Available())
+ return NULL;
+
+ bitmap = CreateHBitmap(width, height);
+ if (!bitmap)
+ return NULL;
+
+ HDC hdc = CreateCompatibleDC(NULL);
+ SkASSERT(hdc);
+ InitializeDC(hdc);
+ HRGN clip = CreateRectRgn(0, 0, width, height);
+ int result = SelectClipRgn(hdc, clip);
+ SkASSERT(result != ERROR);
+ result = DeleteObject(clip);
+ SkASSERT(result);
+
+ SelectObject(hdc, bitmap);
+
+ // need to clean up by calling DeleteDC(hdc) else we leak!
Peter Kasting 2016/02/11 00:19:26 So why aren't we doing that?
tomhudson 2016/02/16 17:47:54 Returning HDC is the entire point of this function
Peter Kasting 2016/02/16 21:12:09 In that case, I would change this to: // The ca
tomhudson 2016/06/21 22:20:22 Done.
+
+ return hdc;
+}
+
} // namespace skia

Powered by Google App Engine
This is Rietveld 408576698