blob: 73dd6e3581b67d74fd306e87b77818b4749a9465 [file] [log] [blame]
[email protected]b95a2ee2014-06-01 12:25:391// 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.
4
jbaumanf400ce532015-02-03 04:54:595#include "cc/surfaces/surface_display_output_surface.h"
[email protected]b95a2ee2014-06-01 12:25:396
jamesr9b8fda32015-03-16 19:11:057#include "base/bind.h"
[email protected]b95a2ee2014-06-01 12:25:398#include "cc/output/compositor_frame.h"
[email protected]387b59d2014-06-27 01:17:349#include "cc/output/compositor_frame_ack.h"
[email protected]b95a2ee2014-06-01 12:25:3910#include "cc/surfaces/display.h"
11#include "cc/surfaces/surface.h"
danakj324faea2016-06-11 01:39:3112#include "cc/surfaces/surface_id_allocator.h"
[email protected]b95a2ee2014-06-01 12:25:3913#include "cc/surfaces/surface_manager.h"
14
jbaumanf400ce532015-02-03 04:54:5915namespace cc {
[email protected]b95a2ee2014-06-01 12:25:3916
17SurfaceDisplayOutputSurface::SurfaceDisplayOutputSurface(
jbaumanf400ce532015-02-03 04:54:5918 SurfaceManager* surface_manager,
danakj324faea2016-06-11 01:39:3119 SurfaceIdAllocator* surface_id_allocator,
20 Display* display,
vmpstr4065174a2016-03-16 20:44:0221 scoped_refptr<ContextProvider> context_provider,
22 scoped_refptr<ContextProvider> worker_context_provider)
23 : OutputSurface(std::move(context_provider),
hajimehoshiea85b9dc2016-05-25 09:52:3124 std::move(worker_context_provider),
25 nullptr),
danakj324faea2016-06-11 01:39:3126 surface_manager_(surface_manager),
27 surface_id_allocator_(surface_id_allocator),
28 display_(display),
29 factory_(surface_manager, this) {
30 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]b95a2ee2014-06-01 12:25:3931 capabilities_.delegated_rendering = true;
jbaumane32b9232015-03-20 01:25:5432 capabilities_.adjust_deadline_for_parent = true;
jbauman9bfb1a52015-01-09 08:16:0333 capabilities_.can_force_reclaim_resources = true;
danakj324faea2016-06-11 01:39:3134
jbauman65182a32015-04-11 00:32:1235 // Display and SurfaceDisplayOutputSurface share a GL context, so sync
36 // points aren't needed when passing resources between them.
37 capabilities_.delegated_sync_points_required = false;
danakj324faea2016-06-11 01:39:3138 factory_.set_needs_sync_points(false);
[email protected]b95a2ee2014-06-01 12:25:3939}
40
dyenb52ecd962016-04-20 23:23:4441SurfaceDisplayOutputSurface::SurfaceDisplayOutputSurface(
42 SurfaceManager* surface_manager,
danakj324faea2016-06-11 01:39:3143 SurfaceIdAllocator* surface_id_allocator,
44 Display* display,
dyenb52ecd962016-04-20 23:23:4445 scoped_refptr<VulkanContextProvider> vulkan_context_provider)
hajimehoshiea85b9dc2016-05-25 09:52:3146 : OutputSurface(std::move(vulkan_context_provider)),
danakj324faea2016-06-11 01:39:3147 surface_manager_(surface_manager),
48 surface_id_allocator_(surface_id_allocator),
49 display_(display),
50 factory_(surface_manager, this) {
51 DCHECK(thread_checker_.CalledOnValidThread());
dyenb52ecd962016-04-20 23:23:4452 capabilities_.delegated_rendering = true;
53 capabilities_.adjust_deadline_for_parent = true;
54 capabilities_.can_force_reclaim_resources = true;
55}
56
[email protected]b95a2ee2014-06-01 12:25:3957SurfaceDisplayOutputSurface::~SurfaceDisplayOutputSurface() {
danakj324faea2016-06-11 01:39:3158 DCHECK(thread_checker_.CalledOnValidThread());
enne4e3c9d52016-03-09 00:25:1259 if (HasClient())
60 DetachFromClient();
[email protected]b95a2ee2014-06-01 12:25:3961}
62
jbaumanf400ce532015-02-03 04:54:5963void SurfaceDisplayOutputSurface::SwapBuffers(CompositorFrame* frame) {
[email protected]b95a2ee2014-06-01 12:25:3964 gfx::Size frame_size =
65 frame->delegated_frame_data->render_pass_list.back()->output_rect.size();
danakj324faea2016-06-11 01:39:3166 if (frame_size.IsEmpty() || frame_size != last_swap_frame_size_) {
67 if (!delegated_surface_id_.is_null()) {
68 factory_.Destroy(delegated_surface_id_);
[email protected]cde792132014-07-02 06:52:4669 }
danakj324faea2016-06-11 01:39:3170 delegated_surface_id_ = surface_id_allocator_->GenerateId();
71 factory_.Create(delegated_surface_id_);
72 last_swap_frame_size_ = frame_size;
[email protected]cde792132014-07-02 06:52:4673 }
danakj324faea2016-06-11 01:39:3174 display_->SetSurfaceId(delegated_surface_id_,
75 frame->metadata.device_scale_factor);
[email protected]b95a2ee2014-06-01 12:25:3976
briandersonf54b2472015-06-11 00:12:3577 client_->DidSwapBuffers();
78
danakj60bc3bc2016-04-09 00:24:4879 std::unique_ptr<CompositorFrame> frame_copy(new CompositorFrame());
[email protected]b95a2ee2014-06-01 12:25:3980 frame->AssignTo(frame_copy.get());
fsamuelcb87fbf2015-08-24 16:41:0181 factory_.SubmitCompositorFrame(
danakj324faea2016-06-11 01:39:3182 delegated_surface_id_, std::move(frame_copy),
jbauman878e9532014-08-23 22:10:2383 base::Bind(&SurfaceDisplayOutputSurface::SwapBuffersComplete,
84 base::Unretained(this)));
[email protected]b95a2ee2014-06-01 12:25:3985}
86
jbaumanf400ce532015-02-03 04:54:5987bool SurfaceDisplayOutputSurface::BindToClient(OutputSurfaceClient* client) {
danakj324faea2016-06-11 01:39:3188 DCHECK(thread_checker_.CalledOnValidThread());
89 if (!OutputSurface::BindToClient(client))
90 return false;
enne4e3c9d52016-03-09 00:25:1291
danakj324faea2016-06-11 01:39:3192 // We want the Display's output surface to hear about lost context, and since
93 // this shares a context with it, we should not be listening for lost context
94 // callbacks on the context here.
95 if (context_provider())
96 context_provider()->SetLostContextCallback(base::Closure());
97
98 surface_manager_->RegisterSurfaceFactoryClient(
99 surface_id_allocator_->id_namespace(), this);
jbaumana8c5cc92014-10-03 20:51:23100 // Avoid initializing GL context here, as this should be sharing the
101 // Display's context.
danakj324faea2016-06-11 01:39:31102 bool init = display_->Initialize(this);
103 // Since this class shares its GL context with the Display, Initialize should
104 // not be possible to fail.
105 DCHECK(init);
106 return true;
jbaumana8c5cc92014-10-03 20:51:23107}
108
jbauman9bfb1a52015-01-09 08:16:03109void SurfaceDisplayOutputSurface::ForceReclaimResources() {
danakj324faea2016-06-11 01:39:31110 if (!delegated_surface_id_.is_null()) {
111 factory_.SubmitCompositorFrame(delegated_surface_id_, nullptr,
fsamuelcb87fbf2015-08-24 16:41:01112 SurfaceFactory::DrawCallback());
danakj324faea2016-06-11 01:39:31113 }
jbauman9bfb1a52015-01-09 08:16:03114}
115
enne4e3c9d52016-03-09 00:25:12116void SurfaceDisplayOutputSurface::DetachFromClient() {
117 DCHECK(HasClient());
118 // Unregister the SurfaceFactoryClient here instead of the dtor so that only
119 // one client is alive for this namespace at any given time.
danakj324faea2016-06-11 01:39:31120 surface_manager_->UnregisterSurfaceFactoryClient(
121 surface_id_allocator_->id_namespace());
122 if (!delegated_surface_id_.is_null())
123 factory_.Destroy(delegated_surface_id_);
124
enne4e3c9d52016-03-09 00:25:12125 OutputSurface::DetachFromClient();
enne4e3c9d52016-03-09 00:25:12126}
127
[email protected]387b59d2014-06-27 01:17:34128void SurfaceDisplayOutputSurface::ReturnResources(
jbaumanf400ce532015-02-03 04:54:59129 const ReturnedResourceArray& resources) {
130 CompositorFrameAck ack;
[email protected]387b59d2014-06-27 01:17:34131 ack.resources = resources;
[email protected]72728b12014-07-18 03:39:17132 if (client_)
133 client_->ReclaimResources(&ack);
[email protected]387b59d2014-06-27 01:17:34134}
135
brianderson877996b02015-10-20 20:35:31136void SurfaceDisplayOutputSurface::SetBeginFrameSource(
brianderson877996b02015-10-20 20:35:31137 BeginFrameSource* begin_frame_source) {
enne19c108582016-04-14 03:35:32138 DCHECK(client_);
139 client_->SetBeginFrameSource(begin_frame_source);
brianderson877996b02015-10-20 20:35:31140}
141
danakj324faea2016-06-11 01:39:31142void SurfaceDisplayOutputSurface::DisplayOutputSurfaceLost() {
143 output_surface_lost_ = true;
144 DidLoseOutputSurface();
145}
146
147void SurfaceDisplayOutputSurface::DisplaySetMemoryPolicy(
148 const ManagedMemoryPolicy& policy) {
149 SetMemoryPolicy(policy);
150}
151
jbaumanf400ce532015-02-03 04:54:59152void SurfaceDisplayOutputSurface::SwapBuffersComplete(SurfaceDrawStatus drawn) {
danakj324faea2016-06-11 01:39:31153 // TODO(danakj): Why the lost check?
154 if (!output_surface_lost_)
jbauman6a2b78c72014-12-11 03:21:28155 client_->DidSwapBuffersComplete();
jbauman878e9532014-08-23 22:10:23156}
157
jbaumanf400ce532015-02-03 04:54:59158} // namespace cc