1 // Copyright 2015 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/offscreen_browser_compositor_output_surface.h"
7 #include "base/logging.h"
8 #include "cc/output/compositor_frame.h"
9 #include "cc/output/compositor_frame_ack.h"
10 #include "cc/output/gl_frame_data.h"
11 #include "cc/output/output_surface_client.h"
12 #include "cc/resources/resource_provider.h"
13 #include "content/browser/compositor/browser_compositor_overlay_candidate_validator.h"
14 #include "content/browser/compositor/reflector_impl.h"
15 #include "content/browser/compositor/reflector_texture.h"
16 #include "content/common/gpu/client/context_provider_command_buffer.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "gpu/command_buffer/client/context_support.h"
19 #include "gpu/command_buffer/client/gles2_interface.h"
20 #include "third_party/khronos/GLES2/gl2.h"
21 #include "third_party/khronos/GLES2/gl2ext.h"
23 using cc::CompositorFrame
;
24 using cc::GLFrameData
;
25 using cc::ResourceProvider
;
26 using gpu::gles2::GLES2Interface
;
30 OffscreenBrowserCompositorOutputSurface::
31 OffscreenBrowserCompositorOutputSurface(
32 const scoped_refptr
<ContextProviderCommandBuffer
>& context
,
33 const scoped_refptr
<ContextProviderCommandBuffer
>& worker_context
,
34 const scoped_refptr
<ui::CompositorVSyncManager
>& vsync_manager
,
35 scoped_ptr
<BrowserCompositorOverlayCandidateValidator
>
36 overlay_candidate_validator
)
37 : BrowserCompositorOutputSurface(context
,
40 overlay_candidate_validator
.Pass()),
42 is_backbuffer_discarded_(false),
43 weak_ptr_factory_(this) {
44 capabilities_
.max_frames_pending
= 1;
45 capabilities_
.uses_default_gl_framebuffer
= false;
48 OffscreenBrowserCompositorOutputSurface::
49 ~OffscreenBrowserCompositorOutputSurface() {
53 void OffscreenBrowserCompositorOutputSurface::EnsureBackbuffer() {
54 is_backbuffer_discarded_
= false;
56 if (!reflector_texture_
.get()) {
57 reflector_texture_
.reset(new ReflectorTexture(context_provider()));
59 GLES2Interface
* gl
= context_provider_
->ContextGL();
61 int max_texture_size
=
62 context_provider_
->ContextCapabilities().gpu
.max_texture_size
;
63 int texture_width
= std::min(max_texture_size
, surface_size_
.width());
64 int texture_height
= std::min(max_texture_size
, surface_size_
.height());
66 cc::ResourceFormat format
= cc::RGBA_8888
;
67 gl
->BindTexture(GL_TEXTURE_2D
, reflector_texture_
->texture_id());
68 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
69 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
70 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
71 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
72 gl
->TexImage2D(GL_TEXTURE_2D
, 0, GLInternalFormat(format
),
73 texture_width
, texture_height
, 0,
74 GLDataFormat(format
), GLDataType(format
), nullptr);
76 gl
->GenFramebuffers(1, &fbo_
);
78 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
79 gl
->FramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
80 GL_TEXTURE_2D
, reflector_texture_
->texture_id(),
82 reflector_
->OnSourceTextureMailboxUpdated(
83 reflector_texture_
->mailbox());
87 void OffscreenBrowserCompositorOutputSurface::DiscardBackbuffer() {
88 is_backbuffer_discarded_
= true;
90 GLES2Interface
* gl
= context_provider_
->ContextGL();
92 if (reflector_texture_
) {
93 reflector_texture_
.reset();
95 reflector_
->OnSourceTextureMailboxUpdated(nullptr);
99 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
100 gl
->DeleteFramebuffers(1, &fbo_
);
105 void OffscreenBrowserCompositorOutputSurface::Reshape(const gfx::Size
& size
,
106 float scale_factor
) {
107 if (size
== surface_size_
)
110 surface_size_
= size
;
111 device_scale_factor_
= scale_factor
;
116 void OffscreenBrowserCompositorOutputSurface::BindFramebuffer() {
117 bool need_to_bind
= !!reflector_texture_
.get();
119 DCHECK(reflector_texture_
.get());
122 GLES2Interface
* gl
= context_provider_
->ContextGL();
123 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
127 void OffscreenBrowserCompositorOutputSurface::SwapBuffers(
128 cc::CompositorFrame
* frame
) {
130 if (frame
->gl_frame_data
->sub_buffer_rect
==
131 gfx::Rect(frame
->gl_frame_data
->size
))
132 reflector_
->OnSourceSwapBuffers();
134 reflector_
->OnSourcePostSubBuffer(frame
->gl_frame_data
->sub_buffer_rect
);
137 client_
->DidSwapBuffers();
139 // TODO(oshima): sync with the reflector's SwapBuffersComplete
140 // (crbug.com/520567).
141 // The original implementation had a flickering issue (crbug.com/515332).
142 uint32_t sync_point
=
143 context_provider_
->ContextGL()->InsertSyncPointCHROMIUM();
144 context_provider_
->ContextSupport()->SignalSyncPoint(
145 sync_point
, base::Bind(&OutputSurface::OnSwapBuffersComplete
,
146 weak_ptr_factory_
.GetWeakPtr()));
149 void OffscreenBrowserCompositorOutputSurface::OnReflectorChanged() {
155 OffscreenBrowserCompositorOutputSurface::CreateCompositionStartedCallback() {
156 return base::Closure();
159 #if defined(OS_MACOSX)
161 bool OffscreenBrowserCompositorOutputSurface::
162 SurfaceShouldNotShowFramesAfterSuspendForRecycle() const {
168 } // namespace content