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 // The maximum number of GDI objects per process is 10K. If we're very close | |
29 // to that, it's probably the problem. | |
30 const int kLotsOfGDIObjs = 9990; | |
31 if (GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS) > kLotsOfGDIObjs) | |
32 return; | |
33 | |
34 // If the bitmap is ginormous, then we probably can't allocate it. | |
35 // We use 64M pixels. | |
36 const int64 kGinormousBitmapPxl = 64000000; | |
37 if (static_cast<int64>(w) * static_cast<int64>(h) > kGinormousBitmapPxl) | |
38 return; | |
39 | |
40 // If we're using a crazy amount of virtual address space, then maybe there | |
41 // isn't enough for our bitmap. | |
42 const int64 kLotsOfMem = 1500000000; // 1.5GB. | |
43 scoped_ptr<base::ProcessMetrics> process_metrics( | |
44 base::ProcessMetrics::CreateProcessMetrics(GetCurrentProcess())); | |
45 if (process_metrics->GetPagefileUsage() > kLotsOfMem) | |
46 return; | |
47 | |
48 DWORD last_error = GetLastError(); | |
vandebo (ex-Chrome)
2011/11/10 00:52:13
Wouldn't it be better to call this at the start of
| |
49 LPVOID lpMsgBuf = NULL; | |
50 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | | |
51 FORMAT_MESSAGE_FROM_SYSTEM | | |
52 FORMAT_MESSAGE_IGNORE_INSERTS, | |
53 NULL, | |
54 last_error, | |
55 0, | |
56 reinterpret_cast<LPTSTR>(&lpMsgBuf), | |
57 0, | |
58 NULL); | |
59 | |
60 CHECK(NO_ERROR == last_error); | |
61 LocalFree(lpMsgBuf); | |
62 } | |
63 | |
64 } | |
65 | |
16 namespace skia { | 66 namespace skia { |
17 | 67 |
18 BitmapPlatformDevice::BitmapPlatformDeviceData::BitmapPlatformDeviceData( | 68 BitmapPlatformDevice::BitmapPlatformDeviceData::BitmapPlatformDeviceData( |
19 HBITMAP hbitmap) | 69 HBITMAP hbitmap) |
20 : bitmap_context_(hbitmap), | 70 : bitmap_context_(hbitmap), |
21 hdc_(NULL), | 71 hdc_(NULL), |
22 config_dirty_(true) { // Want to load the config next time. | 72 config_dirty_(true) { // Want to load the config next time. |
23 // Initialize the clip region to the entire bitmap. | 73 // Initialize the clip region to the entire bitmap. |
24 BITMAP bitmap_data; | 74 BITMAP bitmap_data; |
25 if (GetObject(bitmap_context_, sizeof(BITMAP), &bitmap_data)) { | 75 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; | 160 hdr.biPlanes = 1; |
111 hdr.biBitCount = 32; | 161 hdr.biBitCount = 32; |
112 hdr.biCompression = BI_RGB; // no compression | 162 hdr.biCompression = BI_RGB; // no compression |
113 hdr.biSizeImage = 0; | 163 hdr.biSizeImage = 0; |
114 hdr.biXPelsPerMeter = 1; | 164 hdr.biXPelsPerMeter = 1; |
115 hdr.biYPelsPerMeter = 1; | 165 hdr.biYPelsPerMeter = 1; |
116 hdr.biClrUsed = 0; | 166 hdr.biClrUsed = 0; |
117 hdr.biClrImportant = 0; | 167 hdr.biClrImportant = 0; |
118 | 168 |
119 void* data = NULL; | 169 void* data = NULL; |
170 | |
171 // Force the last error to success so that we can accurately track failures | |
172 // in CrashForBitmapAllocationFailure. | |
173 SetLastError(ERROR_SUCCESS); | |
120 HBITMAP hbitmap = CreateDIBSection(screen_dc, | 174 HBITMAP hbitmap = CreateDIBSection(screen_dc, |
121 reinterpret_cast<BITMAPINFO*>(&hdr), 0, | 175 reinterpret_cast<BITMAPINFO*>(&hdr), 0, |
122 &data, | 176 &data, |
123 shared_section, 0); | 177 shared_section, 0); |
124 if (!hbitmap) { | 178 if (!hbitmap) { |
179 CrashForBitmapAllocationFailure(width, height); | |
125 return NULL; | 180 return NULL; |
126 } | 181 } |
127 | 182 |
128 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 183 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
129 bitmap.setPixels(data); | 184 bitmap.setPixels(data); |
130 bitmap.setIsOpaque(is_opaque); | 185 bitmap.setIsOpaque(is_opaque); |
131 | 186 |
132 // If we were given data, then don't clobber it! | 187 // If we were given data, then don't clobber it! |
133 if (!shared_section) { | 188 if (!shared_section) { |
134 if (is_opaque) { | 189 if (is_opaque) { |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 } | 311 } |
257 | 312 |
258 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice( | 313 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice( |
259 SkBitmap::Config config, int width, int height, bool isOpaque, | 314 SkBitmap::Config config, int width, int height, bool isOpaque, |
260 Usage /*usage*/) { | 315 Usage /*usage*/) { |
261 SkASSERT(config == SkBitmap::kARGB_8888_Config); | 316 SkASSERT(config == SkBitmap::kARGB_8888_Config); |
262 return BitmapPlatformDevice::create(width, height, isOpaque, NULL); | 317 return BitmapPlatformDevice::create(width, height, isOpaque, NULL); |
263 } | 318 } |
264 | 319 |
265 } // namespace skia | 320 } // namespace skia |
OLD | NEW |