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

Side by Side Diff: skia/ext/canvas_paint_mac.h

Issue 172046: A canvas_paint for OS X. (Closed)
Patch Set: Created 11 years, 4 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
« no previous file with comments | « skia/ext/canvas_paint.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
(Empty)
1
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
4 // found in the LICENSE file.
5
6 #ifndef SKIA_EXT_CANVAS_PAINT_MAC_H_
7 #define SKIA_EXT_CANVAS_PAINT_MAC_H_
8
9 #include "skia/ext/platform_canvas.h"
10
11 #import <Cocoa/Cocoa.h>
12
13 namespace skia {
14
15 // A class designed to translate skia painting into a region to the current
16 // graphics context. This class has been adapted from the class with the same
17 // name in platform_canvas_win.h. On construction, it will set up a context for
18 // painting into, and on destruction, it will commit it to the current context.
19 template <class T>
20 class CanvasPaintT : public T {
21 public:
22 // This constructor assumes the result is opaque.
23 explicit CanvasPaintT(NSRect dirtyRect)
24 : context_(NULL),
25 rectangle_(dirtyRect),
26 composite_alpha_(false) {
27 init(true);
28 }
29
30 CanvasPaintT(NSRect dirtyRect, bool opaque)
31 : context_(NULL),
32 rectangle_(dirtyRect),
33 composite_alpha_(false) {
34 init(opaque);
35 }
36
37 virtual ~CanvasPaintT() {
38 if (!is_empty()) {
39 T::restoreToCount(1);
40
41 // Blit the dirty rect to the current context.
42 CGImageRef image = CGBitmapContextCreateImage(context_);
43 CGRect destRect = NSRectToCGRect(rectangle_);
44
45 CGContextRef context =
46 (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
47 CGContextSaveGState(context);
48 CGContextSetBlendMode(
49 context, composite_alpha_ ? kCGBlendModeNormal : kCGBlendModeCopy);
50 CGContextDrawImage(context, destRect, image);
51 CGContextRestoreGState(context);
52
53 CFRelease(image);
54 }
55 }
56
57 // If true, the data painted into the CanvasPaintT is blended onto the current
58 // context, else it is copied.
59 void set_composite_alpha(bool composite_alpha) {
60 composite_alpha_ = composite_alpha;
61 }
62
63 // Returns true if the invalid region is empty. The caller should call this
64 // function to determine if anything needs painting.
65 bool is_empty() const {
66 return rectangle_.size.width == 0 || rectangle_.size.height == 0;
67 }
68
69 const NSRect& rectangle() const {
70 return rectangle_;
71 }
72
73 private:
74 void init(bool opaque) {
75 if (!T::initialize(rectangle_.size.width, rectangle_.size.height,
76 opaque, NULL)) {
77 // Cause a deliberate crash;
78 *(char*) 0 = 0;
79 }
80
81 // Need to translate so that the dirty region appears at the origin of the
82 // surface.
83 T::translate(-SkIntToScalar(rectangle_.origin.x),
84 -SkIntToScalar(rectangle_.origin.y));
85
86 context_ = T::getTopPlatformDevice().GetBitmapContext();
87 }
88
89 CGContext* context_;
90 NSRect rectangle_;
91 // See description above setter.
92 bool composite_alpha_;
93
94 // Disallow copy and assign.
95 CanvasPaintT(const CanvasPaintT&);
96 CanvasPaintT& operator=(const CanvasPaintT&);
97 };
98
99 typedef CanvasPaintT<PlatformCanvas> PlatformCanvasPaint;
100
101 } // namespace skia
102
103
104 #endif // SKIA_EXT_CANVAS_PAINT_MAC_H_
OLDNEW
« no previous file with comments | « skia/ext/canvas_paint.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698