OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <windows.h> | 5 #include <windows.h> |
6 #include <psapi.h> | 6 #include <psapi.h> |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/debug/gdi_debug_util_win.h" | 9 #include "base/debug/gdi_debug_util_win.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/win/win_util.h" | 11 #include "base/win/win_util.h" |
12 #include "skia/ext/bitmap_platform_device_win.h" | 12 #include "skia/ext/bitmap_platform_device_win.h" |
13 #include "skia/ext/platform_canvas.h" | 13 #include "skia/ext/platform_canvas.h" |
14 #include "skia/ext/skia_utils_win.h" | 14 #include "skia/ext/skia_utils_win.h" |
15 #include "third_party/skia/include/core/SkMatrix.h" | 15 #include "third_party/skia/include/core/SkMatrix.h" |
16 #include "third_party/skia/include/core/SkPath.h" | 16 #include "third_party/skia/include/core/SkPath.h" |
17 #include "third_party/skia/include/core/SkRefCnt.h" | 17 #include "third_party/skia/include/core/SkRefCnt.h" |
18 #include "third_party/skia/include/core/SkRect.h" | 18 #include "third_party/skia/include/core/SkRect.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 HBITMAP CreateHBitmap(int width, int height, bool is_opaque, | |
23 HANDLE shared_section, void** data) { | |
24 // CreateDIBSection appears to get unhappy if we create an empty bitmap, so | |
25 // just create a minimal bitmap | |
26 if ((width == 0) || (height == 0)) { | |
27 width = 1; | |
28 height = 1; | |
29 } | |
30 | |
31 BITMAPINFOHEADER hdr = {0}; | |
32 hdr.biSize = sizeof(BITMAPINFOHEADER); | |
33 hdr.biWidth = width; | |
34 hdr.biHeight = -height; // minus means top-down bitmap | |
35 hdr.biPlanes = 1; | |
36 hdr.biBitCount = 32; | |
37 hdr.biCompression = BI_RGB; // no compression | |
38 hdr.biSizeImage = 0; | |
39 hdr.biXPelsPerMeter = 1; | |
40 hdr.biYPelsPerMeter = 1; | |
41 hdr.biClrUsed = 0; | |
42 hdr.biClrImportant = 0; | |
43 | |
44 HBITMAP hbitmap = CreateDIBSection(NULL, reinterpret_cast<BITMAPINFO*>(&hdr), | |
45 0, data, shared_section, 0); | |
46 | |
47 #if !defined(_WIN64) | |
48 // If this call fails, we're gonna crash hard. Try to get some useful | |
49 // information out before we crash for post-mortem analysis. | |
50 if (!hbitmap) | |
51 base::debug::GDIBitmapAllocFailure(&hdr, shared_section); | |
52 #endif | |
53 | |
54 return hbitmap; | |
55 } | |
56 | |
57 void LoadClippingRegionToDC(HDC context, | 22 void LoadClippingRegionToDC(HDC context, |
58 const SkIRect& clip_bounds, | 23 const SkIRect& clip_bounds, |
59 const SkMatrix& transformation) { | 24 const SkMatrix& transformation) { |
60 HRGN hrgn = CreateRectRgnIndirect(&skia::SkIRectToRECT(clip_bounds)); | 25 HRGN hrgn = CreateRectRgnIndirect(&skia::SkIRectToRECT(clip_bounds)); |
61 int result = SelectClipRgn(context, hrgn); | 26 int result = SelectClipRgn(context, hrgn); |
62 SkASSERT(result != ERROR); | 27 SkASSERT(result != ERROR); |
63 result = DeleteObject(hrgn); | 28 result = DeleteObject(hrgn); |
64 SkASSERT(result != 0); | 29 SkASSERT(result != 0); |
65 } | 30 } |
66 | 31 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 void* data; | 117 void* data; |
153 HBITMAP hbitmap = NULL; | 118 HBITMAP hbitmap = NULL; |
154 | 119 |
155 // This function contains an implementation of a Skia platform bitmap for | 120 // This function contains an implementation of a Skia platform bitmap for |
156 // drawing and compositing graphics. The original implementation uses Windows | 121 // drawing and compositing graphics. The original implementation uses Windows |
157 // GDI to create the backing bitmap memory, however it's possible for a | 122 // GDI to create the backing bitmap memory, however it's possible for a |
158 // process to not have access to GDI which will cause this code to fail. It's | 123 // process to not have access to GDI which will cause this code to fail. It's |
159 // possible to detect when GDI is unavailable and instead directly map the | 124 // possible to detect when GDI is unavailable and instead directly map the |
160 // shared memory as the bitmap. | 125 // shared memory as the bitmap. |
161 if (base::win::IsUser32AndGdi32Available()) { | 126 if (base::win::IsUser32AndGdi32Available()) { |
162 hbitmap = CreateHBitmap(width, height, is_opaque, shared_section, &data); | 127 hbitmap = skia::CreateHBitmap(width, height, is_opaque, shared_section, |
| 128 &data); |
163 if (!hbitmap) { | 129 if (!hbitmap) { |
164 LOG(ERROR) << "CreateHBitmap failed"; | 130 LOG(ERROR) << "CreateHBitmap failed"; |
165 return NULL; | 131 return NULL; |
166 } | 132 } |
167 } else { | 133 } else { |
168 DCHECK(shared_section != NULL); | 134 DCHECK(shared_section != NULL); |
169 data = MapViewOfFile(shared_section, FILE_MAP_WRITE, 0, 0, | 135 data = MapViewOfFile(shared_section, FILE_MAP_WRITE, 0, 0, |
170 PlatformCanvasStrideForWidth(width) * height); | 136 PlatformCanvasStrideForWidth(width) * height); |
171 if (!data) { | 137 if (!data) { |
172 LOG(ERROR) << "MapViewOfFile failed"; | 138 LOG(ERROR) << "MapViewOfFile failed"; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 int height, | 219 int height, |
254 bool is_opaque, | 220 bool is_opaque, |
255 HANDLE shared_section, | 221 HANDLE shared_section, |
256 OnFailureType failureType) { | 222 OnFailureType failureType) { |
257 sk_sp<SkBaseDevice> dev( | 223 sk_sp<SkBaseDevice> dev( |
258 BitmapPlatformDevice::Create(width, height, is_opaque, shared_section)); | 224 BitmapPlatformDevice::Create(width, height, is_opaque, shared_section)); |
259 return CreateCanvas(dev, failureType); | 225 return CreateCanvas(dev, failureType); |
260 } | 226 } |
261 | 227 |
262 } // namespace skia | 228 } // namespace skia |
OLD | NEW |