1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <windows.h>
#include <psapi.h>
#include "base/logging.h"
#include "base/debug/alias.h"
#include "skia/ext/bitmap_platform_device_win.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/skia/include/core/SkMatrix.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkRegion.h"
#include "third_party/skia/include/core/SkUtils.h"
namespace {
// PlatformBitmapPixelRef is an SkPixelRef that, on Windows, is backed by an
// HBITMAP.
class SK_API PlatformBitmapPixelRef : public SkPixelRef {
public:
PlatformBitmapPixelRef(HBITMAP bitmap_handle, void* pixels);
virtual ~PlatformBitmapPixelRef();
SK_DECLARE_UNFLATTENABLE_OBJECT();
protected:
virtual void* onLockPixels(SkColorTable**) SK_OVERRIDE;
virtual void onUnlockPixels() SK_OVERRIDE;
private:
HBITMAP bitmap_handle_;
void* pixels_;
};
HBITMAP CreateHBitmap(int width, int height, bool is_opaque,
HANDLE shared_section, void** data) {
// CreateDIBSection appears to get unhappy if we create an empty bitmap, so
// just create a minimal bitmap
if ((width == 0) || (height == 0)) {
width = 1;
height = 1;
}
BITMAPINFOHEADER hdr = {0};
hdr.biSize = sizeof(BITMAPINFOHEADER);
hdr.biWidth = width;
hdr.biHeight = -height; // minus means top-down bitmap
hdr.biPlanes = 1;
hdr.biBitCount = 32;
hdr.biCompression = BI_RGB; // no compression
hdr.biSizeImage = 0;
hdr.biXPelsPerMeter = 1;
hdr.biYPelsPerMeter = 1;
hdr.biClrUsed = 0;
hdr.biClrImportant = 0;
HBITMAP hbitmap = CreateDIBSection(NULL, reinterpret_cast<BITMAPINFO*>(&hdr),
0, data, shared_section, 0);
// If this call fails, we're gonna crash hard. Try to get some useful
// information out before we crash for post-mortem analysis.
if (!hbitmap) {
// Make sure parameters are saved in the minidump.
base::debug::Alias(&width);
base::debug::Alias(&height);
int last_error = GetLastError();
base::debug::Alias(&last_error);
int num_gdi_handles = GetGuiResources(GetCurrentProcess(),
GR_GDIOBJECTS);
if (num_gdi_handles == 0) {
int get_gui_resources_error = GetLastError();
base::debug::Alias(&get_gui_resources_error);
CHECK(false);
}
base::debug::Alias(&num_gdi_handles);
const int kLotsOfHandles = 9990;
if (num_gdi_handles > kLotsOfHandles)
CHECK(false);
PROCESS_MEMORY_COUNTERS_EX pmc;
pmc.cb = sizeof(pmc);
if (!GetProcessMemoryInfo(GetCurrentProcess(),
reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc),
sizeof(pmc))) {
CHECK(false);
}
const size_t kLotsOfMemory = 1500 * 1024 * 1024; // 1.5GB
if (pmc.PagefileUsage > kLotsOfMemory)
CHECK(false);
if (pmc.PrivateUsage > kLotsOfMemory)
CHECK(false);
// Huh, that's weird. We don't have crazy handle count, we don't have
// ridiculous memory usage. Try to allocate a small bitmap and see if that
// fails too.
hdr.biWidth = 5;
hdr.biHeight = 5;
void* small_data;
HBITMAP small_bitmap = CreateDIBSection(
NULL, reinterpret_cast<BITMAPINFO*>(&hdr),
0, &small_data, shared_section, 0);
if (!small_bitmap)
CHECK(false);
BITMAP bitmap_data;
if (GetObject(small_bitmap, sizeof(BITMAP), &bitmap_data)) {
if (!DeleteObject(small_bitmap))
CHECK(false);
}
// No idea what's going on. Die!
CHECK(false);
}
return hbitmap;
}
PlatformBitmapPixelRef::PlatformBitmapPixelRef(HBITMAP bitmap_handle,
void* pixels)
: bitmap_handle_(bitmap_handle),
pixels_(pixels) {
setPreLocked(pixels, NULL);
}
PlatformBitmapPixelRef::~PlatformBitmapPixelRef() {
if (bitmap_handle_)
DeleteObject(bitmap_handle_);
}
void* PlatformBitmapPixelRef::onLockPixels(SkColorTable** color_table) {
*color_table = NULL;
return pixels_;
}
void PlatformBitmapPixelRef::onUnlockPixels() {
// Nothing to do.
return;
}
} // namespace
namespace skia {
HDC BitmapPlatformDevice::GetBitmapDC() {
if (!hdc_) {
hdc_ = CreateCompatibleDC(NULL);
InitializeDC(hdc_);
old_hbitmap_ = static_cast<HBITMAP>(SelectObject(hdc_, hbitmap_));
}
LoadConfig();
return hdc_;
}
void BitmapPlatformDevice::ReleaseBitmapDC() {
SkASSERT(hdc_);
SelectObject(hdc_, old_hbitmap_);
DeleteDC(hdc_);
hdc_ = NULL;
old_hbitmap_ = NULL;
}
bool BitmapPlatformDevice::IsBitmapDCCreated()
const {
return hdc_ != NULL;
}
void BitmapPlatformDevice::SetMatrixClip(
const SkMatrix& transform,
const SkRegion& region) {
transform_ = transform;
clip_region_ = region;
config_dirty_ = true;
}
void BitmapPlatformDevice::LoadConfig() {
if (!config_dirty_ || !hdc_)
return; // Nothing to do.
config_dirty_ = false;
// Transform.
LoadTransformToDC(hdc_, transform_);
LoadClippingRegionToDC(hdc_, clip_region_, transform_);
}
// We use this static factory function instead of the regular constructor so
// that we can create the pixel data before calling the constructor. This is
// required so that we can call the base class' constructor with the pixel
// data.
BitmapPlatformDevice* BitmapPlatformDevice::Create(
int width,
int height,
bool is_opaque,
HANDLE shared_section) {
void* data;
HBITMAP hbitmap = CreateHBitmap(width, height, is_opaque, shared_section,
&data);
if (!hbitmap)
return NULL;
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0,
is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
RefPtr<SkPixelRef> pixel_ref = AdoptRef(new PlatformBitmapPixelRef(hbitmap,
data));
bitmap.setPixelRef(pixel_ref.get());
#ifndef NDEBUG
// If we were given data, then don't clobber it!
if (!shared_section && is_opaque)
// To aid in finding bugs, we set the background color to something
// obviously wrong so it will be noticable when it is not cleared
bitmap.eraseARGB(255, 0, 255, 128); // bright bluish green
#endif
// The device object will take ownership of the HBITMAP. The initial refcount
// of the data object will be 1, which is what the constructor expects.
return new BitmapPlatformDevice(hbitmap, bitmap);
}
// static
BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height,
bool is_opaque) {
return Create(width, height, is_opaque, NULL);
}
// static
BitmapPlatformDevice* BitmapPlatformDevice::CreateAndClear(int width,
int height,
bool is_opaque) {
BitmapPlatformDevice* device = BitmapPlatformDevice::Create(width, height,
is_opaque);
if (device && !is_opaque)
device->accessBitmap(true).eraseARGB(0, 0, 0, 0);
return device;
}
// The device will own the HBITMAP, which corresponds to also owning the pixel
// data. Therefore, we do not transfer ownership to the SkBitmapDevice's bitmap.
BitmapPlatformDevice::BitmapPlatformDevice(
HBITMAP hbitmap,
const SkBitmap& bitmap)
: SkBitmapDevice(bitmap),
hbitmap_(hbitmap),
old_hbitmap_(NULL),
hdc_(NULL),
config_dirty_(true), // Want to load the config next time.
transform_(SkMatrix::I()) {
// The data object is already ref'ed for us by create().
SkDEBUGCODE(begin_paint_count_ = 0);
SetPlatformDevice(this, this);
// Initialize the clip region to the entire bitmap.
BITMAP bitmap_data;
if (GetObject(hbitmap_, sizeof(BITMAP), &bitmap_data)) {
SkIRect rect;
rect.set(0, 0, bitmap_data.bmWidth, bitmap_data.bmHeight);
clip_region_ = SkRegion(rect);
}
}
BitmapPlatformDevice::~BitmapPlatformDevice() {
SkASSERT(begin_paint_count_ == 0);
if (hdc_)
ReleaseBitmapDC();
}
HDC BitmapPlatformDevice::BeginPlatformPaint() {
SkDEBUGCODE(begin_paint_count_++);
return GetBitmapDC();
}
void BitmapPlatformDevice::EndPlatformPaint() {
SkASSERT(begin_paint_count_--);
PlatformDevice::EndPlatformPaint();
}
void BitmapPlatformDevice::setMatrixClip(const SkMatrix& transform,
const SkRegion& region,
const SkClipStack&) {
SetMatrixClip(transform, region);
}
void BitmapPlatformDevice::DrawToNativeContext(HDC dc, int x, int y,
const RECT* src_rect) {
bool created_dc = !IsBitmapDCCreated();
HDC source_dc = BeginPlatformPaint();
RECT temp_rect;
if (!src_rect) {
temp_rect.left = 0;
temp_rect.right = width();
temp_rect.top = 0;
temp_rect.bottom = height();
src_rect = &temp_rect;
}
int copy_width = src_rect->right - src_rect->left;
int copy_height = src_rect->bottom - src_rect->top;
// We need to reset the translation for our bitmap or (0,0) won't be in the
// upper left anymore
SkMatrix identity;
identity.reset();
LoadTransformToDC(source_dc, identity);
if (isOpaque()) {
BitBlt(dc,
x,
y,
copy_width,
copy_height,
source_dc,
src_rect->left,
src_rect->top,
SRCCOPY);
} else {
SkASSERT(copy_width != 0 && copy_height != 0);
BLENDFUNCTION blend_function = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA};
GdiAlphaBlend(dc,
x,
y,
copy_width,
copy_height,
source_dc,
src_rect->left,
src_rect->top,
copy_width,
copy_height,
blend_function);
}
LoadTransformToDC(source_dc, transform_);
EndPlatformPaint();
if (created_dc)
ReleaseBitmapDC();
}
const SkBitmap& BitmapPlatformDevice::onAccessBitmap() {
// FIXME(brettw) OPTIMIZATION: We should only flush if we know a GDI
// operation has occurred on our DC.
if (IsBitmapDCCreated())
GdiFlush();
return SkBitmapDevice::onAccessBitmap();
}
SkBaseDevice* BitmapPlatformDevice::onCreateCompatibleDevice(
SkBitmap::Config config, int width, int height, bool isOpaque, Usage) {
SkASSERT(config == SkBitmap::kARGB_8888_Config);
return BitmapPlatformDevice::CreateAndClear(width, height, isOpaque);
}
// PlatformCanvas impl
SkCanvas* CreatePlatformCanvas(int width,
int height,
bool is_opaque,
HANDLE shared_section,
OnFailureType failureType) {
skia::RefPtr<SkBaseDevice> dev = skia::AdoptRef(
BitmapPlatformDevice::Create(width, height, is_opaque, shared_section));
return CreateCanvas(dev, failureType);
}
// Port of PlatformBitmap to win
PlatformBitmap::~PlatformBitmap() {
if (surface_) {
if (platform_extra_)
SelectObject(surface_, reinterpret_cast<HGDIOBJ>(platform_extra_));
DeleteDC(surface_);
}
}
bool PlatformBitmap::Allocate(int width, int height, bool is_opaque) {
void* data;
HBITMAP hbitmap = CreateHBitmap(width, height, is_opaque, 0, &data);
if (!hbitmap)
return false;
surface_ = CreateCompatibleDC(NULL);
InitializeDC(surface_);
// When the memory DC is created, its display surface is exactly one
// monochrome pixel wide and one monochrome pixel high. Save this object
// off, we'll restore it just before deleting the memory DC.
HGDIOBJ stock_bitmap = SelectObject(surface_, hbitmap);
platform_extra_ = reinterpret_cast<intptr_t>(stock_bitmap);
bitmap_.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0,
is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
// PlatformBitmapPixelRef takes ownership of |hbitmap|.
RefPtr<SkPixelRef> pixel_ref = AdoptRef(new PlatformBitmapPixelRef(hbitmap,
data));
bitmap_.setPixelRef(pixel_ref.get());
bitmap_.lockPixels();
return true;
}
} // namespace skia
|