Chromium Code Reviews| 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 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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.fLeft, bounds.fTop); |
| 360 canvas_->restore(); | 360 canvas_->restore(); |
| 361 } | 361 } |
| 362 CGContextRelease(cgContext_); | 362 CGContextRelease(cgContext_); |
| 363 cgContext_ = 0; | 363 cgContext_ = 0; |
| 364 } | 364 } |
| 365 | 365 |
| 366 CGContextRef SkiaBitLocker::cgContext() { | 366 CGContextRef SkiaBitLocker::cgContext() { |
| 367 SkIRect clipBounds; | |
| 368 if (!canvas_->getClipDeviceBounds(&clipBounds)) | |
| 369 return 0; // the clip is empty, nothing to draw | |
| 370 | |
| 367 SkBaseDevice* device = canvas_->getTopDevice(); | 371 SkBaseDevice* device = canvas_->getTopDevice(); |
| 368 DCHECK(device); | 372 DCHECK(device); |
| 369 if (!device) | 373 if (!device) |
| 370 return 0; | 374 return 0; |
| 375 | |
| 376 const SkIPoint& pt = device->getOrigin(); | |
|
f(malita)
2014/06/19 15:15:46
Nit: maybe inline this since we're only using pt o
reed1
2014/06/19 16:40:54
We use it down on line 412
| |
| 377 // get clipbounds into the coordinate systme of the top layer/device | |
|
f(malita)
2014/06/19 15:15:47
Typo: system
reed1
2014/06/19 16:40:54
Done.
| |
| 378 clipBounds.offset(-pt); | |
| 379 | |
| 371 releaseIfNeeded(); // This flushes any prior bitmap use | 380 releaseIfNeeded(); // This flushes any prior bitmap use |
| 372 const SkBitmap& deviceBits = device->accessBitmap(true); | 381 const SkBitmap& deviceBits = device->accessBitmap(true); |
| 373 useDeviceBits_ = deviceBits.getPixels(); | 382 // Only draw directly if we have pixels, and we're only rect-clipped. |
| 383 // If not, we allocate an offscreen and draw into that, relying on the | |
| 384 // compositing step to apply skia's clip. | |
| 385 useDeviceBits_ = deviceBits.getPixels() && canvas_->isClipRect(); | |
| 374 if (useDeviceBits_) { | 386 if (useDeviceBits_) { |
| 375 bitmap_ = deviceBits; | 387 bitmap_ = deviceBits; |
| 376 bitmap_.lockPixels(); | 388 bitmap_.lockPixels(); |
| 377 } else { | 389 } else { |
| 378 if (!bitmap_.allocN32Pixels(deviceBits.width(), deviceBits.height())) | 390 if (!bitmap_.allocN32Pixels(clipBounds.width(), clipBounds.height())) |
|
f(malita)
2014/06/19 15:15:47
We used to allocate a bitmap matching the device b
reed1
2014/06/19 16:40:54
I think you're right, I will need to adjust the of
| |
| 379 return 0; | 391 return 0; |
| 380 bitmap_.eraseColor(0); | 392 bitmap_.eraseColor(0); |
| 381 } | 393 } |
| 382 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( | 394 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( |
| 383 CGColorSpaceCreateDeviceRGB()); | 395 CGColorSpaceCreateDeviceRGB()); |
| 384 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), | 396 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), |
| 385 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, | 397 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, |
| 386 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); | 398 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); |
| 387 | 399 |
| 388 // Apply device matrix. | 400 // Apply device matrix. |
| 389 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1); | 401 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1); |
| 390 contentsTransform = CGAffineTransformTranslate(contentsTransform, 0, | 402 contentsTransform = CGAffineTransformTranslate(contentsTransform, 0, |
| 391 -device->height()); | 403 -device->height()); |
| 392 CGContextConcatCTM(cgContext_, contentsTransform); | 404 CGContextConcatCTM(cgContext_, contentsTransform); |
| 393 | 405 |
| 394 const SkIPoint& pt = device->getOrigin(); | |
| 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_) { | 406 if (useDeviceBits_) { |
| 398 // Apply clip in device coordinates. | 407 CGContextClipToRect(cgContext_, SkIRectToCGRect(clipBounds)); |
| 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 } | 408 } |
| 420 | 409 |
| 421 // Apply content matrix. | 410 // Apply content matrix. |
| 422 SkMatrix skMatrix = canvas_->getTotalMatrix(); | 411 SkMatrix skMatrix = canvas_->getTotalMatrix(); |
| 423 skMatrix.postTranslate(-SkIntToScalar(pt.fX), -SkIntToScalar(pt.fY)); | 412 skMatrix.postTranslate(-SkIntToScalar(pt.fX), -SkIntToScalar(pt.fY)); |
| 424 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); | 413 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); |
| 425 CGContextConcatCTM(cgContext_, affine); | 414 CGContextConcatCTM(cgContext_, affine); |
| 426 | 415 |
| 427 return cgContext_; | 416 return cgContext_; |
| 428 } | 417 } |
| 429 | 418 |
| 430 } // namespace gfx | 419 } // namespace gfx |
| OLD | NEW |