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