| 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 #include "skia/ext/bitmap_platform_device_data.h" | 9 #include "skia/ext/bitmap_platform_device_data.h" |
| 10 #include "skia/ext/platform_canvas.h" | 10 #include "skia/ext/platform_canvas.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 hdr.biBitCount = 32; | 50 hdr.biBitCount = 32; |
| 51 hdr.biCompression = BI_RGB; // no compression | 51 hdr.biCompression = BI_RGB; // no compression |
| 52 hdr.biSizeImage = 0; | 52 hdr.biSizeImage = 0; |
| 53 hdr.biXPelsPerMeter = 1; | 53 hdr.biXPelsPerMeter = 1; |
| 54 hdr.biYPelsPerMeter = 1; | 54 hdr.biYPelsPerMeter = 1; |
| 55 hdr.biClrUsed = 0; | 55 hdr.biClrUsed = 0; |
| 56 hdr.biClrImportant = 0; | 56 hdr.biClrImportant = 0; |
| 57 | 57 |
| 58 HBITMAP hbitmap = CreateDIBSection(NULL, reinterpret_cast<BITMAPINFO*>(&hdr), | 58 HBITMAP hbitmap = CreateDIBSection(NULL, reinterpret_cast<BITMAPINFO*>(&hdr), |
| 59 0, data, shared_section, 0); | 59 0, data, shared_section, 0); |
| 60 |
| 61 if (!hbitmap) { |
| 62 // Attempt to create a smaller DIBSection to debug whether |
| 63 // the failure was due to memory exhaustion/fragmentation |
| 64 // or GDI resource exhaution. |
| 65 // See crbug.com/275046 |
| 66 hdr.biWidth = 5; |
| 67 hdr.biHeight = 5; |
| 68 void* small_data; |
| 69 HBITMAP small_bitmap = CreateDIBSection( |
| 70 NULL, reinterpret_cast<BITMAPINFO*>(&hdr), |
| 71 0, &small_data, shared_section, 0); |
| 72 if (!small_bitmap) |
| 73 SK_CRASH(); |
| 74 BITMAP bitmap_data; |
| 75 if (GetObject(small_bitmap, sizeof(BITMAP), &bitmap_data)) |
| 76 if (!DeleteObject(small_bitmap)) |
| 77 SK_CRASH(); |
| 78 } |
| 79 |
| 60 return hbitmap; | 80 return hbitmap; |
| 61 } | 81 } |
| 62 | 82 |
| 63 PlatformBitmapPixelRef::PlatformBitmapPixelRef(HBITMAP bitmap_handle, | 83 PlatformBitmapPixelRef::PlatformBitmapPixelRef(HBITMAP bitmap_handle, |
| 64 void* pixels) | 84 void* pixels) |
| 65 : bitmap_handle_(bitmap_handle), | 85 : bitmap_handle_(bitmap_handle), |
| 66 pixels_(pixels) { | 86 pixels_(pixels) { |
| 67 setPreLocked(pixels, NULL); | 87 setPreLocked(pixels, NULL); |
| 68 } | 88 } |
| 69 | 89 |
| 70 PlatformBitmapPixelRef::~PlatformBitmapPixelRef() { | 90 PlatformBitmapPixelRef::~PlatformBitmapPixelRef() { |
| 71 if (bitmap_handle_) | 91 if (bitmap_handle_) |
| 72 DeleteObject(bitmap_handle_); | 92 if (!DeleteObject(bitmap_handle_)) |
| 93 SK_CRASH(); |
| 73 } | 94 } |
| 74 | 95 |
| 75 void* PlatformBitmapPixelRef::onLockPixels(SkColorTable** color_table) { | 96 void* PlatformBitmapPixelRef::onLockPixels(SkColorTable** color_table) { |
| 76 *color_table = NULL; | 97 *color_table = NULL; |
| 77 return pixels_; | 98 return pixels_; |
| 78 } | 99 } |
| 79 | 100 |
| 80 void PlatformBitmapPixelRef::onUnlockPixels() { | 101 void PlatformBitmapPixelRef::onUnlockPixels() { |
| 81 // Nothing to do. | 102 // Nothing to do. |
| 82 return; | 103 return; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 99 rect.set(0, 0, bitmap_data.bmWidth, bitmap_data.bmHeight); | 120 rect.set(0, 0, bitmap_data.bmWidth, bitmap_data.bmHeight); |
| 100 clip_region_ = SkRegion(rect); | 121 clip_region_ = SkRegion(rect); |
| 101 } | 122 } |
| 102 } | 123 } |
| 103 | 124 |
| 104 BitmapPlatformDevice::BitmapPlatformDeviceData::~BitmapPlatformDeviceData() { | 125 BitmapPlatformDevice::BitmapPlatformDeviceData::~BitmapPlatformDeviceData() { |
| 105 if (hdc_) | 126 if (hdc_) |
| 106 ReleaseBitmapDC(); | 127 ReleaseBitmapDC(); |
| 107 | 128 |
| 108 // this will free the bitmap data as well as the bitmap handle | 129 // this will free the bitmap data as well as the bitmap handle |
| 109 DeleteObject(bitmap_context_); | 130 if (!DeleteObject(bitmap_context_)) |
| 131 SK_CRASH(); |
| 110 } | 132 } |
| 111 | 133 |
| 112 HDC BitmapPlatformDevice::BitmapPlatformDeviceData::GetBitmapDC() { | 134 HDC BitmapPlatformDevice::BitmapPlatformDeviceData::GetBitmapDC() { |
| 113 if (!hdc_) { | 135 if (!hdc_) { |
| 114 hdc_ = CreateCompatibleDC(NULL); | 136 hdc_ = CreateCompatibleDC(NULL); |
| 115 InitializeDC(hdc_); | 137 InitializeDC(hdc_); |
| 116 HGDIOBJ old_bitmap = SelectObject(hdc_, bitmap_context_); | 138 HGDIOBJ old_bitmap = SelectObject(hdc_, bitmap_context_); |
| 117 // When the memory DC is created, its display surface is exactly one | 139 // When the memory DC is created, its display surface is exactly one |
| 118 // monochrome pixel wide and one monochrome pixel high. Since we select our | 140 // monochrome pixel wide and one monochrome pixel high. Since we select our |
| 119 // own bitmap, we must delete the previous one. | 141 // own bitmap, we must delete the previous one. |
| 120 DeleteObject(old_bitmap); | 142 if (!DeleteObject(old_bitmap)) |
| 143 SK_CRASH(); |
| 121 } | 144 } |
| 122 | 145 |
| 123 LoadConfig(); | 146 LoadConfig(); |
| 124 return hdc_; | 147 return hdc_; |
| 125 } | 148 } |
| 126 | 149 |
| 127 void BitmapPlatformDevice::BitmapPlatformDeviceData::ReleaseBitmapDC() { | 150 void BitmapPlatformDevice::BitmapPlatformDeviceData::ReleaseBitmapDC() { |
| 128 SkASSERT(hdc_); | 151 SkASSERT(hdc_); |
| 129 DeleteDC(hdc_); | 152 if (!DeleteDC(hdc_)) |
| 153 SK_CRASH(); |
| 130 hdc_ = NULL; | 154 hdc_ = NULL; |
| 131 } | 155 } |
| 132 | 156 |
| 133 bool BitmapPlatformDevice::BitmapPlatformDeviceData::IsBitmapDCCreated() | 157 bool BitmapPlatformDevice::BitmapPlatformDeviceData::IsBitmapDCCreated() |
| 134 const { | 158 const { |
| 135 return hdc_ != NULL; | 159 return hdc_ != NULL; |
| 136 } | 160 } |
| 137 | 161 |
| 138 | 162 |
| 139 void BitmapPlatformDevice::BitmapPlatformDeviceData::SetMatrixClip( | 163 void BitmapPlatformDevice::BitmapPlatformDeviceData::SetMatrixClip( |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 BitmapPlatformDevice::Create(width, height, is_opaque, shared_section)); | 342 BitmapPlatformDevice::Create(width, height, is_opaque, shared_section)); |
| 319 return CreateCanvas(dev, failureType); | 343 return CreateCanvas(dev, failureType); |
| 320 } | 344 } |
| 321 | 345 |
| 322 // Port of PlatformBitmap to win | 346 // Port of PlatformBitmap to win |
| 323 | 347 |
| 324 PlatformBitmap::~PlatformBitmap() { | 348 PlatformBitmap::~PlatformBitmap() { |
| 325 if (surface_) { | 349 if (surface_) { |
| 326 if (platform_extra_) | 350 if (platform_extra_) |
| 327 SelectObject(surface_, reinterpret_cast<HGDIOBJ>(platform_extra_)); | 351 SelectObject(surface_, reinterpret_cast<HGDIOBJ>(platform_extra_)); |
| 328 DeleteDC(surface_); | 352 if (!DeleteDC(surface_)) |
| 353 SK_CRASH(); |
| 329 } | 354 } |
| 330 } | 355 } |
| 331 | 356 |
| 332 bool PlatformBitmap::Allocate(int width, int height, bool is_opaque) { | 357 bool PlatformBitmap::Allocate(int width, int height, bool is_opaque) { |
| 333 void* data; | 358 void* data; |
| 334 HBITMAP hbitmap = CreateHBitmap(width, height, is_opaque, 0, &data); | 359 HBITMAP hbitmap = CreateHBitmap(width, height, is_opaque, 0, &data); |
| 335 if (!hbitmap) | 360 if (!hbitmap) |
| 336 return false; | 361 return false; |
| 337 | 362 |
| 338 surface_ = CreateCompatibleDC(NULL); | 363 surface_ = CreateCompatibleDC(NULL); |
| 339 InitializeDC(surface_); | 364 InitializeDC(surface_); |
| 340 // When the memory DC is created, its display surface is exactly one | 365 // When the memory DC is created, its display surface is exactly one |
| 341 // monochrome pixel wide and one monochrome pixel high. Save this object | 366 // monochrome pixel wide and one monochrome pixel high. Save this object |
| 342 // off, we'll restore it just before deleting the memory DC. | 367 // off, we'll restore it just before deleting the memory DC. |
| 343 HGDIOBJ stock_bitmap = SelectObject(surface_, hbitmap); | 368 HGDIOBJ stock_bitmap = SelectObject(surface_, hbitmap); |
| 344 platform_extra_ = reinterpret_cast<intptr_t>(stock_bitmap); | 369 platform_extra_ = reinterpret_cast<intptr_t>(stock_bitmap); |
| 345 | 370 |
| 346 bitmap_.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0, | 371 bitmap_.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0, |
| 347 is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); | 372 is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 348 // PlatformBitmapPixelRef takes ownership of |hbitmap|. | 373 // PlatformBitmapPixelRef takes ownership of |hbitmap|. |
| 349 bitmap_.setPixelRef( | 374 bitmap_.setPixelRef( |
| 350 skia::AdoptRef(new PlatformBitmapPixelRef(hbitmap, data)).get()); | 375 skia::AdoptRef(new PlatformBitmapPixelRef(hbitmap, data)).get()); |
| 351 bitmap_.lockPixels(); | 376 bitmap_.lockPixels(); |
| 352 | 377 |
| 353 return true; | 378 return true; |
| 354 } | 379 } |
| 355 | 380 |
| 356 } // namespace skia | 381 } // namespace skia |
| OLD | NEW |