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); | |
Peter Kasting
2016/08/03 01:40:25
Tiny nit: () not actually necessary here ("sizeof
tomhudson
2016/08/03 12:55:02
Done.
| |
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) | |
Peter Kasting
2016/08/03 01:40:25
Nit: Could be combined with the next conditional
tomhudson
2016/08/03 12:55:02
Done.
| |
216 return nullptr; | |
217 if (!DeleteObject(clip)) | |
218 return nullptr; | |
219 | |
220 SelectObject(scoped_hdc.Get(), bitmap); | |
221 | |
222 // The caller must call DeleteDC(hdc) on this object once done with it. | |
223 return scoped_hdc.Take(); | |
224 } | |
225 | |
169 void CreateBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr) { | 226 void CreateBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr) { |
170 CreateBitmapHeaderWithColorDepth(width, height, 32, hdr); | 227 CreateBitmapHeaderWithColorDepth(width, height, 32, hdr); |
171 } | 228 } |
172 | 229 |
173 HBITMAP CreateHBitmap(int width, int height, bool is_opaque, | 230 HBITMAP CreateHBitmap(int width, int height, bool is_opaque, |
174 HANDLE shared_section, void** data) { | 231 HANDLE shared_section, void** data) { |
175 // CreateDIBSection appears to get unhappy if we create an empty bitmap, so | 232 // CreateDIBSection fails to allocate anything if we try to create an empty |
176 // just create a minimal bitmap | 233 // bitmap, so just create a minimal bitmap. |
177 if ((width == 0) || (height == 0)) { | 234 if ((width == 0) || (height == 0)) { |
178 width = 1; | 235 width = 1; |
179 height = 1; | 236 height = 1; |
180 } | 237 } |
181 | 238 |
182 BITMAPINFOHEADER hdr = {0}; | 239 BITMAPINFOHEADER hdr = {0}; |
183 CreateBitmapHeader(width, height, &hdr); | 240 CreateBitmapHeader(width, height, &hdr); |
184 HBITMAP hbitmap = CreateDIBSection(NULL, reinterpret_cast<BITMAPINFO*>(&hdr), | 241 HBITMAP hbitmap = CreateDIBSection(NULL, reinterpret_cast<BITMAPINFO*>(&hdr), |
185 0, data, shared_section, 0); | 242 0, data, shared_section, 0); |
186 | 243 |
187 #if !defined(_WIN64) | 244 // If CreateDIBSection() failed, try to get some useful information out |
f(malita)
2016/08/02 16:36:12
This change seems unrelated.
| |
188 // If this call fails, we're gonna crash hard. Try to get some useful | 245 // before we crash for post-mortem analysis. |
189 // information out before we crash for post-mortem analysis. | |
190 if (!hbitmap) | 246 if (!hbitmap) |
191 base::debug::GDIBitmapAllocFailure(&hdr, shared_section); | 247 base::debug::GDIBitmapAllocFailure(&hdr, shared_section); |
192 #endif | |
193 | 248 |
194 return hbitmap; | 249 return hbitmap; |
195 } | 250 } |
196 | 251 |
197 } // namespace skia | 252 } // namespace skia |
198 | 253 |
OLD | NEW |