OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/process_util.h" |
10 #include "skia/ext/bitmap_platform_device_data.h" | 13 #include "skia/ext/bitmap_platform_device_data.h" |
11 #include "third_party/skia/include/core/SkMatrix.h" | 14 #include "third_party/skia/include/core/SkMatrix.h" |
12 #include "third_party/skia/include/core/SkRefCnt.h" | 15 #include "third_party/skia/include/core/SkRefCnt.h" |
13 #include "third_party/skia/include/core/SkRegion.h" | 16 #include "third_party/skia/include/core/SkRegion.h" |
14 #include "third_party/skia/include/core/SkUtils.h" | 17 #include "third_party/skia/include/core/SkUtils.h" |
15 | 18 |
| 19 namespace { |
| 20 // Crashes the process. This is called when a bitmap allocation fails, and this |
| 21 // function tries to determine why it might have failed, and crash on different |
| 22 // lines. This allows us to see in crash dumps the most likely reason for the |
| 23 // failure. It takes the size of the bitmap we were trying to allocate as its |
| 24 // arguments so we can check that as well. |
| 25 // Bitmap allocation failures are occuring under conditions outside of GDI and |
| 26 // address space pressure, so we only crash when resources are not low. |
| 27 void CrashForBitmapAllocationFailure(int w, int h) { |
| 28 |
| 29 DWORD last_error = GetLastError(); |
| 30 |
| 31 // The maximum number of GDI objects per process is 10K. If we're very close |
| 32 // to that, it's probably the problem. |
| 33 const int kLotsOfGDIObjs = 9990; |
| 34 if (GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS) > kLotsOfGDIObjs) |
| 35 return; |
| 36 |
| 37 // If the bitmap is ginormous, then we probably can't allocate it. |
| 38 // We use 64M pixels. |
| 39 const int64 kGinormousBitmapPxl = 64000000; |
| 40 if (static_cast<int64>(w) * static_cast<int64>(h) > kGinormousBitmapPxl) |
| 41 return; |
| 42 |
| 43 // If we're using a crazy amount of virtual address space, then maybe there |
| 44 // isn't enough for our bitmap. |
| 45 const int64 kLotsOfMem = 1500000000; // 1.5GB. |
| 46 scoped_ptr<base::ProcessMetrics> process_metrics( |
| 47 base::ProcessMetrics::CreateProcessMetrics(GetCurrentProcess())); |
| 48 if (process_metrics->GetPagefileUsage() > kLotsOfMem) |
| 49 return; |
| 50 |
| 51 LPVOID lpMsgBuf = NULL; |
| 52 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 53 FORMAT_MESSAGE_FROM_SYSTEM | |
| 54 FORMAT_MESSAGE_IGNORE_INSERTS, |
| 55 NULL, |
| 56 last_error, |
| 57 0, |
| 58 reinterpret_cast<LPTSTR>(&lpMsgBuf), |
| 59 0, |
| 60 NULL); |
| 61 |
| 62 CHECK(NO_ERROR == last_error); |
| 63 LocalFree(lpMsgBuf); |
| 64 } |
| 65 |
| 66 } |
| 67 |
16 namespace skia { | 68 namespace skia { |
17 | 69 |
18 BitmapPlatformDevice::BitmapPlatformDeviceData::BitmapPlatformDeviceData( | 70 BitmapPlatformDevice::BitmapPlatformDeviceData::BitmapPlatformDeviceData( |
19 HBITMAP hbitmap) | 71 HBITMAP hbitmap) |
20 : bitmap_context_(hbitmap), | 72 : bitmap_context_(hbitmap), |
21 hdc_(NULL), | 73 hdc_(NULL), |
22 config_dirty_(true) { // Want to load the config next time. | 74 config_dirty_(true) { // Want to load the config next time. |
23 // Initialize the clip region to the entire bitmap. | 75 // Initialize the clip region to the entire bitmap. |
24 BITMAP bitmap_data; | 76 BITMAP bitmap_data; |
25 if (GetObject(bitmap_context_, sizeof(BITMAP), &bitmap_data)) { | 77 if (GetObject(bitmap_context_, sizeof(BITMAP), &bitmap_data)) { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 hdr.biPlanes = 1; | 162 hdr.biPlanes = 1; |
111 hdr.biBitCount = 32; | 163 hdr.biBitCount = 32; |
112 hdr.biCompression = BI_RGB; // no compression | 164 hdr.biCompression = BI_RGB; // no compression |
113 hdr.biSizeImage = 0; | 165 hdr.biSizeImage = 0; |
114 hdr.biXPelsPerMeter = 1; | 166 hdr.biXPelsPerMeter = 1; |
115 hdr.biYPelsPerMeter = 1; | 167 hdr.biYPelsPerMeter = 1; |
116 hdr.biClrUsed = 0; | 168 hdr.biClrUsed = 0; |
117 hdr.biClrImportant = 0; | 169 hdr.biClrImportant = 0; |
118 | 170 |
119 void* data = NULL; | 171 void* data = NULL; |
| 172 |
| 173 // Force the last error to success so that we can accurately track failures |
| 174 // in CrashForBitmapAllocationFailure. |
| 175 SetLastError(ERROR_SUCCESS); |
120 HBITMAP hbitmap = CreateDIBSection(screen_dc, | 176 HBITMAP hbitmap = CreateDIBSection(screen_dc, |
121 reinterpret_cast<BITMAPINFO*>(&hdr), 0, | 177 reinterpret_cast<BITMAPINFO*>(&hdr), 0, |
122 &data, | 178 &data, |
123 shared_section, 0); | 179 shared_section, 0); |
124 if (!hbitmap) { | 180 if (!hbitmap) { |
| 181 CrashForBitmapAllocationFailure(width, height); |
125 return NULL; | 182 return NULL; |
126 } | 183 } |
127 | 184 |
128 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 185 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
129 bitmap.setPixels(data); | 186 bitmap.setPixels(data); |
130 bitmap.setIsOpaque(is_opaque); | 187 bitmap.setIsOpaque(is_opaque); |
131 | 188 |
132 // If we were given data, then don't clobber it! | 189 // If we were given data, then don't clobber it! |
133 if (!shared_section) { | 190 if (!shared_section) { |
134 if (is_opaque) { | 191 if (is_opaque) { |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 } | 313 } |
257 | 314 |
258 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice( | 315 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice( |
259 SkBitmap::Config config, int width, int height, bool isOpaque, | 316 SkBitmap::Config config, int width, int height, bool isOpaque, |
260 Usage /*usage*/) { | 317 Usage /*usage*/) { |
261 SkASSERT(config == SkBitmap::kARGB_8888_Config); | 318 SkASSERT(config == SkBitmap::kARGB_8888_Config); |
262 return BitmapPlatformDevice::create(width, height, isOpaque, NULL); | 319 return BitmapPlatformDevice::create(width, height, isOpaque, NULL); |
263 } | 320 } |
264 | 321 |
265 } // namespace skia | 322 } // namespace skia |
OLD | NEW |