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

Side by Side Diff: webkit/port/platform/graphics/skia/public/BitmapPlatformDeviceMac.cpp

Issue 11357: Move the platform files form port to skia for Mac only. Hopefully this won't ... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 years, 1 month 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "config.h"
6
7 #include <time.h>
8
9 #include "SkMatrix.h"
10 #include "SkRegion.h"
11 #include "SkUtils.h"
12
13 #include "base/gfx/bitmap_platform_device_mac.h"
14 #include "base/gfx/skia_utils_mac.h"
15 #include "base/logging.h"
16
17 namespace gfx {
18
19 namespace {
20
21 // Constrains position and size to fit within available_size. If |size| is -1,
22 // all the |available_size| is used. Returns false if the position is out of
23 // |available_size|.
24 bool Constrain(int available_size, int* position, int *size) {
25 if (*size < -2)
26 return false;
27
28 if (*position < 0) {
29 if (*size != -1)
30 *size += *position;
31 *position = 0;
32 }
33 if (*size == 0 || *position >= available_size)
34 return false;
35
36 if (*size > 0) {
37 int overflow = (*position + *size) - available_size;
38 if (overflow > 0) {
39 *size -= overflow;
40 }
41 } else {
42 // Fill up available size.
43 *size = available_size - *position;
44 }
45 return true;
46 }
47
48 } // namespace
49
50 class BitmapPlatformDeviceMac::BitmapPlatformDeviceMacData
51 : public base::RefCounted<BitmapPlatformDeviceMacData> {
52 public:
53 explicit BitmapPlatformDeviceMacData(CGContextRef bitmap);
54
55 // Create/destroy CoreGraphics context for our bitmap data.
56 CGContextRef GetBitmapContext() {
57 LoadConfig();
58 return bitmap_context_;
59 }
60
61 void ReleaseBitmapContext() {
62 DCHECK(bitmap_context_);
63 CGContextRelease(bitmap_context_);
64 bitmap_context_ = NULL;
65 }
66
67 // Sets the transform and clip operations. This will not update the CGContext,
68 // but will mark the config as dirty. The next call of LoadConfig will
69 // pick up these changes.
70 void SetMatrixClip(const SkMatrix& transform, const SkRegion& region);
71
72 // Loads the current transform and clip into the DC. Can be called even when
73 // |bitmap_context_| is NULL (will be a NOP).
74 void LoadConfig();
75
76 // Lazily-created graphics context used to draw into the bitmap.
77 CGContextRef bitmap_context_;
78
79 // True when there is a transform or clip that has not been set to the
80 // CGContext. The CGContext is retrieved for every text operation, and the
81 // transform and clip do not change as much. We can save time by not loading
82 // the clip and transform for every one.
83 bool config_dirty_;
84
85 // Translation assigned to the CGContext: we need to keep track of this
86 // separately so it can be updated even if the CGContext isn't created yet.
87 SkMatrix transform_;
88
89 // The current clipping
90 SkRegion clip_region_;
91
92 private:
93 friend class base::RefCounted<BitmapPlatformDeviceMacData>;
94 ~BitmapPlatformDeviceMacData() {
95 if (bitmap_context_)
96 CGContextRelease(bitmap_context_);
97 }
98
99 DISALLOW_COPY_AND_ASSIGN(BitmapPlatformDeviceMacData);
100 };
101
102 BitmapPlatformDeviceMac::\
103 BitmapPlatformDeviceMacData::BitmapPlatformDeviceMacData(
104 CGContextRef bitmap)
105 : bitmap_context_(bitmap),
106 config_dirty_(true) { // Want to load the config next time.
107 DCHECK(bitmap_context_);
108 // Initialize the clip region to the entire bitmap.
109
110 SkIRect rect;
111 rect.set(0, 0,
112 CGBitmapContextGetWidth(bitmap_context_),
113 CGBitmapContextGetHeight(bitmap_context_));
114 clip_region_ = SkRegion(rect);
115 transform_.reset();
116 CGContextRetain(bitmap_context_);
117 }
118
119 void BitmapPlatformDeviceMac::BitmapPlatformDeviceMacData::SetMatrixClip(
120 const SkMatrix& transform,
121 const SkRegion& region) {
122 transform_ = transform;
123 clip_region_ = region;
124 config_dirty_ = true;
125 }
126
127 void BitmapPlatformDeviceMac::BitmapPlatformDeviceMacData::LoadConfig() {
128 if (!config_dirty_ || !bitmap_context_)
129 return; // Nothing to do.
130 config_dirty_ = false;
131
132 // Transform.
133 SkMatrix t(transform_);
134 LoadTransformToCGContext(bitmap_context_, t);
135 t.setTranslateX(-t.getTranslateX());
136 t.setTranslateY(-t.getTranslateY());
137 LoadClippingRegionToCGContext(bitmap_context_, clip_region_, t);
138 }
139
140
141 // We use this static factory function instead of the regular constructor so
142 // that we can create the pixel data before calling the constructor. This is
143 // required so that we can call the base class' constructor with the pixel
144 // data.
145 BitmapPlatformDeviceMac* BitmapPlatformDeviceMac::Create(CGContextRef context,
146 int width,
147 int height,
148 bool is_opaque) {
149 void* data = malloc(height * width * 4);
150 if (!data) return NULL;
151
152 SkBitmap bitmap;
153 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
154 bitmap.setPixels(data);
155
156 // Note: The Windows implementation clears the Bitmap later on.
157 // This bears mentioning since removal of this line makes the
158 // unit tests only fail periodically (or when MallocPreScribble is set).
159 bitmap.eraseARGB(0, 0, 0, 0);
160
161 bitmap.setIsOpaque(is_opaque);
162
163 if (is_opaque) {
164 #ifndef NDEBUG
165 // To aid in finding bugs, we set the background color to something
166 // obviously wrong so it will be noticable when it is not cleared
167 bitmap.eraseARGB(255, 0, 255, 128); // bright bluish green
168 #endif
169 }
170
171 CGColorSpaceRef color_space =
172 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
173 // allocate a bitmap context with 4 components per pixel (RGBA):
174 CGContextRef bitmap_context =
175 CGBitmapContextCreate(data, width, height, 8, width*4,
176 color_space, kCGImageAlphaPremultipliedLast);
177
178 // Change the coordinate system to match WebCore's
179 CGContextTranslateCTM(bitmap_context, 0, height);
180 CGContextScaleCTM(bitmap_context, 1.0, -1.0);
181 CGColorSpaceRelease(color_space);
182
183 // The device object will take ownership of the graphics context.
184 return new BitmapPlatformDeviceMac(
185 new BitmapPlatformDeviceMacData(bitmap_context), bitmap);
186 }
187
188 // The device will own the bitmap, which corresponds to also owning the pixel
189 // data. Therefore, we do not transfer ownership to the SkDevice's bitmap.
190 BitmapPlatformDeviceMac::BitmapPlatformDeviceMac(
191 BitmapPlatformDeviceMacData* data, const SkBitmap& bitmap)
192 : PlatformDeviceMac(bitmap),
193 data_(data) {
194 }
195
196 // The copy constructor just adds another reference to the underlying data.
197 // We use a const cast since the default Skia definitions don't define the
198 // proper constedness that we expect (accessBitmap should really be const).
199 BitmapPlatformDeviceMac::BitmapPlatformDeviceMac(
200 const BitmapPlatformDeviceMac& other)
201 : PlatformDeviceMac(
202 const_cast<BitmapPlatformDeviceMac&>(other).accessBitmap(true)),
203 data_(other.data_) {
204 }
205
206 BitmapPlatformDeviceMac::~BitmapPlatformDeviceMac() {
207 }
208
209 BitmapPlatformDeviceMac& BitmapPlatformDeviceMac::operator=(
210 const BitmapPlatformDeviceMac& other) {
211 data_ = other.data_;
212 return *this;
213 }
214
215 CGContextRef BitmapPlatformDeviceMac::GetBitmapContext() {
216 return data_->GetBitmapContext();
217 }
218
219 void BitmapPlatformDeviceMac::setMatrixClip(const SkMatrix& transform,
220 const SkRegion& region) {
221 data_->SetMatrixClip(transform, region);
222 }
223
224 void BitmapPlatformDeviceMac::DrawToContext(CGContextRef context, int x, int y,
225 const CGRect* src_rect) {
226 bool created_dc = false;
227 if (!data_->bitmap_context_) {
228 created_dc = true;
229 GetBitmapContext();
230 }
231
232 // this should not make a copy of the bits, since we're not doing
233 // anything to trigger copy on write
234 CGImageRef image = CGBitmapContextCreateImage(data_->bitmap_context_);
235 CGRect bounds;
236 if (src_rect) {
237 bounds = *src_rect;
238 bounds.origin.x = x;
239 bounds.origin.y = y;
240 CGImageRef sub_image = CGImageCreateWithImageInRect(image, *src_rect);
241 CGContextDrawImage(context, bounds, sub_image);
242 CGImageRelease(sub_image);
243 } else {
244 bounds.origin.x = 0;
245 bounds.origin.y = 0;
246 bounds.size.width = width();
247 bounds.size.height = height();
248 CGContextDrawImage(context, bounds, image);
249 }
250 CGImageRelease(image);
251
252 if (created_dc)
253 data_->ReleaseBitmapContext();
254 }
255
256 // Returns the color value at the specified location.
257 SkColor BitmapPlatformDeviceMac::getColorAt(int x, int y) {
258 const SkBitmap& bitmap = accessBitmap(true);
259 SkAutoLockPixels lock(bitmap);
260 uint32_t* data = bitmap.getAddr32(0, 0);
261 return static_cast<SkColor>(data[x + y * width()]);
262 }
263
264 void BitmapPlatformDeviceMac::onAccessBitmap(SkBitmap*) {
265 // Not needed in CoreGraphics
266 }
267
268 void BitmapPlatformDeviceMac::processPixels(int x, int y,
269 int width, int height,
270 adjustAlpha adjustor) {
271 const SkBitmap& bitmap = accessBitmap(true);
272 SkMatrix& matrix = data_->transform_;
273 int bitmap_start_x = SkScalarRound(matrix.getTranslateX()) + x;
274 int bitmap_start_y = SkScalarRound(matrix.getTranslateY()) + y;
275
276 SkAutoLockPixels lock(bitmap);
277 if (Constrain(bitmap.width(), &bitmap_start_x, &width) &&
278 Constrain(bitmap.height(), &bitmap_start_y, &height)) {
279 uint32_t* data = bitmap.getAddr32(0, 0);
280 size_t row_words = bitmap.rowBytes() / 4;
281 for (int i = 0; i < height; i++) {
282 size_t offset = (i + bitmap_start_y) * row_words + bitmap_start_x;
283 for (int j = 0; j < width; j++) {
284 adjustor(data + offset + j);
285 }
286 }
287 }
288 }
289
290 } // namespace gfx
291
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698