| 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 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 // Neutralize the global matrix by concatenating the inverse. In the | 349 // Neutralize the global matrix by concatenating the inverse. In the |
| 350 // future, Skia may provide some mechanism to set the device portion of | 350 // future, Skia may provide some mechanism to set the device portion of |
| 351 // the matrix to identity without clobbering any hosting matrix (e.g., the | 351 // the matrix to identity without clobbering any hosting matrix (e.g., the |
| 352 // picture's matrix). | 352 // picture's matrix). |
| 353 const SkMatrix& skMatrix = canvas_->getTotalMatrix(); | 353 const SkMatrix& skMatrix = canvas_->getTotalMatrix(); |
| 354 SkMatrix inverse; | 354 SkMatrix inverse; |
| 355 if (!skMatrix.invert(&inverse)) | 355 if (!skMatrix.invert(&inverse)) |
| 356 return; | 356 return; |
| 357 canvas_->save(); | 357 canvas_->save(); |
| 358 canvas_->concat(inverse); | 358 canvas_->concat(inverse); |
| 359 canvas_->drawBitmap(subset, bounds.fLeft, bounds.fTop); | 359 canvas_->drawBitmap(subset, bounds.x() + bitmapOffset_.x(), |
| 360 bounds.y() + bitmapOffset_.y()); |
| 360 canvas_->restore(); | 361 canvas_->restore(); |
| 361 } | 362 } |
| 362 CGContextRelease(cgContext_); | 363 CGContextRelease(cgContext_); |
| 363 cgContext_ = 0; | 364 cgContext_ = 0; |
| 364 } | 365 } |
| 365 | 366 |
| 366 CGContextRef SkiaBitLocker::cgContext() { | 367 CGContextRef SkiaBitLocker::cgContext() { |
| 368 SkIRect clip_bounds; |
| 369 if (!canvas_->getClipDeviceBounds(&clip_bounds)) |
| 370 return 0; // the clip is empty, nothing to draw |
| 371 |
| 367 SkBaseDevice* device = canvas_->getTopDevice(); | 372 SkBaseDevice* device = canvas_->getTopDevice(); |
| 368 DCHECK(device); | 373 DCHECK(device); |
| 369 if (!device) | 374 if (!device) |
| 370 return 0; | 375 return 0; |
| 376 |
| 371 releaseIfNeeded(); // This flushes any prior bitmap use | 377 releaseIfNeeded(); // This flushes any prior bitmap use |
| 378 |
| 379 // remember the top/left, in case we need to compose this later |
| 380 bitmapOffset_.set(clip_bounds.x(), clip_bounds.y()); |
| 381 |
| 382 // Now make clip_bounds be relative to the current layer/device |
| 383 clip_bounds.offset(-device->getOrigin()); |
| 384 |
| 372 const SkBitmap& deviceBits = device->accessBitmap(true); | 385 const SkBitmap& deviceBits = device->accessBitmap(true); |
| 373 useDeviceBits_ = deviceBits.getPixels(); | 386 |
| 387 // Only draw directly if we have pixels, and we're only rect-clipped. |
| 388 // If not, we allocate an offscreen and draw into that, relying on the |
| 389 // compositing step to apply skia's clip. |
| 390 useDeviceBits_ = deviceBits.getPixels() && canvas_->isClipRect(); |
| 374 if (useDeviceBits_) { | 391 if (useDeviceBits_) { |
| 375 bitmap_ = deviceBits; | 392 if (!deviceBits.extractSubset(&bitmap_, clip_bounds)) |
| 393 return 0; |
| 376 bitmap_.lockPixels(); | 394 bitmap_.lockPixels(); |
| 377 } else { | 395 } else { |
| 378 if (!bitmap_.allocN32Pixels(deviceBits.width(), deviceBits.height())) | 396 if (!bitmap_.allocN32Pixels(clip_bounds.width(), clip_bounds.height())) |
| 379 return 0; | 397 return 0; |
| 380 bitmap_.eraseColor(0); | 398 bitmap_.eraseColor(0); |
| 381 } | 399 } |
| 382 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( | 400 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( |
| 383 CGColorSpaceCreateDeviceRGB()); | 401 CGColorSpaceCreateDeviceRGB()); |
| 384 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), | 402 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), |
| 385 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, | 403 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, |
| 386 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); | 404 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); |
| 387 | 405 |
| 388 // Apply device matrix. | 406 SkMatrix matrix = canvas_->getTotalMatrix(); |
| 389 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1); | 407 matrix.postTranslate(-SkIntToScalar(bitmapOffset_.x()), |
| 390 contentsTransform = CGAffineTransformTranslate(contentsTransform, 0, | 408 -SkIntToScalar(bitmapOffset_.y())); |
| 391 -device->height()); | 409 matrix.postScale(1, -1); |
| 392 CGContextConcatCTM(cgContext_, contentsTransform); | 410 matrix.postTranslate(0, SkIntToScalar(bitmap_.height())); |
| 393 | 411 |
| 394 const SkIPoint& pt = device->getOrigin(); | 412 CGContextConcatCTM(cgContext_, SkMatrixToCGAffineTransform(matrix)); |
| 395 // Skip applying the clip when not writing directly to device. | |
| 396 // They're applied in the offscreen case when the bitmap is drawn. | |
| 397 if (useDeviceBits_) { | |
| 398 // Apply clip in device coordinates. | |
| 399 CGMutablePathRef clipPath = CGPathCreateMutable(); | |
| 400 const SkRegion& clipRgn = canvas_->getTotalClip(); | |
| 401 if (clipRgn.isEmpty()) { | |
| 402 // CoreGraphics does not consider a newly created path to be empty. | |
| 403 // Explicitly set it to empty so the subsequent drawing is clipped out. | |
| 404 // It would be better to make the CGContext hidden if there was a CG | |
| 405 // call that does that. | |
| 406 CGPathAddRect(clipPath, 0, CGRectMake(0, 0, 0, 0)); | |
| 407 } | |
| 408 SkRegion::Iterator iter(clipRgn); | |
| 409 const SkIPoint& pt = device->getOrigin(); | |
| 410 for (; !iter.done(); iter.next()) { | |
| 411 SkIRect skRect = iter.rect(); | |
| 412 skRect.offset(-pt); | |
| 413 CGRect cgRect = SkIRectToCGRect(skRect); | |
| 414 CGPathAddRect(clipPath, 0, cgRect); | |
| 415 } | |
| 416 CGContextAddPath(cgContext_, clipPath); | |
| 417 CGContextClip(cgContext_); | |
| 418 CGPathRelease(clipPath); | |
| 419 } | |
| 420 | |
| 421 // Apply content matrix. | |
| 422 SkMatrix skMatrix = canvas_->getTotalMatrix(); | |
| 423 skMatrix.postTranslate(-SkIntToScalar(pt.fX), -SkIntToScalar(pt.fY)); | |
| 424 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); | |
| 425 CGContextConcatCTM(cgContext_, affine); | |
| 426 | 413 |
| 427 return cgContext_; | 414 return cgContext_; |
| 428 } | 415 } |
| 429 | 416 |
| 430 } // namespace gfx | 417 } // namespace gfx |
| OLD | NEW |