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

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: 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
31 // just create a minimal bitmap.
32 if ((width == 0) || (height == 0)) {
33 width = 1;
34 height = 1;
35 }
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;
49
50 HBITMAP hbitmap = CreateDIBSection(nullptr, reinterpret_cast<BITMAPINFO*>(&hdr ),
Peter Kasting 2016/02/16 21:12:10 Nit: 80 columns
tomhudson 2016/06/21 22:20:22 Done.
51 0, nullptr, nullptr, 0);
52
53 #if !defined(_WIN64)
54 // If CreateDIBSection() failed, try to get some useful
Peter Kasting 2016/02/16 21:12:10 Nit: Wrap as late as possible
tomhudson 2016/06/21 22:20:22 Done.
55 // information out before we crash for post-mortem analysis.
56 if (!hbitmap)
57 base::debug::GDIBitmapAllocFailure(&hdr, nullptr);
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 28 matching lines...) Expand all
66 // and arcs themselves fully respect the device context's world-to-device 103 // and arcs themselves fully respect the device context's world-to-device
67 // transformation. 104 // transformation.
68 BOOL res = SetGraphicsMode(context, GM_ADVANCED); 105 BOOL res = SetGraphicsMode(context, GM_ADVANCED);
69 SkASSERT(res != 0); 106 SkASSERT(res != 0);
70 107
71 // Enables dithering. 108 // Enables dithering.
72 res = SetStretchBltMode(context, HALFTONE); 109 res = SetStretchBltMode(context, HALFTONE);
73 SkASSERT(res != 0); 110 SkASSERT(res != 0);
74 // As per SetStretchBltMode() documentation, SetBrushOrgEx() must be called 111 // As per SetStretchBltMode() documentation, SetBrushOrgEx() must be called
75 // right after. 112 // right after.
76 res = SetBrushOrgEx(context, 0, 0, NULL); 113 res = SetBrushOrgEx(context, 0, 0, nullptr);
77 SkASSERT(res != 0); 114 SkASSERT(res != 0);
78 115
79 // Sets up default orientation. 116 // Sets up default orientation.
80 res = SetArcDirection(context, AD_CLOCKWISE); 117 res = SetArcDirection(context, AD_CLOCKWISE);
81 SkASSERT(res != 0); 118 SkASSERT(res != 0);
82 119
83 // Sets up default colors. 120 // Sets up default colors.
84 res = SetBkColor(context, RGB(255, 255, 255)); 121 res = SetBkColor(context, RGB(255, 255, 255));
85 SkASSERT(res != CLR_INVALID); 122 SkASSERT(res != CLR_INVALID);
86 res = SetTextColor(context, RGB(0, 0, 0)); 123 res = SetTextColor(context, RGB(0, 0, 0));
87 SkASSERT(res != CLR_INVALID); 124 SkASSERT(res != CLR_INVALID);
88 res = SetDCBrushColor(context, RGB(255, 255, 255)); 125 res = SetDCBrushColor(context, RGB(255, 255, 255));
89 SkASSERT(res != CLR_INVALID); 126 SkASSERT(res != CLR_INVALID);
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 #if 0
138 namespace {
139
140 bool PrepareAllocation(HDC context, SkImageInfo* size, BITMAP* backing) {
141 HBITMAP backing_handle =
142 static_cast<HBITMAP>(GetCurrentObject(context, OBJ_BITMAP));
143 if (GetObject(backing_handle, sizeof(*backing),
144 backing) != sizeof(*backing))
145 return false;
146 *size = SkImageInfo::MakeN32Premul(backing->bmWidth,
147 backing->bmHeight);
148 return true;
149 }
150
151 }
152
153 SkSurface* MapPlatformSurface(HDC context) {
154 SkImageInfo size;
155 Bitmap backing;
156 if (!PrepareAllocation(HDC, &size, &backing))
157 return nullptr;
158 return SkSurface::NewRasterDirect(size, backing.bmBits,
159 backing.bmWidthBytes);
160 }
161
162 SkBitmap MapPlatformBitmap(HDC context) {
163 SkImageInfo size;
164 Bitmap backing;
165 SkBitmap bitmap;
166 if (PrepareAllocation(HDC, &size, &backing))
167 bitmap.installPixels(size, backing.bmBits, size.minRowBytes());
168 return bitmap;
169 }
170 #endif
171
172 SkSurface* MapPlatformSurface(HDC context) {
173 HBITMAP backing_handle =
174 static_cast<HBITMAP>(GetCurrentObject(context, OBJ_BITMAP));
175 BITMAP backing;
176 if (GetObject(backing_handle, sizeof(backing),
177 &backing) != sizeof(backing))
178 return nullptr;
179 return SkSurface::NewRasterDirect(
180 SkImageInfo::MakeN32Premul(backing.bmWidth, backing.bmHeight),
181 backing.bmBits, backing.bmWidthBytes);
182 }
183
184 SkBitmap MapPlatformBitmap(HDC context) {
185 HBITMAP backing_handle =
186 static_cast<HBITMAP>(GetCurrentObject(context, OBJ_BITMAP));
187 BITMAP backing;
188 SkBitmap bitmap;
189 if (GetObject(backing_handle, sizeof(backing),
190 &backing) == sizeof(backing)) {
191 SkImageInfo size = SkImageInfo::MakeN32Premul(backing.bmWidth,
192 backing.bmHeight);
193 bitmap.installPixels(size, backing.bmBits, size.minRowBytes());
194 }
195 return bitmap;
196 }
197
198 HDC CreateOffscreenSurface(int width, int height) {
199 HBITMAP bitmap = nullptr;
200
201 // If this process doesn't have access to GDI, we'll have to use a shared
202 // memory segment instead.
203 if (!base::win::IsUser32AndGdi32Available())
204 return nullptr;
205
206 bitmap = CreateHBitmap(width, height);
207 if (!bitmap)
208 return nullptr;
209
210 HDC hdc = CreateCompatibleDC(nullptr);
211 SkASSERT(hdc);
212 InitializeDC(hdc);
213 HRGN clip = CreateRectRgn(0, 0, width, height);
214 int result = SelectClipRgn(hdc, clip);
215 SkASSERT(result != ERROR);
216 result = DeleteObject(clip);
217 SkASSERT(result);
218
219 SelectObject(hdc, bitmap);
220
221 // need to clean up by calling DeleteDC(hdc) else we leak!
222
223 return hdc;
224 }
225
100 } // namespace skia 226 } // namespace skia
101 227
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698