Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / cc / surfaces / surface.cc
blob600455f12618fb3d841d7bcf69984bfbf4470c90
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, const gfx::Size& size, SurfaceFactory* factory)
22 : surface_id_(id),
23 size_(size),
24 factory_(factory->AsWeakPtr()),
25 frame_index_(kFrameIndexStart) {
28 Surface::~Surface() {
29 ClearCopyRequests();
30 if (current_frame_ && factory_) {
31 ReturnedResourceArray current_resources;
32 TransferableResource::ReturnResources(
33 current_frame_->delegated_frame_data->resource_list,
34 &current_resources);
35 factory_->UnrefResources(current_resources);
39 void Surface::QueueFrame(scoped_ptr<CompositorFrame> frame,
40 const base::Closure& callback) {
41 DCHECK(factory_);
42 ClearCopyRequests();
43 TakeLatencyInfo(&frame->metadata.latency_info);
44 scoped_ptr<CompositorFrame> previous_frame = current_frame_.Pass();
45 current_frame_ = frame.Pass();
46 factory_->ReceiveFromChild(
47 current_frame_->delegated_frame_data->resource_list);
48 ++frame_index_;
50 if (previous_frame) {
51 ReturnedResourceArray previous_resources;
52 TransferableResource::ReturnResources(
53 previous_frame->delegated_frame_data->resource_list,
54 &previous_resources);
55 factory_->UnrefResources(previous_resources);
57 if (!draw_callback_.is_null())
58 draw_callback_.Run();
59 draw_callback_ = callback;
60 factory_->manager()->DidSatisfySequences(
61 SurfaceIdAllocator::NamespaceForId(surface_id_),
62 &current_frame_->metadata.satisfies_sequences);
65 void Surface::RequestCopyOfOutput(scoped_ptr<CopyOutputRequest> copy_request) {
66 if (current_frame_ &&
67 !current_frame_->delegated_frame_data->render_pass_list.empty())
68 current_frame_->delegated_frame_data->render_pass_list.back()
69 ->copy_requests.push_back(copy_request.Pass());
70 else
71 copy_request->SendEmptyResult();
74 void Surface::TakeCopyOutputRequests(
75 std::multimap<RenderPassId, CopyOutputRequest*>* copy_requests) {
76 DCHECK(copy_requests->empty());
77 if (current_frame_) {
78 for (const auto& render_pass :
79 current_frame_->delegated_frame_data->render_pass_list) {
80 while (!render_pass->copy_requests.empty()) {
81 scoped_ptr<CopyOutputRequest> request =
82 render_pass->copy_requests.take_back();
83 render_pass->copy_requests.pop_back();
84 copy_requests->insert(
85 std::make_pair(render_pass->id, request.release()));
91 const CompositorFrame* Surface::GetEligibleFrame() {
92 return current_frame_.get();
95 void Surface::TakeLatencyInfo(std::vector<ui::LatencyInfo>* latency_info) {
96 if (!current_frame_)
97 return;
98 if (latency_info->empty()) {
99 current_frame_->metadata.latency_info.swap(*latency_info);
100 return;
102 std::copy(current_frame_->metadata.latency_info.begin(),
103 current_frame_->metadata.latency_info.end(),
104 std::back_inserter(*latency_info));
105 current_frame_->metadata.latency_info.clear();
108 void Surface::RunDrawCallbacks() {
109 if (!draw_callback_.is_null()) {
110 base::Closure callback = draw_callback_;
111 draw_callback_ = base::Closure();
112 callback.Run();
116 void Surface::AddDestructionDependency(SurfaceSequence sequence) {
117 destruction_dependencies_.push_back(sequence);
120 void Surface::SatisfyDestructionDependencies(
121 base::hash_set<SurfaceSequence>* sequences) {
122 destruction_dependencies_.erase(
123 std::remove_if(
124 destruction_dependencies_.begin(), destruction_dependencies_.end(),
125 [sequences](SurfaceSequence seq) { return !!sequences->erase(seq); }),
126 destruction_dependencies_.end());
129 void Surface::ClearCopyRequests() {
130 if (current_frame_) {
131 for (const auto& render_pass :
132 current_frame_->delegated_frame_data->render_pass_list) {
133 for (const auto& copy_request : render_pass->copy_requests)
134 copy_request->SendEmptyResult();
139 } // namespace cc