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 "skia/ext/skia_utils_win.h" | 5 #include "skia/ext/skia_utils_win.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
10 #include "base/debug/gdi_debug_util_win.h" | 10 #include "base/debug/gdi_debug_util_win.h" |
| 11 #include "base/win/scoped_hdc.h" |
| 12 #include "base/win/win_util.h" |
11 #include "third_party/skia/include/core/SkRect.h" | 13 #include "third_party/skia/include/core/SkRect.h" |
| 14 #include "third_party/skia/include/core/SkSurface.h" |
12 #include "third_party/skia/include/core/SkTypes.h" | 15 #include "third_party/skia/include/core/SkTypes.h" |
13 | 16 |
14 namespace { | 17 namespace { |
15 | 18 |
16 static_assert(offsetof(RECT, left) == offsetof(SkIRect, fLeft), "o1"); | 19 static_assert(offsetof(RECT, left) == offsetof(SkIRect, fLeft), "o1"); |
17 static_assert(offsetof(RECT, top) == offsetof(SkIRect, fTop), "o2"); | 20 static_assert(offsetof(RECT, top) == offsetof(SkIRect, fTop), "o2"); |
18 static_assert(offsetof(RECT, right) == offsetof(SkIRect, fRight), "o3"); | 21 static_assert(offsetof(RECT, right) == offsetof(SkIRect, fRight), "o3"); |
19 static_assert(offsetof(RECT, bottom) == offsetof(SkIRect, fBottom), "o4"); | 22 static_assert(offsetof(RECT, bottom) == offsetof(SkIRect, fBottom), "o4"); |
20 static_assert(sizeof(RECT().left) == sizeof(SkIRect().fLeft), "o5"); | 23 static_assert(sizeof(RECT().left) == sizeof(SkIRect().fLeft), "o5"); |
21 static_assert(sizeof(RECT().top) == sizeof(SkIRect().fTop), "o6"); | 24 static_assert(sizeof(RECT().top) == sizeof(SkIRect().fTop), "o6"); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 source, | 162 source, |
160 src_rect.left, | 163 src_rect.left, |
161 src_rect.top, | 164 src_rect.top, |
162 copy_width, | 165 copy_width, |
163 copy_height, | 166 copy_height, |
164 blend_function); | 167 blend_function); |
165 } | 168 } |
166 LoadTransformToDC(source, transform); | 169 LoadTransformToDC(source, transform); |
167 } | 170 } |
168 | 171 |
| 172 SkImageInfo PrepareAllocation(HDC context, BITMAP* backing) { |
| 173 HBITMAP backing_handle = |
| 174 static_cast<HBITMAP>(GetCurrentObject(context, OBJ_BITMAP)); |
| 175 const size_t backing_size = sizeof *backing; |
| 176 return (GetObject(backing_handle, backing_size, backing) == backing_size) |
| 177 ? SkImageInfo::MakeN32Premul(backing->bmWidth, backing->bmHeight) |
| 178 : SkImageInfo(); |
| 179 } |
| 180 |
| 181 sk_sp<SkSurface> MapPlatformSurface(HDC context) { |
| 182 BITMAP backing; |
| 183 const SkImageInfo size(PrepareAllocation(context, &backing)); |
| 184 return size.isEmpty() ? nullptr |
| 185 : SkSurface::MakeRasterDirect(size, backing.bmBits, |
| 186 backing.bmWidthBytes); |
| 187 } |
| 188 |
| 189 SkBitmap MapPlatformBitmap(HDC context) { |
| 190 BITMAP backing; |
| 191 const SkImageInfo size(PrepareAllocation(context, &backing)); |
| 192 SkBitmap bitmap; |
| 193 if (!size.isEmpty()) |
| 194 bitmap.installPixels(size, backing.bmBits, size.minRowBytes()); |
| 195 return bitmap; |
| 196 } |
| 197 |
| 198 HDC CreateOffscreenSurface(int width, int height) { |
| 199 HBITMAP bitmap = nullptr; |
| 200 |
| 201 // If this process doesn't have access to GDI, we'll have to use a shared |
| 202 // memory segment instead. |
| 203 if (!base::win::IsUser32AndGdi32Available()) |
| 204 return nullptr; |
| 205 |
| 206 bitmap = CreateHBitmap(width, height, false, nullptr, nullptr); |
| 207 if (!bitmap) |
| 208 return nullptr; |
| 209 |
| 210 base::win::ScopedCreateDC scoped_hdc(CreateCompatibleDC(nullptr)); |
| 211 if (!scoped_hdc.IsValid()) |
| 212 return nullptr; |
| 213 InitializeDC(scoped_hdc.Get()); |
| 214 HRGN clip = CreateRectRgn(0, 0, width, height); |
| 215 if ((SelectClipRgn(scoped_hdc.Get(), clip) == ERROR) || |
| 216 (!DeleteObject(clip))) |
| 217 return nullptr; |
| 218 |
| 219 SelectObject(scoped_hdc.Get(), bitmap); |
| 220 |
| 221 // The caller must call DeleteDC(hdc) on this object once done with it. |
| 222 return scoped_hdc.Take(); |
| 223 } |
| 224 |
169 void CreateBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr) { | 225 void CreateBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr) { |
170 CreateBitmapHeaderWithColorDepth(width, height, 32, hdr); | 226 CreateBitmapHeaderWithColorDepth(width, height, 32, hdr); |
171 } | 227 } |
172 | 228 |
173 HBITMAP CreateHBitmap(int width, int height, bool is_opaque, | 229 HBITMAP CreateHBitmap(int width, int height, bool is_opaque, |
174 HANDLE shared_section, void** data) { | 230 HANDLE shared_section, void** data) { |
175 // CreateDIBSection appears to get unhappy if we create an empty bitmap, so | 231 // CreateDIBSection fails to allocate anything if we try to create an empty |
176 // just create a minimal bitmap | 232 // bitmap, so just create a minimal bitmap. |
177 if ((width == 0) || (height == 0)) { | 233 if ((width == 0) || (height == 0)) { |
178 width = 1; | 234 width = 1; |
179 height = 1; | 235 height = 1; |
180 } | 236 } |
181 | 237 |
182 BITMAPINFOHEADER hdr = {0}; | 238 BITMAPINFOHEADER hdr = {0}; |
183 CreateBitmapHeader(width, height, &hdr); | 239 CreateBitmapHeader(width, height, &hdr); |
184 HBITMAP hbitmap = CreateDIBSection(NULL, reinterpret_cast<BITMAPINFO*>(&hdr), | 240 HBITMAP hbitmap = CreateDIBSection(NULL, reinterpret_cast<BITMAPINFO*>(&hdr), |
185 0, data, shared_section, 0); | 241 0, data, shared_section, 0); |
186 | 242 |
187 #if !defined(_WIN64) | 243 // If CreateDIBSection() failed, try to get some useful information out |
188 // If this call fails, we're gonna crash hard. Try to get some useful | 244 // before we crash for post-mortem analysis. |
189 // information out before we crash for post-mortem analysis. | |
190 if (!hbitmap) | 245 if (!hbitmap) |
191 base::debug::GDIBitmapAllocFailure(&hdr, shared_section); | 246 base::debug::GDIBitmapAllocFailure(&hdr, shared_section); |
192 #endif | |
193 | 247 |
194 return hbitmap; | 248 return hbitmap; |
195 } | 249 } |
196 | 250 |
197 } // namespace skia | 251 } // namespace skia |
198 | 252 |
OLD | NEW |