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 | 9 |
10 #include "skia/ext/bitmap_platform_device_data.h" | 10 #include "skia/ext/bitmap_platform_device_data.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 namespace skia { | 16 namespace skia { |
17 | 17 |
| 18 // Disable optimizations during crash analysis. |
| 19 #pragma optimize("", off) |
| 20 |
| 21 // Crash On Failure. |address| should be a number less than 4000. |
| 22 #define COF(address, condition) if (!(condition)) *((int*) address) = 0 |
| 23 |
| 24 // This is called when a bitmap allocation fails, and this function tries to |
| 25 // determine why it might have failed, and crash on different |
| 26 // lines. This allows us to see in crash dumps the most likely reason for the |
| 27 // failure. It takes the size of the bitmap we were trying to allocate as its |
| 28 // arguments so we can check that as well. |
| 29 // |
| 30 // Note that in a sandboxed renderer this function crashes when trying to |
| 31 // call GetProcessMemoryInfo() because it tries to load psapi.dll, which |
| 32 // is fine but gives you a very hard to read crash dump. |
| 33 void CrashForBitmapAllocationFailure(int w, int h, unsigned int error) { |
| 34 // Store the extended error info in a place easy to find at debug time. |
| 35 unsigned int diag_error = 0; |
| 36 // If the bitmap is ginormous, then we probably can't allocate it. |
| 37 // We use 32M pixels = 128MB @ 4 bytes per pixel. |
| 38 const LONG_PTR kGinormousBitmapPxl = 32000000; |
| 39 COF(1, LONG_PTR(w) * LONG_PTR(h) < kGinormousBitmapPxl); |
| 40 |
| 41 // The maximum number of GDI objects per process is 10K. If we're very close |
| 42 // to that, it's probably the problem. |
| 43 const unsigned int kLotsOfGDIObjects = 9990; |
| 44 unsigned int num_gdi_objects = GetGuiResources(GetCurrentProcess(), |
| 45 GR_GDIOBJECTS); |
| 46 if (num_gdi_objects == 0) { |
| 47 diag_error = GetLastError(); |
| 48 COF(2, false); |
| 49 } |
| 50 COF(3, num_gdi_objects < kLotsOfGDIObjects); |
| 51 |
| 52 // If we're using a crazy amount of virtual address space, then maybe there |
| 53 // isn't enough for our bitmap. |
| 54 const SIZE_T kLotsOfMem = 1500000000; // 1.5GB. |
| 55 PROCESS_MEMORY_COUNTERS_EX pmc; |
| 56 pmc.cb = sizeof(pmc); |
| 57 if (!GetProcessMemoryInfo(GetCurrentProcess(), |
| 58 reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc), |
| 59 sizeof(pmc))) { |
| 60 diag_error = GetLastError(); |
| 61 COF(4, false); |
| 62 } |
| 63 COF(5, pmc.PagefileUsage < kLotsOfMem); |
| 64 COF(6, pmc.PrivateUsage < kLotsOfMem); |
| 65 // Ok but we are somehow out of memory? |
| 66 COF(7, error != ERROR_NOT_ENOUGH_MEMORY); |
| 67 } |
| 68 |
| 69 // Crashes the process. This is called when a bitmap allocation fails but |
| 70 // unlike its cousin CrashForBitmapAllocationFailure() it tries to detect if |
| 71 // the issue was a non-valid shared bitmap handle. |
| 72 void CrashIfInvalidSection(HANDLE shared_section) { |
| 73 DWORD handle_info = 0; |
| 74 COF(8, ::GetHandleInformation(shared_section, &handle_info) == TRUE); |
| 75 } |
| 76 |
18 BitmapPlatformDevice::BitmapPlatformDeviceData::BitmapPlatformDeviceData( | 77 BitmapPlatformDevice::BitmapPlatformDeviceData::BitmapPlatformDeviceData( |
19 HBITMAP hbitmap) | 78 HBITMAP hbitmap) |
20 : bitmap_context_(hbitmap), | 79 : bitmap_context_(hbitmap), |
21 hdc_(NULL), | 80 hdc_(NULL), |
22 config_dirty_(true), // Want to load the config next time. | 81 config_dirty_(true), // Want to load the config next time. |
23 transform_(SkMatrix::I()) { | 82 transform_(SkMatrix::I()) { |
24 // Initialize the clip region to the entire bitmap. | 83 // Initialize the clip region to the entire bitmap. |
25 BITMAP bitmap_data; | 84 BITMAP bitmap_data; |
26 if (GetObject(bitmap_context_, sizeof(BITMAP), &bitmap_data)) { | 85 if (GetObject(bitmap_context_, sizeof(BITMAP), &bitmap_data)) { |
27 SkIRect rect; | 86 SkIRect rect; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 hdr.biYPelsPerMeter = 1; | 172 hdr.biYPelsPerMeter = 1; |
114 hdr.biClrUsed = 0; | 173 hdr.biClrUsed = 0; |
115 hdr.biClrImportant = 0; | 174 hdr.biClrImportant = 0; |
116 | 175 |
117 void* data = NULL; | 176 void* data = NULL; |
118 HBITMAP hbitmap = CreateDIBSection(NULL, | 177 HBITMAP hbitmap = CreateDIBSection(NULL, |
119 reinterpret_cast<BITMAPINFO*>(&hdr), 0, | 178 reinterpret_cast<BITMAPINFO*>(&hdr), 0, |
120 &data, | 179 &data, |
121 shared_section, 0); | 180 shared_section, 0); |
122 if (!hbitmap) { | 181 if (!hbitmap) { |
| 182 // Investigate we failed. If we know the reason, crash in a specific place. |
| 183 unsigned int error = GetLastError(); |
| 184 if (shared_section) |
| 185 CrashIfInvalidSection(shared_section); |
| 186 CrashForBitmapAllocationFailure(width, height, error); |
123 return NULL; | 187 return NULL; |
124 } | 188 } |
125 | 189 |
126 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 190 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
127 bitmap.setPixels(data); | 191 bitmap.setPixels(data); |
128 bitmap.setIsOpaque(is_opaque); | 192 bitmap.setIsOpaque(is_opaque); |
129 | 193 |
130 #ifndef NDEBUG | 194 #ifndef NDEBUG |
131 // If we were given data, then don't clobber it! | 195 // If we were given data, then don't clobber it! |
132 if (!shared_section && is_opaque) | 196 if (!shared_section && is_opaque) |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice( | 321 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice( |
258 SkBitmap::Config config, int width, int height, bool isOpaque, | 322 SkBitmap::Config config, int width, int height, bool isOpaque, |
259 Usage /*usage*/) { | 323 Usage /*usage*/) { |
260 SkASSERT(config == SkBitmap::kARGB_8888_Config); | 324 SkASSERT(config == SkBitmap::kARGB_8888_Config); |
261 SkDevice* bitmap_device = BitmapPlatformDevice::CreateAndClear(width, height, | 325 SkDevice* bitmap_device = BitmapPlatformDevice::CreateAndClear(width, height, |
262 isOpaque); | 326 isOpaque); |
263 return bitmap_device; | 327 return bitmap_device; |
264 } | 328 } |
265 | 329 |
266 } // namespace skia | 330 } // namespace skia |
OLD | NEW |