Change DtmfSenderHandler to handle events on the signaling thread.
[chromium-blink-merge.git] / cc / surfaces / surface.cc
blob32e61c5ed778a1b49da97aaa18fb5bf955b9625b
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_manager.h"
14 namespace cc {
16 // The frame index starts at 2 so that empty frames will be treated as
17 // completely damaged the first time they're drawn from.
18 static const int kFrameIndexStart = 2;
20 Surface::Surface(SurfaceId id, const gfx::Size& size, SurfaceFactory* factory)
21 : surface_id_(id),
22 size_(size),
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);
38 void Surface::QueueFrame(scoped_ptr<CompositorFrame> frame,
39 const base::Closure& callback) {
40 DCHECK(factory_);
41 ClearCopyRequests();
42 TakeLatencyInfo(&frame->metadata.latency_info);
43 scoped_ptr<CompositorFrame> previous_frame = current_frame_.Pass();
44 current_frame_ = frame.Pass();
45 factory_->ReceiveFromChild(
46 current_frame_->delegated_frame_data->resource_list);
47 ++frame_index_;
49 if (previous_frame) {
50 ReturnedResourceArray previous_resources;
51 TransferableResource::ReturnResources(
52 previous_frame->delegated_frame_data->resource_list,
53 &previous_resources);
54 factory_->UnrefResources(previous_resources);
56 if (!draw_callback_.is_null())
57 draw_callback_.Run();
58 draw_callback_ = callback;
59 factory_->manager()->DidSatisfySequences(
60 surface_id_, &current_frame_->metadata.satisfies_sequences);
63 void Surface::RequestCopyOfOutput(scoped_ptr<CopyOutputRequest> copy_request) {
64 if (current_frame_ &&
65 !current_frame_->delegated_frame_data->render_pass_list.empty())
66 current_frame_->delegated_frame_data->render_pass_list.back()
67 ->copy_requests.push_back(copy_request.Pass());
68 else
69 copy_request->SendEmptyResult();
72 void Surface::TakeCopyOutputRequests(
73 std::multimap<RenderPassId, CopyOutputRequest*>* copy_requests) {
74 DCHECK(copy_requests->empty());
75 if (current_frame_) {
76 for (const auto& render_pass :
77 current_frame_->delegated_frame_data->render_pass_list) {
78 while (!render_pass->copy_requests.empty()) {
79 scoped_ptr<CopyOutputRequest> request =
80 render_pass->copy_requests.take_back();
81 render_pass->copy_requests.pop_back();
82 copy_requests->insert(
83 std::make_pair(render_pass->id, request.release()));
89 const CompositorFrame* Surface::GetEligibleFrame() {
90 return current_frame_.get();
93 void Surface::TakeLatencyInfo(std::vector<ui::LatencyInfo>* latency_info) {
94 if (!current_frame_)
95 return;
96 if (latency_info->empty()) {
97 current_frame_->metadata.latency_info.swap(*latency_info);
98 return;
100 std::copy(current_frame_->metadata.latency_info.begin(),
101 current_frame_->metadata.latency_info.end(),
102 std::back_inserter(*latency_info));
103 current_frame_->metadata.latency_info.clear();
106 void Surface::RunDrawCallbacks() {
107 if (!draw_callback_.is_null()) {
108 base::Closure callback = draw_callback_;
109 draw_callback_ = base::Closure();
110 callback.Run();
114 void Surface::ClearCopyRequests() {
115 if (current_frame_) {
116 for (const auto& render_pass :
117 current_frame_->delegated_frame_data->render_pass_list) {
118 for (const auto& copy_request : render_pass->copy_requests)
119 copy_request->SendEmptyResult();
124 } // namespace cc