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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "skia/ext/skia_utils_win.h" 5 #include "skia/ext/skia_utils_win.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <windows.h> 8 #include <windows.h>
9 9
10 #include "base/debug/gdi_debug_util_win.h"
11 #include "base/win/win_util.h"
10 #include "third_party/skia/include/core/SkRect.h" 12 #include "third_party/skia/include/core/SkRect.h"
13 #include "third_party/skia/include/core/SkSurface.h"
11 #include "third_party/skia/include/core/SkTypes.h" 14 #include "third_party/skia/include/core/SkTypes.h"
12 #include "third_party/skia/include/effects/SkGradientShader.h" 15 #include "third_party/skia/include/effects/SkGradientShader.h"
13 16
14 namespace { 17 namespace {
15 18
16 static_assert(offsetof(RECT, left) == offsetof(SkIRect, fLeft), "o1"); 19 static_assert(offsetof(RECT, left) == offsetof(SkIRect, fLeft), "o1");
17 static_assert(offsetof(RECT, top) == offsetof(SkIRect, fTop), "o2"); 20 static_assert(offsetof(RECT, top) == offsetof(SkIRect, fTop), "o2");
18 static_assert(offsetof(RECT, right) == offsetof(SkIRect, fRight), "o3"); 21 static_assert(offsetof(RECT, right) == offsetof(SkIRect, fRight), "o3");
19 static_assert(offsetof(RECT, bottom) == offsetof(SkIRect, fBottom), "o4"); 22 static_assert(offsetof(RECT, bottom) == offsetof(SkIRect, fBottom), "o4");
20 static_assert(sizeof(RECT().left) == sizeof(SkIRect().fLeft), "o5"); 23 static_assert(sizeof(RECT().left) == sizeof(SkIRect().fLeft), "o5");
21 static_assert(sizeof(RECT().top) == sizeof(SkIRect().fTop), "o6"); 24 static_assert(sizeof(RECT().top) == sizeof(SkIRect().fTop), "o6");
22 static_assert(sizeof(RECT().right) == sizeof(SkIRect().fRight), "o7"); 25 static_assert(sizeof(RECT().right) == sizeof(SkIRect().fRight), "o7");
23 static_assert(sizeof(RECT().bottom) == sizeof(SkIRect().fBottom), "o8"); 26 static_assert(sizeof(RECT().bottom) == sizeof(SkIRect().fBottom), "o8");
24 static_assert(sizeof(RECT) == sizeof(SkIRect), "o9"); 27 static_assert(sizeof(RECT) == sizeof(SkIRect), "o9");
25 28
29 HBITMAP CreateHBitmap(int width, int height) {
30 // 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
31 // just create a minimal bitmap
Peter Kasting 2016/02/11 00:19:26 Nit: Trailing period
tomhudson 2016/02/16 17:47:54 Done.
32 if ((width == 0) || (height == 0)) {
33 width = 1;
34 height = 1;
35 }
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
36
37 BITMAPINFOHEADER hdr = {0};
38 hdr.biSize = sizeof(BITMAPINFOHEADER);
39 hdr.biWidth = width;
40 hdr.biHeight = -height; // minus means top-down bitmap
41 hdr.biPlanes = 1;
42 hdr.biBitCount = 32;
43 hdr.biCompression = BI_RGB; // no compression
44 hdr.biSizeImage = 0;
45 hdr.biXPelsPerMeter = 1;
46 hdr.biYPelsPerMeter = 1;
47 hdr.biClrUsed = 0;
48 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.
49
50 HBITMAP hbitmap = CreateDIBSection(NULL, reinterpret_cast<BITMAPINFO*>(&hdr),
51 0, NULL, NULL, 0);
52
53 #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
54 // 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
55 // information out before we crash for post-mortem analysis.
56 if (!hbitmap)
57 base::debug::GDIBitmapAllocFailure(&hdr, NULL);
58 #endif
59
60 return hbitmap;
61 }
62
63
26 } // namespace 64 } // namespace
27
28 namespace skia { 65 namespace skia {
29 66
30 POINT SkPointToPOINT(const SkPoint& point) { 67 POINT SkPointToPOINT(const SkPoint& point) {
31 POINT win_point = { 68 POINT win_point = {
32 SkScalarRoundToInt(point.fX), SkScalarRoundToInt(point.fY) 69 SkScalarRoundToInt(point.fX), SkScalarRoundToInt(point.fY)
33 }; 70 };
34 return win_point; 71 return win_point;
35 } 72 }
36 73
37 SkRect RECTToSkRect(const RECT& rect) { 74 SkRect RECTToSkRect(const RECT& rect) {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 res = SetDCPenColor(context, RGB(0, 0, 0)); 127 res = SetDCPenColor(context, RGB(0, 0, 0));
91 SkASSERT(res != CLR_INVALID); 128 SkASSERT(res != CLR_INVALID);
92 129
93 // Sets up default transparency. 130 // Sets up default transparency.
94 res = SetBkMode(context, OPAQUE); 131 res = SetBkMode(context, OPAQUE);
95 SkASSERT(res != 0); 132 SkASSERT(res != 0);
96 res = SetROP2(context, R2_COPYPEN); 133 res = SetROP2(context, R2_COPYPEN);
97 SkASSERT(res != 0); 134 SkASSERT(res != 0);
98 } 135 }
99 136
137 SkSurface* MapPlatformSurface(HDC context) {
138 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.
139 BITMAP backing;
140 int byte_count = GetObject(backing_handle, sizeof(backing),
141 &backing);
142 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
143 return nullptr;
144 SkImageInfo size = SkImageInfo::MakeN32Premul(backing.bmWidth,
145 backing.bmHeight);
146 return SkSurface::NewRasterDirect(size, backing.bmBits,
147 backing.bmWidthBytes);
Peter Kasting 2016/02/11 00:19:25 Nit: Shorter: return SkSurface::NewRasterDirect
tomhudson 2016/02/16 17:47:54 Done.
148 }
149
150 SkBitmap MapPlatformBitmap(HDC context) {
151 HBITMAP backing_handle = (HBITMAP)GetCurrentObject(context, OBJ_BITMAP);
152 BITMAP backing;
153 SkBitmap bitmap;
154 int byte_count = GetObject(backing_handle, sizeof(backing),
155 &backing);
156 if (byte_count != sizeof(backing))
157 return bitmap;
158 SkImageInfo size = SkImageInfo::MakeN32Premul(backing.bmWidth,
159 backing.bmHeight);
160 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 ...
161 return bitmap;
162 }
163
164 HDC CreateOffscreenSurface(int width, int height) {
165
Peter Kasting 2016/02/11 00:19:26 Nit: Remove this blank line
tomhudson 2016/02/16 17:47:54 Done.
166 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.
167
168 // If this process doesn't have access to GDI, we'll have to use a shared
169 // memory segment instead.
170 if (!base::win::IsUser32AndGdi32Available())
171 return NULL;
172
173 bitmap = CreateHBitmap(width, height);
174 if (!bitmap)
175 return NULL;
176
177 HDC hdc = CreateCompatibleDC(NULL);
178 SkASSERT(hdc);
179 InitializeDC(hdc);
180 HRGN clip = CreateRectRgn(0, 0, width, height);
181 int result = SelectClipRgn(hdc, clip);
182 SkASSERT(result != ERROR);
183 result = DeleteObject(clip);
184 SkASSERT(result);
185
186 SelectObject(hdc, bitmap);
187
188 // 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.
189
190 return hdc;
191 }
192
100 } // namespace skia 193 } // namespace skia
101 194
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698