Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(735)

Side by Side Diff: skia/ext/bitmap_platform_device_win.cc

Issue 8565027: Revert 109461 - (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | skia/ext/canvas_paint_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
13 #include "skia/ext/bitmap_platform_device_data.h" 10 #include "skia/ext/bitmap_platform_device_data.h"
14 #include "third_party/skia/include/core/SkMatrix.h" 11 #include "third_party/skia/include/core/SkMatrix.h"
15 #include "third_party/skia/include/core/SkRefCnt.h" 12 #include "third_party/skia/include/core/SkRefCnt.h"
16 #include "third_party/skia/include/core/SkRegion.h" 13 #include "third_party/skia/include/core/SkRegion.h"
17 #include "third_party/skia/include/core/SkUtils.h" 14 #include "third_party/skia/include/core/SkUtils.h"
18 15
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
68 namespace skia { 16 namespace skia {
69 17
70 BitmapPlatformDevice::BitmapPlatformDeviceData::BitmapPlatformDeviceData( 18 BitmapPlatformDevice::BitmapPlatformDeviceData::BitmapPlatformDeviceData(
71 HBITMAP hbitmap) 19 HBITMAP hbitmap)
72 : bitmap_context_(hbitmap), 20 : bitmap_context_(hbitmap),
73 hdc_(NULL), 21 hdc_(NULL),
74 config_dirty_(true) { // Want to load the config next time. 22 config_dirty_(true) { // Want to load the config next time.
75 // Initialize the clip region to the entire bitmap. 23 // Initialize the clip region to the entire bitmap.
76 BITMAP bitmap_data; 24 BITMAP bitmap_data;
77 if (GetObject(bitmap_context_, sizeof(BITMAP), &bitmap_data)) { 25 if (GetObject(bitmap_context_, sizeof(BITMAP), &bitmap_data)) {
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 hdr.biPlanes = 1; 110 hdr.biPlanes = 1;
163 hdr.biBitCount = 32; 111 hdr.biBitCount = 32;
164 hdr.biCompression = BI_RGB; // no compression 112 hdr.biCompression = BI_RGB; // no compression
165 hdr.biSizeImage = 0; 113 hdr.biSizeImage = 0;
166 hdr.biXPelsPerMeter = 1; 114 hdr.biXPelsPerMeter = 1;
167 hdr.biYPelsPerMeter = 1; 115 hdr.biYPelsPerMeter = 1;
168 hdr.biClrUsed = 0; 116 hdr.biClrUsed = 0;
169 hdr.biClrImportant = 0; 117 hdr.biClrImportant = 0;
170 118
171 void* data = NULL; 119 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);
176 HBITMAP hbitmap = CreateDIBSection(screen_dc, 120 HBITMAP hbitmap = CreateDIBSection(screen_dc,
177 reinterpret_cast<BITMAPINFO*>(&hdr), 0, 121 reinterpret_cast<BITMAPINFO*>(&hdr), 0,
178 &data, 122 &data,
179 shared_section, 0); 123 shared_section, 0);
180 if (!hbitmap) { 124 if (!hbitmap) {
181 CrashForBitmapAllocationFailure(width, height);
182 return NULL; 125 return NULL;
183 } 126 }
184 127
185 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); 128 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
186 bitmap.setPixels(data); 129 bitmap.setPixels(data);
187 bitmap.setIsOpaque(is_opaque); 130 bitmap.setIsOpaque(is_opaque);
188 131
189 // If we were given data, then don't clobber it! 132 // If we were given data, then don't clobber it!
190 if (!shared_section) { 133 if (!shared_section) {
191 if (is_opaque) { 134 if (is_opaque) {
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 } 256 }
314 257
315 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice( 258 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice(
316 SkBitmap::Config config, int width, int height, bool isOpaque, 259 SkBitmap::Config config, int width, int height, bool isOpaque,
317 Usage /*usage*/) { 260 Usage /*usage*/) {
318 SkASSERT(config == SkBitmap::kARGB_8888_Config); 261 SkASSERT(config == SkBitmap::kARGB_8888_Config);
319 return BitmapPlatformDevice::create(width, height, isOpaque, NULL); 262 return BitmapPlatformDevice::create(width, height, isOpaque, NULL);
320 } 263 }
321 264
322 } // namespace skia 265 } // namespace skia
OLDNEW
« no previous file with comments | « no previous file | skia/ext/canvas_paint_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698