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 | |
77 // Restore the optimization options. | |
78 #pragma optimize("", on) | |
79 | |
18 BitmapPlatformDevice::BitmapPlatformDeviceData::BitmapPlatformDeviceData( | 80 BitmapPlatformDevice::BitmapPlatformDeviceData::BitmapPlatformDeviceData( |
19 HBITMAP hbitmap) | 81 HBITMAP hbitmap) |
20 : bitmap_context_(hbitmap), | 82 : bitmap_context_(hbitmap), |
21 hdc_(NULL), | 83 hdc_(NULL), |
22 config_dirty_(true), // Want to load the config next time. | 84 config_dirty_(true), // Want to load the config next time. |
23 transform_(SkMatrix::I()) { | 85 transform_(SkMatrix::I()) { |
24 // Initialize the clip region to the entire bitmap. | 86 // Initialize the clip region to the entire bitmap. |
25 BITMAP bitmap_data; | 87 BITMAP bitmap_data; |
26 if (GetObject(bitmap_context_, sizeof(BITMAP), &bitmap_data)) { | 88 if (GetObject(bitmap_context_, sizeof(BITMAP), &bitmap_data)) { |
27 SkIRect rect; | 89 SkIRect rect; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 hdr.biYPelsPerMeter = 1; | 175 hdr.biYPelsPerMeter = 1; |
114 hdr.biClrUsed = 0; | 176 hdr.biClrUsed = 0; |
115 hdr.biClrImportant = 0; | 177 hdr.biClrImportant = 0; |
116 | 178 |
117 void* data = NULL; | 179 void* data = NULL; |
118 HBITMAP hbitmap = CreateDIBSection(NULL, | 180 HBITMAP hbitmap = CreateDIBSection(NULL, |
119 reinterpret_cast<BITMAPINFO*>(&hdr), 0, | 181 reinterpret_cast<BITMAPINFO*>(&hdr), 0, |
120 &data, | 182 &data, |
121 shared_section, 0); | 183 shared_section, 0); |
122 if (!hbitmap) { | 184 if (!hbitmap) { |
185 // Investigate we failed. If we know the reason, crash in a specific place. | |
186 unsigned int error = GetLastError(); | |
187 if (shared_section) | |
188 CrashIfInvalidSection(shared_section); | |
189 CrashForBitmapAllocationFailure(width, height, error); | |
Alexei Svitkine (slow)
2012/04/16 19:01:44
Can you explain why it makes sense to move this he
| |
123 return NULL; | 190 return NULL; |
124 } | 191 } |
125 | 192 |
126 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 193 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
127 bitmap.setPixels(data); | 194 bitmap.setPixels(data); |
128 bitmap.setIsOpaque(is_opaque); | 195 bitmap.setIsOpaque(is_opaque); |
129 | 196 |
130 #ifndef NDEBUG | 197 #ifndef NDEBUG |
131 // If we were given data, then don't clobber it! | 198 // If we were given data, then don't clobber it! |
132 if (!shared_section && is_opaque) | 199 if (!shared_section && is_opaque) |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
257 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice( | 324 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice( |
258 SkBitmap::Config config, int width, int height, bool isOpaque, | 325 SkBitmap::Config config, int width, int height, bool isOpaque, |
259 Usage /*usage*/) { | 326 Usage /*usage*/) { |
260 SkASSERT(config == SkBitmap::kARGB_8888_Config); | 327 SkASSERT(config == SkBitmap::kARGB_8888_Config); |
261 SkDevice* bitmap_device = BitmapPlatformDevice::CreateAndClear(width, height, | 328 SkDevice* bitmap_device = BitmapPlatformDevice::CreateAndClear(width, height, |
262 isOpaque); | 329 isOpaque); |
263 return bitmap_device; | 330 return bitmap_device; |
264 } | 331 } |
265 | 332 |
266 } // namespace skia | 333 } // namespace skia |
OLD | NEW |