summaryrefslogtreecommitdiffstats
path: root/skia/ext/canvas_paint_linux.h
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-14 15:14:53 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-14 15:14:53 +0000
commitbd63dc9cdf640a6d1e833ce0b5ed7ed9090973b0 (patch)
tree149a64e70ca634bd43019d639d9ae29ca6d45a9a /skia/ext/canvas_paint_linux.h
parentb39627f8eef41a2d4e1f4516bf905d6ad895e56b (diff)
downloadchromium_src-bd63dc9cdf640a6d1e833ce0b5ed7ed9090973b0.zip
chromium_src-bd63dc9cdf640a6d1e833ce0b5ed7ed9090973b0.tar.gz
chromium_src-bd63dc9cdf640a6d1e833ce0b5ed7ed9090973b0.tar.bz2
Refactor the PlatformContext layer to have only one class.
Previously we had three classes of PlatformCanvas*, one for each platform. Then we had a typedef of PlatformContext to PlatformCanvas[Mac|Win|Linux] for the specific platform. This means that it was almost impossible to forward-declare PlatformCanvas and there were a bunch of unnecessary includes of platform_canvas.h in header files. This change makes there be only one platform_canvas.h header with ifdefs, which removes a decent amount of duplicated code. There is a platform-independent file, and one platform-dependent file of platform_canvas for each platform. I also renamed PlatformDevice[Mac|Win|Linux] to PlatformDevice, althouth in this case I kept the separate headers since there was much less overlap. I also broke out CanvasPaint into separate headers so this template doesn't need to be included all over the project (only a couple of files actually need it). Review URL: http://codereview.chromium.org/125109 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@18363 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'skia/ext/canvas_paint_linux.h')
-rw-r--r--skia/ext/canvas_paint_linux.h99
1 files changed, 99 insertions, 0 deletions
diff --git a/skia/ext/canvas_paint_linux.h b/skia/ext/canvas_paint_linux.h
new file mode 100644
index 0000000..d05f1d6
--- /dev/null
+++ b/skia/ext/canvas_paint_linux.h
@@ -0,0 +1,99 @@
+
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SKIA_EXT_CANVAS_PAINT_LINUX_H_
+#define SKIA_EXT_CANVAS_PAINT_LINUX_H_
+
+#include "skia/ext/platform_canvas.h"
+
+namespace skia {
+
+// A class designed to translate skia painting into a region in a
+// GdkWindow. This class has been adapted from the class with the same name in
+// platform_canvas_win.h. On construction, it will set up a context for
+// painting into, and on destruction, it will commit it to the GdkWindow.
+template <class T>
+class CanvasPaintT : public T {
+ public:
+ // This constructor assumes the result is opaque.
+ explicit CanvasPaintT(GdkEventExpose* event)
+ : surface_(NULL),
+ window_(event->window),
+ rectangle_(event->area),
+ composite_alpha_(false) {
+ init(true);
+ }
+
+ CanvasPaintT(GdkEventExpose* event, bool opaque)
+ : surface_(NULL),
+ window_(event->window),
+ rectangle_(event->area),
+ composite_alpha_(false) {
+ init(opaque);
+ }
+
+ virtual ~CanvasPaintT() {
+ if (!is_empty()) {
+ T::restoreToCount(1);
+
+ // Blit the dirty rect to the window.
+ cairo_t* cr = gdk_cairo_create(window_);
+ if (composite_alpha_)
+ cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
+ cairo_set_source_surface(cr, surface_, rectangle_.x, rectangle_.y);
+ cairo_rectangle(cr, rectangle_.x, rectangle_.y,
+ rectangle_.width, rectangle_.height);
+ cairo_fill(cr);
+ cairo_destroy(cr);
+ }
+ }
+
+ // Sets whether the bitmap is composited in such a way that the alpha channel
+ // is honored. This is only useful if you've enabled an RGBA colormap on the
+ // widget. The default is false.
+ void set_composite_alpha(bool composite_alpha) {
+ composite_alpha_ = composite_alpha;
+ }
+
+ // Returns true if the invalid region is empty. The caller should call this
+ // function to determine if anything needs painting.
+ bool is_empty() const {
+ return rectangle_.width == 0 || rectangle_.height == 0;
+ }
+
+ const GdkRectangle& rectangle() const {
+ return rectangle_;
+ }
+
+ private:
+ void init(bool opaque) {
+ if (!T::initialize(rectangle_.width, rectangle_.height, opaque, NULL)) {
+ // Cause a deliberate crash;
+ *(char*) 0 = 0;
+ }
+
+ // Need to translate so that the dirty region appears at the origin of the
+ // surface.
+ T::translate(-SkIntToScalar(rectangle_.x), -SkIntToScalar(rectangle_.y));
+
+ surface_ = T::getTopPlatformDevice().beginPlatformPaint();
+ }
+
+ cairo_surface_t* surface_;
+ GdkWindow* window_;
+ GdkRectangle rectangle_;
+ // See description above setter.
+ bool composite_alpha_;
+
+ // Disallow copy and assign.
+ CanvasPaintT(const CanvasPaintT&);
+ CanvasPaintT& operator=(const CanvasPaintT&);
+};
+
+typedef CanvasPaintT<PlatformCanvas> PlatformCanvasPaint;
+
+} // namespace skia
+
+#endif // SKIA_EXT_CANVAS_PAINT_LINUX_H_