Move remaining resources from java_staging/res to java/res.
[chromium-blink-merge.git] / skia / ext / bitmap_platform_device_win.cc
blob63108209abc313b305256319246a06fa7d03424c
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.
5 #include <windows.h>
6 #include <psapi.h>
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"
17 namespace {
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)) {
24 width = 1;
25 height = 1;
28 BITMAPINFOHEADER hdr = {0};
29 hdr.biSize = sizeof(BITMAPINFOHEADER);
30 hdr.biWidth = width;
31 hdr.biHeight = -height; // minus means top-down bitmap
32 hdr.biPlanes = 1;
33 hdr.biBitCount = 32;
34 hdr.biCompression = BI_RGB; // no compression
35 hdr.biSizeImage = 0;
36 hdr.biXPelsPerMeter = 1;
37 hdr.biYPelsPerMeter = 1;
38 hdr.biClrUsed = 0;
39 hdr.biClrImportant = 0;
41 HBITMAP hbitmap = CreateDIBSection(NULL, reinterpret_cast<BITMAPINFO*>(&hdr),
42 0, data, shared_section, 0);
44 #if !defined(_WIN64)
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.
47 if (!hbitmap)
48 base::debug::GDIBitmapAllocFailure(&hdr, shared_section);
49 #endif
51 return hbitmap;
54 } // namespace
56 namespace skia {
58 void DrawToNativeContext(SkCanvas* canvas, HDC hdc, int x, int y,
59 const RECT* src_rect) {
60 PlatformDevice* platform_device = GetPlatformDevice(GetTopDevice(*canvas));
61 if (platform_device)
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() {
68 if (!hdc_) {
69 hdc_ = CreateCompatibleDC(NULL);
70 InitializeDC(hdc_);
71 old_hbitmap_ = static_cast<HBITMAP>(SelectObject(hdc_, hbitmap_));
74 LoadConfig();
75 return hdc_;
78 void BitmapPlatformDevice::ReleaseBitmapDC() {
79 SkASSERT(hdc_);
80 SelectObject(hdc_, old_hbitmap_);
81 DeleteDC(hdc_);
82 hdc_ = NULL;
83 old_hbitmap_ = NULL;
86 bool BitmapPlatformDevice::IsBitmapDCCreated()
87 const {
88 return hdc_ != NULL;
92 void BitmapPlatformDevice::SetMatrixClip(
93 const SkMatrix& transform,
94 const SkRegion& region) {
95 transform_ = transform;
96 clip_region_ = region;
97 config_dirty_ = true;
100 void BitmapPlatformDevice::LoadConfig() {
101 if (!config_dirty_ || !hdc_)
102 return; // Nothing to do.
103 config_dirty_ = false;
105 // Transform.
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
127 // data.
128 BitmapPlatformDevice* BitmapPlatformDevice::Create(
129 int width,
130 int height,
131 bool is_opaque,
132 HANDLE shared_section,
133 bool do_clear) {
135 void* data;
136 HBITMAP hbitmap = CreateHBitmap(width, height, is_opaque, shared_section,
137 &data);
138 if (!hbitmap)
139 return NULL;
141 SkBitmap bitmap;
142 if (!InstallHBitmapPixels(&bitmap, width, height, is_opaque, data, hbitmap))
143 return NULL;
145 if (do_clear)
146 bitmap.eraseColor(0);
148 #ifndef NDEBUG
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
154 #endif
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);
161 // static
162 BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height,
163 bool is_opaque) {
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(
172 HBITMAP hbitmap,
173 const SkBitmap& bitmap)
174 : SkBitmapDevice(bitmap),
175 hbitmap_(hbitmap),
176 old_hbitmap_(NULL),
177 hdc_(NULL),
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.
184 BITMAP bitmap_data;
185 if (GetObject(hbitmap_, sizeof(BITMAP), &bitmap_data)) {
186 SkIRect rect;
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);
194 if (hdc_)
195 ReleaseBitmapDC();
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();
219 RECT temp_rect;
220 if (!src_rect) {
221 temp_rect.left = 0;
222 temp_rect.right = width();
223 temp_rect.top = 0;
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
233 SkMatrix identity;
234 identity.reset();
236 LoadTransformToDC(source_dc, identity);
237 if (isOpaque()) {
238 BitBlt(dc,
241 copy_width,
242 copy_height,
243 source_dc,
244 src_rect->left,
245 src_rect->top,
246 SRCCOPY);
247 } else {
248 SkASSERT(copy_width != 0 && copy_height != 0);
249 BLENDFUNCTION blend_function = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA};
250 GdiAlphaBlend(dc,
253 copy_width,
254 copy_height,
255 source_dc,
256 src_rect->left,
257 src_rect->top,
258 copy_width,
259 copy_height,
260 blend_function);
262 LoadTransformToDC(source_dc, transform_);
264 EndPlatformPaint();
265 if (created_dc)
266 ReleaseBitmapDC();
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())
273 GdiFlush();
274 return SkBitmapDevice::onAccessBitmap();
277 SkBaseDevice* BitmapPlatformDevice::onCreateDevice(const CreateInfo& cinfo,
278 const SkPaint*) {
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,
288 int height,
289 bool is_opaque,
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() {
300 if (surface_) {
301 if (platform_extra_)
302 SelectObject(surface_, reinterpret_cast<HGDIOBJ>(platform_extra_));
303 DeleteDC(surface_);
307 bool PlatformBitmap::Allocate(int width, int height, bool is_opaque) {
308 void* data;
309 HBITMAP hbitmap = CreateHBitmap(width, height, is_opaque, 0, &data);
310 if (!hbitmap)
311 return false;
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))
322 return false;
323 bitmap_.lockPixels();
325 return true;
328 } // namespace skia