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