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 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 NSImage* SkBitmapToNSImage(const SkBitmap& skiaBitmap) { | 262 NSImage* SkBitmapToNSImage(const SkBitmap& skiaBitmap) { |
| 263 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( | 263 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( |
| 264 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); | 264 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); |
| 265 return SkBitmapToNSImageWithColorSpace(skiaBitmap, colorSpace.get()); | 265 return SkBitmapToNSImageWithColorSpace(skiaBitmap, colorSpace.get()); |
| 266 } | 266 } |
| 267 | 267 |
| 268 SkiaBitLocker::SkiaBitLocker(SkCanvas* canvas) | 268 SkiaBitLocker::SkiaBitLocker(SkCanvas* canvas) |
| 269 : canvas_(canvas), | 269 : canvas_(canvas), |
| 270 userClipRectSpecified_(false), | 270 userClipRectSpecified_(false), |
| 271 cgContext_(0), | 271 cgContext_(0), |
| 272 bitmapScaleFactor_(1), | |
| 272 useDeviceBits_(false), | 273 useDeviceBits_(false), |
| 273 bitmapIsDummy_(false) { | 274 bitmapIsDummy_(false) { |
| 274 } | 275 } |
| 275 | 276 |
| 276 SkiaBitLocker::SkiaBitLocker(SkCanvas* canvas, const SkIRect& userClipRect) | 277 SkiaBitLocker::SkiaBitLocker(SkCanvas* canvas, |
| 278 const SkIRect& userClipRect, | |
| 279 SkScalar bitmapScaleFactor) | |
| 277 : canvas_(canvas), | 280 : canvas_(canvas), |
| 278 userClipRectSpecified_(true), | 281 userClipRectSpecified_(true), |
| 279 cgContext_(0), | 282 cgContext_(0), |
| 283 bitmapScaleFactor_(bitmapScaleFactor), | |
| 280 useDeviceBits_(false), | 284 useDeviceBits_(false), |
| 281 bitmapIsDummy_(false) { | 285 bitmapIsDummy_(false) { |
| 282 canvas_->save(); | 286 canvas_->save(); |
| 283 canvas_->clipRect(SkRect::MakeFromIRect(userClipRect)); | 287 canvas_->clipRect(SkRect::MakeFromIRect(userClipRect)); |
| 284 } | 288 } |
| 285 | 289 |
| 286 SkiaBitLocker::~SkiaBitLocker() { | 290 SkiaBitLocker::~SkiaBitLocker() { |
| 287 releaseIfNeeded(); | 291 releaseIfNeeded(); |
| 288 if (userClipRectSpecified_) | 292 if (userClipRectSpecified_) |
| 289 canvas_->restore(); | 293 canvas_->restore(); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 // Neutralize the global matrix by concatenating the inverse. In the | 379 // Neutralize the global matrix by concatenating the inverse. In the |
| 376 // future, Skia may provide some mechanism to set the device portion of | 380 // future, Skia may provide some mechanism to set the device portion of |
| 377 // the matrix to identity without clobbering any hosting matrix (e.g., the | 381 // the matrix to identity without clobbering any hosting matrix (e.g., the |
| 378 // picture's matrix). | 382 // picture's matrix). |
| 379 const SkMatrix& skMatrix = canvas_->getTotalMatrix(); | 383 const SkMatrix& skMatrix = canvas_->getTotalMatrix(); |
| 380 SkMatrix inverse; | 384 SkMatrix inverse; |
| 381 if (!skMatrix.invert(&inverse)) | 385 if (!skMatrix.invert(&inverse)) |
| 382 return; | 386 return; |
| 383 canvas_->save(); | 387 canvas_->save(); |
| 384 canvas_->concat(inverse); | 388 canvas_->concat(inverse); |
| 385 canvas_->drawBitmap(subset, bounds.x() + bitmapOffset_.x(), | 389 canvas_->translate(bounds.x() + bitmapOffset_.x(), |
| 386 bounds.y() + bitmapOffset_.y()); | 390 bounds.y() + bitmapOffset_.y()); |
| 391 canvas_->scale(1.f / bitmapScaleFactor_, 1.f / bitmapScaleFactor_); | |
| 392 canvas_->drawBitmap(subset, 0, 0); | |
| 387 canvas_->restore(); | 393 canvas_->restore(); |
| 388 } | 394 } |
| 389 CGContextRelease(cgContext_); | 395 CGContextRelease(cgContext_); |
| 390 cgContext_ = 0; | 396 cgContext_ = 0; |
| 391 useDeviceBits_ = false; | 397 useDeviceBits_ = false; |
| 392 bitmapIsDummy_ = false; | 398 bitmapIsDummy_ = false; |
| 393 } | 399 } |
| 394 | 400 |
| 395 CGContextRef SkiaBitLocker::cgContext() { | 401 CGContextRef SkiaBitLocker::cgContext() { |
| 396 SkIRect clip_bounds; | 402 SkIRect clip_bounds; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 424 canvas_->isClipRect() && | 430 canvas_->isClipRect() && |
| 425 !bitmapIsDummy_; | 431 !bitmapIsDummy_; |
| 426 if (useDeviceBits_) { | 432 if (useDeviceBits_) { |
| 427 bool result = deviceBits.extractSubset(&bitmap_, clip_bounds); | 433 bool result = deviceBits.extractSubset(&bitmap_, clip_bounds); |
| 428 DCHECK(result); | 434 DCHECK(result); |
| 429 if (!result) | 435 if (!result) |
| 430 return 0; | 436 return 0; |
| 431 bitmap_.lockPixels(); | 437 bitmap_.lockPixels(); |
| 432 } else { | 438 } else { |
| 433 bool result = bitmap_.tryAllocN32Pixels( | 439 bool result = bitmap_.tryAllocN32Pixels( |
| 434 clip_bounds.width(), clip_bounds.height()); | 440 SkScalarCeilToInt(bitmapScaleFactor_ * clip_bounds.width()), |
| 441 SkScalarCeilToInt(bitmapScaleFactor_ * clip_bounds.height())); | |
| 435 DCHECK(result); | 442 DCHECK(result); |
| 436 if (!result) | 443 if (!result) |
| 437 return 0; | 444 return 0; |
| 438 bitmap_.eraseColor(0); | 445 bitmap_.eraseColor(0); |
| 439 } | 446 } |
| 440 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( | 447 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( |
| 441 CGColorSpaceCreateDeviceRGB()); | 448 CGColorSpaceCreateDeviceRGB()); |
| 442 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), | 449 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), |
| 443 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, | 450 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, |
| 444 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); | 451 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); |
| 445 DCHECK(cgContext_); | 452 DCHECK(cgContext_); |
| 446 | 453 |
| 447 SkMatrix matrix = canvas_->getTotalMatrix(); | 454 SkMatrix matrix = canvas_->getTotalMatrix(); |
| 448 matrix.postTranslate(-SkIntToScalar(bitmapOffset_.x()), | 455 matrix.postTranslate(-SkIntToScalar(bitmapOffset_.x()), |
| 449 -SkIntToScalar(bitmapOffset_.y())); | 456 -SkIntToScalar(bitmapOffset_.y())); |
| 450 matrix.postScale(1, -1); | 457 matrix.postScale(1, -1); |
| 458 matrix.postScale(bitmapScaleFactor_, bitmapScaleFactor_); | |
|
reed1
2014/09/30 18:57:24
nit : seems like we can combine these two postScal
ccameron
2014/09/30 19:01:46
Done.
| |
| 451 matrix.postTranslate(0, SkIntToScalar(bitmap_.height())); | 459 matrix.postTranslate(0, SkIntToScalar(bitmap_.height())); |
| 452 | 460 |
| 453 CGContextConcatCTM(cgContext_, SkMatrixToCGAffineTransform(matrix)); | 461 CGContextConcatCTM(cgContext_, SkMatrixToCGAffineTransform(matrix)); |
| 454 | 462 |
| 455 return cgContext_; | 463 return cgContext_; |
| 456 } | 464 } |
| 457 | 465 |
| 458 bool SkiaBitLocker::hasEmptyClipRegion() const { | 466 bool SkiaBitLocker::hasEmptyClipRegion() const { |
| 459 return canvas_->isClipEmpty(); | 467 return canvas_->isClipEmpty(); |
| 460 } | 468 } |
| 461 | 469 |
| 462 } // namespace gfx | 470 } // namespace gfx |
| OLD | NEW |