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
3 // found in the LICENSE file.
8 #include "base/debug/gdi_debug_util_win.h"
9 #include "base/logging.h"
10 #include "skia/ext/bitmap_platform_device_win.h"
11 #include "skia/ext/platform_canvas.h"
12 #include "third_party/skia/include/core/SkMatrix.h"
13 #include "third_party/skia/include/core/SkRefCnt.h"
14 #include "third_party/skia/include/core/SkRegion.h"
15 #include "third_party/skia/include/core/SkUtils.h"
19 HBITMAP
CreateHBitmap(int width
, int height
, bool is_opaque
,
20 HANDLE shared_section
, void** data
) {
21 // CreateDIBSection appears to get unhappy if we create an empty bitmap, so
22 // just create a minimal bitmap
23 if ((width
== 0) || (height
== 0)) {
28 BITMAPINFOHEADER hdr
= {0};
29 hdr
.biSize
= sizeof(BITMAPINFOHEADER
);
31 hdr
.biHeight
= -height
; // minus means top-down bitmap
34 hdr
.biCompression
= BI_RGB
; // no compression
36 hdr
.biXPelsPerMeter
= 1;
37 hdr
.biYPelsPerMeter
= 1;
39 hdr
.biClrImportant
= 0;
41 HBITMAP hbitmap
= CreateDIBSection(NULL
, reinterpret_cast<BITMAPINFO
*>(&hdr
),
42 0, data
, shared_section
, 0);
45 // If this call fails, we're gonna crash hard. Try to get some useful
46 // information out before we crash for post-mortem analysis.
48 base::debug::GDIBitmapAllocFailure(&hdr
, shared_section
);
58 void DrawToNativeContext(SkCanvas
* canvas
, HDC hdc
, int x
, int y
,
59 const RECT
* src_rect
) {
60 PlatformDevice
* platform_device
= GetPlatformDevice(GetTopDevice(*canvas
));
62 platform_device
->DrawToHDC(hdc
, x
, y
, src_rect
);
65 void PlatformDevice::DrawToHDC(HDC
, int x
, int y
, const RECT
* src_rect
) {}
67 HDC
BitmapPlatformDevice::GetBitmapDC() {
69 hdc_
= CreateCompatibleDC(NULL
);
71 old_hbitmap_
= static_cast<HBITMAP
>(SelectObject(hdc_
, hbitmap_
));
78 void BitmapPlatformDevice::ReleaseBitmapDC() {
80 SelectObject(hdc_
, old_hbitmap_
);
86 bool BitmapPlatformDevice::IsBitmapDCCreated()
92 void BitmapPlatformDevice::SetMatrixClip(
93 const SkMatrix
& transform
,
94 const SkRegion
& region
) {
95 transform_
= transform
;
96 clip_region_
= region
;
100 void BitmapPlatformDevice::LoadConfig() {
101 if (!config_dirty_
|| !hdc_
)
102 return; // Nothing to do.
103 config_dirty_
= false;
106 LoadTransformToDC(hdc_
, transform_
);
107 LoadClippingRegionToDC(hdc_
, clip_region_
, transform_
);
110 static void DeleteHBitmapCallback(void* addr
, void* context
) {
111 DeleteObject(static_cast<HBITMAP
>(context
));
114 static bool InstallHBitmapPixels(SkBitmap
* bitmap
, int width
, int height
,
115 bool is_opaque
, void* data
, HBITMAP hbitmap
) {
116 const SkAlphaType at
= is_opaque
? kOpaque_SkAlphaType
: kPremul_SkAlphaType
;
117 const SkImageInfo info
= SkImageInfo::MakeN32(width
, height
, at
);
118 const size_t rowBytes
= info
.minRowBytes();
119 SkColorTable
* color_table
= NULL
;
120 return bitmap
->installPixels(info
, data
, rowBytes
, color_table
,
121 DeleteHBitmapCallback
, hbitmap
);
124 // 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
126 // required so that we can call the base class' constructor with the pixel
128 BitmapPlatformDevice
* BitmapPlatformDevice::Create(
132 HANDLE shared_section
,
136 HBITMAP hbitmap
= CreateHBitmap(width
, height
, is_opaque
, shared_section
,
142 if (!InstallHBitmapPixels(&bitmap
, width
, height
, is_opaque
, data
, hbitmap
))
146 bitmap
.eraseColor(0);
149 // If we were given data, then don't clobber it!
150 if (!shared_section
&& is_opaque
)
151 // To aid in finding bugs, we set the background color to something
152 // obviously wrong so it will be noticable when it is not cleared
153 bitmap
.eraseARGB(255, 0, 255, 128); // bright bluish green
156 // The device object will take ownership of the HBITMAP. The initial refcount
157 // of the data object will be 1, which is what the constructor expects.
158 return new BitmapPlatformDevice(hbitmap
, bitmap
);
162 BitmapPlatformDevice
* BitmapPlatformDevice::Create(int width
, int height
,
164 const HANDLE shared_section
= NULL
;
165 const bool do_clear
= false;
166 return Create(width
, height
, is_opaque
, shared_section
, do_clear
);
169 // The device will own the HBITMAP, which corresponds to also owning the pixel
170 // data. Therefore, we do not transfer ownership to the SkBitmapDevice's bitmap.
171 BitmapPlatformDevice::BitmapPlatformDevice(
173 const SkBitmap
& bitmap
)
174 : SkBitmapDevice(bitmap
),
178 config_dirty_(true), // Want to load the config next time.
179 transform_(SkMatrix::I()) {
180 // The data object is already ref'ed for us by create().
181 SkDEBUGCODE(begin_paint_count_
= 0);
182 SetPlatformDevice(this, this);
183 // Initialize the clip region to the entire bitmap.
185 if (GetObject(hbitmap_
, sizeof(BITMAP
), &bitmap_data
)) {
187 rect
.set(0, 0, bitmap_data
.bmWidth
, bitmap_data
.bmHeight
);
188 clip_region_
= SkRegion(rect
);
192 BitmapPlatformDevice::~BitmapPlatformDevice() {
193 SkASSERT(begin_paint_count_
== 0);
198 HDC
BitmapPlatformDevice::BeginPlatformPaint() {
199 SkDEBUGCODE(begin_paint_count_
++);
200 return GetBitmapDC();
203 void BitmapPlatformDevice::EndPlatformPaint() {
204 SkASSERT(begin_paint_count_
--);
205 PlatformDevice::EndPlatformPaint();
208 void BitmapPlatformDevice::setMatrixClip(const SkMatrix
& transform
,
209 const SkRegion
& region
,
210 const SkClipStack
&) {
211 SetMatrixClip(transform
, region
);
214 void BitmapPlatformDevice::DrawToHDC(HDC dc
, int x
, int y
,
215 const RECT
* src_rect
) {
216 bool created_dc
= !IsBitmapDCCreated();
217 HDC source_dc
= BeginPlatformPaint();
222 temp_rect
.right
= width();
224 temp_rect
.bottom
= height();
225 src_rect
= &temp_rect
;
228 int copy_width
= src_rect
->right
- src_rect
->left
;
229 int copy_height
= src_rect
->bottom
- src_rect
->top
;
231 // We need to reset the translation for our bitmap or (0,0) won't be in the
232 // upper left anymore
236 LoadTransformToDC(source_dc
, identity
);
248 SkASSERT(copy_width
!= 0 && copy_height
!= 0);
249 BLENDFUNCTION blend_function
= {AC_SRC_OVER
, 0, 255, AC_SRC_ALPHA
};
262 LoadTransformToDC(source_dc
, transform_
);
269 const SkBitmap
& BitmapPlatformDevice::onAccessBitmap() {
270 // FIXME(brettw) OPTIMIZATION: We should only flush if we know a GDI
271 // operation has occurred on our DC.
272 if (IsBitmapDCCreated())
274 return SkBitmapDevice::onAccessBitmap();
277 SkBaseDevice
* BitmapPlatformDevice::onCreateDevice(const CreateInfo
& cinfo
,
279 const SkImageInfo
& info
= cinfo
.fInfo
;
280 const bool do_clear
= !info
.isOpaque();
281 SkASSERT(info
.colorType() == kN32_SkColorType
);
282 return Create(info
.width(), info
.height(), info
.isOpaque(), NULL
, do_clear
);
285 // PlatformCanvas impl
287 SkCanvas
* CreatePlatformCanvas(int width
,
290 HANDLE shared_section
,
291 OnFailureType failureType
) {
292 skia::RefPtr
<SkBaseDevice
> dev
= skia::AdoptRef(
293 BitmapPlatformDevice::Create(width
, height
, is_opaque
, shared_section
));
294 return CreateCanvas(dev
, failureType
);
297 // Port of PlatformBitmap to win
299 PlatformBitmap::~PlatformBitmap() {
302 SelectObject(surface_
, reinterpret_cast<HGDIOBJ
>(platform_extra_
));
307 bool PlatformBitmap::Allocate(int width
, int height
, bool is_opaque
) {
309 HBITMAP hbitmap
= CreateHBitmap(width
, height
, is_opaque
, 0, &data
);
313 surface_
= CreateCompatibleDC(NULL
);
314 InitializeDC(surface_
);
315 // When the memory DC is created, its display surface is exactly one
316 // monochrome pixel wide and one monochrome pixel high. Save this object
317 // off, we'll restore it just before deleting the memory DC.
318 HGDIOBJ stock_bitmap
= SelectObject(surface_
, hbitmap
);
319 platform_extra_
= reinterpret_cast<intptr_t>(stock_bitmap
);
321 if (!InstallHBitmapPixels(&bitmap_
, width
, height
, is_opaque
, data
, hbitmap
))
323 bitmap_
.lockPixels();