Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: skia/ext/bitmap_platform_device_win.cc

Issue 1321913002: Modified platform bitmap to support GDI being unavailable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes from review. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 <windows.h> 5 #include <windows.h>
6 #include <psapi.h> 6 #include <psapi.h>
7 7
8 #include "base/debug/gdi_debug_util_win.h" 8 #include "base/debug/gdi_debug_util_win.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/win/win_util.h"
10 #include "skia/ext/bitmap_platform_device_win.h" 11 #include "skia/ext/bitmap_platform_device_win.h"
11 #include "skia/ext/platform_canvas.h" 12 #include "skia/ext/platform_canvas.h"
12 #include "third_party/skia/include/core/SkMatrix.h" 13 #include "third_party/skia/include/core/SkMatrix.h"
13 #include "third_party/skia/include/core/SkRefCnt.h" 14 #include "third_party/skia/include/core/SkRefCnt.h"
14 #include "third_party/skia/include/core/SkRegion.h" 15 #include "third_party/skia/include/core/SkRegion.h"
15 #include "third_party/skia/include/core/SkUtils.h" 16 #include "third_party/skia/include/core/SkUtils.h"
16 17
17 namespace { 18 namespace {
18 19
19 HBITMAP CreateHBitmap(int width, int height, bool is_opaque, 20 HBITMAP CreateHBitmap(int width, int height, bool is_opaque,
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 if (!config_dirty_ || !hdc_) 102 if (!config_dirty_ || !hdc_)
102 return; // Nothing to do. 103 return; // Nothing to do.
103 config_dirty_ = false; 104 config_dirty_ = false;
104 105
105 // Transform. 106 // Transform.
106 LoadTransformToDC(hdc_, transform_); 107 LoadTransformToDC(hdc_, transform_);
107 LoadClippingRegionToDC(hdc_, clip_region_, transform_); 108 LoadClippingRegionToDC(hdc_, clip_region_, transform_);
108 } 109 }
109 110
110 static void DeleteHBitmapCallback(void* addr, void* context) { 111 static void DeleteHBitmapCallback(void* addr, void* context) {
111 DeleteObject(static_cast<HBITMAP>(context)); 112 // If context is not NULL then it's a valid HBITMAP to delete.
113 // Otherwise we just unmap the pixel memory.
114 if (context)
115 DeleteObject(static_cast<HBITMAP>(context));
116 else
117 UnmapViewOfFile(addr);
112 } 118 }
113 119
114 static bool InstallHBitmapPixels(SkBitmap* bitmap, int width, int height, 120 static bool InstallHBitmapPixels(SkBitmap* bitmap, int width, int height,
115 bool is_opaque, void* data, HBITMAP hbitmap) { 121 bool is_opaque, void* data, HBITMAP hbitmap) {
116 const SkAlphaType at = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; 122 const SkAlphaType at = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
117 const SkImageInfo info = SkImageInfo::MakeN32(width, height, at); 123 const SkImageInfo info = SkImageInfo::MakeN32(width, height, at);
118 const size_t rowBytes = info.minRowBytes(); 124 const size_t rowBytes = info.minRowBytes();
119 SkColorTable* color_table = NULL; 125 SkColorTable* color_table = NULL;
120 return bitmap->installPixels(info, data, rowBytes, color_table, 126 return bitmap->installPixels(info, data, rowBytes, color_table,
121 DeleteHBitmapCallback, hbitmap); 127 DeleteHBitmapCallback, hbitmap);
122 } 128 }
123 129
124 // We use this static factory function instead of the regular constructor so 130 // 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 131 // 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 132 // required so that we can call the base class' constructor with the pixel
127 // data. 133 // data.
128 BitmapPlatformDevice* BitmapPlatformDevice::Create( 134 BitmapPlatformDevice* BitmapPlatformDevice::Create(
129 int width, 135 int width,
130 int height, 136 int height,
131 bool is_opaque, 137 bool is_opaque,
132 HANDLE shared_section, 138 HANDLE shared_section,
133 bool do_clear) { 139 bool do_clear) {
134 140
135 void* data; 141 void* data;
136 HBITMAP hbitmap = CreateHBitmap(width, height, is_opaque, shared_section, 142 HBITMAP hbitmap = NULL;
137 &data); 143
138 if (!hbitmap) 144 // This function contains an implementation of a Skia platform bitmap for
139 return NULL; 145 // drawing and compositing graphics. The original implementation uses Windows
146 // GDI to create the backing bitmap memory, however it's possible for a
147 // process to not have access to GDI which will cause this code to fail. It's
148 // possible to detect when GDI is unavailable and instead directly map the
149 // shared memory as the bitmap.
150 if (base::win::IsUser32AndGdi32Available()) {
151 hbitmap = CreateHBitmap(width, height, is_opaque, shared_section, &data);
152 if (!hbitmap)
153 return NULL;
154 } else {
155 DCHECK(shared_section != NULL);
156 data = MapViewOfFile(shared_section, FILE_MAP_WRITE, 0, 0,
157 PlatformCanvasStrideForWidth(width) * height);
158 if (!data)
159 return NULL;
160 }
140 161
141 SkBitmap bitmap; 162 SkBitmap bitmap;
142 if (!InstallHBitmapPixels(&bitmap, width, height, is_opaque, data, hbitmap)) 163 if (!InstallHBitmapPixels(&bitmap, width, height, is_opaque, data, hbitmap))
143 return NULL; 164 return NULL;
144 165
145 if (do_clear) 166 if (do_clear)
146 bitmap.eraseColor(0); 167 bitmap.eraseColor(0);
147 168
148 #ifndef NDEBUG 169 #ifndef NDEBUG
149 // If we were given data, then don't clobber it! 170 // If we were given data, then don't clobber it!
(...skipping 22 matching lines...) Expand all
172 HBITMAP hbitmap, 193 HBITMAP hbitmap,
173 const SkBitmap& bitmap) 194 const SkBitmap& bitmap)
174 : SkBitmapDevice(bitmap), 195 : SkBitmapDevice(bitmap),
175 hbitmap_(hbitmap), 196 hbitmap_(hbitmap),
176 old_hbitmap_(NULL), 197 old_hbitmap_(NULL),
177 hdc_(NULL), 198 hdc_(NULL),
178 config_dirty_(true), // Want to load the config next time. 199 config_dirty_(true), // Want to load the config next time.
179 transform_(SkMatrix::I()) { 200 transform_(SkMatrix::I()) {
180 // The data object is already ref'ed for us by create(). 201 // The data object is already ref'ed for us by create().
181 SkDEBUGCODE(begin_paint_count_ = 0); 202 SkDEBUGCODE(begin_paint_count_ = 0);
182 SetPlatformDevice(this, this); 203 if (hbitmap) {
183 // Initialize the clip region to the entire bitmap. 204 SetPlatformDevice(this, this);
184 BITMAP bitmap_data; 205 // Initialize the clip region to the entire bitmap.
185 if (GetObject(hbitmap_, sizeof(BITMAP), &bitmap_data)) { 206 BITMAP bitmap_data;
186 SkIRect rect; 207 if (GetObject(hbitmap_, sizeof(BITMAP), &bitmap_data)) {
187 rect.set(0, 0, bitmap_data.bmWidth, bitmap_data.bmHeight); 208 SkIRect rect;
188 clip_region_ = SkRegion(rect); 209 rect.set(0, 0, bitmap_data.bmWidth, bitmap_data.bmHeight);
210 clip_region_ = SkRegion(rect);
211 }
189 } 212 }
190 } 213 }
191 214
192 BitmapPlatformDevice::~BitmapPlatformDevice() { 215 BitmapPlatformDevice::~BitmapPlatformDevice() {
193 SkASSERT(begin_paint_count_ == 0); 216 SkASSERT(begin_paint_count_ == 0);
194 if (hdc_) 217 if (hdc_)
195 ReleaseBitmapDC(); 218 ReleaseBitmapDC();
196 } 219 }
197 220
198 HDC BitmapPlatformDevice::BeginPlatformPaint() { 221 HDC BitmapPlatformDevice::BeginPlatformPaint() {
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 platform_extra_ = reinterpret_cast<intptr_t>(stock_bitmap); 342 platform_extra_ = reinterpret_cast<intptr_t>(stock_bitmap);
320 343
321 if (!InstallHBitmapPixels(&bitmap_, width, height, is_opaque, data, hbitmap)) 344 if (!InstallHBitmapPixels(&bitmap_, width, height, is_opaque, data, hbitmap))
322 return false; 345 return false;
323 bitmap_.lockPixels(); 346 bitmap_.lockPixels();
324 347
325 return true; 348 return true;
326 } 349 }
327 350
328 } // namespace skia 351 } // namespace skia
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698