| 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 13 matching lines...) Expand all Loading... |
| 80 src_rect = &temp_rect; | 45 src_rect = &temp_rect; |
| 81 } | 46 } |
| 82 skia::CopyHDC(p.GetPlatformSurface(), destination_hdc, x, y, | 47 skia::CopyHDC(p.GetPlatformSurface(), destination_hdc, x, y, |
| 83 canvas->imageInfo().isOpaque(), *src_rect, | 48 canvas->imageInfo().isOpaque(), *src_rect, |
| 84 canvas->getTotalMatrix()); | 49 canvas->getTotalMatrix()); |
| 85 } | 50 } |
| 86 | 51 |
| 87 HDC BitmapPlatformDevice::GetBitmapDC(const SkMatrix& transform, | 52 HDC BitmapPlatformDevice::GetBitmapDC(const SkMatrix& transform, |
| 88 const SkIRect& clip_bounds) { | 53 const SkIRect& clip_bounds) { |
| 89 if (!hdc_) { | 54 if (!hdc_) { |
| 90 hdc_ = CreateCompatibleDC(NULL); | 55 hdc_ = CreateCompatibleDC(nullptr); |
| 91 InitializeDC(hdc_); | 56 skia::InitializeDC(hdc_); |
| 92 old_hbitmap_ = static_cast<HBITMAP>(SelectObject(hdc_, hbitmap_)); | 57 old_hbitmap_ = static_cast<HBITMAP>(SelectObject(hdc_, hbitmap_)); |
| 93 } | 58 } |
| 94 | 59 |
| 95 LoadConfig(transform, clip_bounds); | 60 LoadConfig(transform, clip_bounds); |
| 96 return hdc_; | 61 return hdc_; |
| 97 } | 62 } |
| 98 | 63 |
| 99 void BitmapPlatformDevice::ReleaseBitmapDC() { | 64 void BitmapPlatformDevice::ReleaseBitmapDC() { |
| 100 SkASSERT(hdc_); | 65 SkASSERT(hdc_); |
| 101 SelectObject(hdc_, old_hbitmap_); | 66 SelectObject(hdc_, old_hbitmap_); |
| (...skipping 50 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, shared_section, &data); |
| 163 if (!hbitmap) { | 128 if (!hbitmap) { |
| 164 LOG(ERROR) << "CreateHBitmap failed"; | 129 LOG(ERROR) << "CreateHBitmap failed"; |
| 165 return NULL; | 130 return NULL; |
| 166 } | 131 } |
| 167 } else { | 132 } else { |
| 168 DCHECK(shared_section != NULL); | 133 DCHECK(shared_section != NULL); |
| 169 data = MapViewOfFile(shared_section, FILE_MAP_WRITE, 0, 0, | 134 data = MapViewOfFile(shared_section, FILE_MAP_WRITE, 0, 0, |
| 170 PlatformCanvasStrideForWidth(width) * height); | 135 PlatformCanvasStrideForWidth(width) * height); |
| 171 if (!data) { | 136 if (!data) { |
| 172 LOG(ERROR) << "MapViewOfFile failed"; | 137 LOG(ERROR) << "MapViewOfFile failed"; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 } | 213 } |
| 249 | 214 |
| 250 // PlatformCanvas impl | 215 // PlatformCanvas impl |
| 251 | 216 |
| 252 SkCanvas* CreatePlatformCanvas(int width, | 217 SkCanvas* CreatePlatformCanvas(int width, |
| 253 int height, | 218 int height, |
| 254 bool is_opaque, | 219 bool is_opaque, |
| 255 HANDLE shared_section, | 220 HANDLE shared_section, |
| 256 OnFailureType failureType) { | 221 OnFailureType failureType) { |
| 257 sk_sp<SkBaseDevice> dev( | 222 sk_sp<SkBaseDevice> dev( |
| 258 BitmapPlatformDevice::Create(width, height, is_opaque, shared_section)); | 223 BitmapPlatformDevice::Create(width, height, shared_section)); |
| 259 return CreateCanvas(dev, failureType); | 224 return CreateCanvas(dev, failureType); |
| 260 } | 225 } |
| 261 | 226 |
| 262 } // namespace skia | 227 } // namespace skia |
| OLD | NEW |