| 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 "SkMatrix.h" | 9 #include "SkMatrix.h" |
| 10 #include "SkRegion.h" | 10 #include "SkRegion.h" |
| 11 #include "SkUtils.h" | 11 #include "SkUtils.h" |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "skia/ext/skia_utils_mac.h" | 14 #include "skia/ext/skia_utils_mac.h" |
| 15 | 15 |
| 16 namespace gfx { | 16 namespace skia { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // Constrains position and size to fit within available_size. If |size| is -1, | 20 // Constrains position and size to fit within available_size. If |size| is -1, |
| 21 // all the |available_size| is used. Returns false if the position is out of | 21 // all the |available_size| is used. Returns false if the position is out of |
| 22 // |available_size|. | 22 // |available_size|. |
| 23 bool Constrain(int available_size, int* position, int *size) { | 23 bool Constrain(int available_size, int* position, int *size) { |
| 24 if (*size < -2) | 24 if (*size < -2) |
| 25 return false; | 25 return false; |
| 26 | 26 |
| 27 if (*position < 0) { | 27 if (*position < 0) { |
| 28 if (*size != -1) | 28 if (*size != -1) |
| 29 *size += *position; | 29 *size += *position; |
| 30 *position = 0; | 30 *position = 0; |
| 31 } | 31 } |
| 32 if (*size == 0 || *position >= available_size) | 32 if (*size == 0 || *position >= available_size) |
| 33 return false; | 33 return false; |
| 34 | 34 |
| 35 if (*size > 0) { | 35 if (*size > 0) { |
| 36 int overflow = (*position + *size) - available_size; | 36 int overflow = (*position + *size) - available_size; |
| 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 } // namespace | 47 } // namespace |
| 48 | 48 |
| 49 class BitmapPlatformDeviceMac::BitmapPlatformDeviceMacData | 49 class BitmapPlatformDeviceMac::BitmapPlatformDeviceMacData |
| 50 : public base::RefCounted<BitmapPlatformDeviceMacData> { | 50 : public base::RefCounted<BitmapPlatformDeviceMacData> { |
| 51 public: | 51 public: |
| 52 explicit BitmapPlatformDeviceMacData(CGContextRef bitmap); | 52 explicit BitmapPlatformDeviceMacData(CGContextRef bitmap); |
| 53 | 53 |
| 54 // Create/destroy CoreGraphics context for our bitmap data. | 54 // Create/destroy CoreGraphics context for our bitmap data. |
| 55 CGContextRef GetBitmapContext() { | 55 CGContextRef GetBitmapContext() { |
| 56 LoadConfig(); | 56 LoadConfig(); |
| 57 return bitmap_context_; | 57 return bitmap_context_; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(BitmapPlatformDeviceMacData); | 98 DISALLOW_COPY_AND_ASSIGN(BitmapPlatformDeviceMacData); |
| 99 }; | 99 }; |
| 100 | 100 |
| 101 BitmapPlatformDeviceMac::\ | 101 BitmapPlatformDeviceMac::\ |
| 102 BitmapPlatformDeviceMacData::BitmapPlatformDeviceMacData( | 102 BitmapPlatformDeviceMacData::BitmapPlatformDeviceMacData( |
| 103 CGContextRef bitmap) | 103 CGContextRef bitmap) |
| 104 : bitmap_context_(bitmap), | 104 : bitmap_context_(bitmap), |
| 105 config_dirty_(true) { // Want to load the config next time. | 105 config_dirty_(true) { // Want to load the config next time. |
| 106 DCHECK(bitmap_context_); | 106 DCHECK(bitmap_context_); |
| 107 // Initialize the clip region to the entire bitmap. | 107 // Initialize the clip region to the entire bitmap. |
| 108 | 108 |
| 109 SkIRect rect; | 109 SkIRect rect; |
| 110 rect.set(0, 0, | 110 rect.set(0, 0, |
| 111 CGBitmapContextGetWidth(bitmap_context_), | 111 CGBitmapContextGetWidth(bitmap_context_), |
| 112 CGBitmapContextGetHeight(bitmap_context_)); | 112 CGBitmapContextGetHeight(bitmap_context_)); |
| 113 clip_region_ = SkRegion(rect); | 113 clip_region_ = SkRegion(rect); |
| 114 transform_.reset(); | 114 transform_.reset(); |
| 115 CGContextRetain(bitmap_context_); | 115 CGContextRetain(bitmap_context_); |
| 116 } | 116 } |
| 117 | 117 |
| 118 void BitmapPlatformDeviceMac::BitmapPlatformDeviceMacData::SetMatrixClip( | 118 void BitmapPlatformDeviceMac::BitmapPlatformDeviceMacData::SetMatrixClip( |
| (...skipping 25 matching lines...) Expand all Loading... |
| 144 BitmapPlatformDeviceMac* BitmapPlatformDeviceMac::Create(CGContextRef context, | 144 BitmapPlatformDeviceMac* BitmapPlatformDeviceMac::Create(CGContextRef context, |
| 145 int width, | 145 int width, |
| 146 int height, | 146 int height, |
| 147 bool is_opaque) { | 147 bool is_opaque) { |
| 148 void* data = malloc(height * width * 4); | 148 void* data = malloc(height * width * 4); |
| 149 if (!data) return NULL; | 149 if (!data) return NULL; |
| 150 | 150 |
| 151 SkBitmap bitmap; | 151 SkBitmap bitmap; |
| 152 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 152 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 153 bitmap.setPixels(data); | 153 bitmap.setPixels(data); |
| 154 | 154 |
| 155 // Note: The Windows implementation clears the Bitmap later on. | 155 // Note: The Windows implementation clears the Bitmap later on. |
| 156 // This bears mentioning since removal of this line makes the | 156 // This bears mentioning since removal of this line makes the |
| 157 // unit tests only fail periodically (or when MallocPreScribble is set). | 157 // unit tests only fail periodically (or when MallocPreScribble is set). |
| 158 bitmap.eraseARGB(0, 0, 0, 0); | 158 bitmap.eraseARGB(0, 0, 0, 0); |
| 159 | 159 |
| 160 bitmap.setIsOpaque(is_opaque); | 160 bitmap.setIsOpaque(is_opaque); |
| 161 | 161 |
| 162 if (is_opaque) { | 162 if (is_opaque) { |
| 163 #ifndef NDEBUG | 163 #ifndef NDEBUG |
| 164 // To aid in finding bugs, we set the background color to something | 164 // To aid in finding bugs, we set the background color to something |
| 165 // obviously wrong so it will be noticable when it is not cleared | 165 // obviously wrong so it will be noticable when it is not cleared |
| 166 bitmap.eraseARGB(255, 0, 255, 128); // bright bluish green | 166 bitmap.eraseARGB(255, 0, 255, 128); // bright bluish green |
| 167 #endif | 167 #endif |
| 168 } | 168 } |
| 169 | 169 |
| 170 CGColorSpaceRef color_space = | 170 CGColorSpaceRef color_space = |
| 171 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); | 171 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); |
| 172 // allocate a bitmap context with 4 components per pixel (RGBA): | 172 // allocate a bitmap context with 4 components per pixel (RGBA): |
| 173 CGContextRef bitmap_context = | 173 CGContextRef bitmap_context = |
| 174 CGBitmapContextCreate(data, width, height, 8, width*4, | 174 CGBitmapContextCreate(data, width, height, 8, width*4, |
| 175 color_space, kCGImageAlphaPremultipliedLast); | 175 color_space, kCGImageAlphaPremultipliedLast); |
| 176 | 176 |
| 177 // Change the coordinate system to match WebCore's | 177 // Change the coordinate system to match WebCore's |
| 178 CGContextTranslateCTM(bitmap_context, 0, height); | 178 CGContextTranslateCTM(bitmap_context, 0, height); |
| 179 CGContextScaleCTM(bitmap_context, 1.0, -1.0); | 179 CGContextScaleCTM(bitmap_context, 1.0, -1.0); |
| 180 CGColorSpaceRelease(color_space); | 180 CGColorSpaceRelease(color_space); |
| 181 | 181 |
| 182 // The device object will take ownership of the graphics context. | 182 // The device object will take ownership of the graphics context. |
| 183 return new BitmapPlatformDeviceMac( | 183 return new BitmapPlatformDeviceMac( |
| 184 new BitmapPlatformDeviceMacData(bitmap_context), bitmap); | 184 new BitmapPlatformDeviceMacData(bitmap_context), bitmap); |
| 185 } | 185 } |
| 186 | 186 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 bounds.origin.y = 0; | 244 bounds.origin.y = 0; |
| 245 bounds.size.width = width(); | 245 bounds.size.width = width(); |
| 246 bounds.size.height = height(); | 246 bounds.size.height = height(); |
| 247 CGContextDrawImage(context, bounds, image); | 247 CGContextDrawImage(context, bounds, image); |
| 248 } | 248 } |
| 249 CGImageRelease(image); | 249 CGImageRelease(image); |
| 250 | 250 |
| 251 if (created_dc) | 251 if (created_dc) |
| 252 data_->ReleaseBitmapContext(); | 252 data_->ReleaseBitmapContext(); |
| 253 } | 253 } |
| 254 | 254 |
| 255 // Returns the color value at the specified location. | 255 // Returns the color value at the specified location. |
| 256 SkColor BitmapPlatformDeviceMac::getColorAt(int x, int y) { | 256 SkColor BitmapPlatformDeviceMac::getColorAt(int x, int y) { |
| 257 const SkBitmap& bitmap = accessBitmap(true); | 257 const SkBitmap& bitmap = accessBitmap(true); |
| 258 SkAutoLockPixels lock(bitmap); | 258 SkAutoLockPixels lock(bitmap); |
| 259 uint32_t* data = bitmap.getAddr32(0, 0); | 259 uint32_t* data = bitmap.getAddr32(0, 0); |
| 260 return static_cast<SkColor>(data[x + y * width()]); | 260 return static_cast<SkColor>(data[x + y * width()]); |
| 261 } | 261 } |
| 262 | 262 |
| 263 void BitmapPlatformDeviceMac::onAccessBitmap(SkBitmap*) { | 263 void BitmapPlatformDeviceMac::onAccessBitmap(SkBitmap*) { |
| 264 // Not needed in CoreGraphics | 264 // Not needed in CoreGraphics |
| 265 } | 265 } |
| 266 | 266 |
| 267 void BitmapPlatformDeviceMac::processPixels(int x, int y, | 267 void BitmapPlatformDeviceMac::processPixels(int x, int y, |
| 268 int width, int height, | 268 int width, int height, |
| 269 adjustAlpha adjustor) { | 269 adjustAlpha adjustor) { |
| 270 const SkBitmap& bitmap = accessBitmap(true); | 270 const SkBitmap& bitmap = accessBitmap(true); |
| 271 SkMatrix& matrix = data_->transform_; | 271 SkMatrix& matrix = data_->transform_; |
| 272 int bitmap_start_x = SkScalarRound(matrix.getTranslateX()) + x; | 272 int bitmap_start_x = SkScalarRound(matrix.getTranslateX()) + x; |
| 273 int bitmap_start_y = SkScalarRound(matrix.getTranslateY()) + y; | 273 int bitmap_start_y = SkScalarRound(matrix.getTranslateY()) + y; |
| 274 | 274 |
| 275 SkAutoLockPixels lock(bitmap); | 275 SkAutoLockPixels lock(bitmap); |
| 276 if (Constrain(bitmap.width(), &bitmap_start_x, &width) && | 276 if (Constrain(bitmap.width(), &bitmap_start_x, &width) && |
| 277 Constrain(bitmap.height(), &bitmap_start_y, &height)) { | 277 Constrain(bitmap.height(), &bitmap_start_y, &height)) { |
| 278 uint32_t* data = bitmap.getAddr32(0, 0); | 278 uint32_t* data = bitmap.getAddr32(0, 0); |
| 279 size_t row_words = bitmap.rowBytes() / 4; | 279 size_t row_words = bitmap.rowBytes() / 4; |
| 280 for (int i = 0; i < height; i++) { | 280 for (int i = 0; i < height; i++) { |
| 281 size_t offset = (i + bitmap_start_y) * row_words + bitmap_start_x; | 281 size_t offset = (i + bitmap_start_y) * row_words + bitmap_start_x; |
| 282 for (int j = 0; j < width; j++) { | 282 for (int j = 0; j < width; j++) { |
| 283 adjustor(data + offset + j); | 283 adjustor(data + offset + j); |
| 284 } | 284 } |
| 285 } | 285 } |
| 286 } | 286 } |
| 287 } | 287 } |
| 288 | 288 |
| 289 } // namespace gfx | 289 } // namespace skia |
| 290 | 290 |
| OLD | NEW |