OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 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 "skia/ext/bitmap_platform_device_mac.h" | 5 #include "skia/ext/bitmap_platform_device_mac.h" |
6 | 6 |
7 #include <time.h> | 7 #include <time.h> |
8 | 8 |
9 #include "base/ref_counted.h" | 9 #include "base/ref_counted.h" |
10 #include "skia/ext/skia_utils_mac.h" | 10 #include "skia/ext/skia_utils_mac.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 if (overflow > 0) { | 37 if (overflow > 0) { |
38 *size -= overflow; | 38 *size -= overflow; |
39 } | 39 } |
40 } else { | 40 } else { |
41 // Fill up available size. | 41 // Fill up available size. |
42 *size = available_size - *position; | 42 *size = available_size - *position; |
43 } | 43 } |
44 return true; | 44 return true; |
45 } | 45 } |
46 | 46 |
| 47 static CGContextRef CGContextForData(void* data, int width, int height) { |
| 48 CGColorSpaceRef color_space = |
| 49 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); |
| 50 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple |
| 51 // recommends these flags for improved CG performance. |
| 52 CGContextRef context = |
| 53 CGBitmapContextCreate(data, width, height, 8, width * 4, |
| 54 color_space, |
| 55 kCGImageAlphaPremultipliedFirst | |
| 56 kCGBitmapByteOrder32Host); |
| 57 CGColorSpaceRelease(color_space); |
| 58 |
| 59 if (!context) |
| 60 return NULL; |
| 61 |
| 62 // Change the coordinate system to match WebCore's |
| 63 CGContextTranslateCTM(context, 0, height); |
| 64 CGContextScaleCTM(context, 1.0, -1.0); |
| 65 |
| 66 return context; |
| 67 } |
| 68 |
47 } // namespace | 69 } // namespace |
48 | 70 |
49 class BitmapPlatformDevice::BitmapPlatformDeviceData : public SkRefCnt { | 71 class BitmapPlatformDevice::BitmapPlatformDeviceData : public SkRefCnt { |
50 public: | 72 public: |
51 explicit BitmapPlatformDeviceData(CGContextRef bitmap); | 73 explicit BitmapPlatformDeviceData(CGContextRef bitmap); |
52 | 74 |
53 // Create/destroy CoreGraphics context for our bitmap data. | 75 // Create/destroy CoreGraphics context for our bitmap data. |
54 CGContextRef GetBitmapContext() { | 76 CGContextRef GetBitmapContext() { |
55 LoadConfig(); | 77 LoadConfig(); |
56 return bitmap_context_; | 78 return bitmap_context_; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 t.setTranslateX(-t.getTranslateX()); | 168 t.setTranslateX(-t.getTranslateX()); |
147 t.setTranslateY(-t.getTranslateY()); | 169 t.setTranslateY(-t.getTranslateY()); |
148 LoadClippingRegionToCGContext(bitmap_context_, clip_region_, t); | 170 LoadClippingRegionToCGContext(bitmap_context_, clip_region_, t); |
149 } | 171 } |
150 | 172 |
151 | 173 |
152 // We use this static factory function instead of the regular constructor so | 174 // We use this static factory function instead of the regular constructor so |
153 // that we can create the pixel data before calling the constructor. This is | 175 // that we can create the pixel data before calling the constructor. This is |
154 // required so that we can call the base class' constructor with the pixel | 176 // required so that we can call the base class' constructor with the pixel |
155 // data. | 177 // data. |
156 BitmapPlatformDevice* BitmapPlatformDevice::Create(CGContextRef context, | 178 BitmapPlatformDevice* BitmapPlatformDevice::CreateWithContext( |
157 int width, | 179 CGContextRef context, int width, int height, bool is_opaque) { |
158 int height, | |
159 bool is_opaque) { | |
160 SkBitmap bitmap; | 180 SkBitmap bitmap; |
161 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 181 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
162 if (bitmap.allocPixels() != true) | 182 if (bitmap.allocPixels() != true) |
163 return NULL; | 183 return NULL; |
164 void* data = bitmap.getPixels(); | 184 |
| 185 void* data = NULL; |
| 186 if (context) { |
| 187 data = CGBitmapContextGetData(context); |
| 188 bitmap.setPixels(data); |
| 189 } else { |
| 190 data = bitmap.getPixels(); |
| 191 } |
165 | 192 |
166 // Note: The Windows implementation clears the Bitmap later on. | 193 // Note: The Windows implementation clears the Bitmap later on. |
167 // This bears mentioning since removal of this line makes the | 194 // This bears mentioning since removal of this line makes the |
168 // unit tests only fail periodically (or when MallocPreScribble is set). | 195 // unit tests only fail periodically (or when MallocPreScribble is set). |
169 bitmap.eraseARGB(0, 0, 0, 0); | 196 bitmap.eraseARGB(0, 0, 0, 0); |
170 | 197 |
171 bitmap.setIsOpaque(is_opaque); | 198 bitmap.setIsOpaque(is_opaque); |
172 | 199 |
| 200 #ifndef NDEBUG |
173 if (is_opaque) { | 201 if (is_opaque) { |
174 #ifndef NDEBUG | |
175 // To aid in finding bugs, we set the background color to something | 202 // To aid in finding bugs, we set the background color to something |
176 // obviously wrong so it will be noticable when it is not cleared | 203 // obviously wrong so it will be noticable when it is not cleared |
177 bitmap.eraseARGB(255, 0, 255, 128); // bright bluish green | 204 bitmap.eraseARGB(255, 0, 255, 128); // bright bluish green |
| 205 } |
178 #endif | 206 #endif |
179 } | |
180 | 207 |
181 if (!context) { | 208 if (!context) |
182 CGColorSpaceRef color_space = | 209 context = CGContextForData(data, width, height); |
183 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); | |
184 // allocate a bitmap context with 4 components per pixel (BGRA). Apple | |
185 // recommends these flags for improved CG performance. | |
186 context = | |
187 CGBitmapContextCreate(data, width, height, 8, width*4, | |
188 color_space, | |
189 kCGImageAlphaPremultipliedFirst | | |
190 kCGBitmapByteOrder32Host); | |
191 | |
192 // Change the coordinate system to match WebCore's | |
193 CGContextTranslateCTM(context, 0, height); | |
194 CGContextScaleCTM(context, 1.0, -1.0); | |
195 CGColorSpaceRelease(color_space); | |
196 } | |
197 | 210 |
198 // The device object will take ownership of the graphics context. | 211 // The device object will take ownership of the graphics context. |
199 return new BitmapPlatformDevice( | 212 return new BitmapPlatformDevice( |
200 new BitmapPlatformDeviceData(context), bitmap); | 213 new BitmapPlatformDeviceData(context), bitmap); |
201 } | 214 } |
202 | 215 |
| 216 BitmapPlatformDevice* BitmapPlatformDevice::CreateWithData(uint8_t* data, |
| 217 int width, |
| 218 int height, |
| 219 bool is_opaque) { |
| 220 CGContextRef context = NULL; |
| 221 if (data) |
| 222 context = CGContextForData(data, width, height); |
| 223 |
| 224 return CreateWithContext(context, width, height, is_opaque); |
| 225 } |
| 226 |
203 // The device will own the bitmap, which corresponds to also owning the pixel | 227 // The device will own the bitmap, which corresponds to also owning the pixel |
204 // data. Therefore, we do not transfer ownership to the SkDevice's bitmap. | 228 // data. Therefore, we do not transfer ownership to the SkDevice's bitmap. |
205 BitmapPlatformDevice::BitmapPlatformDevice( | 229 BitmapPlatformDevice::BitmapPlatformDevice( |
206 BitmapPlatformDeviceData* data, const SkBitmap& bitmap) | 230 BitmapPlatformDeviceData* data, const SkBitmap& bitmap) |
207 : PlatformDevice(bitmap), | 231 : PlatformDevice(bitmap), |
208 data_(data) { | 232 data_(data) { |
209 } | 233 } |
210 | 234 |
211 // The copy constructor just adds another reference to the underlying data. | 235 // The copy constructor just adds another reference to the underlying data. |
212 // We use a const cast since the default Skia definitions don't define the | 236 // We use a const cast since the default Skia definitions don't define the |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 for (int i = 0; i < height; i++) { | 323 for (int i = 0; i < height; i++) { |
300 size_t offset = (i + bitmap_start_y) * row_words + bitmap_start_x; | 324 size_t offset = (i + bitmap_start_y) * row_words + bitmap_start_x; |
301 for (int j = 0; j < width; j++) { | 325 for (int j = 0; j < width; j++) { |
302 adjustor(data + offset + j); | 326 adjustor(data + offset + j); |
303 } | 327 } |
304 } | 328 } |
305 } | 329 } |
306 } | 330 } |
307 | 331 |
308 } // namespace skia | 332 } // namespace skia |
OLD | NEW |