Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / compositor / gpu_surfaceless_browser_compositor_output_surface.cc
blobb70436b17c7c007b3c5151a2d68ebc43e0c24eae
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 "content/browser/compositor/gpu_surfaceless_browser_compositor_output_surface.h"
7 #include "cc/output/compositor_frame.h"
8 #include "cc/output/output_surface_client.h"
9 #include "content/browser/compositor/browser_compositor_overlay_candidate_validator.h"
10 #include "content/browser/compositor/buffer_queue.h"
11 #include "content/browser/compositor/reflector_impl.h"
12 #include "content/browser/gpu/gpu_surface_tracker.h"
13 #include "content/common/gpu/client/context_provider_command_buffer.h"
14 #include "content/common/gpu/client/gl_helper.h"
15 #include "gpu/GLES2/gl2extchromium.h"
16 #include "gpu/command_buffer/client/gles2_interface.h"
18 namespace content {
20 GpuSurfacelessBrowserCompositorOutputSurface::
21 GpuSurfacelessBrowserCompositorOutputSurface(
22 const scoped_refptr<ContextProviderCommandBuffer>& context,
23 const scoped_refptr<ContextProviderCommandBuffer>& worker_context,
24 int surface_id,
25 const scoped_refptr<ui::CompositorVSyncManager>& vsync_manager,
26 scoped_ptr<BrowserCompositorOverlayCandidateValidator>
27 overlay_candidate_validator,
28 unsigned int target,
29 unsigned int internalformat,
30 BrowserGpuMemoryBufferManager* gpu_memory_buffer_manager)
31 : GpuBrowserCompositorOutputSurface(context,
32 worker_context,
33 vsync_manager,
34 overlay_candidate_validator.Pass()),
35 internalformat_(internalformat),
36 gpu_memory_buffer_manager_(gpu_memory_buffer_manager) {
37 capabilities_.uses_default_gl_framebuffer = false;
38 capabilities_.flipped_output_surface = true;
39 // Set |max_frames_pending| to 2 for surfaceless, which aligns scheduling
40 // more closely with the previous surfaced behavior.
41 // With a surface, swap buffer ack used to return early, before actually
42 // presenting the back buffer, enabling the browser compositor to run ahead.
43 // Surfaceless implementation acks at the time of actual buffer swap, which
44 // shifts the start of the new frame forward relative to the old
45 // implementation.
46 capabilities_.max_frames_pending = 2;
48 gl_helper_.reset(new GLHelper(context_provider_->ContextGL(),
49 context_provider_->ContextSupport()));
50 output_surface_.reset(new BufferQueue(
51 context_provider_, target, internalformat_, gl_helper_.get(),
52 gpu_memory_buffer_manager_, surface_id));
53 output_surface_->Initialize();
56 GpuSurfacelessBrowserCompositorOutputSurface::
57 ~GpuSurfacelessBrowserCompositorOutputSurface() {
60 unsigned GpuSurfacelessBrowserCompositorOutputSurface::GetOverlayTextureId()
61 const {
62 return output_surface_->current_texture_id();
65 void GpuSurfacelessBrowserCompositorOutputSurface::SwapBuffers(
66 cc::CompositorFrame* frame) {
67 DCHECK(output_surface_);
69 GLuint texture = output_surface_->current_texture_id();
70 output_surface_->SwapBuffers(frame->gl_frame_data->sub_buffer_rect);
71 const gfx::Size& size = frame->gl_frame_data->size;
72 context_provider_->ContextGL()->ScheduleOverlayPlaneCHROMIUM(
74 GL_OVERLAY_TRANSFORM_NONE_CHROMIUM,
75 texture,
78 size.width(),
79 size.height(),
82 1.0f,
83 1.0f);
84 GpuBrowserCompositorOutputSurface::SwapBuffers(frame);
87 void GpuSurfacelessBrowserCompositorOutputSurface::OnSwapBuffersComplete() {
88 DCHECK(output_surface_);
89 output_surface_->PageFlipComplete();
90 GpuBrowserCompositorOutputSurface::OnSwapBuffersComplete();
93 void GpuSurfacelessBrowserCompositorOutputSurface::BindFramebuffer() {
94 DCHECK(output_surface_);
95 output_surface_->BindFramebuffer();
98 void GpuSurfacelessBrowserCompositorOutputSurface::Reshape(
99 const gfx::Size& size,
100 float scale_factor) {
101 GpuBrowserCompositorOutputSurface::Reshape(size, scale_factor);
102 DCHECK(output_surface_);
103 output_surface_->Reshape(SurfaceSize(), scale_factor);
106 void GpuSurfacelessBrowserCompositorOutputSurface::OnSwapBuffersCompleted(
107 const std::vector<ui::LatencyInfo>& latency_info,
108 gfx::SwapResult result) {
109 #if defined(OS_MACOSX)
110 NOTREACHED();
111 #else
112 bool force_swap = false;
113 if (result == gfx::SwapResult::SWAP_NAK_RECREATE_BUFFERS) {
114 // Even through the swap failed, this is a fixable error so we can pretend
115 // it succeeded to the rest of the system.
116 result = gfx::SwapResult::SWAP_ACK;
117 output_surface_->RecreateBuffers();
118 force_swap = true;
120 GpuBrowserCompositorOutputSurface::OnSwapBuffersCompleted(latency_info,
121 result);
122 if (force_swap)
123 client_->SetNeedsRedrawRect(gfx::Rect(SurfaceSize()));
124 #endif
127 #if defined(OS_MACOSX)
128 void GpuSurfacelessBrowserCompositorOutputSurface::OnSurfaceDisplayed() {
129 OnSwapBuffersComplete();
131 #endif
133 } // namespace content