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
<ui::CompositorVSyncManager
>& vsync_manager
,
34 scoped_ptr
<BrowserCompositorOverlayCandidateValidator
>
35 overlay_candidate_validator
)
36 : BrowserCompositorOutputSurface(context
,
38 overlay_candidate_validator
.Pass()),
40 is_backbuffer_discarded_(false),
41 weak_ptr_factory_(this) {
42 capabilities_
.max_frames_pending
= 1;
43 capabilities_
.uses_default_gl_framebuffer
= false;
46 OffscreenBrowserCompositorOutputSurface::
47 ~OffscreenBrowserCompositorOutputSurface() {
51 void OffscreenBrowserCompositorOutputSurface::EnsureBackbuffer() {
52 is_backbuffer_discarded_
= false;
54 if (!reflector_texture_
.get()) {
55 reflector_texture_
.reset(new ReflectorTexture(context_provider()));
57 GLES2Interface
* gl
= context_provider_
->ContextGL();
59 int max_texture_size
=
60 context_provider_
->ContextCapabilities().gpu
.max_texture_size
;
61 int texture_width
= std::min(max_texture_size
, surface_size_
.width());
62 int texture_height
= std::min(max_texture_size
, surface_size_
.height());
64 cc::ResourceFormat format
= cc::RGBA_8888
;
65 gl
->BindTexture(GL_TEXTURE_2D
, reflector_texture_
->texture_id());
66 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
67 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
68 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
69 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
70 gl
->TexImage2D(GL_TEXTURE_2D
, 0, GLInternalFormat(format
),
71 texture_width
, texture_height
, 0,
72 GLDataFormat(format
), GLDataType(format
), nullptr);
74 gl
->GenFramebuffers(1, &fbo_
);
76 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
77 gl
->FramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
78 GL_TEXTURE_2D
, reflector_texture_
->texture_id(),
80 reflector_
->OnSourceTextureMailboxUpdated(
81 reflector_texture_
->mailbox());
85 void OffscreenBrowserCompositorOutputSurface::DiscardBackbuffer() {
86 is_backbuffer_discarded_
= true;
88 GLES2Interface
* gl
= context_provider_
->ContextGL();
90 if (reflector_texture_
) {
91 reflector_texture_
.reset();
93 reflector_
->OnSourceTextureMailboxUpdated(nullptr);
97 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
98 gl
->DeleteFramebuffers(1, &fbo_
);
103 void OffscreenBrowserCompositorOutputSurface::Reshape(const gfx::Size
& size
,
104 float scale_factor
) {
105 if (size
== surface_size_
)
108 surface_size_
= size
;
109 device_scale_factor_
= scale_factor
;
114 void OffscreenBrowserCompositorOutputSurface::BindFramebuffer() {
115 bool need_to_bind
= !!reflector_texture_
.get();
117 DCHECK(reflector_texture_
.get());
120 GLES2Interface
* gl
= context_provider_
->ContextGL();
121 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
125 void OffscreenBrowserCompositorOutputSurface::SwapBuffers(
126 cc::CompositorFrame
* frame
) {
128 if (frame
->gl_frame_data
->sub_buffer_rect
==
129 gfx::Rect(frame
->gl_frame_data
->size
))
130 reflector_
->OnSourceSwapBuffers();
132 reflector_
->OnSourcePostSubBuffer(frame
->gl_frame_data
->sub_buffer_rect
);
135 client_
->DidSwapBuffers();
138 void OffscreenBrowserCompositorOutputSurface::OnReflectorChanged() {
144 OffscreenBrowserCompositorOutputSurface::CreateCompositionStartedCallback() {
145 return base::Bind(&OutputSurface::OnSwapBuffersComplete
,
146 weak_ptr_factory_
.GetWeakPtr());
149 #if defined(OS_MACOSX)
151 bool OffscreenBrowserCompositorOutputSurface::
152 SurfaceShouldNotShowFramesAfterSuspendForRecycle() const {
158 } // namespace content