| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/platform_canvas_win.h" | 8 #include "skia/ext/platform_canvas_win.h" |
| 9 | 9 |
| 10 #include "skia/ext/bitmap_platform_device_win.h" | 10 #include "skia/ext/bitmap_platform_device_win.h" |
| 11 | 11 |
| 12 namespace skia { | 12 namespace skia { |
| 13 | 13 |
| 14 // Crash on failure. | 14 // Crash on failure. |
| 15 #define CHECK(condition) if (!(condition)) __debugbreak(); | 15 #define CHECK(condition) if (!(condition)) __debugbreak(); |
| 16 | 16 |
| 17 // Crashes the process. This is called when a bitmap allocation fails, and this | 17 // Crashes the process. This is called when a bitmap allocation fails, and this |
| 18 // function tries to determine why it might have failed, and crash on different | 18 // function tries to determine why it might have failed, and crash on different |
| 19 // lines. This allows us to see in crash dumps the most likely reason for the | 19 // lines. This allows us to see in crash dumps the most likely reason for the |
| 20 // failure. It takes the size of the bitmap we were trying to allocate as its | 20 // failure. It takes the size of the bitmap we were trying to allocate as its |
| 21 // arguments so we can check that as well. | 21 // arguments so we can check that as well. |
| 22 void CrashForBitmapAllocationFailure(int w, int h) { | 22 // |
| 23 // The maximum number of GDI objects per process is 10K. If we're very close | 23 // Note that in a sandboxed renderer this function crashes when trying to |
| 24 // to that, it's probably the problem. | 24 // call GetProcessMemoryInfo() because it tries to load psapi.dll, which |
| 25 const int kLotsOfGDIObjs = 9990; | 25 // is fine but gives you a very hard to read crash dump. |
| 26 CHECK(GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS) < kLotsOfGDIObjs); | 26 __declspec(noinline) void CrashForBitmapAllocationFailure(int w, int h) { |
| 27 | |
| 28 // If the bitmap is ginormous, then we probably can't allocate it. | 27 // If the bitmap is ginormous, then we probably can't allocate it. |
| 29 // We use 64M pixels = 256MB @ 4 bytes per pixel. | 28 // We use 64M pixels = 256MB @ 4 bytes per pixel. |
| 30 const __int64 kGinormousBitmapPxl = 64000000; | 29 const __int64 kGinormousBitmapPxl = 64000000; |
| 31 CHECK(static_cast<__int64>(w) * static_cast<__int64>(h) < | 30 CHECK(static_cast<__int64>(w) * static_cast<__int64>(h) < |
| 32 kGinormousBitmapPxl); | 31 kGinormousBitmapPxl); |
| 33 | 32 |
| 33 // The maximum number of GDI objects per process is 10K. If we're very close |
| 34 // to that, it's probably the problem. |
| 35 const int kLotsOfGDIObjs = 9990; |
| 36 CHECK(GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS) < kLotsOfGDIObjs); |
| 37 |
| 34 // If we're using a crazy amount of virtual address space, then maybe there | 38 // If we're using a crazy amount of virtual address space, then maybe there |
| 35 // isn't enough for our bitmap. | 39 // isn't enough for our bitmap. |
| 36 const __int64 kLotsOfMem = 1500000000; // 1.5GB. | 40 const __int64 kLotsOfMem = 1500000000; // 1.5GB. |
| 37 PROCESS_MEMORY_COUNTERS pmc; | 41 PROCESS_MEMORY_COUNTERS pmc; |
| 38 if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) | 42 if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) |
| 39 CHECK(pmc.PagefileUsage < kLotsOfMem); | 43 CHECK(pmc.PagefileUsage < kLotsOfMem); |
| 40 | 44 |
| 41 // Everything else. | 45 // Everything else. |
| 42 CHECK(0); | 46 CHECK(0); |
| 43 } | 47 } |
| 44 | 48 |
| 49 // Crashes the process. This is called when a bitmap allocation fails but |
| 50 // unlike its cousin CrashForBitmapAllocationFailure() it tries to detect if |
| 51 // the issue was a non-valid shared bitmap handle. |
| 52 __declspec(noinline) void CrashIfInvalidSection(HANDLE shared_section) { |
| 53 DWORD handle_info = 0; |
| 54 CHECK(::GetHandleInformation(shared_section, &handle_info) == TRUE); |
| 55 } |
| 56 |
| 45 PlatformCanvasWin::PlatformCanvasWin() : SkCanvas() { | 57 PlatformCanvasWin::PlatformCanvasWin() : SkCanvas() { |
| 46 } | 58 } |
| 47 | 59 |
| 48 PlatformCanvasWin::PlatformCanvasWin(int width, int height, bool is_opaque) | 60 PlatformCanvasWin::PlatformCanvasWin(int width, int height, bool is_opaque) |
| 49 : SkCanvas() { | 61 : SkCanvas() { |
| 50 bool initialized = initialize(width, height, is_opaque, NULL); | 62 bool initialized = initialize(width, height, is_opaque, NULL); |
| 51 if (!initialized) | 63 if (!initialized) |
| 52 CrashForBitmapAllocationFailure(width, height); | 64 CrashForBitmapAllocationFailure(width, height); |
| 53 } | 65 } |
| 54 | 66 |
| 55 PlatformCanvasWin::PlatformCanvasWin(int width, | 67 PlatformCanvasWin::PlatformCanvasWin(int width, |
| 56 int height, | 68 int height, |
| 57 bool is_opaque, | 69 bool is_opaque, |
| 58 HANDLE shared_section) | 70 HANDLE shared_section) |
| 59 : SkCanvas() { | 71 : SkCanvas() { |
| 60 bool initialized = initialize(width, height, is_opaque, shared_section); | 72 bool initialized = initialize(width, height, is_opaque, shared_section); |
| 61 if (!initialized) | 73 if (!initialized) { |
| 74 CrashIfInvalidSection(shared_section); |
| 62 CrashForBitmapAllocationFailure(width, height); | 75 CrashForBitmapAllocationFailure(width, height); |
| 76 } |
| 63 } | 77 } |
| 64 | 78 |
| 65 PlatformCanvasWin::~PlatformCanvasWin() { | 79 PlatformCanvasWin::~PlatformCanvasWin() { |
| 66 } | 80 } |
| 67 | 81 |
| 68 bool PlatformCanvasWin::initialize(int width, | 82 bool PlatformCanvasWin::initialize(int width, |
| 69 int height, | 83 int height, |
| 70 bool is_opaque, | 84 bool is_opaque, |
| 71 HANDLE shared_section) { | 85 HANDLE shared_section) { |
| 72 SkDevice* device = | 86 SkDevice* device = |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 SkASSERT(false); // Should not be called. | 131 SkASSERT(false); // Should not be called. |
| 118 return NULL; | 132 return NULL; |
| 119 } | 133 } |
| 120 | 134 |
| 121 // static | 135 // static |
| 122 size_t PlatformCanvasWin::StrideForWidth(unsigned width) { | 136 size_t PlatformCanvasWin::StrideForWidth(unsigned width) { |
| 123 return 4 * width; | 137 return 4 * width; |
| 124 } | 138 } |
| 125 | 139 |
| 126 } // namespace skia | 140 } // namespace skia |
| OLD | NEW |