1 // Copyright 2013 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/layers/delegated_frame_provider.h" 6 7 #include "cc/layers/delegated_frame_resource_collection.h" 8 #include "cc/layers/delegated_renderer_layer.h" 9 #include "cc/output/delegated_frame_data.h" 10 #include "cc/quads/render_pass_draw_quad.h" 11 12 namespace cc { 13 14 DelegatedFrameProvider::DelegatedFrameProvider( 15 const scoped_refptr<DelegatedFrameResourceCollection>& resource_collection, 16 scoped_ptr<DelegatedFrameData> frame) 17 : resource_collection_(resource_collection) { 18 RenderPass* root_pass = frame->render_pass_list.back(); 19 frame_size_ = root_pass->output_rect.size(); 20 DCHECK(!frame_size_.IsEmpty()); 21 SetFrameData(frame.Pass()); 22 } 23 24 DelegatedFrameProvider::~DelegatedFrameProvider() { 25 ReturnedResourceArray returned; 26 TransferableResource::ReturnResources(frame_->resource_list, &returned); 27 resource_collection_->UnrefResources(returned); 28 } 29 30 void DelegatedFrameProvider::AddObserver(DelegatedRendererLayer* layer) { 31 if (DCHECK_IS_ON()) { 32 for (size_t i = 0; i < observers_.size(); ++i) 33 DCHECK(observers_[i].layer != layer); 34 } 35 36 observers_.push_back(Observer(layer, gfx::RectF(frame_size_))); 37 38 DCHECK(frame_) << "Must have a frame when given to a DelegatedRendererLayer."; 39 } 40 41 void DelegatedFrameProvider::RemoveObserver(DelegatedRendererLayer* layer) { 42 bool found_observer = false; 43 for (size_t i = 0; i < observers_.size(); ++i) { 44 if (observers_[i].layer != layer) 45 continue; 46 observers_.erase(observers_.begin() + i); 47 found_observer = true; 48 break; 49 } 50 DCHECK(found_observer); 51 } 52 53 void DelegatedFrameProvider::SetFrameData( 54 scoped_ptr<DelegatedFrameData> frame) { 55 DCHECK(frame); 56 DCHECK_NE(0u, frame->render_pass_list.size()); 57 58 if (frame_) { 59 ReturnedResourceArray returned; 60 TransferableResource::ReturnResources(frame_->resource_list, &returned); 61 resource_collection_->UnrefResources(returned); 62 } 63 64 frame_ = frame.Pass(); 65 66 resource_collection_->ReceivedResources(frame_->resource_list); 67 resource_collection_->RefResources(frame_->resource_list); 68 69 RenderPass* root_pass = frame_->render_pass_list.back(); 70 DCHECK_EQ(frame_size_.ToString(), root_pass->output_rect.size().ToString()) 71 << "All frames in a single DelegatedFrameProvider must have the same " 72 << "size. Use a new frame provider for frames of a different size."; 73 74 for (size_t i = 0; i < observers_.size(); ++i) { 75 observers_[i].damage = 76 gfx::UnionRects(observers_[i].damage, root_pass->damage_rect); 77 observers_[i].layer->ProviderHasNewFrame(); 78 } 79 } 80 81 DelegatedFrameData* DelegatedFrameProvider::GetFrameDataAndRefResources( 82 DelegatedRendererLayer* observer, 83 gfx::RectF* damage) { 84 85 bool found_observer = false; 86 for (size_t i = 0; i < observers_.size(); ++i) { 87 if (observers_[i].layer != observer) 88 continue; 89 *damage = observers_[i].damage; 90 // The observer is now responsible for the damage. 91 observers_[i].damage = gfx::RectF(); 92 found_observer = true; 93 } 94 DCHECK(found_observer); 95 96 resource_collection_->RefResources(frame_->resource_list); 97 return frame_.get(); 98 } 99 100 ReturnCallback 101 DelegatedFrameProvider::GetReturnResourcesCallbackForImplThread() { 102 return resource_collection_->GetReturnResourcesCallbackForImplThread(); 103 } 104 105 void DelegatedFrameProvider::UnrefResourcesOnMainThread( 106 const ReturnedResourceArray& returned) { 107 resource_collection_->UnrefResources(returned); 108 } 109 110 } // namespace cc 111