| OLD | NEW |
| (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_LINUX_H_ |
| 7 #define SKIA_EXT_CANVAS_PAINT_LINUX_H_ |
| 8 |
| 9 #include "skia/ext/platform_canvas.h" |
| 10 |
| 11 namespace skia { |
| 12 |
| 13 // A class designed to translate skia painting into a region in a |
| 14 // GdkWindow. This class has been adapted from the class with the same name in |
| 15 // platform_canvas_win.h. On construction, it will set up a context for |
| 16 // painting into, and on destruction, it will commit it to the GdkWindow. |
| 17 template <class T> |
| 18 class CanvasPaintT : public T { |
| 19 public: |
| 20 // This constructor assumes the result is opaque. |
| 21 explicit CanvasPaintT(GdkEventExpose* event) |
| 22 : surface_(NULL), |
| 23 window_(event->window), |
| 24 rectangle_(event->area), |
| 25 composite_alpha_(false) { |
| 26 init(true); |
| 27 } |
| 28 |
| 29 CanvasPaintT(GdkEventExpose* event, bool opaque) |
| 30 : surface_(NULL), |
| 31 window_(event->window), |
| 32 rectangle_(event->area), |
| 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 window. |
| 42 cairo_t* cr = gdk_cairo_create(window_); |
| 43 if (composite_alpha_) |
| 44 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 45 cairo_set_source_surface(cr, surface_, rectangle_.x, rectangle_.y); |
| 46 cairo_rectangle(cr, rectangle_.x, rectangle_.y, |
| 47 rectangle_.width, rectangle_.height); |
| 48 cairo_fill(cr); |
| 49 cairo_destroy(cr); |
| 50 } |
| 51 } |
| 52 |
| 53 // Sets whether the bitmap is composited in such a way that the alpha channel |
| 54 // is honored. This is only useful if you've enabled an RGBA colormap on the |
| 55 // widget. The default is false. |
| 56 void set_composite_alpha(bool composite_alpha) { |
| 57 composite_alpha_ = composite_alpha; |
| 58 } |
| 59 |
| 60 // Returns true if the invalid region is empty. The caller should call this |
| 61 // function to determine if anything needs painting. |
| 62 bool is_empty() const { |
| 63 return rectangle_.width == 0 || rectangle_.height == 0; |
| 64 } |
| 65 |
| 66 const GdkRectangle& rectangle() const { |
| 67 return rectangle_; |
| 68 } |
| 69 |
| 70 private: |
| 71 void init(bool opaque) { |
| 72 if (!T::initialize(rectangle_.width, rectangle_.height, opaque, NULL)) { |
| 73 // Cause a deliberate crash; |
| 74 *(char*) 0 = 0; |
| 75 } |
| 76 |
| 77 // Need to translate so that the dirty region appears at the origin of the |
| 78 // surface. |
| 79 T::translate(-SkIntToScalar(rectangle_.x), -SkIntToScalar(rectangle_.y)); |
| 80 |
| 81 surface_ = T::getTopPlatformDevice().beginPlatformPaint(); |
| 82 } |
| 83 |
| 84 cairo_surface_t* surface_; |
| 85 GdkWindow* window_; |
| 86 GdkRectangle rectangle_; |
| 87 // See description above setter. |
| 88 bool composite_alpha_; |
| 89 |
| 90 // Disallow copy and assign. |
| 91 CanvasPaintT(const CanvasPaintT&); |
| 92 CanvasPaintT& operator=(const CanvasPaintT&); |
| 93 }; |
| 94 |
| 95 typedef CanvasPaintT<PlatformCanvas> PlatformCanvasPaint; |
| 96 |
| 97 } // namespace skia |
| 98 |
| 99 #endif // SKIA_EXT_CANVAS_PAINT_LINUX_H_ |
| OLD | NEW |