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

Side by Side Diff: skia/ext/skia_utils_mac.mm

Issue 344793003: Stop using (deprecated) getTotalClip (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: simplify matrix, eliminate need for clipping Created 6 years, 6 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « skia/ext/skia_utils_mac.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
377 // remember the top/left, in case we need to compose this later
378 bitmapOffset_.set(clip_bounds.x(), clip_bounds.y());
f(malita) 2014/06/19 20:58:42 I think this needs to happen after the releaseIfNe
379
380 // Now make clip_bounds be relative to the current layer/device
381 clip_bounds.offset(-device->getOrigin());
382
371 releaseIfNeeded(); // This flushes any prior bitmap use 383 releaseIfNeeded(); // This flushes any prior bitmap use
372 const SkBitmap& deviceBits = device->accessBitmap(true); 384 const SkBitmap& deviceBits = device->accessBitmap(true);
373 useDeviceBits_ = deviceBits.getPixels(); 385
386 // Only draw directly if we have pixels, and we're only rect-clipped.
387 // If not, we allocate an offscreen and draw into that, relying on the
388 // compositing step to apply skia's clip.
389 useDeviceBits_ = deviceBits.getPixels() && canvas_->isClipRect();
374 if (useDeviceBits_) { 390 if (useDeviceBits_) {
375 bitmap_ = deviceBits; 391 if (!deviceBits.extractSubset(&bitmap_, clip_bounds))
392 return 0;
376 bitmap_.lockPixels(); 393 bitmap_.lockPixels();
377 } else { 394 } else {
378 if (!bitmap_.allocN32Pixels(deviceBits.width(), deviceBits.height())) 395 if (!bitmap_.allocN32Pixels(clip_bounds.width(), clip_bounds.height()))
379 return 0; 396 return 0;
380 bitmap_.eraseColor(0); 397 bitmap_.eraseColor(0);
381 } 398 }
382 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( 399 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace(
383 CGColorSpaceCreateDeviceRGB()); 400 CGColorSpaceCreateDeviceRGB());
384 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), 401 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(),
385 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, 402 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace,
386 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); 403 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst);
387 404
388 // Apply device matrix. 405 SkMatrix matrix = canvas_->getTotalMatrix();
389 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1); 406 matrix.postTranslate(-SkIntToScalar(bitmapOffset_.x()),
390 contentsTransform = CGAffineTransformTranslate(contentsTransform, 0, 407 -SkIntToScalar(bitmapOffset_.y()));
391 -device->height()); 408 matrix.postScale(1, -1);
392 CGContextConcatCTM(cgContext_, contentsTransform); 409 matrix.postTranslate(0, SkIntToScalar(bitmap_.height()));
393 410
394 const SkIPoint& pt = device->getOrigin(); 411 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 412
427 return cgContext_; 413 return cgContext_;
428 } 414 }
429 415
430 } // namespace gfx 416 } // namespace gfx
OLDNEW
« no previous file with comments | « skia/ext/skia_utils_mac.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698