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 3 // found in the LICENSE file. 4 5 #include "cc/test/tiled_layer_test_common.h" 6 7 namespace cc { 8 9 FakeLayerUpdater::Resource::Resource(FakeLayerUpdater* layer, 10 scoped_ptr<PrioritizedResource> texture) 11 : LayerUpdater::Resource(texture.Pass()), layer_(layer) { 12 bitmap_.allocN32Pixels(10, 10); 13 } 14 15 FakeLayerUpdater::Resource::~Resource() {} 16 17 void FakeLayerUpdater::Resource::Update(ResourceUpdateQueue* queue, 18 const gfx::Rect& source_rect, 19 const gfx::Vector2d& dest_offset, 20 bool partial_update) { 21 const gfx::Rect kRect(0, 0, 10, 10); 22 ResourceUpdate upload = ResourceUpdate::Create( 23 texture(), &bitmap_, kRect, kRect, gfx::Vector2d()); 24 if (partial_update) 25 queue->AppendPartialUpload(upload); 26 else 27 queue->AppendFullUpload(upload); 28 29 layer_->Update(); 30 } 31 32 FakeLayerUpdater::FakeLayerUpdater() 33 : prepare_count_(0), update_count_(0), last_contents_width_scale_(0.f) { 34 } 35 36 FakeLayerUpdater::~FakeLayerUpdater() {} 37 38 void FakeLayerUpdater::PrepareToUpdate(const gfx::Size& content_size, 39 const gfx::Rect& paint_rect, 40 const gfx::Size& tile_size, 41 float contents_width_scale, 42 float contents_height_scale) { 43 prepare_count_++; 44 last_update_rect_ = paint_rect; 45 last_contents_width_scale_ = contents_width_scale; 46 if (!rect_to_invalidate_.IsEmpty()) { 47 layer_->InvalidateContentRect(rect_to_invalidate_); 48 rect_to_invalidate_ = gfx::Rect(); 49 layer_ = NULL; 50 } 51 } 52 53 void FakeLayerUpdater::SetRectToInvalidate(const gfx::Rect& rect, 54 FakeTiledLayer* layer) { 55 rect_to_invalidate_ = rect; 56 layer_ = layer; 57 } 58 59 scoped_ptr<LayerUpdater::Resource> FakeLayerUpdater::CreateResource( 60 PrioritizedResourceManager* manager) { 61 return scoped_ptr<LayerUpdater::Resource>( 62 new Resource(this, PrioritizedResource::Create(manager))); 63 } 64 65 FakeTiledLayerImpl::FakeTiledLayerImpl(LayerTreeImpl* tree_impl, int id) 66 : TiledLayerImpl(tree_impl, id) {} 67 68 FakeTiledLayerImpl::~FakeTiledLayerImpl() {} 69 70 FakeTiledLayer::FakeTiledLayer(PrioritizedResourceManager* resource_manager) 71 : TiledLayer(), 72 fake_updater_(make_scoped_refptr(new FakeLayerUpdater)), 73 resource_manager_(resource_manager) { 74 SetTileSize(tile_size()); 75 SetTextureFormat(RGBA_8888); 76 SetBorderTexelOption(LayerTilingData::NO_BORDER_TEXELS); 77 // So that we don't get false positives if any of these 78 // tests expect to return false from DrawsContent() for other reasons. 79 SetIsDrawable(true); 80 } 81 82 FakeTiledLayerWithScaledBounds::FakeTiledLayerWithScaledBounds( 83 PrioritizedResourceManager* resource_manager) 84 : FakeTiledLayer(resource_manager) {} 85 86 FakeTiledLayerWithScaledBounds::~FakeTiledLayerWithScaledBounds() {} 87 88 FakeTiledLayer::~FakeTiledLayer() {} 89 90 void FakeTiledLayer::SetNeedsDisplayRect(const gfx::RectF& rect) { 91 last_needs_display_rect_ = rect; 92 TiledLayer::SetNeedsDisplayRect(rect); 93 } 94 95 void FakeTiledLayer::SetTexturePriorities( 96 const PriorityCalculator& calculator) { 97 // Ensure there is always a target render surface available. If none has been 98 // set (the layer is an orphan for the test), then just set a surface on 99 // itself. 100 bool missing_target_render_surface = !render_target(); 101 102 if (missing_target_render_surface) 103 CreateRenderSurface(); 104 105 TiledLayer::SetTexturePriorities(calculator); 106 107 if (missing_target_render_surface) { 108 ClearRenderSurface(); 109 draw_properties().render_target = 0; 110 } 111 } 112 113 PrioritizedResourceManager* FakeTiledLayer::ResourceManager() { 114 return resource_manager_; 115 } 116 117 void FakeTiledLayer::UpdateContentsScale(float ideal_contents_scale) { 118 CalculateContentsScale(ideal_contents_scale, 119 &draw_properties().contents_scale_x, 120 &draw_properties().contents_scale_y, 121 &draw_properties().content_bounds); 122 } 123 124 void FakeTiledLayer::ResetNumDependentsNeedPushProperties() { 125 size_t num = 0; 126 if (mask_layer()) { 127 if (mask_layer()->needs_push_properties() || 128 mask_layer()->descendant_needs_push_properties()) 129 ++num; 130 } 131 if (replica_layer()) { 132 if (replica_layer()->needs_push_properties() || 133 replica_layer()->descendant_needs_push_properties()) 134 ++num; 135 } 136 for (size_t i = 0; i < children().size(); ++i) { 137 if (children()[i]->needs_push_properties() || 138 children()[i]->descendant_needs_push_properties()) 139 ++num; 140 } 141 num_dependents_need_push_properties_ = num; 142 } 143 144 LayerUpdater* FakeTiledLayer::Updater() const { 145 return fake_updater_.get(); 146 } 147 148 void FakeTiledLayerWithScaledBounds::SetContentBounds( 149 const gfx::Size& content_bounds) { 150 forced_content_bounds_ = content_bounds; 151 draw_properties().content_bounds = forced_content_bounds_; 152 } 153 154 void FakeTiledLayerWithScaledBounds::CalculateContentsScale( 155 float ideal_contents_scale, 156 float* contents_scale_x, 157 float* contents_scale_y, 158 gfx::Size* content_bounds) { 159 *contents_scale_x = 160 static_cast<float>(forced_content_bounds_.width()) / bounds().width(); 161 *contents_scale_y = 162 static_cast<float>(forced_content_bounds_.height()) / bounds().height(); 163 *content_bounds = forced_content_bounds_; 164 } 165 166 } // namespace cc 167