Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/layers/nine_patch_layer.h" | 5 #include "cc/layers/nine_patch_layer.h" |
| 6 | 6 |
| 7 #include "cc/layers/nine_patch_layer_impl.h" | 7 #include "cc/layers/nine_patch_layer_impl.h" |
| 8 #include "cc/resources/prioritized_resource.h" | 8 #include "cc/resources/prioritized_resource.h" |
| 9 #include "cc/resources/resource_update.h" | 9 #include "cc/resources/resource_update.h" |
| 10 #include "cc/resources/resource_update_queue.h" | 10 #include "cc/resources/resource_update_queue.h" |
| 11 #include "cc/resources/scoped_ui_resource.h" | |
| 12 #include "cc/resources/ui_resource_bitmap.h" | |
| 11 #include "cc/trees/layer_tree_host.h" | 13 #include "cc/trees/layer_tree_host.h" |
| 12 | 14 |
| 15 namespace { | |
| 16 | |
| 17 class ScopedUIResourceHolder : public cc::NinePatchLayer::UIResourceHolder { | |
|
aelias_OOO_until_Jul13
2013/08/30 06:20:41
I'm pretty sure you can put the namespace {} insid
powei
2013/09/03 23:36:05
Done.
| |
| 18 public: | |
| 19 static scoped_ptr<ScopedUIResourceHolder> Create(cc::LayerTreeHost* host, | |
| 20 const SkBitmap& skbitmap) { | |
| 21 return make_scoped_ptr(new ScopedUIResourceHolder(host, skbitmap)); | |
| 22 } | |
| 23 cc::UIResourceId id() OVERRIDE { return resource_->id(); } | |
| 24 | |
| 25 private: | |
| 26 ScopedUIResourceHolder(cc::LayerTreeHost* host, const SkBitmap& skbitmap) { | |
| 27 resource_ = cc::ScopedUIResource::Create( | |
| 28 host, cc::UIResourceBitmap::CreateFromSkBitmap(skbitmap)); | |
| 29 } | |
| 30 | |
| 31 scoped_ptr<cc::ScopedUIResource> resource_; | |
| 32 }; | |
| 33 | |
| 34 class SharedUIResourceHolder : public cc::NinePatchLayer::UIResourceHolder { | |
| 35 public: | |
| 36 static scoped_ptr<SharedUIResourceHolder> Create(cc::UIResourceId id) { | |
| 37 return make_scoped_ptr(new SharedUIResourceHolder(id)); | |
| 38 } | |
| 39 | |
| 40 cc::UIResourceId id() OVERRIDE { return id_; } | |
| 41 | |
| 42 private: | |
| 43 explicit SharedUIResourceHolder(cc::UIResourceId id) : id_(id) {} | |
| 44 | |
| 45 cc::UIResourceId id_; | |
| 46 }; | |
| 47 | |
| 48 } // anonymous namespace | |
| 49 | |
| 13 namespace cc { | 50 namespace cc { |
| 14 | 51 |
| 52 NinePatchLayer::UIResourceHolder::~UIResourceHolder() {} | |
| 53 | |
| 15 scoped_refptr<NinePatchLayer> NinePatchLayer::Create() { | 54 scoped_refptr<NinePatchLayer> NinePatchLayer::Create() { |
| 16 return make_scoped_refptr(new NinePatchLayer()); | 55 return make_scoped_refptr(new NinePatchLayer()); |
| 17 } | 56 } |
| 18 | 57 |
| 19 NinePatchLayer::NinePatchLayer() | 58 NinePatchLayer::NinePatchLayer() : fill_center_(true) {} |
|
aelias_OOO_until_Jul13
2013/08/30 06:20:41
Should this default to false? You're setting it t
powei
2013/09/03 23:36:05
Done.
| |
| 20 : bitmap_dirty_(false) {} | |
| 21 | 59 |
| 22 NinePatchLayer::~NinePatchLayer() {} | 60 NinePatchLayer::~NinePatchLayer() {} |
| 23 | 61 |
| 24 scoped_ptr<LayerImpl> NinePatchLayer::CreateLayerImpl( | 62 scoped_ptr<LayerImpl> NinePatchLayer::CreateLayerImpl( |
| 25 LayerTreeImpl* tree_impl) { | 63 LayerTreeImpl* tree_impl) { |
| 26 return NinePatchLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>(); | 64 return NinePatchLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>(); |
| 27 } | 65 } |
| 28 | 66 |
| 29 void NinePatchLayer::SetTexturePriorities( | 67 void NinePatchLayer::SetLayerTreeHost(LayerTreeHost* host) { |
| 30 const PriorityCalculator& priority_calc) { | 68 // When the LTH is set to null or has changed, then this layer should remove |
| 31 if (resource_ && !resource_->texture()->resource_manager()) { | 69 // all of its associated resources. |
| 32 // Release the resource here, as it is no longer tied to a resource manager. | 70 if (!host || host != layer_tree_host()) |
| 33 resource_.reset(); | 71 resource_.reset(); |
| 34 if (!bitmap_.isNull()) | 72 |
| 35 CreateResource(); | 73 Layer::SetLayerTreeHost(host); |
| 36 } else if (bitmap_dirty_ && DrawsContent()) { | 74 } |
| 37 CreateResource(); | 75 |
| 76 void NinePatchLayer::SetBorder(gfx::Rect border) { | |
| 77 if (border == border_) | |
| 78 return; | |
| 79 border_ = border; | |
| 80 SetNeedsCommit(); | |
| 81 } | |
| 82 | |
| 83 void NinePatchLayer::SetBitmap(const SkBitmap& skbitmap, gfx::Rect aperture) { | |
| 84 if (!layer_tree_host()) | |
| 85 return; | |
| 86 image_aperture_ = aperture; | |
| 87 fill_center_ = false; | |
| 88 resource_ = ScopedUIResourceHolder::Create(layer_tree_host(), skbitmap); | |
| 89 | |
| 90 SetNeedsPushProperties(); | |
| 91 SetNeedsCommit(); | |
| 92 } | |
| 93 | |
| 94 void NinePatchLayer::SetUIResourceId(UIResourceId resource_id, | |
| 95 gfx::Rect aperture, | |
| 96 bool fill_center) { | |
| 97 if (resource_ && resource_->id() == resource_id && | |
| 98 image_aperture_ == aperture && fill_center == fill_center_) | |
| 99 return; | |
| 100 | |
| 101 image_aperture_ = aperture; | |
| 102 fill_center_ = fill_center; | |
| 103 if (resource_id) { | |
| 104 resource_ = SharedUIResourceHolder::Create(resource_id); | |
| 105 } else { | |
| 106 resource_.reset(); | |
| 38 } | 107 } |
| 39 | 108 |
| 40 if (resource_) { | 109 SetNeedsPushProperties(); |
| 41 resource_->texture()->set_request_priority( | 110 SetNeedsCommit(); |
| 42 PriorityCalculator::UIPriority(true)); | |
| 43 GLenum texture_format = | |
| 44 layer_tree_host()->GetRendererCapabilities().best_texture_format; | |
| 45 resource_->texture()->SetDimensions( | |
| 46 gfx::Size(bitmap_.width(), bitmap_.height()), texture_format); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 void NinePatchLayer::SetBitmap(const SkBitmap& bitmap, gfx::Rect aperture) { | |
| 51 bitmap_ = bitmap; | |
| 52 image_aperture_ = aperture; | |
| 53 bitmap_dirty_ = true; | |
| 54 SetNeedsDisplay(); | |
| 55 } | |
| 56 | |
| 57 bool NinePatchLayer::Update(ResourceUpdateQueue* queue, | |
| 58 const OcclusionTracker* occlusion) { | |
| 59 bool updated = Layer::Update(queue, occlusion); | |
| 60 | |
| 61 CreateUpdaterIfNeeded(); | |
| 62 | |
| 63 if (resource_ && | |
| 64 (bitmap_dirty_ || resource_->texture()->resource_id() == 0)) { | |
| 65 gfx::Rect content_rect(0, 0, bitmap_.width(), bitmap_.height()); | |
| 66 ResourceUpdate upload = ResourceUpdate::Create(resource_->texture(), | |
| 67 &bitmap_, | |
| 68 content_rect, | |
| 69 content_rect, | |
| 70 gfx::Vector2d()); | |
| 71 queue->AppendFullUpload(upload); | |
| 72 bitmap_dirty_ = false; | |
| 73 updated = true; | |
| 74 } | |
| 75 | |
| 76 return updated; | |
| 77 } | |
| 78 | |
| 79 void NinePatchLayer::CreateUpdaterIfNeeded() { | |
| 80 if (updater_.get()) | |
| 81 return; | |
| 82 | |
| 83 updater_ = ImageLayerUpdater::Create(); | |
| 84 } | |
| 85 | |
| 86 void NinePatchLayer::CreateResource() { | |
| 87 DCHECK(!bitmap_.isNull()); | |
| 88 CreateUpdaterIfNeeded(); | |
| 89 updater_->SetBitmap(bitmap_); | |
| 90 | |
| 91 if (!resource_) { | |
| 92 resource_ = updater_->CreateResource( | |
| 93 layer_tree_host()->contents_texture_manager()); | |
| 94 } | |
| 95 } | 111 } |
| 96 | 112 |
| 97 bool NinePatchLayer::DrawsContent() const { | 113 bool NinePatchLayer::DrawsContent() const { |
| 98 bool draws = !bitmap_.isNull() && | 114 return resource_ && resource_->id() && Layer::DrawsContent(); |
| 99 Layer::DrawsContent() && | |
| 100 bitmap_.width() && | |
| 101 bitmap_.height(); | |
| 102 return draws; | |
| 103 } | 115 } |
| 104 | 116 |
| 105 void NinePatchLayer::PushPropertiesTo(LayerImpl* layer) { | 117 void NinePatchLayer::PushPropertiesTo(LayerImpl* layer) { |
| 106 Layer::PushPropertiesTo(layer); | 118 Layer::PushPropertiesTo(layer); |
| 107 NinePatchLayerImpl* layer_impl = static_cast<NinePatchLayerImpl*>(layer); | 119 NinePatchLayerImpl* layer_impl = static_cast<NinePatchLayerImpl*>(layer); |
| 108 | 120 |
| 109 if (resource_) { | 121 if (!resource_) { |
| 110 DCHECK(!bitmap_.isNull()); | 122 layer_impl->SetUIResourceId(0); |
| 111 layer_impl->SetResourceId(resource_->texture()->resource_id()); | 123 } else { |
| 112 layer_impl->SetLayout( | 124 DCHECK(layer_tree_host()); |
| 113 gfx::Size(bitmap_.width(), bitmap_.height()), image_aperture_); | 125 |
| 126 gfx::Size image_size = | |
| 127 layer_tree_host()->GetUIResourceSize(resource_->id()); | |
| 128 layer_impl->SetUIResourceId(resource_->id()); | |
| 129 layer_impl->SetLayout(image_size, image_aperture_, border_, fill_center_); | |
| 114 } | 130 } |
| 115 | |
| 116 // NinePatchLayer must push properties every commit to make sure | |
| 117 // NinePatchLayerImpl::resource_id_ is valid. | |
| 118 // http://crbug.com/276482 | |
| 119 needs_push_properties_ = true; | |
| 120 } | 131 } |
| 121 | 132 |
| 122 } // namespace cc | 133 } // namespace cc |
| OLD | NEW |