Chromium Code Reviews| 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/gdi_debug_util_win.h" | 8 #include "base/debug/gdi_debug_util_win.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "skia/ext/bitmap_platform_device_win.h" | 10 #include "skia/ext/bitmap_platform_device_win.h" |
| 11 #include "skia/ext/platform_canvas.h" | 11 #include "skia/ext/platform_canvas.h" |
| 12 #include "third_party/skia/include/core/SkMatrix.h" | 12 #include "third_party/skia/include/core/SkMatrix.h" |
| 13 #include "third_party/skia/include/core/SkRefCnt.h" | 13 #include "third_party/skia/include/core/SkRefCnt.h" |
| 14 #include "third_party/skia/include/core/SkRegion.h" | 14 #include "third_party/skia/include/core/SkRegion.h" |
| 15 #include "third_party/skia/include/core/SkUtils.h" | 15 #include "third_party/skia/include/core/SkUtils.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 typedef decltype(GetProcessMitigationPolicy)* GetProcessMitigationPolicyType; | |
| 20 | |
| 21 bool IsWin32kLockdownEnabled() { | |
| 22 GetProcessMitigationPolicyType GetProcessMitigationPolicyFunc = | |
|
scottmg
2015/09/04 02:46:19
GetProcessMitigationPolicyFunc should be get_proce
forshaw
2015/09/04 09:58:05
Done.
| |
| 23 reinterpret_cast<GetProcessMitigationPolicyType>(::GetProcAddress( | |
|
scottmg
2015/09/04 02:46:19
remove unnecessary :: qualifiers (or add them ever
forshaw
2015/09/04 09:58:05
Done.
| |
| 24 ::GetModuleHandle(L"kernel32.dll"), | |
| 25 "GetProcessMitigationPolicy")); | |
| 26 | |
| 27 if (!GetProcessMitigationPolicyFunc) | |
| 28 return false; | |
| 29 | |
| 30 PROCESS_MITIGATION_SYSTEM_CALL_DISABLE_POLICY policy = {0}; | |
| 31 if (GetProcessMitigationPolicyFunc(GetCurrentProcess(), | |
| 32 ProcessSystemCallDisablePolicy, | |
| 33 &policy, | |
| 34 sizeof(policy))) | |
| 35 return policy.DisallowWin32kSystemCalls != 0; | |
| 36 | |
| 37 return false; | |
|
Will Harris
2015/09/04 02:25:50
can you cache the result of this function with a s
forshaw
2015/09/04 09:58:05
Will cache with static, don't think it needs to be
| |
| 38 } | |
| 39 | |
| 19 HBITMAP CreateHBitmap(int width, int height, bool is_opaque, | 40 HBITMAP CreateHBitmap(int width, int height, bool is_opaque, |
| 20 HANDLE shared_section, void** data) { | 41 HANDLE shared_section, void** data) { |
| 21 // CreateDIBSection appears to get unhappy if we create an empty bitmap, so | 42 // CreateDIBSection appears to get unhappy if we create an empty bitmap, so |
| 22 // just create a minimal bitmap | 43 // just create a minimal bitmap |
| 23 if ((width == 0) || (height == 0)) { | 44 if ((width == 0) || (height == 0)) { |
| 24 width = 1; | 45 width = 1; |
| 25 height = 1; | 46 height = 1; |
| 26 } | 47 } |
| 27 | 48 |
| 28 BITMAPINFOHEADER hdr = {0}; | 49 BITMAPINFOHEADER hdr = {0}; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 50 | 71 |
| 51 return hbitmap; | 72 return hbitmap; |
| 52 } | 73 } |
| 53 | 74 |
| 54 } // namespace | 75 } // namespace |
| 55 | 76 |
| 56 namespace skia { | 77 namespace skia { |
| 57 | 78 |
| 58 void DrawToNativeContext(SkCanvas* canvas, HDC hdc, int x, int y, | 79 void DrawToNativeContext(SkCanvas* canvas, HDC hdc, int x, int y, |
| 59 const RECT* src_rect) { | 80 const RECT* src_rect) { |
| 81 DCHECK(!IsWin32kLockdownEnabled()); | |
| 60 PlatformDevice* platform_device = GetPlatformDevice(GetTopDevice(*canvas)); | 82 PlatformDevice* platform_device = GetPlatformDevice(GetTopDevice(*canvas)); |
| 61 if (platform_device) | 83 if (platform_device) |
| 62 platform_device->DrawToHDC(hdc, x, y, src_rect); | 84 platform_device->DrawToHDC(hdc, x, y, src_rect); |
| 63 } | 85 } |
| 64 | 86 |
| 65 void PlatformDevice::DrawToHDC(HDC, int x, int y, const RECT* src_rect) {} | 87 void PlatformDevice::DrawToHDC(HDC, int x, int y, const RECT* src_rect) {} |
| 66 | 88 |
| 67 HDC BitmapPlatformDevice::GetBitmapDC() { | 89 HDC BitmapPlatformDevice::GetBitmapDC() { |
| 90 DCHECK(!IsWin32kLockdownEnabled()); | |
| 68 if (!hdc_) { | 91 if (!hdc_) { |
| 69 hdc_ = CreateCompatibleDC(NULL); | 92 hdc_ = CreateCompatibleDC(NULL); |
| 70 InitializeDC(hdc_); | 93 InitializeDC(hdc_); |
| 71 old_hbitmap_ = static_cast<HBITMAP>(SelectObject(hdc_, hbitmap_)); | 94 old_hbitmap_ = static_cast<HBITMAP>(SelectObject(hdc_, hbitmap_)); |
| 72 } | 95 } |
| 73 | 96 |
| 74 LoadConfig(); | 97 LoadConfig(); |
| 75 return hdc_; | 98 return hdc_; |
| 76 } | 99 } |
| 77 | 100 |
| 78 void BitmapPlatformDevice::ReleaseBitmapDC() { | 101 void BitmapPlatformDevice::ReleaseBitmapDC() { |
| 79 SkASSERT(hdc_); | 102 if (!IsWin32kLockdownEnabled()) { |
| 80 SelectObject(hdc_, old_hbitmap_); | 103 SkASSERT(hdc_); |
| 81 DeleteDC(hdc_); | 104 SelectObject(hdc_, old_hbitmap_); |
| 82 hdc_ = NULL; | 105 DeleteDC(hdc_); |
| 83 old_hbitmap_ = NULL; | 106 hdc_ = NULL; |
| 107 old_hbitmap_ = NULL; | |
| 108 } | |
| 84 } | 109 } |
| 85 | 110 |
| 86 bool BitmapPlatformDevice::IsBitmapDCCreated() | 111 bool BitmapPlatformDevice::IsBitmapDCCreated() |
| 87 const { | 112 const { |
| 88 return hdc_ != NULL; | 113 return hdc_ != NULL; |
| 89 } | 114 } |
| 90 | 115 |
| 91 | 116 |
| 92 void BitmapPlatformDevice::SetMatrixClip( | 117 void BitmapPlatformDevice::SetMatrixClip( |
| 93 const SkMatrix& transform, | 118 const SkMatrix& transform, |
| 94 const SkRegion& region) { | 119 const SkRegion& region) { |
| 95 transform_ = transform; | 120 transform_ = transform; |
| 96 clip_region_ = region; | 121 clip_region_ = region; |
| 97 config_dirty_ = true; | 122 config_dirty_ = true; |
| 98 } | 123 } |
| 99 | 124 |
| 100 void BitmapPlatformDevice::LoadConfig() { | 125 void BitmapPlatformDevice::LoadConfig() { |
| 101 if (!config_dirty_ || !hdc_) | 126 if (!config_dirty_ || !hdc_) |
| 102 return; // Nothing to do. | 127 return; // Nothing to do. |
| 103 config_dirty_ = false; | 128 config_dirty_ = false; |
| 104 | 129 |
| 105 // Transform. | 130 // Transform. |
| 106 LoadTransformToDC(hdc_, transform_); | 131 LoadTransformToDC(hdc_, transform_); |
| 107 LoadClippingRegionToDC(hdc_, clip_region_, transform_); | 132 LoadClippingRegionToDC(hdc_, clip_region_, transform_); |
| 108 } | 133 } |
| 109 | 134 |
| 110 static void DeleteHBitmapCallback(void* addr, void* context) { | 135 static void DeleteHBitmapCallback(void* addr, void* context) { |
| 111 DeleteObject(static_cast<HBITMAP>(context)); | 136 // If context is not NULL then it's a valid HBITMAP to delete. |
| 137 // Otherwise we just unmap the pixel memory. | |
| 138 if (context) | |
| 139 DeleteObject(static_cast<HBITMAP>(context)); | |
| 140 else | |
| 141 UnmapViewOfFile(addr); | |
| 112 } | 142 } |
| 113 | 143 |
| 114 static bool InstallHBitmapPixels(SkBitmap* bitmap, int width, int height, | 144 static bool InstallHBitmapPixels(SkBitmap* bitmap, int width, int height, |
| 115 bool is_opaque, void* data, HBITMAP hbitmap) { | 145 bool is_opaque, void* data, HBITMAP hbitmap) { |
| 116 const SkAlphaType at = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; | 146 const SkAlphaType at = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 117 const SkImageInfo info = SkImageInfo::MakeN32(width, height, at); | 147 const SkImageInfo info = SkImageInfo::MakeN32(width, height, at); |
| 118 const size_t rowBytes = info.minRowBytes(); | 148 const size_t rowBytes = info.minRowBytes(); |
| 119 SkColorTable* color_table = NULL; | 149 SkColorTable* color_table = NULL; |
| 150 | |
|
Will Harris
2015/09/04 02:25:50
nit: line
| |
| 120 return bitmap->installPixels(info, data, rowBytes, color_table, | 151 return bitmap->installPixels(info, data, rowBytes, color_table, |
| 121 DeleteHBitmapCallback, hbitmap); | 152 DeleteHBitmapCallback, hbitmap); |
| 122 } | 153 } |
| 123 | 154 |
| 124 // We use this static factory function instead of the regular constructor so | 155 // We use this static factory function instead of the regular constructor so |
| 125 // that we can create the pixel data before calling the constructor. This is | 156 // that we can create the pixel data before calling the constructor. This is |
| 126 // required so that we can call the base class' constructor with the pixel | 157 // required so that we can call the base class' constructor with the pixel |
| 127 // data. | 158 // data. |
| 128 BitmapPlatformDevice* BitmapPlatformDevice::Create( | 159 BitmapPlatformDevice* BitmapPlatformDevice::Create( |
| 129 int width, | 160 int width, |
| 130 int height, | 161 int height, |
| 131 bool is_opaque, | 162 bool is_opaque, |
| 132 HANDLE shared_section, | 163 HANDLE shared_section, |
| 133 bool do_clear) { | 164 bool do_clear) { |
| 134 | 165 |
| 135 void* data; | 166 void* data; |
| 136 HBITMAP hbitmap = CreateHBitmap(width, height, is_opaque, shared_section, | 167 |
| 168 HBITMAP hbitmap = NULL; | |
|
scottmg
2015/09/04 02:46:19
NULL -> nullptr, and elsewhere
forshaw
2015/09/04 09:58:05
Was just being consistent with the rest of the fil
| |
| 169 | |
| 170 if (IsWin32kLockdownEnabled()) { | |
| 171 CHECK(shared_section != NULL); | |
| 172 data = MapViewOfFile(shared_section, FILE_MAP_WRITE, | |
| 173 0, 0, width * height * 4); | |
| 174 DCHECK(data != NULL); | |
| 175 if (!data) | |
| 176 return NULL; | |
| 177 } else { | |
| 178 hbitmap = CreateHBitmap(width, height, is_opaque, shared_section, | |
| 137 &data); | 179 &data); |
| 138 if (!hbitmap) | 180 if (!hbitmap) |
| 139 return NULL; | 181 return NULL; |
| 182 } | |
| 140 | 183 |
| 141 SkBitmap bitmap; | 184 SkBitmap bitmap; |
| 142 if (!InstallHBitmapPixels(&bitmap, width, height, is_opaque, data, hbitmap)) | 185 if (!InstallHBitmapPixels(&bitmap, width, height, is_opaque, data, hbitmap)) |
| 143 return NULL; | 186 return NULL; |
| 144 | 187 |
| 145 if (do_clear) | 188 if (do_clear) |
| 146 bitmap.eraseColor(0); | 189 bitmap.eraseColor(0); |
| 147 | 190 |
| 148 #ifndef NDEBUG | 191 #ifndef NDEBUG |
| 149 // If we were given data, then don't clobber it! | 192 // If we were given data, then don't clobber it! |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 173 const SkBitmap& bitmap) | 216 const SkBitmap& bitmap) |
| 174 : SkBitmapDevice(bitmap), | 217 : SkBitmapDevice(bitmap), |
| 175 hbitmap_(hbitmap), | 218 hbitmap_(hbitmap), |
| 176 old_hbitmap_(NULL), | 219 old_hbitmap_(NULL), |
| 177 hdc_(NULL), | 220 hdc_(NULL), |
| 178 config_dirty_(true), // Want to load the config next time. | 221 config_dirty_(true), // Want to load the config next time. |
| 179 transform_(SkMatrix::I()) { | 222 transform_(SkMatrix::I()) { |
| 180 // The data object is already ref'ed for us by create(). | 223 // The data object is already ref'ed for us by create(). |
| 181 SkDEBUGCODE(begin_paint_count_ = 0); | 224 SkDEBUGCODE(begin_paint_count_ = 0); |
| 182 SetPlatformDevice(this, this); | 225 SetPlatformDevice(this, this); |
| 183 // Initialize the clip region to the entire bitmap. | 226 if (hbitmap) { |
| 184 BITMAP bitmap_data; | 227 // Initialize the clip region to the entire bitmap. |
| 185 if (GetObject(hbitmap_, sizeof(BITMAP), &bitmap_data)) { | 228 BITMAP bitmap_data; |
| 186 SkIRect rect; | 229 if (GetObject(hbitmap_, sizeof(BITMAP), &bitmap_data)) { |
| 187 rect.set(0, 0, bitmap_data.bmWidth, bitmap_data.bmHeight); | 230 SkIRect rect; |
| 188 clip_region_ = SkRegion(rect); | 231 rect.set(0, 0, bitmap_data.bmWidth, bitmap_data.bmHeight); |
| 232 clip_region_ = SkRegion(rect); | |
| 233 } | |
| 189 } | 234 } |
| 190 } | 235 } |
| 191 | 236 |
| 192 BitmapPlatformDevice::~BitmapPlatformDevice() { | 237 BitmapPlatformDevice::~BitmapPlatformDevice() { |
| 193 SkASSERT(begin_paint_count_ == 0); | 238 SkASSERT(begin_paint_count_ == 0); |
| 194 if (hdc_) | 239 if (hdc_) |
| 195 ReleaseBitmapDC(); | 240 ReleaseBitmapDC(); |
| 196 } | 241 } |
| 197 | 242 |
| 198 HDC BitmapPlatformDevice::BeginPlatformPaint() { | 243 HDC BitmapPlatformDevice::BeginPlatformPaint() { |
| 199 SkDEBUGCODE(begin_paint_count_++); | 244 SkDEBUGCODE(begin_paint_count_++); |
| 200 return GetBitmapDC(); | 245 return GetBitmapDC(); |
| 201 } | 246 } |
| 202 | 247 |
| 203 void BitmapPlatformDevice::EndPlatformPaint() { | 248 void BitmapPlatformDevice::EndPlatformPaint() { |
| 204 SkASSERT(begin_paint_count_--); | 249 SkASSERT(begin_paint_count_--); |
| 205 PlatformDevice::EndPlatformPaint(); | 250 PlatformDevice::EndPlatformPaint(); |
| 206 } | 251 } |
| 207 | 252 |
| 208 void BitmapPlatformDevice::setMatrixClip(const SkMatrix& transform, | 253 void BitmapPlatformDevice::setMatrixClip(const SkMatrix& transform, |
| 209 const SkRegion& region, | 254 const SkRegion& region, |
| 210 const SkClipStack&) { | 255 const SkClipStack&) { |
| 211 SetMatrixClip(transform, region); | 256 SetMatrixClip(transform, region); |
| 212 } | 257 } |
| 213 | 258 |
| 214 void BitmapPlatformDevice::DrawToHDC(HDC dc, int x, int y, | 259 void BitmapPlatformDevice::DrawToHDC(HDC dc, int x, int y, |
| 215 const RECT* src_rect) { | 260 const RECT* src_rect) { |
| 261 DCHECK(!IsWin32kLockdownEnabled()); | |
| 262 | |
| 216 bool created_dc = !IsBitmapDCCreated(); | 263 bool created_dc = !IsBitmapDCCreated(); |
| 217 HDC source_dc = BeginPlatformPaint(); | 264 HDC source_dc = BeginPlatformPaint(); |
| 218 | 265 |
| 219 RECT temp_rect; | 266 RECT temp_rect; |
| 220 if (!src_rect) { | 267 if (!src_rect) { |
| 221 temp_rect.left = 0; | 268 temp_rect.left = 0; |
| 222 temp_rect.right = width(); | 269 temp_rect.right = width(); |
| 223 temp_rect.top = 0; | 270 temp_rect.top = 0; |
| 224 temp_rect.bottom = height(); | 271 temp_rect.bottom = height(); |
| 225 src_rect = &temp_rect; | 272 src_rect = &temp_rect; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 | 345 |
| 299 PlatformBitmap::~PlatformBitmap() { | 346 PlatformBitmap::~PlatformBitmap() { |
| 300 if (surface_) { | 347 if (surface_) { |
| 301 if (platform_extra_) | 348 if (platform_extra_) |
| 302 SelectObject(surface_, reinterpret_cast<HGDIOBJ>(platform_extra_)); | 349 SelectObject(surface_, reinterpret_cast<HGDIOBJ>(platform_extra_)); |
| 303 DeleteDC(surface_); | 350 DeleteDC(surface_); |
| 304 } | 351 } |
| 305 } | 352 } |
| 306 | 353 |
| 307 bool PlatformBitmap::Allocate(int width, int height, bool is_opaque) { | 354 bool PlatformBitmap::Allocate(int width, int height, bool is_opaque) { |
| 355 DCHECK(!IsWin32kLockdownEnabled()); | |
| 308 void* data; | 356 void* data; |
| 309 HBITMAP hbitmap = CreateHBitmap(width, height, is_opaque, 0, &data); | 357 HBITMAP hbitmap = CreateHBitmap(width, height, is_opaque, 0, &data); |
| 310 if (!hbitmap) | 358 if (!hbitmap) |
| 311 return false; | 359 return false; |
| 312 | 360 |
| 313 surface_ = CreateCompatibleDC(NULL); | 361 surface_ = CreateCompatibleDC(NULL); |
| 314 InitializeDC(surface_); | 362 InitializeDC(surface_); |
| 315 // When the memory DC is created, its display surface is exactly one | 363 // When the memory DC is created, its display surface is exactly one |
| 316 // monochrome pixel wide and one monochrome pixel high. Save this object | 364 // monochrome pixel wide and one monochrome pixel high. Save this object |
| 317 // off, we'll restore it just before deleting the memory DC. | 365 // off, we'll restore it just before deleting the memory DC. |
| 318 HGDIOBJ stock_bitmap = SelectObject(surface_, hbitmap); | 366 HGDIOBJ stock_bitmap = SelectObject(surface_, hbitmap); |
| 319 platform_extra_ = reinterpret_cast<intptr_t>(stock_bitmap); | 367 platform_extra_ = reinterpret_cast<intptr_t>(stock_bitmap); |
| 320 | 368 |
| 321 if (!InstallHBitmapPixels(&bitmap_, width, height, is_opaque, data, hbitmap)) | 369 if (!InstallHBitmapPixels(&bitmap_, width, height, is_opaque, data, hbitmap)) |
| 322 return false; | 370 return false; |
| 323 bitmap_.lockPixels(); | 371 bitmap_.lockPixels(); |
| 324 | 372 |
| 325 return true; | 373 return true; |
| 326 } | 374 } |
| 327 | 375 |
| 328 } // namespace skia | 376 } // namespace skia |
| OLD | NEW |