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 "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "skia/ext/bitmap_platform_device_win.h" | 9 #include "skia/ext/bitmap_platform_device_win.h" |
10 #include "skia/ext/platform_canvas.h" | 10 #include "skia/ext/platform_canvas.h" |
11 | 11 |
12 namespace skia { | 12 namespace skia { |
13 | 13 |
14 // Disable optimizations during crash analysis. | |
15 #pragma optimize("", off) | |
16 | |
17 // Crash On Failure. |address| should be a number less than 4000. | |
18 #define COF(address, condition) if (!(condition)) *((int*) address) = 0 | |
19 | |
20 // This is called when a bitmap allocation fails, and this function tries to | |
21 // 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 // | |
26 // Note that in a sandboxed renderer this function crashes when trying to | |
27 // call GetProcessMemoryInfo() because it tries to load psapi.dll, which | |
28 // is fine but gives you a very hard to read crash dump. | |
29 void CrashForBitmapAllocationFailure(int w, int h, unsigned int error) { | |
30 // Store the extended error info in a place easy to find at debug time. | |
31 unsigned int diag_error = 0; | |
32 // If the bitmap is ginormous, then we probably can't allocate it. | |
33 // We use 32M pixels = 128MB @ 4 bytes per pixel. | |
34 const LONG_PTR kGinormousBitmapPxl = 32000000; | |
35 COF(1, LONG_PTR(w) * LONG_PTR(h) < kGinormousBitmapPxl); | |
36 | |
37 // The maximum number of GDI objects per process is 10K. If we're very close | |
38 // to that, it's probably the problem. | |
39 const unsigned int kLotsOfGDIObjects = 9990; | |
40 unsigned int num_gdi_objects = GetGuiResources(GetCurrentProcess(), | |
41 GR_GDIOBJECTS); | |
42 if (num_gdi_objects == 0) { | |
43 diag_error = GetLastError(); | |
44 COF(2, false); | |
45 } | |
46 COF(3, num_gdi_objects < kLotsOfGDIObjects); | |
47 | |
48 // If we're using a crazy amount of virtual address space, then maybe there | |
49 // isn't enough for our bitmap. | |
50 const SIZE_T kLotsOfMem = 1500000000; // 1.5GB. | |
51 PROCESS_MEMORY_COUNTERS_EX pmc; | |
52 pmc.cb = sizeof(pmc); | |
53 if (!GetProcessMemoryInfo(GetCurrentProcess(), | |
54 reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc), | |
55 sizeof(pmc))) { | |
56 diag_error = GetLastError(); | |
57 COF(4, false); | |
58 } | |
59 COF(5, pmc.PagefileUsage < kLotsOfMem); | |
60 COF(6, pmc.PrivateUsage < kLotsOfMem); | |
61 // Ok but we are somehow out of memory? | |
62 COF(7, error != ERROR_NOT_ENOUGH_MEMORY); | |
63 } | |
64 | |
65 // Crashes the process. This is called when a bitmap allocation fails but | |
66 // unlike its cousin CrashForBitmapAllocationFailure() it tries to detect if | |
67 // the issue was a non-valid shared bitmap handle. | |
68 void CrashIfInvalidSection(HANDLE shared_section) { | |
69 DWORD handle_info = 0; | |
70 COF(8, ::GetHandleInformation(shared_section, &handle_info) == TRUE); | |
71 } | |
72 | |
73 // Restore the optimization options. | |
74 #pragma optimize("", on) | |
75 | |
76 PlatformCanvas::PlatformCanvas(int width, int height, bool is_opaque) { | 14 PlatformCanvas::PlatformCanvas(int width, int height, bool is_opaque) { |
77 TRACE_EVENT2("skia", "PlatformCanvas::PlatformCanvas", | 15 TRACE_EVENT2("skia", "PlatformCanvas::PlatformCanvas", |
78 "width", width, "height", height); | 16 "width", width, "height", height); |
79 initialize(width, height, is_opaque, NULL); | 17 initialize(width, height, is_opaque, NULL); |
80 } | 18 } |
81 | 19 |
82 PlatformCanvas::PlatformCanvas(int width, | 20 PlatformCanvas::PlatformCanvas(int width, |
83 int height, | 21 int height, |
84 bool is_opaque, | 22 bool is_opaque, |
85 HANDLE shared_section) { | 23 HANDLE shared_section) { |
86 TRACE_EVENT2("skia", "PlatformCanvas::PlatformCanvas", | 24 TRACE_EVENT2("skia", "PlatformCanvas::PlatformCanvas", |
87 "width", width, "height", height); | 25 "width", width, "height", height); |
88 initialize(width, height, is_opaque, shared_section); | 26 initialize(width, height, is_opaque, shared_section); |
89 } | 27 } |
90 | 28 |
91 PlatformCanvas::~PlatformCanvas() { | 29 PlatformCanvas::~PlatformCanvas() { |
92 } | 30 } |
93 | 31 |
94 bool PlatformCanvas::initialize(int width, | 32 bool PlatformCanvas::initialize(int width, |
95 int height, | 33 int height, |
96 bool is_opaque, | 34 bool is_opaque, |
97 HANDLE shared_section) { | 35 HANDLE shared_section) { |
98 if (initializeWithDevice(BitmapPlatformDevice::Create(width, | 36 return initializeWithDevice(BitmapPlatformDevice::Create( |
99 height, | 37 width, height, is_opaque, shared_section)); |
100 is_opaque, | |
101 shared_section))) | |
102 return true; | |
103 // Investigate we failed. If we know the reason, crash in a specific place. | |
104 unsigned int error = GetLastError(); | |
105 if (shared_section) | |
106 CrashIfInvalidSection(shared_section); | |
107 CrashForBitmapAllocationFailure(width, height, error); | |
108 return false; | |
109 } | 38 } |
110 | 39 |
111 } // namespace skia | 40 } // namespace skia |
OLD | NEW |