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 |
372 const SkBitmap& deviceBits = device->accessBitmap(true); | 378 const SkBitmap& deviceBits = device->accessBitmap(true); |
373 useDeviceBits_ = deviceBits.getPixels(); | 379 // Only draw directly if we have pixels, and we're only rect-clipped. |
380 // If not, we allocate an offscreen and draw into that, relying on the | |
381 // compositing step to apply skia's clip. | |
382 useDeviceBits_ = deviceBits.getPixels() && canvas_->isClipRect(); | |
374 if (useDeviceBits_) { | 383 if (useDeviceBits_) { |
375 bitmap_ = deviceBits; | 384 bitmap_ = deviceBits; |
376 bitmap_.lockPixels(); | 385 bitmap_.lockPixels(); |
f(malita)
2014/06/19 18:39:54
bitmapOffset_.setZero();
reed1
2014/06/19 18:57:18
It won't get read in this case, but I will add the
| |
377 } else { | 386 } else { |
378 if (!bitmap_.allocN32Pixels(deviceBits.width(), deviceBits.height())) | 387 if (!bitmap_.allocN32Pixels(clip_bounds.width(), clip_bounds.height())) |
379 return 0; | 388 return 0; |
380 bitmap_.eraseColor(0); | 389 bitmap_.eraseColor(0); |
390 bitmapOffset_.set(clip_bounds.x(), clip_bounds.y()); | |
381 } | 391 } |
382 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( | 392 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( |
383 CGColorSpaceCreateDeviceRGB()); | 393 CGColorSpaceCreateDeviceRGB()); |
384 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), | 394 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), |
385 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, | 395 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, |
386 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); | 396 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); |
387 | 397 |
388 // Apply device matrix. | 398 // Apply device matrix. |
389 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1); | 399 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1); |
390 contentsTransform = CGAffineTransformTranslate(contentsTransform, 0, | 400 contentsTransform = CGAffineTransformTranslate(contentsTransform, 0, |
391 -device->height()); | 401 -device->height()); |
392 CGContextConcatCTM(cgContext_, contentsTransform); | 402 CGContextConcatCTM(cgContext_, contentsTransform); |
393 | 403 |
394 const SkIPoint& pt = device->getOrigin(); | 404 // get clip_bounds into the coordinate system of the top layer/device |
395 // Skip applying the clip when not writing directly to device. | 405 clip_bounds.offset(-device->getOrigin()); |
396 // They're applied in the offscreen case when the bitmap is drawn. | 406 |
397 if (useDeviceBits_) { | 407 if (useDeviceBits_) { |
398 // Apply clip in device coordinates. | 408 CGContextClipToRect(cgContext_, SkIRectToCGRect(clip_bounds)); |
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 } | 409 } |
420 | 410 |
421 // Apply content matrix. | 411 // Apply content matrix. |
422 SkMatrix skMatrix = canvas_->getTotalMatrix(); | 412 SkMatrix skMatrix = canvas_->getTotalMatrix(); |
423 skMatrix.postTranslate(-SkIntToScalar(pt.fX), -SkIntToScalar(pt.fY)); | 413 skMatrix.postTranslate(-SkIntToScalar(clip_bounds.x()), |
f(malita)
2014/06/19 18:39:54
Do we still need to include the clip offset here w
reed1
2014/06/19 18:57:18
I think so. From CG's perspective, both scenarios
| |
414 -SkIntToScalar(clip_bounds.y())); | |
424 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); | 415 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); |
425 CGContextConcatCTM(cgContext_, affine); | 416 CGContextConcatCTM(cgContext_, affine); |
426 | 417 |
427 return cgContext_; | 418 return cgContext_; |
428 } | 419 } |
429 | 420 |
430 } // namespace gfx | 421 } // namespace gfx |
OLD | NEW |