summaryrefslogtreecommitdiffstats
path: root/o3d/core
diff options
context:
space:
mode:
authortschmelcher@chromium.org <tschmelcher@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-24 22:58:47 +0000
committertschmelcher@chromium.org <tschmelcher@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-24 22:58:47 +0000
commitab31b8f682af3ef1019b9a9bb0dbc8063236f13c (patch)
tree5795b382a5fc8eaf95e1fb36a398892e53a0165d /o3d/core
parent2c3b7580b590f96d12e332bcfe3ade421fb483a5 (diff)
downloadchromium_src-ab31b8f682af3ef1019b9a9bb0dbc8063236f13c.zip
chromium_src-ab31b8f682af3ef1019b9a9bb0dbc8063236f13c.tar.gz
chromium_src-ab31b8f682af3ef1019b9a9bb0dbc8063236f13c.tar.bz2
O2D:
(1) De-couple the Layer abstraction from the implementations details of Texture, giving JavaScript more control over the scene: - Create a Pattern class backed by Cairo's cairo_pattern_t types (which we were already using implicitly) to represent a particular pattern to paint on a layer (think of it like a brush in PhotoShop/GIMP). A Pattern can be created from either a Texture or a solid colour. This will later allow us to move the PaintBackground() method out in to JS-space. - Axe the overloaded Layer scaling parameters (which were used to represent both the width/height of the Layer and the scaling applied to the Texture), and replace them with separate width/height parameters to describe the Layer dimensions and scale_x/scale_y parameters to describe the scaling applied to the Pattern. (2) Code clean-up: - Use properties instead of functions in the Layer class. - Use double instead of float. - Get rid of unnecessary inheritance of Layer from ParamObject. - Get rid of many unnecessary #include directives in layer.h. - Get rid of super-spammy log message in RendererCairo::Paint(). - Improve comments. TEST=compiled and loaded on Linux in both GL mode and Cairo mode with simple test scenes BUG=none Review URL: http://codereview.chromium.org/5276006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@67335 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'o3d/core')
-rw-r--r--o3d/core/core.gyp2
-rw-r--r--o3d/core/cross/cairo/layer.cc19
-rw-r--r--o3d/core/cross/cairo/layer.h117
-rw-r--r--o3d/core/cross/cairo/pattern.cc95
-rw-r--r--o3d/core/cross/cairo/pattern.h86
-rw-r--r--o3d/core/cross/cairo/renderer_cairo.cc33
-rw-r--r--o3d/core/cross/class_manager.cc2
-rw-r--r--o3d/core/cross/pack.h15
8 files changed, 285 insertions, 84 deletions
diff --git a/o3d/core/core.gyp b/o3d/core/core.gyp
index ad64ada..9e544c3 100644
--- a/o3d/core/core.gyp
+++ b/o3d/core/core.gyp
@@ -476,6 +476,8 @@
'cross/cairo/install_check.cc',
'cross/cairo/layer.cc',
'cross/cairo/layer.h',
+ 'cross/cairo/pattern.cc',
+ 'cross/cairo/pattern.h',
'cross/cairo/renderer_cairo.cc',
'cross/cairo/renderer_cairo.h',
'cross/cairo/texture_cairo.cc',
diff --git a/o3d/core/cross/cairo/layer.cc b/o3d/core/cross/cairo/layer.cc
index 23d4dfc..3d536f0 100644
--- a/o3d/core/cross/cairo/layer.cc
+++ b/o3d/core/cross/cairo/layer.cc
@@ -31,25 +31,26 @@
#include "core/cross/cairo/layer.h"
+
#include "core/cross/error.h"
#include "core/cross/renderer.h"
#include "core/cross/cairo/renderer_cairo.h"
-#include "core/cross/cairo/texture_cairo.h"
namespace o3d {
namespace o2d {
-O3D_DEFN_CLASS(Layer, ParamObject);
+O3D_DEFN_CLASS(Layer, ObjectBase);
Layer::Layer(ServiceLocator* service_locator)
- : ParamObject(service_locator),
- texture_(NULL),
- alpha_(0),
- scale_x_(0),
- scale_y_(0),
- translate_x_(0),
- translate_y_(0) {
+ : ObjectBase(service_locator),
+ alpha_(1.0),
+ x_(0),
+ y_(0),
+ width_(0),
+ height_(0),
+ scale_x_(1.0),
+ scale_y_(1.0) {
DLOG(INFO) << "Create Layer";
}
diff --git a/o3d/core/cross/cairo/layer.h b/o3d/core/cross/cairo/layer.h
index 8c341d5..6408dc0 100644
--- a/o3d/core/cross/cairo/layer.h
+++ b/o3d/core/cross/cairo/layer.h
@@ -29,70 +29,89 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-// An alternative class of Transform for 2d Image Rendering Mode.
+// A Layer is a rectangular region of the O2D canvas to be filled with a
+// particular Pattern, with automatic clipping based on stacking order.
#ifndef O3D_CORE_CROSS_CAIRO_LAYER_H_
#define O3D_CORE_CROSS_CAIRO_LAYER_H_
#include <vector>
-#include "core/cross/bounding_box.h"
-#include "core/cross/param.h"
-#include "core/cross/param_cache.h"
-#include "core/cross/param_object.h"
-#include "core/cross/types.h"
-#include "core/cross/cairo/texture_cairo.h"
+
+#include "core/cross/object_base.h"
+#include "core/cross/cairo/pattern.h"
namespace o3d {
+class IClassManager;
+
namespace o2d {
-class Layer : public ParamObject {
- friend class Client;
+class Layer : public ObjectBase {
public:
typedef SmartPointer<Layer> Ref;
- // Set the corresponding texture for this Layer instance.
- void SetTexture(Texture* texture) {
- texture_ = TextureCairo::Ref(down_cast<TextureCairo*>(texture));
+ Pattern* pattern() const {
+ return pattern_;
}
- TextureCairo* GetTexture() {
- return texture_;
+ void set_pattern(Pattern* pattern) {
+ pattern_ = Pattern::Ref(pattern);
}
- float GetAlpha() {
+ double alpha() const {
return alpha_;
}
- float GetScaleX() {
- return scale_x_;
+ void set_alpha(double alpha) {
+ alpha_ = alpha;
}
- float GetScaleY() {
- return scale_y_;
+ double x() const {
+ return x_;
}
- int GetTranslateX() {
- return translate_x_;
+ void set_x(double x) {
+ x_ = x;
}
- int GetTranslateY() {
- return translate_y_;
+ double y() const {
+ return y_;
}
- // Set the transparency of the Layer.
- void SetAlpha(float alpha) { alpha_ = alpha; }
+ void set_y(double y) {
+ y_ = y;
+ }
- // Translate the given x,y from its origin.
- void Translate(float x, float y) {
- translate_x_ = x;
- translate_y_ = y;
+ double width() const {
+ return width_;
}
- // Scale the image to the given x,y.
- void Scale(float x, float y) {
- scale_x_ = x;
- scale_y_ = y;
+ void set_width(double width) {
+ width_ = width;
+ }
+
+ double height() const {
+ return height_;
+ }
+
+ void set_height(double height) {
+ height_ = height;
+ }
+
+ double scale_x() const {
+ return scale_x_;
+ }
+
+ void set_scale_x(double scale_x) {
+ scale_x_ = scale_x;
+ }
+
+ double scale_y() const {
+ return scale_y_;
+ }
+
+ void set_scale_y(double scale_y) {
+ scale_y_ = scale_y;
}
private:
@@ -101,25 +120,31 @@ class Layer : public ParamObject {
friend class o3d::IClassManager;
static ObjectBase::Ref Create(ServiceLocator* service_locator);
- // Texture Container.
- TextureCairo::Ref texture_;
+ // The pattern used to paint this Layer.
+ Pattern::Ref pattern_;
+
+ // Transparancy of this layer.
+ double alpha_;
+
+ // The x coordinate of the top-left corner of this layer.
+ double x_;
- // Transparancy of the scene.
- float alpha_;
+ // The y coordinate of the top-left corner of this layer.
+ double y_;
- // The end-x-size of which the current size needs to scale.
- float scale_x_;
+ // The width of this layer.
+ double width_;
- // The end-y-size of which the current size needs to scale.
- float scale_y_;
+ // The height of this layer.
+ double height_;
- // Size of x to translate.
- float translate_x_;
+ // A scaling factor to apply to the pattern's x-axis.
+ double scale_x_;
- // Size of y to translate.
- float translate_y_;
+ // A scaling factor to apply to the pattern's y-axis.
+ double scale_y_;
- O3D_DECL_CLASS(Layer, ParamObject)
+ O3D_DECL_CLASS(Layer, ObjectBase)
}; // Layer
} // namespace o2d
diff --git a/o3d/core/cross/cairo/pattern.cc b/o3d/core/cross/cairo/pattern.cc
new file mode 100644
index 0000000..8a59d3e
--- /dev/null
+++ b/o3d/core/cross/cairo/pattern.cc
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2010, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+#include "core/cross/cairo/pattern.h"
+
+#include <cairo.h>
+
+#include "core/cross/pack.h"
+#include "core/cross/cairo/texture_cairo.h"
+
+namespace o3d {
+
+namespace o2d {
+
+O3D_DEFN_CLASS(Pattern, ObjectBase);
+
+// Cairo supports more pattern types than just these three, but we don't expose
+// the others.
+
+Pattern* Pattern::CreateTexturePattern(Pack* pack, Texture* texture) {
+ return WrapCairoPattern(pack,
+ cairo_pattern_create_for_surface(
+ down_cast<TextureCairo*>(texture)->image_surface()));
+}
+
+Pattern* Pattern::CreateRgbPattern(Pack* pack,
+ double red,
+ double green,
+ double blue) {
+ return WrapCairoPattern(pack,
+ cairo_pattern_create_rgb(red, green, blue));
+}
+
+Pattern* Pattern::CreateRgbaPattern(Pack* pack,
+ double red,
+ double green,
+ double blue,
+ double alpha) {
+ return WrapCairoPattern(pack,
+ cairo_pattern_create_rgba(red, green, blue, alpha));
+}
+
+Pattern::~Pattern() {
+ cairo_pattern_destroy(pattern_);
+}
+
+Pattern::Pattern(ServiceLocator* service_locator, cairo_pattern_t* pattern)
+ : ObjectBase(service_locator),
+ pattern_(pattern) {
+}
+
+Pattern* Pattern::WrapCairoPattern(Pack* pack, cairo_pattern_t* pattern) {
+ cairo_status_t status = cairo_pattern_status(pattern);
+ if (CAIRO_STATUS_SUCCESS != status) {
+ DLOG(ERROR) << "Error creating Cairo pattern: " << status;
+ cairo_pattern_destroy(pattern);
+ return NULL;
+ }
+ Pattern* p = new Pattern(pack->service_locator(), pattern);
+ pack->RegisterObject(p);
+ return p;
+}
+
+} // namespace o2d
+
+} // namespace o3d
diff --git a/o3d/core/cross/cairo/pattern.h b/o3d/core/cross/cairo/pattern.h
new file mode 100644
index 0000000..8828c6d
--- /dev/null
+++ b/o3d/core/cross/cairo/pattern.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2010, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// A Pattern is a container for pixel content for painting to a Layer.
+
+#ifndef O3D_CORE_CROSS_CAIRO_PATTERN_H_
+#define O3D_CORE_CROSS_CAIRO_PATTERN_H_
+
+#include "core/cross/object_base.h"
+
+typedef struct _cairo_pattern cairo_pattern_t;
+
+namespace o3d {
+
+class Pack;
+class Texture;
+
+namespace o2d {
+
+class Pattern : public ObjectBase {
+ public:
+ typedef SmartPointer<Pattern> Ref;
+
+ // Create a pattern that paints the content of a texture.
+ static Pattern* CreateTexturePattern(Pack* pack, Texture* texture);
+
+ // Create a pattern that paints a solid colour.
+ static Pattern* CreateRgbPattern(Pack* pack,
+ double red,
+ double green,
+ double blue);
+
+ // Create a pattern that paints a solid colour with transparency.
+ static Pattern* CreateRgbaPattern(Pack* pack,
+ double red,
+ double green,
+ double blue,
+ double alpha);
+
+ virtual ~Pattern();
+
+ cairo_pattern_t* pattern() const { return pattern_; }
+
+ private:
+ Pattern(ServiceLocator* service_locator, cairo_pattern_t* pattern);
+
+ static Pattern* WrapCairoPattern(Pack* pack, cairo_pattern_t* pattern);
+
+ cairo_pattern_t* pattern_;
+
+ O3D_DECL_CLASS(Pattern, ObjectBase)
+}; // Pattern
+
+} // namespace o2d
+
+} // namespace o3d
+
+#endif // O3D_CORE_CROSS_CAIRO_PATTERN_H_
diff --git a/o3d/core/cross/cairo/renderer_cairo.cc b/o3d/core/cross/cairo/renderer_cairo.cc
index 018b79e2..8bd698a 100644
--- a/o3d/core/cross/cairo/renderer_cairo.cc
+++ b/o3d/core/cross/cairo/renderer_cairo.cc
@@ -73,7 +73,6 @@ void RendererCairo::Destroy() {
}
void RendererCairo::Paint() {
- DLOG(INFO) << "To paint";
cairo_t* current_drawing = cairo_create(main_surface_);
// Paint the background.
@@ -87,10 +86,9 @@ void RendererCairo::Paint() {
// Preparing and updating the Layer.
Layer* cur = *i;
- TextureCairo* cur_texture = cur->GetTexture();
-
- if (!cur_texture) {
- // Skip layers with no texture assigned.
+ Pattern* pattern = cur->pattern();
+ if (!pattern) {
+ // Skip layers with no pattern assigned.
continue;
}
@@ -99,23 +97,14 @@ void RendererCairo::Paint() {
start_mask_it++;
MaskArea(current_drawing, start_mask_it);
- // Preparing the image to render.
- cairo_surface_t* image = cur_texture->image_surface();
-
- // Scale the image.
- double width_scaling =
- (static_cast<double>(cur->GetScaleX())) / cur_texture->width();
- double height_scaling =
- (static_cast<double>(cur->GetScaleY())) / cur_texture->height();
+ cairo_translate(current_drawing, cur->x(), cur->y());
- cairo_scale(current_drawing, width_scaling, height_scaling);
+ cairo_scale(current_drawing, cur->scale_x(), cur->scale_y());
// Painting the image to the surface.
- cairo_set_source_surface(current_drawing, image,
- cur->GetTranslateX() / width_scaling,
- cur->GetTranslateY() / height_scaling);
+ cairo_set_source(current_drawing, pattern->pattern());
- cairo_paint_with_alpha(current_drawing, cur->GetAlpha());
+ cairo_paint_with_alpha(current_drawing, cur->alpha());
// Restore to the state with no mask.
cairo_restore(current_drawing);
@@ -142,10 +131,10 @@ void RendererCairo::MaskArea(cairo_t* cr, LayerRefList::iterator it) {
cairo_rectangle(cr, 0, 0, display_width(), display_height());
cairo_rectangle(cr,
- cur_mask->GetTranslateX(),
- cur_mask->GetTranslateY(),
- static_cast<double>(cur_mask->GetScaleX()),
- static_cast<double>(cur_mask->GetScaleY()));
+ cur_mask->x(),
+ cur_mask->y(),
+ cur_mask->width(),
+ cur_mask->height());
cairo_clip(cr);
}
}
diff --git a/o3d/core/cross/class_manager.cc b/o3d/core/cross/class_manager.cc
index bb51ae4..3b36a8c 100644
--- a/o3d/core/cross/class_manager.cc
+++ b/o3d/core/cross/class_manager.cc
@@ -66,7 +66,9 @@
#include "core/cross/transform.h"
#include "core/cross/tree_traversal.h"
#include "core/cross/viewport.h"
+#if defined(RENDERER_CAIRO)
#include "core/cross/cairo/layer.h"
+#endif
namespace o3d {
diff --git a/o3d/core/cross/pack.h b/o3d/core/cross/pack.h
index 1d17c83..3a9f0ed 100644
--- a/o3d/core/cross/pack.h
+++ b/o3d/core/cross/pack.h
@@ -394,6 +394,14 @@ class Pack : public NamedObject {
// Array of Object Pointers.
ObjectBaseArray GetObjectsByClassName(const String& class_type_name) const;
+ // Register the given object with the Pack. The pack will add a reference
+ // to the object, guaranteeing its existence as long as the pack has not
+ // been destroyed. This is public so that it can be called by static
+ // factory methods.
+ // Parameters:
+ // object: Pointer to a ObjectBase to register within the pack
+ void RegisterObject(ObjectBase *object);
+
private:
// Texture objects function as factories for RenderSurface objects.
// Constructed RenderSurfaces are registered with the pack associated with
@@ -405,13 +413,6 @@ class Pack : public NamedObject {
virtual ~Pack();
- // Register the given object with the Pack. The pack will add a reference
- // to the object, guaranteeing its existence as long as the pack has not
- // been destroyed.
- // Parameters:
- // object: Pointer to a ObjectBase to register within the pack
- void RegisterObject(ObjectBase *object);
-
// Unregister a registered object from the pack. If this is the last reference
// to the object it will be destroyed.
// Parameters: