| OLD | NEW |
| 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 "skia/ext/skia_utils_mac.h" | 5 #include "skia/ext/skia_utils_mac.h" |
| 6 | 6 |
| 7 #import <AppKit/AppKit.h> | 7 #import <AppKit/AppKit.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 } | 277 } |
| 278 | 278 |
| 279 SkiaBitLocker::~SkiaBitLocker() { | 279 SkiaBitLocker::~SkiaBitLocker() { |
| 280 releaseIfNeeded(); | 280 releaseIfNeeded(); |
| 281 } | 281 } |
| 282 | 282 |
| 283 // This must be called to balance calls to cgContext | 283 // This must be called to balance calls to cgContext |
| 284 void SkiaBitLocker::releaseIfNeeded() { | 284 void SkiaBitLocker::releaseIfNeeded() { |
| 285 if (!cgContext_) | 285 if (!cgContext_) |
| 286 return; | 286 return; |
| 287 if (useDeviceBits_) { | 287 if (useBitmap_) { |
| 288 bitmap_.unlockPixels(); | |
| 289 } else { | |
| 290 // Find the bits that were drawn to. | 288 // Find the bits that were drawn to. |
| 291 SkAutoLockPixels lockedPixels(bitmap_); | |
| 292 const uint32_t* pixelBase | 289 const uint32_t* pixelBase |
| 293 = reinterpret_cast<uint32_t*>(bitmap_.getPixels()); | 290 = reinterpret_cast<uint32_t*>(bitmap_.getPixels()); |
| 294 int rowPixels = bitmap_.rowBytesAsPixels(); | 291 int rowPixels = bitmap_.rowBytesAsPixels(); |
| 295 int width = bitmap_.width(); | 292 int width = bitmap_.width(); |
| 296 int height = bitmap_.height(); | 293 int height = bitmap_.height(); |
| 297 SkIRect bounds; | 294 SkIRect bounds; |
| 298 bounds.fTop = 0; | 295 bounds.fTop = 0; |
| 299 int x; | 296 int x; |
| 300 int y = -1; | 297 int y = -1; |
| 301 const uint32_t* pixels = pixelBase; | 298 const uint32_t* pixels = pixelBase; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 canvas_->save(); | 360 canvas_->save(); |
| 364 canvas_->concat(inverse); | 361 canvas_->concat(inverse); |
| 365 canvas_->drawBitmap(subset, bounds.fLeft, bounds.fTop); | 362 canvas_->drawBitmap(subset, bounds.fLeft, bounds.fTop); |
| 366 canvas_->restore(); | 363 canvas_->restore(); |
| 367 } | 364 } |
| 368 CGContextRelease(cgContext_); | 365 CGContextRelease(cgContext_); |
| 369 cgContext_ = 0; | 366 cgContext_ = 0; |
| 370 } | 367 } |
| 371 | 368 |
| 372 CGContextRef SkiaBitLocker::cgContext() { | 369 CGContextRef SkiaBitLocker::cgContext() { |
| 373 SkBaseDevice* device = canvas_->getTopDevice(); | |
| 374 DCHECK(device); | |
| 375 if (!device) | |
| 376 return 0; | |
| 377 releaseIfNeeded(); // This flushes any prior bitmap use | 370 releaseIfNeeded(); // This flushes any prior bitmap use |
| 378 const SkBitmap& deviceBits = device->accessBitmap(true); | 371 |
| 379 useDeviceBits_ = deviceBits.getPixels(); | 372 SkIRect clip_bounds; |
| 380 if (useDeviceBits_) { | 373 const bool clip_is_empty = !canvas_->getClipDeviceBounds(&clip_bounds); |
| 381 bitmap_ = deviceBits; | 374 |
| 382 bitmap_.lockPixels(); | 375 SkImageInfo info; |
| 376 size_t row_bytes; |
| 377 SkIPoint origin; |
| 378 void* top_pixels = canvas_->accessTopLayerPixels(&info, &row_bytes, &origin); |
| 379 if (top_pixels && (clip_is_empty || canvas_->isClipRect())) { |
| 380 useBitmap_ = false; |
| 383 } else { | 381 } else { |
| 384 bitmap_.setConfig( | 382 useBitmap_ = true; |
| 385 SkBitmap::kARGB_8888_Config, deviceBits.width(), deviceBits.height()); | 383 info = SkImageInfo::MakeN32Premul(clip_bounds.width(), clip_bounds.height())
; |
| 386 bitmap_.allocPixels(); | 384 origin.set(clip_bounds.x(), clip_bounds.y()); |
| 385 CHECK(bitmap_.allocPixels(info)); |
| 387 bitmap_.eraseColor(0); | 386 bitmap_.eraseColor(0); |
| 387 top_pixels = bitmap_.getPixels(); |
| 388 } | 388 } |
| 389 |
| 389 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( | 390 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( |
| 390 CGColorSpaceCreateDeviceRGB()); | 391 CGColorSpaceCreateDeviceRGB()); |
| 391 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), | 392 cgContext_ = CGBitmapContextCreate(top_pixels, info.width(), |
| 392 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, | 393 info.height(), 8, row_bytes, colorSpace, |
| 393 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); | 394 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); |
| 394 | 395 |
| 395 // Apply device matrix. | 396 // Apply device matrix. |
| 396 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1); | 397 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1); |
| 397 contentsTransform = CGAffineTransformTranslate(contentsTransform, 0, | 398 contentsTransform = CGAffineTransformTranslate(contentsTransform, 0, |
| 398 -device->height()); | 399 -info.height()); |
| 399 CGContextConcatCTM(cgContext_, contentsTransform); | 400 CGContextConcatCTM(cgContext_, contentsTransform); |
| 400 | 401 |
| 401 const SkIPoint& pt = device->getOrigin(); | |
| 402 // Skip applying the clip when not writing directly to device. | 402 // Skip applying the clip when not writing directly to device. |
| 403 // They're applied in the offscreen case when the bitmap is drawn. | 403 // They're applied in the offscreen case when the bitmap is drawn. |
| 404 if (useDeviceBits_) { | 404 if (!useBitmap_) { |
| 405 // Apply clip in device coordinates. | 405 // Apply clip in device coordinates. |
| 406 CGMutablePathRef clipPath = CGPathCreateMutable(); | 406 CGMutablePathRef clipPath = CGPathCreateMutable(); |
| 407 const SkRegion& clipRgn = canvas_->getTotalClip(); | 407 if (clip_is_empty) { |
| 408 if (clipRgn.isEmpty()) { | |
| 409 // CoreGraphics does not consider a newly created path to be empty. | 408 // CoreGraphics does not consider a newly created path to be empty. |
| 410 // Explicitly set it to empty so the subsequent drawing is clipped out. | 409 // Explicitly set it to empty so the subsequent drawing is clipped out. |
| 411 // It would be better to make the CGContext hidden if there was a CG | 410 // It would be better to make the CGContext hidden if there was a CG |
| 412 // call that does that. | 411 // call that does that. |
| 413 CGPathAddRect(clipPath, 0, CGRectMake(0, 0, 0, 0)); | 412 CGPathAddRect(clipPath, 0, CGRectMake(0, 0, 0, 0)); |
| 414 } | 413 } else { |
| 415 SkRegion::Iterator iter(clipRgn); | 414 SkIRect r = clip_bounds; |
| 416 const SkIPoint& pt = device->getOrigin(); | 415 r.offset(-origin); |
| 417 for (; !iter.done(); iter.next()) { | 416 CGPathAddRect(clipPath, 0, SkIRectToCGRect(r)); |
| 418 SkIRect skRect = iter.rect(); | |
| 419 skRect.offset(-pt); | |
| 420 CGRect cgRect = SkIRectToCGRect(skRect); | |
| 421 CGPathAddRect(clipPath, 0, cgRect); | |
| 422 } | 417 } |
| 423 CGContextAddPath(cgContext_, clipPath); | 418 CGContextAddPath(cgContext_, clipPath); |
| 424 CGContextClip(cgContext_); | 419 CGContextClip(cgContext_); |
| 425 CGPathRelease(clipPath); | 420 CGPathRelease(clipPath); |
| 426 } | 421 } |
| 427 | 422 |
| 428 // Apply content matrix. | 423 // Apply content matrix. |
| 429 SkMatrix skMatrix = canvas_->getTotalMatrix(); | 424 SkMatrix skMatrix = canvas_->getTotalMatrix(); |
| 430 skMatrix.postTranslate(-SkIntToScalar(pt.fX), -SkIntToScalar(pt.fY)); | 425 skMatrix.postTranslate(-SkIntToScalar(origin.fX), -SkIntToScalar(origin.fY)); |
| 431 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); | 426 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); |
| 432 CGContextConcatCTM(cgContext_, affine); | 427 CGContextConcatCTM(cgContext_, affine); |
| 433 | 428 |
| 434 return cgContext_; | 429 return cgContext_; |
| 435 } | 430 } |
| 436 | 431 |
| 437 } // namespace gfx | 432 } // namespace gfx |
| OLD | NEW |