diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-19 21:04:19 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-19 21:04:19 +0000 |
commit | 28cd2bb36606c077618ec0976b889ae11ca55b0c (patch) | |
tree | 0a65fe9a993c9d92f915ba300a2730eed46d6703 /ui/gfx/compositor | |
parent | 4a7d639c0eaf559eda83a5c0e375f83bb8cf7171 (diff) | |
download | chromium_src-28cd2bb36606c077618ec0976b889ae11ca55b0c.zip chromium_src-28cd2bb36606c077618ec0976b889ae11ca55b0c.tar.gz chromium_src-28cd2bb36606c077618ec0976b889ae11ca55b0c.tar.bz2 |
Make specifying a texture optional on a Layer.
This is for Layers that participate in Draw() ordering, transforming and clipping, but do not themselves actually render anything.
Also changes LayerTest to use RunAllPending() rather than using the weird QuitOnComposited test type I had.
BUG=none
TEST=see unittest
Review URL: http://codereview.chromium.org/7941016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@101829 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx/compositor')
-rw-r--r-- | ui/gfx/compositor/layer.cc | 24 | ||||
-rw-r--r-- | ui/gfx/compositor/layer.h | 16 | ||||
-rw-r--r-- | ui/gfx/compositor/layer_unittest.cc | 124 |
3 files changed, 94 insertions, 70 deletions
diff --git a/ui/gfx/compositor/layer.cc b/ui/gfx/compositor/layer.cc index b9475b1..2c9f1a7 100644 --- a/ui/gfx/compositor/layer.cc +++ b/ui/gfx/compositor/layer.cc @@ -23,6 +23,17 @@ Layer::Layer(Compositor* compositor) delegate_(NULL) { } +Layer::Layer(Compositor* compositor, TextureParam texture_param) + : compositor_(compositor), + texture_(texture_param == LAYER_HAS_TEXTURE ? + compositor->CreateTexture() : NULL), + parent_(NULL), + visible_(true), + fills_bounds_opaquely_(false), + layer_updated_externally_(false), + delegate_(NULL) { +} + Layer::~Layer() { if (parent_) parent_->Remove(this); @@ -103,13 +114,9 @@ void Layer::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) { } void Layer::SetExternalTexture(ui::Texture* texture) { - if (texture == NULL) { - layer_updated_externally_ = false; - texture_ = compositor_->CreateTexture(); - } else { - layer_updated_externally_ = true; - texture_ = texture; - } + DCHECK(texture); + layer_updated_externally_ = true; + texture_ = texture; } void Layer::SetCanvas(const SkCanvas& canvas, const gfx::Point& origin) { @@ -123,6 +130,9 @@ void Layer::SchedulePaint(const gfx::Rect& invalid_rect) { } void Layer::Draw() { + if (!texture_.get()) + return; + UpdateLayerCanvas(); ui::TextureDrawParams texture_draw_params; diff --git a/ui/gfx/compositor/layer.h b/ui/gfx/compositor/layer.h index 734cb0d..2a37c2d 100644 --- a/ui/gfx/compositor/layer.h +++ b/ui/gfx/compositor/layer.h @@ -24,13 +24,21 @@ class Texture; // Layer manages a texture, transform and a set of child Layers. Any View that // has enabled layers ends up creating a Layer to manage the texture. +// A Layer can also be created without a texture, in which case it renders +// nothing and is simply used as a node in a hierarchy of layers. // // NOTE: unlike Views, each Layer does *not* own its children views. If you // delete a Layer and it has children, the parent of each child layer is set to // NULL, but the children are not deleted. class COMPOSITOR_EXPORT Layer { public: + enum TextureParam { + LAYER_HAS_NO_TEXTURE = 0, + LAYER_HAS_TEXTURE = 1 + }; + explicit Layer(Compositor* compositor); + Layer(Compositor* compositor, TextureParam texture_param); ~Layer(); LayerDelegate* delegate() { return delegate_; } @@ -81,10 +89,14 @@ class COMPOSITOR_EXPORT Layer { const Compositor* compositor() const { return compositor_; } Compositor* compositor() { return compositor_; } - // Passing NULL will cause the layer to get a texture from its compositor. - void SetExternalTexture(ui::Texture* texture); const ui::Texture* texture() const { return texture_.get(); } + // |texture| cannot be NULL, and this function cannot be called more than + // once. + // TODO(beng): This can be removed from the API when we are in a + // single-compositor world. + void SetExternalTexture(ui::Texture* texture); + // Resets the canvas of the texture. void SetCanvas(const SkCanvas& canvas, const gfx::Point& origin); diff --git a/ui/gfx/compositor/layer_unittest.cc b/ui/gfx/compositor/layer_unittest.cc index fdc607b..fcac281 100644 --- a/ui/gfx/compositor/layer_unittest.cc +++ b/ui/gfx/compositor/layer_unittest.cc @@ -15,23 +15,8 @@ namespace ui { namespace { -// Simple class that exits the current message loop when compositing finishes. -class CompositingEndedObserver : public CompositorObserver { - public: - CompositingEndedObserver() {} - virtual ~CompositingEndedObserver() {} - - // Overridden from CompositorObserver: - virtual void OnCompositingEnded() OVERRIDE { - MessageLoop::current()->Quit(); - } - - private: - DISALLOW_COPY_AND_ASSIGN(CompositingEndedObserver); -}; - class TestLayerDelegate : public LayerDelegate { -public: + public: explicit TestLayerDelegate(Layer* owner) : owner_(owner), color_index_(0) {} virtual ~TestLayerDelegate() {} @@ -52,7 +37,7 @@ public: color_index_ = ++color_index_ % colors_.size(); } -private: + private: Layer* owner_; std::vector<SkColor> colors_; int color_index_; @@ -80,17 +65,23 @@ class LayerTest : public testing::Test { return window_->GetCompositor(); } - Layer* CreateLayer() { - return new Layer(GetCompositor()); + Layer* CreateLayer(Layer::TextureParam texture_param) { + return new Layer(GetCompositor(), texture_param); } Layer* CreateColorLayer(SkColor color, const gfx::Rect& bounds) { - Layer* layer = CreateLayer(); + Layer* layer = CreateLayer(Layer::LAYER_HAS_TEXTURE); layer->SetBounds(bounds); PaintColorToLayer(layer, color); return layer; } + Layer* CreateNoTextureLayer(const gfx::Rect& bounds) { + Layer* layer = CreateLayer(Layer::LAYER_HAS_NO_TEXTURE); + layer->SetBounds(bounds); + return layer; + } + gfx::Canvas* CreateCanvasForLayer(const Layer* layer) { return gfx::Canvas::CreateCanvas(layer->bounds().width(), layer->bounds().height(), @@ -110,7 +101,7 @@ class LayerTest : public testing::Test { } void RunPendingMessages() { - MessageLoopForUI::current()->Run(NULL); + MessageLoopForUI::current()->RunAllPending(); } private: @@ -120,26 +111,26 @@ class LayerTest : public testing::Test { DISALLOW_COPY_AND_ASSIGN(LayerTest); }; -class LayerQuitOnCompositedTest : public LayerTest { +class DrawTreeLayerDelegate : public LayerDelegate { public: - LayerQuitOnCompositedTest() {} - virtual ~LayerQuitOnCompositedTest() {} + DrawTreeLayerDelegate() : painted_(false) {} + virtual ~DrawTreeLayerDelegate() {} - // Overridden from LayerTest: - virtual void SetUp() OVERRIDE { - LayerTest::SetUp(); - GetCompositor()->AddObserver(&compositor_observer_); + void Reset() { + painted_ = false; } - virtual void TearDown() OVERRIDE { - LayerTest::TearDown(); - GetCompositor()->RemoveObserver(&compositor_observer_); + bool painted() const { return painted_; } + + // Overridden from LayerDelegate: + virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE { + painted_ = true; } private: - CompositingEndedObserver compositor_observer_; + bool painted_; - DISALLOW_COPY_AND_ASSIGN(LayerQuitOnCompositedTest); + DISALLOW_COPY_AND_ASSIGN(DrawTreeLayerDelegate); }; } @@ -219,7 +210,7 @@ TEST_F(LayerTest, ConvertPointToLayer_Medium) { EXPECT_EQ(point2_in_l3_coords, point2_in_l1_coords); } -TEST_F(LayerQuitOnCompositedTest, Delegate) { +TEST_F(LayerTest, Delegate) { scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorBLACK, gfx::Rect(20, 20, 400, 400))); TestLayerDelegate delegate(l1.get()); @@ -246,33 +237,7 @@ TEST_F(LayerQuitOnCompositedTest, Delegate) { EXPECT_EQ(delegate.paint_size(), gfx::Size(50, 50)); } -namespace { - -class DrawTreeLayerDelegate : public LayerDelegate { - public: - DrawTreeLayerDelegate() : painted_(false) {} - virtual ~DrawTreeLayerDelegate() {} - - void Reset() { - painted_ = false; - } - - bool painted() const { return painted_; } - - // Overridden from LayerDelegate: - virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE { - painted_ = true; - } - - private: - bool painted_; - - DISALLOW_COPY_AND_ASSIGN(DrawTreeLayerDelegate); -}; - -} // namespace - -TEST_F(LayerQuitOnCompositedTest, DrawTree) { +TEST_F(LayerTest, DrawTree) { scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED, gfx::Rect(20, 20, 400, 400))); scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE, @@ -298,5 +263,42 @@ TEST_F(LayerQuitOnCompositedTest, DrawTree) { EXPECT_FALSE(d3.painted()); } +// Tests no-texture Layers. +// Create this hierarchy: +// L1 - red +// +-- L2 - NO TEXTURE +// | +-- L3 - yellow +// +-- L4 - magenta +// +TEST_F(LayerTest, HierarchyNoTexture) { + scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED, + gfx::Rect(20, 20, 400, 400))); + scoped_ptr<Layer> l2(CreateNoTextureLayer(gfx::Rect(10, 10, 350, 350))); + scoped_ptr<Layer> l3(CreateColorLayer(SK_ColorYELLOW, + gfx::Rect(5, 5, 25, 25))); + scoped_ptr<Layer> l4(CreateColorLayer(SK_ColorMAGENTA, + gfx::Rect(300, 300, 100, 100))); + + l1->Add(l2.get()); + l1->Add(l4.get()); + l2->Add(l3.get()); + + DrawTreeLayerDelegate d2; + l2->set_delegate(&d2); + DrawTreeLayerDelegate d3; + l3->set_delegate(&d3); + + GetCompositor()->set_root_layer(l1.get()); + + l2->SchedulePaint(gfx::Rect(5, 5, 5, 5)); + l3->SchedulePaint(gfx::Rect(5, 5, 5, 5)); + RunPendingMessages(); + + // |d2| should not have received a paint notification since it has no texture. + EXPECT_FALSE(d2.painted()); + // |d3| should have received a paint notification. + EXPECT_TRUE(d3.painted()); +} + } // namespace ui |