| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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_PLATFORM_CANVAS_LINUX_H_ | |
| 6 #define SKIA_EXT_PLATFORM_CANVAS_LINUX_H_ | |
| 7 | |
| 8 #include <unistd.h> | |
| 9 | |
| 10 #include "skia/ext/platform_device_linux.h" | |
| 11 | |
| 12 #include <cairo/cairo.h> | |
| 13 #include <gdk/gdk.h> | |
| 14 | |
| 15 namespace skia { | |
| 16 | |
| 17 // This class is a specialization of the regular SkCanvas that is designed to | |
| 18 // work with a gfx::PlatformDevice to manage platform-specific drawing. It | |
| 19 // allows using both Skia operations and platform-specific operations. | |
| 20 class PlatformCanvasLinux : public SkCanvas { | |
| 21 public: | |
| 22 // Set is_opaque if you are going to erase the bitmap and not use | |
| 23 // tranparency: this will enable some optimizations. The shared_section | |
| 24 // parameter is passed to gfx::PlatformDevice::create. See it for details. | |
| 25 // | |
| 26 // If you use the version with no arguments, you MUST call initialize() | |
| 27 PlatformCanvasLinux(); | |
| 28 PlatformCanvasLinux(int width, int height, bool is_opaque); | |
| 29 // Construct a canvas from the given memory region. The memory is not cleared | |
| 30 // first. @data must be, at least, @height * StrideForWidth(@width) bytes. | |
| 31 PlatformCanvasLinux(int width, int height, bool is_opaque, uint8_t* data); | |
| 32 virtual ~PlatformCanvasLinux(); | |
| 33 | |
| 34 // For two-part init, call if you use the no-argument constructor above | |
| 35 bool initialize(int width, int height, bool is_opaque); | |
| 36 bool initialize(int width, int height, bool is_opaque, uint8_t* data); | |
| 37 | |
| 38 // These calls should surround calls to platform-specific drawing routines. | |
| 39 // The cairo_surface_t* returned by beginPlatformPaint represents the | |
| 40 // memory that can be used to draw into. | |
| 41 // endPlatformPaint is a no-op; it is used for symmetry with Windows. | |
| 42 cairo_surface_t* beginPlatformPaint(); | |
| 43 void endPlatformPaint() {} | |
| 44 | |
| 45 // Returns the platform device pointer of the topmost rect with a non-empty | |
| 46 // clip. Both the windows and mac versions have an equivalent of this method; | |
| 47 // a Linux version is added for compatibility. | |
| 48 PlatformDeviceLinux& getTopPlatformDevice() const; | |
| 49 | |
| 50 // Return the stride (length of a line in bytes) for the given width. Because | |
| 51 // we use 32-bits per pixel, this will be roughly 4*width. However, for | |
| 52 // alignment reasons we may wish to increase that. | |
| 53 static size_t StrideForWidth(unsigned width); | |
| 54 | |
| 55 protected: | |
| 56 // Creates a device store for use by the canvas. We override this so that | |
| 57 // the device is always our own so we know that we can use GDI operations | |
| 58 // on it. Simply calls into createPlatformDevice(). | |
| 59 virtual SkDevice* createDevice(SkBitmap::Config, int width, int height, | |
| 60 bool is_opaque, bool isForLayer); | |
| 61 | |
| 62 // Creates a device store for use by the canvas. By default, it creates a | |
| 63 // BitmapPlatformDevice object. Can be overridden to change the object type. | |
| 64 virtual SkDevice* createPlatformDevice(int width, int height, bool is_opaque); | |
| 65 | |
| 66 // Disallow copy and assign. | |
| 67 PlatformCanvasLinux(const PlatformCanvasLinux&); | |
| 68 PlatformCanvasLinux& operator=(const PlatformCanvasLinux&); | |
| 69 }; | |
| 70 | |
| 71 // A class designed to translate skia painting into a region in a | |
| 72 // GdkWindow. This class has been adapted from the class with the same name in | |
| 73 // platform_canvas_win.h. On construction, it will set up a context for | |
| 74 // painting into, and on destruction, it will commit it to the GdkWindow. | |
| 75 template <class T> | |
| 76 class CanvasPaintT : public T { | |
| 77 public: | |
| 78 explicit CanvasPaintT(GdkEventExpose* event) | |
| 79 : surface_(NULL), | |
| 80 window_(event->window), | |
| 81 rectangle_(event->area), | |
| 82 composite_alpha_(false) { | |
| 83 init(true); | |
| 84 } | |
| 85 | |
| 86 CanvasPaintT(GdkEventExpose* event, bool opaque) | |
| 87 : surface_(NULL), | |
| 88 window_(event->window), | |
| 89 rectangle_(event->area), | |
| 90 composite_alpha_(false) { | |
| 91 init(opaque); | |
| 92 } | |
| 93 | |
| 94 virtual ~CanvasPaintT() { | |
| 95 if (!isEmpty()) { | |
| 96 T::restoreToCount(1); | |
| 97 | |
| 98 // Blit the dirty rect to the window. | |
| 99 cairo_t* cr = gdk_cairo_create(window_); | |
| 100 if (composite_alpha_) | |
| 101 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); | |
| 102 cairo_set_source_surface(cr, surface_, rectangle_.x, rectangle_.y); | |
| 103 cairo_rectangle(cr, rectangle_.x, rectangle_.y, | |
| 104 rectangle_.width, rectangle_.height); | |
| 105 cairo_fill(cr); | |
| 106 cairo_destroy(cr); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 // Sets whether the bitmap is composited in such a way that the alpha channel | |
| 111 // is honored. This is only useful if you've enabled an RGBA colormap on the | |
| 112 // widget. The default is false. | |
| 113 void set_composite_alpha(bool composite_alpha) { | |
| 114 composite_alpha_ = composite_alpha; | |
| 115 } | |
| 116 | |
| 117 // Returns true if the invalid region is empty. The caller should call this | |
| 118 // function to determine if anything needs painting. | |
| 119 bool isEmpty() const { | |
| 120 return rectangle_.width == 0 || rectangle_.height == 0; | |
| 121 } | |
| 122 | |
| 123 const GdkRectangle& rectangle() const { | |
| 124 return rectangle_; | |
| 125 } | |
| 126 | |
| 127 private: | |
| 128 void init(bool opaque) { | |
| 129 if (!T::initialize(rectangle_.width, rectangle_.height, opaque, NULL)) { | |
| 130 // Cause a deliberate crash; | |
| 131 *(char*) 0 = 0; | |
| 132 } | |
| 133 | |
| 134 // Need to translate so that the dirty region appears at the origin of the | |
| 135 // surface. | |
| 136 T::translate(-SkIntToScalar(rectangle_.x), -SkIntToScalar(rectangle_.y)); | |
| 137 | |
| 138 surface_ = T::getTopPlatformDevice().beginPlatformPaint(); | |
| 139 } | |
| 140 | |
| 141 cairo_surface_t* surface_; | |
| 142 GdkWindow* window_; | |
| 143 GdkRectangle rectangle_; | |
| 144 // See description above setter. | |
| 145 bool composite_alpha_; | |
| 146 | |
| 147 // Disallow copy and assign. | |
| 148 CanvasPaintT(const CanvasPaintT&); | |
| 149 CanvasPaintT& operator=(const CanvasPaintT&); | |
| 150 }; | |
| 151 | |
| 152 } // namespace skia | |
| 153 | |
| 154 #endif // SKIA_EXT_PLATFORM_CANVAS_LINUX_H_ | |
| OLD | NEW |