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