Wrapper script for isolating telemetry_gpu_unittests.
[chromium-blink-merge.git] / cc / surfaces / surface.cc
blobbb3f2828bb4e8b60f86cfe61529aca7c22b1ecf7
1 // Copyright 2014 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.
5 #include "cc/surfaces/surface.h"
7 #include <algorithm>
9 #include "cc/output/compositor_frame.h"
10 #include "cc/output/copy_output_request.h"
11 #include "cc/surfaces/surface_factory.h"
12 #include "cc/surfaces/surface_id_allocator.h"
13 #include "cc/surfaces/surface_manager.h"
15 namespace cc {
17 // The frame index starts at 2 so that empty frames will be treated as
18 // completely damaged the first time they're drawn from.
19 static const int kFrameIndexStart = 2;
21 Surface::Surface(SurfaceId id, SurfaceFactory* factory)
22 : surface_id_(id),
23 factory_(factory->AsWeakPtr()),
24 frame_index_(kFrameIndexStart) {
27 Surface::~Surface() {
28 ClearCopyRequests();
29 if (current_frame_ && factory_) {
30 ReturnedResourceArray current_resources;
31 TransferableResource::ReturnResources(
32 current_frame_->delegated_frame_data->resource_list,
33 &current_resources);
34 factory_->UnrefResources(current_resources);
36 if (!draw_callback_.is_null())
37 draw_callback_.Run(SurfaceDrawStatus::DRAW_SKIPPED);
40 void Surface::QueueFrame(scoped_ptr<CompositorFrame> frame,
41 const DrawCallback& callback) {
42 DCHECK(factory_);
43 ClearCopyRequests();
45 if (frame) {
46 TakeLatencyInfo(&frame->metadata.latency_info);
49 scoped_ptr<CompositorFrame> previous_frame = current_frame_.Pass();
50 current_frame_ = frame.Pass();
52 if (current_frame_) {
53 factory_->ReceiveFromChild(
54 current_frame_->delegated_frame_data->resource_list);
57 // Empty frames shouldn't be drawn and shouldn't contribute damage, so don't
58 // increment frame index for them.
59 if (current_frame_ &&
60 !current_frame_->delegated_frame_data->render_pass_list.empty())
61 ++frame_index_;
63 if (previous_frame) {
64 ReturnedResourceArray previous_resources;
65 TransferableResource::ReturnResources(
66 previous_frame->delegated_frame_data->resource_list,
67 &previous_resources);
68 factory_->UnrefResources(previous_resources);
70 if (!draw_callback_.is_null())
71 draw_callback_.Run(SurfaceDrawStatus::DRAW_SKIPPED);
72 draw_callback_ = callback;
74 if (current_frame_) {
75 factory_->manager()->DidSatisfySequences(
76 SurfaceIdAllocator::NamespaceForId(surface_id_),
77 &current_frame_->metadata.satisfies_sequences);
81 void Surface::RequestCopyOfOutput(scoped_ptr<CopyOutputRequest> copy_request) {
82 if (current_frame_ &&
83 !current_frame_->delegated_frame_data->render_pass_list.empty())
84 current_frame_->delegated_frame_data->render_pass_list.back()
85 ->copy_requests.push_back(copy_request.Pass());
86 else
87 copy_request->SendEmptyResult();
90 void Surface::TakeCopyOutputRequests(
91 std::multimap<RenderPassId, CopyOutputRequest*>* copy_requests) {
92 DCHECK(copy_requests->empty());
93 if (current_frame_) {
94 for (const auto& render_pass :
95 current_frame_->delegated_frame_data->render_pass_list) {
96 while (!render_pass->copy_requests.empty()) {
97 scoped_ptr<CopyOutputRequest> request =
98 render_pass->copy_requests.take_back();
99 render_pass->copy_requests.pop_back();
100 copy_requests->insert(
101 std::make_pair(render_pass->id, request.release()));
107 const CompositorFrame* Surface::GetEligibleFrame() {
108 return current_frame_.get();
111 void Surface::TakeLatencyInfo(std::vector<ui::LatencyInfo>* latency_info) {
112 if (!current_frame_)
113 return;
114 if (latency_info->empty()) {
115 current_frame_->metadata.latency_info.swap(*latency_info);
116 return;
118 std::copy(current_frame_->metadata.latency_info.begin(),
119 current_frame_->metadata.latency_info.end(),
120 std::back_inserter(*latency_info));
121 current_frame_->metadata.latency_info.clear();
124 void Surface::RunDrawCallbacks(SurfaceDrawStatus drawn) {
125 if (!draw_callback_.is_null()) {
126 DrawCallback callback = draw_callback_;
127 draw_callback_ = DrawCallback();
128 callback.Run(drawn);
132 void Surface::AddDestructionDependency(SurfaceSequence sequence) {
133 destruction_dependencies_.push_back(sequence);
136 void Surface::SatisfyDestructionDependencies(
137 base::hash_set<SurfaceSequence>* sequences,
138 base::hash_set<uint32_t>* valid_id_namespaces) {
139 destruction_dependencies_.erase(
140 std::remove_if(destruction_dependencies_.begin(),
141 destruction_dependencies_.end(),
142 [sequences, valid_id_namespaces](SurfaceSequence seq) {
143 return (!!sequences->erase(seq) ||
144 !valid_id_namespaces->count(seq.id_namespace));
146 destruction_dependencies_.end());
149 void Surface::ClearCopyRequests() {
150 if (current_frame_) {
151 for (const auto& render_pass :
152 current_frame_->delegated_frame_data->render_pass_list) {
153 for (const auto& copy_request : render_pass->copy_requests)
154 copy_request->SendEmptyResult();
159 } // namespace cc