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_display_output_surface.h"
7 #include "cc/output/compositor_frame.h"
8 #include "cc/output/compositor_frame_ack.h"
9 #include "cc/surfaces/display.h"
10 #include "cc/surfaces/onscreen_display_client.h"
11 #include "cc/surfaces/surface.h"
12 #include "cc/surfaces/surface_manager.h"
16 SurfaceDisplayOutputSurface::SurfaceDisplayOutputSurface(
17 SurfaceManager
* surface_manager
,
18 SurfaceIdAllocator
* allocator
,
19 const scoped_refptr
<ContextProvider
>& context_provider
)
20 : OutputSurface(context_provider
),
21 display_client_(NULL
),
22 surface_manager_(surface_manager
),
23 factory_(surface_manager
, this),
24 allocator_(allocator
) {
25 capabilities_
.delegated_rendering
= true;
26 capabilities_
.max_frames_pending
= 1;
27 capabilities_
.can_force_reclaim_resources
= true;
28 // Frame always needs to be swapped because forced resource reclaiming
29 // destroys the Display's copy.
30 capabilities_
.draw_and_swap_full_viewport_every_frame
= true;
33 SurfaceDisplayOutputSurface::~SurfaceDisplayOutputSurface() {
35 if (!surface_id_
.is_null()) {
36 factory_
.Destroy(surface_id_
);
40 void SurfaceDisplayOutputSurface::ReceivedVSyncParameters(
41 base::TimeTicks timebase
,
42 base::TimeDelta interval
) {
43 CommitVSyncParameters(timebase
, interval
);
46 void SurfaceDisplayOutputSurface::SwapBuffers(CompositorFrame
* frame
) {
47 gfx::Size frame_size
=
48 frame
->delegated_frame_data
->render_pass_list
.back()->output_rect
.size();
49 if (frame_size
!= display_size_
) {
50 if (!surface_id_
.is_null()) {
51 factory_
.Destroy(surface_id_
);
53 surface_id_
= allocator_
->GenerateId();
54 factory_
.Create(surface_id_
);
55 display_size_
= frame_size
;
57 display_client_
->display()->SetSurfaceId(surface_id_
,
58 frame
->metadata
.device_scale_factor
);
60 scoped_ptr
<CompositorFrame
> frame_copy(new CompositorFrame());
61 frame
->AssignTo(frame_copy
.get());
63 surface_id_
, frame_copy
.Pass(),
64 base::Bind(&SurfaceDisplayOutputSurface::SwapBuffersComplete
,
65 base::Unretained(this)));
67 client_
->DidSwapBuffers();
70 bool SurfaceDisplayOutputSurface::BindToClient(OutputSurfaceClient
* client
) {
72 DCHECK(display_client_
);
74 // Avoid initializing GL context here, as this should be sharing the
76 return display_client_
->Initialize();
79 void SurfaceDisplayOutputSurface::ForceReclaimResources() {
80 if (!surface_id_
.is_null()) {
81 scoped_ptr
<CompositorFrame
> empty_frame(new CompositorFrame());
82 empty_frame
->delegated_frame_data
.reset(new DelegatedFrameData
);
83 factory_
.SubmitFrame(surface_id_
, empty_frame
.Pass(),
84 SurfaceFactory::DrawCallback());
88 void SurfaceDisplayOutputSurface::ReturnResources(
89 const ReturnedResourceArray
& resources
) {
90 CompositorFrameAck ack
;
91 ack
.resources
= resources
;
93 client_
->ReclaimResources(&ack
);
96 void SurfaceDisplayOutputSurface::SwapBuffersComplete(SurfaceDrawStatus drawn
) {
97 if (client_
&& !display_client_
->output_surface_lost())
98 client_
->DidSwapBuffersComplete();