| OLD | NEW |
| 1 | 1 |
| 2 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 2 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 3 // Use of this source code is governed by a BSD-style license that can be | 3 // Use of this source code is governed by a BSD-style license that can be |
| 4 // found in the LICENSE file. | 4 // found in the LICENSE file. |
| 5 | 5 |
| 6 #ifndef SKIA_EXT_CANVAS_PAINT_MAC_H_ | 6 #ifndef SKIA_EXT_CANVAS_PAINT_MAC_H_ |
| 7 #define SKIA_EXT_CANVAS_PAINT_MAC_H_ | 7 #define SKIA_EXT_CANVAS_PAINT_MAC_H_ |
| 8 #pragma once | 8 #pragma once |
| 9 | 9 |
| 10 #include "skia/ext/canvas_paint_common.h" |
| 10 #include "skia/ext/platform_canvas.h" | 11 #include "skia/ext/platform_canvas.h" |
| 11 | 12 |
| 12 #import <Cocoa/Cocoa.h> | 13 #import <Cocoa/Cocoa.h> |
| 13 | 14 |
| 14 namespace skia { | 15 namespace skia { |
| 15 | 16 |
| 16 // A class designed to translate skia painting into a region to the current | 17 // A class designed to translate skia painting into a region to the current |
| 17 // graphics context. On construction, it will set up a context for painting | 18 // graphics context. On construction, it will set up a context for painting |
| 18 // into, and on destruction, it will commit it to the current context. | 19 // into, and on destruction, it will commit it to the current context. |
| 19 template <class T> | 20 template <class T> |
| 20 class CanvasPaintT : public T { | 21 class CanvasPaintT : public T { |
| 21 public: | 22 public: |
| 22 // This constructor assumes the result is opaque. | 23 // This constructor assumes the result is opaque. |
| 23 explicit CanvasPaintT(NSRect dirtyRect) | 24 explicit CanvasPaintT(NSRect dirtyRect) |
| 24 : context_(NULL), | 25 : context_(NULL), |
| 25 rectangle_(dirtyRect), | 26 rectangle_(dirtyRect), |
| 26 composite_alpha_(false) { | 27 composite_alpha_(false) { |
| 27 init(true); | 28 init(true); |
| 28 } | 29 } |
| 29 | 30 |
| 30 CanvasPaintT(NSRect dirtyRect, bool opaque) | 31 CanvasPaintT(NSRect dirtyRect, bool opaque) |
| 31 : context_(NULL), | 32 : context_(NULL), |
| 32 rectangle_(dirtyRect), | 33 rectangle_(dirtyRect), |
| 33 composite_alpha_(false) { | 34 composite_alpha_(false) { |
| 34 init(opaque); | 35 init(opaque); |
| 35 } | 36 } |
| 36 | 37 |
| 37 virtual ~CanvasPaintT() { | 38 virtual ~CanvasPaintT() { |
| 38 if (!is_empty()) { | 39 if (!is_empty()) { |
| 39 T::restoreToCount(1); | 40 GetPlatformCanvas(this)->restoreToCount(1); |
| 40 | 41 |
| 41 // Blit the dirty rect to the current context. | 42 // Blit the dirty rect to the current context. |
| 42 CGImageRef image = CGBitmapContextCreateImage(context_); | 43 CGImageRef image = CGBitmapContextCreateImage(context_); |
| 43 CGRect destRect = NSRectToCGRect(rectangle_); | 44 CGRect destRect = NSRectToCGRect(rectangle_); |
| 44 | 45 |
| 45 CGContextRef destinationContext = | 46 CGContextRef destinationContext = |
| 46 (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]; | 47 (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]; |
| 47 CGContextSaveGState(destinationContext); | 48 CGContextSaveGState(destinationContext); |
| 48 CGContextSetBlendMode( | 49 CGContextSetBlendMode( |
| 49 destinationContext, | 50 destinationContext, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 74 bool is_empty() const { | 75 bool is_empty() const { |
| 75 return rectangle_.size.width == 0 || rectangle_.size.height == 0; | 76 return rectangle_.size.width == 0 || rectangle_.size.height == 0; |
| 76 } | 77 } |
| 77 | 78 |
| 78 const NSRect& rectangle() const { | 79 const NSRect& rectangle() const { |
| 79 return rectangle_; | 80 return rectangle_; |
| 80 } | 81 } |
| 81 | 82 |
| 82 private: | 83 private: |
| 83 void init(bool opaque) { | 84 void init(bool opaque) { |
| 84 if (!T::initialize(rectangle_.size.width, rectangle_.size.height, | 85 PlatformCanvas* canvas = GetPlatformCanvas(this); |
| 85 opaque, NULL)) { | 86 if (!canvas->initialize(rectangle_.size.width, |
| 87 rectangle_.size.height, |
| 88 opaque, NULL)) { |
| 86 // Cause a deliberate crash; | 89 // Cause a deliberate crash; |
| 87 *(volatile char*) 0 = 0; | 90 *(volatile char*) 0 = 0; |
| 88 } | 91 } |
| 89 | 92 |
| 90 // Need to translate so that the dirty region appears at the origin of the | 93 // Need to translate so that the dirty region appears at the origin of the |
| 91 // surface. | 94 // surface. |
| 92 T::translate(-SkDoubleToScalar(rectangle_.origin.x), | 95 canvas->translate(-SkDoubleToScalar(rectangle_.origin.x), |
| 93 -SkDoubleToScalar(rectangle_.origin.y)); | 96 -SkDoubleToScalar(rectangle_.origin.y)); |
| 94 | 97 |
| 95 context_ = GetBitmapContext(GetTopDevice(*this)); | 98 context_ = GetBitmapContext(GetTopDevice(*canvas)); |
| 96 } | 99 } |
| 97 | 100 |
| 98 CGContext* context_; | 101 CGContext* context_; |
| 99 NSRect rectangle_; | 102 NSRect rectangle_; |
| 100 // See description above setter. | 103 // See description above setter. |
| 101 bool composite_alpha_; | 104 bool composite_alpha_; |
| 102 | 105 |
| 103 // Disallow copy and assign. | 106 // Disallow copy and assign. |
| 104 CanvasPaintT(const CanvasPaintT&); | 107 CanvasPaintT(const CanvasPaintT&); |
| 105 CanvasPaintT& operator=(const CanvasPaintT&); | 108 CanvasPaintT& operator=(const CanvasPaintT&); |
| 106 }; | 109 }; |
| 107 | 110 |
| 108 typedef CanvasPaintT<PlatformCanvas> PlatformCanvasPaint; | 111 typedef CanvasPaintT<PlatformCanvas> PlatformCanvasPaint; |
| 109 | 112 |
| 110 } // namespace skia | 113 } // namespace skia |
| 111 | 114 |
| 112 | 115 |
| 113 #endif // SKIA_EXT_CANVAS_PAINT_MAC_H_ | 116 #endif // SKIA_EXT_CANVAS_PAINT_MAC_H_ |
| OLD | NEW |