Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Unified Diff: skia/ext/skia_utils_mac.mm

Issue 2705723002: Convert SkiaBitLocker to use PaintCanvas (Closed)
Patch Set: Remove rogue cc:: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « skia/ext/skia_utils_mac.h ('k') | skia/ext/skia_utils_mac_unittest.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: skia/ext/skia_utils_mac.mm
diff --git a/skia/ext/skia_utils_mac.mm b/skia/ext/skia_utils_mac.mm
index 1b0a2feff046c6a4895784cbdc4bd89470a68992..a851888523bc1de5fd15a31e521e4686a8b771f6 100644
--- a/skia/ext/skia_utils_mac.mm
+++ b/skia/ext/skia_utils_mac.mm
@@ -241,126 +241,4 @@ NSImage* SkBitmapToNSImage(const SkBitmap& skiaBitmap) {
return SkBitmapToNSImageWithColorSpace(skiaBitmap, colorSpace.get());
}
-SkiaBitLocker::SkiaBitLocker(SkCanvas* canvas,
- const SkIRect& userClipRect,
- SkScalar bitmapScaleFactor)
- : canvas_(canvas),
- cgContext_(0),
- bitmapScaleFactor_(bitmapScaleFactor),
- useDeviceBits_(false),
- bitmapIsDummy_(false) {
- canvas_->save();
- canvas_->clipRect(SkRect::MakeFromIRect(userClipRect));
-}
-
-SkiaBitLocker::~SkiaBitLocker() {
- releaseIfNeeded();
- canvas_->restore();
-}
-
-SkIRect SkiaBitLocker::computeDirtyRect() {
- // If the user specified a clip region, assume that it was tight and that the
- // dirty rect is approximately the whole bitmap.
- return SkIRect::MakeWH(offscreen_.width(), offscreen_.height());
-}
-
-// This must be called to balance calls to cgContext
-void SkiaBitLocker::releaseIfNeeded() {
- if (!cgContext_)
- return;
- if (!useDeviceBits_ && !bitmapIsDummy_) {
- // Find the bits that were drawn to.
- SkIRect bounds = computeDirtyRect();
- SkBitmap subset;
- if (!offscreen_.extractSubset(&subset, bounds)) {
- return;
- }
- subset.setImmutable(); // Prevents a defensive copy inside Skia.
- canvas_->save();
- canvas_->setMatrix(SkMatrix::I()); // Reset back to device space.
- canvas_->translate(bounds.x() + bitmapOffset_.x(),
- bounds.y() + bitmapOffset_.y());
- canvas_->scale(1.f / bitmapScaleFactor_, 1.f / bitmapScaleFactor_);
- canvas_->drawBitmap(subset, 0, 0);
- canvas_->restore();
- }
- CGContextRelease(cgContext_);
- cgContext_ = 0;
- useDeviceBits_ = false;
- bitmapIsDummy_ = false;
-}
-
-CGContextRef SkiaBitLocker::cgContext() {
- releaseIfNeeded(); // This flushes any prior bitmap use
-
- SkIRect clip_bounds;
- if (!canvas_->getDeviceClipBounds(&clip_bounds)) {
- // If the clip is empty, then there is nothing to draw. The caller may
- // attempt to draw (to-be-clipped) results, so ensure there is a dummy
- // non-NULL CGContext to use.
- bitmapIsDummy_ = true;
- clip_bounds = SkIRect::MakeXYWH(0, 0, 1, 1);
- }
-
- // remember the top/left, in case we need to compose this later
- bitmapOffset_.set(clip_bounds.x(), clip_bounds.y());
-
- // Now make clip_bounds be relative to the current layer/device
- if (!bitmapIsDummy_) {
- canvas_->temporary_internal_describeTopLayer(nullptr, &clip_bounds);
- }
-
- SkPixmap devicePixels;
- skia::GetWritablePixels(canvas_, &devicePixels);
-
- // Only draw directly if we have pixels, and we're only rect-clipped.
- // If not, we allocate an offscreen and draw into that, relying on the
- // compositing step to apply skia's clip.
- useDeviceBits_ = devicePixels.addr() &&
- canvas_->isClipRect() &&
- !bitmapIsDummy_;
- base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace(
- CGColorSpaceCreateDeviceRGB());
-
- int displayHeight;
- if (useDeviceBits_) {
- SkPixmap subset;
- bool result = devicePixels.extractSubset(&subset, clip_bounds);
- DCHECK(result);
- if (!result)
- return 0;
- displayHeight = subset.height();
- cgContext_ = CGBitmapContextCreate(subset.writable_addr(), subset.width(),
- subset.height(), 8, subset.rowBytes(), colorSpace,
- kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst);
- } else {
- bool result = offscreen_.tryAllocN32Pixels(
- SkScalarCeilToInt(bitmapScaleFactor_ * clip_bounds.width()),
- SkScalarCeilToInt(bitmapScaleFactor_ * clip_bounds.height()));
- DCHECK(result);
- if (!result)
- return 0;
- offscreen_.eraseColor(0);
- displayHeight = offscreen_.height();
- cgContext_ = CGBitmapContextCreate(offscreen_.getPixels(),
- offscreen_.width(), offscreen_.height(), 8, offscreen_.rowBytes(),
- colorSpace, kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst);
- }
- DCHECK(cgContext_);
-
- SkMatrix matrix = canvas_->getTotalMatrix();
- matrix.postTranslate(-SkIntToScalar(bitmapOffset_.x()),
- -SkIntToScalar(bitmapOffset_.y()));
- matrix.postScale(bitmapScaleFactor_, -bitmapScaleFactor_);
- matrix.postTranslate(0, SkIntToScalar(displayHeight));
-
- CGContextConcatCTM(cgContext_, SkMatrixToCGAffineTransform(matrix));
-
- return cgContext_;
-}
-
-bool SkiaBitLocker::hasEmptyClipRegion() const {
- return canvas_->isClipEmpty();
-}
-
} // namespace skia
« no previous file with comments | « skia/ext/skia_utils_mac.h ('k') | skia/ext/skia_utils_mac_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698