OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef SKIA_EXT_CANVAS_PAINT_WAYLAND_H_ | |
6 #define SKIA_EXT_CANVAS_PAINT_WAYLAND_H_ | |
7 #pragma once | |
8 | |
9 #include "base/logging.h" | |
10 #include "skia/ext/canvas_paint_common.h" | |
11 #include "skia/ext/platform_canvas.h" | |
12 | |
13 namespace skia { | |
14 | |
15 // A class designed to translate skia painting into a region in a Wayland window | |
16 // surface. On construction, it will set up a context for painting into, and on | |
17 // destruction, it will commit it to the Wayland window surface. | |
18 // Note: The created context is always inialized to (0, 0, 0, 0). | |
19 template <class T> | |
20 class CanvasPaintT : public T { | |
21 public: | |
22 // This constructor assumes the result is opaque. | |
23 CanvasPaintT(cairo_surface_t* cairo_window_surface, | |
24 cairo_rectangle_int_t* region) | |
25 : context_(NULL), | |
26 cairo_window_surface_(cairo_window_surface), | |
27 region_(region), | |
28 composite_alpha_(false) { | |
29 init(true); | |
30 } | |
31 | |
32 CanvasPaintT(cairo_surface_t* cairo_window_surface, | |
33 cairo_rectangle_int_t* region, | |
34 bool opaque) | |
35 : context_(NULL), | |
36 cairo_window_surface_(cairo_window_surface), | |
37 region_(region), | |
38 composite_alpha_(false) { | |
39 init(opaque); | |
40 } | |
41 | |
42 virtual ~CanvasPaintT() { | |
43 if (!is_empty()) { | |
44 PlatformCanvas* canvas = GetPlatformCanvas(this); | |
45 canvas->restoreToCount(1); | |
46 | |
47 // Blit the dirty rect to the window. | |
48 CHECK(cairo_window_surface_); | |
49 cairo_t* cr = cairo_create(cairo_window_surface_); | |
50 CHECK(cr); | |
51 | |
52 if (composite_alpha_) | |
53 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); | |
54 | |
55 cairo_surface_t* source_surface = cairo_get_target(context_); | |
56 CHECK(source_surface); | |
57 // Flush cairo's cache of the surface. | |
58 cairo_surface_mark_dirty(source_surface); | |
59 cairo_set_source_surface(cr, source_surface, region_->x, region_->y); | |
60 cairo_rectangle(cr, | |
61 region_->x, | |
62 region_->y, | |
63 region_->width, | |
64 region_->height); | |
65 cairo_fill(cr); | |
66 cairo_destroy(cr); | |
67 } | |
68 } | |
69 | |
70 // Sets whether the bitmap is composited in such a way that the alpha channel | |
71 // is honored. This is only useful if you've enabled an RGBA colormap on the | |
72 // widget. The default is false. | |
73 void set_composite_alpha(bool composite_alpha) { | |
74 composite_alpha_ = composite_alpha; | |
75 } | |
76 | |
77 // Returns true if the invalid region is empty. The caller should call this | |
78 // function to determine if anything needs painting. | |
79 bool is_empty() const { | |
80 return region_->width == 0 && region_->height == 0; | |
81 } | |
82 | |
83 private: | |
84 void init(bool opaque) { | |
85 PlatformCanvas* canvas = GetPlatformCanvas(this); | |
86 if (!canvas->initialize(region_->width, region_->height, opaque, NULL)) { | |
87 // Cause a deliberate crash; | |
88 CHECK(false); | |
89 } | |
90 // No need to clear the canvas, because cairo automatically performs the | |
91 // clear. | |
92 | |
93 // Need to translate so that the dirty region appears at the origin of the | |
94 // surface. | |
95 canvas->translate(-SkIntToScalar(region_->x), -SkIntToScalar(region_->y)); | |
96 | |
97 context_ = BeginPlatformPaint(canvas); | |
98 } | |
99 | |
100 cairo_t* context_; | |
101 cairo_surface_t* cairo_window_surface_; | |
102 cairo_rectangle_int_t* region_; | |
103 // See description above setter. | |
104 bool composite_alpha_; | |
105 | |
106 // Disallow copy and assign. | |
107 CanvasPaintT(const CanvasPaintT&); | |
108 CanvasPaintT& operator=(const CanvasPaintT&); | |
109 }; | |
110 | |
111 typedef CanvasPaintT<PlatformCanvas> PlatformCanvasPaint; | |
112 | |
113 } // namespace skia | |
114 | |
115 #endif // SKIA_EXT_CANVAS_PAINT_WAYLAND_H_ | |
OLD | NEW |