diff options
-rw-r--r-- | o3d/core/core.gyp | 2 | ||||
-rw-r--r-- | o3d/core/cross/cairo/layer.cc | 19 | ||||
-rw-r--r-- | o3d/core/cross/cairo/layer.h | 117 | ||||
-rw-r--r-- | o3d/core/cross/cairo/pattern.cc | 95 | ||||
-rw-r--r-- | o3d/core/cross/cairo/pattern.h | 86 | ||||
-rw-r--r-- | o3d/core/cross/cairo/renderer_cairo.cc | 33 | ||||
-rw-r--r-- | o3d/core/cross/class_manager.cc | 2 | ||||
-rw-r--r-- | o3d/core/cross/pack.h | 15 | ||||
-rw-r--r-- | o3d/plugin/idl/idl.gyp | 1 | ||||
-rw-r--r-- | o3d/plugin/idl/layer.idl | 44 | ||||
-rw-r--r-- | o3d/plugin/idl/pattern.idl | 82 |
11 files changed, 395 insertions, 101 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: diff --git a/o3d/plugin/idl/idl.gyp b/o3d/plugin/idl/idl.gyp index 1a16027..453486d 100644 --- a/o3d/plugin/idl/idl.gyp +++ b/o3d/plugin/idl/idl.gyp @@ -18,6 +18,7 @@ { 'idl_files': [ 'layer.idl', + 'pattern.idl', ], }, ], diff --git a/o3d/plugin/idl/layer.idl b/o3d/plugin/idl/layer.idl index 9525783..964feaf 100644 --- a/o3d/plugin/idl/layer.idl +++ b/o3d/plugin/idl/layer.idl @@ -34,39 +34,49 @@ namespace o3d { namespace o2d { %[ - Layer defines a container for a layer of scene. - It enables JavaScript to manipulate the layer. + A Layer is a rectangular region of the O2D canvas to be filled with a + particular Pattern, with automatic clipping based on stacking order. %] -[nocpp, include="core/cross/cairo/layer.h"] class Layer : ParamObject { +[nocpp, include="core/cross/cairo/layer.h"] class Layer : ObjectBase { %[ - Translate the object from its current coordinate. + The Pattern used to paint this Layer. + %] + [getter, setter] Pattern? pattern; - \param x amount to translate in x. - \param y amount to translate in y. + %[ + Transparancy of this layer. %] - void Translate(float x, float y); + [getter, setter] double alpha; %[ - Scale the size of the object. + The x coordinate of the top-left corner of this layer. + %] + [getter, setter] double x; - \param x amount to scale in the x dimension. - \param y amount to scale in the y dimension. + %[ + The y coordinate of the top-left corner of this layer. %] - void Scale(float x, float y); + [getter, setter] double y; %[ - Set the transparancy of the object. + The width of this layer. + %] + [getter, setter] double width; - \param alpha amount of the transparancy. + %[ + The height of this layer. %] - void SetAlpha(float alpha); + [getter, setter] double height; %[ - Set the texture for this object. + A scaling factor to apply to the pattern's x-axis. + %] + [getter, setter] double scale_x; - \param texture the texture to assign. + %[ + A scaling factor to apply to the pattern's y-axis. %] - void SetTexture(Texture? texture); + [getter, setter] double scale_y; }; // Layer } // namespace o2d diff --git a/o3d/plugin/idl/pattern.idl b/o3d/plugin/idl/pattern.idl new file mode 100644 index 0000000..c511809 --- /dev/null +++ b/o3d/plugin/idl/pattern.idl @@ -0,0 +1,82 @@ +/* + * 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. + */ + +namespace o3d { + +namespace o2d { + +%[ + A Pattern is a container for pixel content for painting to a Layer. +%] +[nocpp, include="core/cross/cairo/pattern.h"] class Pattern : ObjectBase { + %[ + Create a pattern that paints the content of a texture. + + \param pack The Pack to add the created object to. + \param texture The texture to paint. + \return The created pattern + %] + [static] Pattern? CreateTexturePattern(Pack pack, Texture texture); + + %[ + Create a pattern that paints a solid colour. + + \param pack The Pack to add the created object to. + \param red The red component. + \param blue The blue component. + \param green The green component. + \return The created pattern + %] + [static] Pattern? CreateRgbPattern(Pack pack, + double red, + double green, + double blue); + + %[ + Create a pattern that paints a solid colour with transparency. + + \param pack The Pack to add the created object to. + \param red The red component. + \param blue The blue component. + \param green The green component. + \param alpha The alpha component. + \return The created pattern + %] + [static] Pattern? CreateRgbaPattern(Pack pack, + double red, + double green, + double blue, + double alpha); +}; // Pattern + +} // namespace o2d + +} // namespace o3d |