Chromium Code Reviews| 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 | 7 |
| 8 #include "skia/ext/bitmap_platform_device_win.h" | 8 #include "skia/ext/bitmap_platform_device_win.h" |
| 9 | |
| 10 #include "skia/ext/bitmap_platform_device_data.h" | 9 #include "skia/ext/bitmap_platform_device_data.h" |
| 10 #include "skia/ext/platform_canvas.h" | |
| 11 #include "third_party/skia/include/core/SkMatrix.h" | 11 #include "third_party/skia/include/core/SkMatrix.h" |
| 12 #include "third_party/skia/include/core/SkRefCnt.h" | 12 #include "third_party/skia/include/core/SkRefCnt.h" |
| 13 #include "third_party/skia/include/core/SkRegion.h" | 13 #include "third_party/skia/include/core/SkRegion.h" |
| 14 #include "third_party/skia/include/core/SkUtils.h" | 14 #include "third_party/skia/include/core/SkUtils.h" |
| 15 | 15 |
| 16 static HBITMAP CreateHBitmap(int width, int height, bool is_opaque, | |
| 17 HANDLE shared_section, SkBitmap* output) { | |
| 18 // CreateDIBSection appears to get unhappy if we create an empty bitmap, so | |
| 19 // just create a minimal bitmap | |
| 20 if ((width == 0) || (height == 0)) { | |
| 21 width = 1; | |
| 22 height = 1; | |
| 23 } | |
| 24 | |
| 25 BITMAPINFOHEADER hdr = {0}; | |
| 26 hdr.biSize = sizeof(BITMAPINFOHEADER); | |
| 27 hdr.biWidth = width; | |
| 28 hdr.biHeight = -height; // minus means top-down bitmap | |
| 29 hdr.biPlanes = 1; | |
| 30 hdr.biBitCount = 32; | |
| 31 hdr.biCompression = BI_RGB; // no compression | |
| 32 hdr.biSizeImage = 0; | |
| 33 hdr.biXPelsPerMeter = 1; | |
| 34 hdr.biYPelsPerMeter = 1; | |
| 35 hdr.biClrUsed = 0; | |
| 36 hdr.biClrImportant = 0; | |
| 37 | |
| 38 void* data = NULL; | |
| 39 HBITMAP hbitmap = CreateDIBSection(NULL, reinterpret_cast<BITMAPINFO*>(&hdr), 0, | |
|
brettw
2012/10/09 20:40:12
Line too long.
reed1
2012/10/09 21:06:43
Done.
| |
| 40 &data, shared_section, 0); | |
| 41 if (hbitmap) { | |
| 42 output->setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
| 43 output->setPixels(data); | |
| 44 output->setIsOpaque(is_opaque); | |
| 45 } | |
| 46 return hbitmap; | |
| 47 } | |
| 48 | |
| 16 namespace skia { | 49 namespace skia { |
| 17 | 50 |
| 18 BitmapPlatformDevice::BitmapPlatformDeviceData::BitmapPlatformDeviceData( | 51 BitmapPlatformDevice::BitmapPlatformDeviceData::BitmapPlatformDeviceData( |
| 19 HBITMAP hbitmap) | 52 HBITMAP hbitmap) |
| 20 : bitmap_context_(hbitmap), | 53 : bitmap_context_(hbitmap), |
| 21 hdc_(NULL), | 54 hdc_(NULL), |
| 22 config_dirty_(true), // Want to load the config next time. | 55 config_dirty_(true), // Want to load the config next time. |
| 23 transform_(SkMatrix::I()) { | 56 transform_(SkMatrix::I()) { |
| 24 // Initialize the clip region to the entire bitmap. | 57 // Initialize the clip region to the entire bitmap. |
| 25 BITMAP bitmap_data; | 58 BITMAP bitmap_data; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 // that we can create the pixel data before calling the constructor. This is | 120 // that we can create the pixel data before calling the constructor. This is |
| 88 // required so that we can call the base class' constructor with the pixel | 121 // required so that we can call the base class' constructor with the pixel |
| 89 // data. | 122 // data. |
| 90 BitmapPlatformDevice* BitmapPlatformDevice::Create( | 123 BitmapPlatformDevice* BitmapPlatformDevice::Create( |
| 91 int width, | 124 int width, |
| 92 int height, | 125 int height, |
| 93 bool is_opaque, | 126 bool is_opaque, |
| 94 HANDLE shared_section) { | 127 HANDLE shared_section) { |
| 95 SkBitmap bitmap; | 128 SkBitmap bitmap; |
| 96 | 129 |
| 97 // CreateDIBSection appears to get unhappy if we create an empty bitmap, so | 130 HBITMAP hbitmap = CreateHBitmap(width, height, is_opaque, shared_section, |
| 98 // just create a minimal bitmap | 131 &bitmap); |
| 99 if ((width == 0) || (height == 0)) { | |
| 100 width = 1; | |
| 101 height = 1; | |
| 102 } | |
| 103 | |
| 104 BITMAPINFOHEADER hdr = {0}; | |
| 105 hdr.biSize = sizeof(BITMAPINFOHEADER); | |
| 106 hdr.biWidth = width; | |
| 107 hdr.biHeight = -height; // minus means top-down bitmap | |
| 108 hdr.biPlanes = 1; | |
| 109 hdr.biBitCount = 32; | |
| 110 hdr.biCompression = BI_RGB; // no compression | |
| 111 hdr.biSizeImage = 0; | |
| 112 hdr.biXPelsPerMeter = 1; | |
| 113 hdr.biYPelsPerMeter = 1; | |
| 114 hdr.biClrUsed = 0; | |
| 115 hdr.biClrImportant = 0; | |
| 116 | |
| 117 void* data = NULL; | |
| 118 HBITMAP hbitmap = CreateDIBSection(NULL, | |
| 119 reinterpret_cast<BITMAPINFO*>(&hdr), 0, | |
| 120 &data, | |
| 121 shared_section, 0); | |
| 122 if (!hbitmap) | 132 if (!hbitmap) |
| 123 return NULL; | 133 return NULL; |
| 124 | 134 |
| 125 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
| 126 bitmap.setPixels(data); | |
| 127 bitmap.setIsOpaque(is_opaque); | |
| 128 | |
| 129 #ifndef NDEBUG | 135 #ifndef NDEBUG |
| 130 // If we were given data, then don't clobber it! | 136 // If we were given data, then don't clobber it! |
| 131 if (!shared_section && is_opaque) | 137 if (!shared_section && is_opaque) |
| 132 // To aid in finding bugs, we set the background color to something | 138 // To aid in finding bugs, we set the background color to something |
| 133 // obviously wrong so it will be noticable when it is not cleared | 139 // obviously wrong so it will be noticable when it is not cleared |
| 134 bitmap.eraseARGB(255, 0, 255, 128); // bright bluish green | 140 bitmap.eraseARGB(255, 0, 255, 128); // bright bluish green |
| 135 #endif | 141 #endif |
| 136 | 142 |
| 137 // The device object will take ownership of the HBITMAP. The initial refcount | 143 // The device object will take ownership of the HBITMAP. The initial refcount |
| 138 // of the data object will be 1, which is what the constructor expects. | 144 // of the data object will be 1, which is what the constructor expects. |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 255 | 261 |
| 256 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice( | 262 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice( |
| 257 SkBitmap::Config config, int width, int height, bool isOpaque, | 263 SkBitmap::Config config, int width, int height, bool isOpaque, |
| 258 Usage /*usage*/) { | 264 Usage /*usage*/) { |
| 259 SkASSERT(config == SkBitmap::kARGB_8888_Config); | 265 SkASSERT(config == SkBitmap::kARGB_8888_Config); |
| 260 SkDevice* bitmap_device = BitmapPlatformDevice::CreateAndClear(width, height, | 266 SkDevice* bitmap_device = BitmapPlatformDevice::CreateAndClear(width, height, |
| 261 isOpaque); | 267 isOpaque); |
| 262 return bitmap_device; | 268 return bitmap_device; |
| 263 } | 269 } |
| 264 | 270 |
| 271 // Port of PlatformBitmap to win | |
| 272 | |
| 273 PlatformBitmap::~PlatformBitmap() { | |
| 274 if (surface_) | |
| 275 DeleteDC(surface_); | |
| 276 } | |
| 277 | |
| 278 bool PlatformBitmap::Allocate(int width, int height, bool isOpaque) { | |
|
brettw
2012/10/09 20:40:12
is_opaque.
| |
| 279 HBITMAP hbitmap = CreateHBitmap(width, height, isOpaque, 0, &bitmap_); | |
| 280 if (!hbitmap) | |
| 281 return false; | |
| 282 | |
| 283 surface_ = CreateCompatibleDC(NULL); | |
| 284 InitializeDC(surface_); | |
| 285 HGDIOBJ old_bitmap = SelectObject(surface_, hbitmap); | |
| 286 // When the memory DC is created, its display surface is exactly one | |
| 287 // monochrome pixel wide and one monochrome pixel high. Since we select our | |
| 288 // own bitmap, we must delete the previous one. | |
| 289 DeleteObject(old_bitmap); | |
| 290 return true; | |
| 291 } | |
| 292 | |
| 293 PlatformSurface PlatformBitmap::LockSurface() { | |
| 294 return surface_; | |
| 295 } | |
| 296 | |
| 297 void PlatformBitmap::UnlockSurface() { | |
| 298 // nothing to do for our surface_. It will be released in our destructor | |
|
brettw
2012/10/09 20:40:12
Style nit: should be a complete sentence with cap
| |
| 299 } | |
| 300 | |
| 265 } // namespace skia | 301 } // namespace skia |
| OLD | NEW |