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();
58 cc::ResourceFormat format
= cc::RGBA_8888
;
59 gl
->BindTexture(GL_TEXTURE_2D
, reflector_texture_
->texture_id());
60 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
61 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
62 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
63 gl
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
64 gl
->TexImage2D(GL_TEXTURE_2D
, 0, GLInternalFormat(format
),
65 surface_size_
.width(), surface_size_
.height(), 0,
66 GLDataFormat(format
), GLDataType(format
), nullptr);
68 gl
->GenFramebuffers(1, &fbo_
);
70 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
71 gl
->FramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
72 GL_TEXTURE_2D
, reflector_texture_
->texture_id(),
74 reflector_
->OnSourceTextureMailboxUpdated(
75 reflector_texture_
->mailbox());
79 void OffscreenBrowserCompositorOutputSurface::DiscardBackbuffer() {
80 is_backbuffer_discarded_
= true;
82 GLES2Interface
* gl
= context_provider_
->ContextGL();
84 if (reflector_texture_
) {
85 reflector_texture_
.reset();
87 reflector_
->OnSourceTextureMailboxUpdated(nullptr);
91 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
92 gl
->DeleteFramebuffers(1, &fbo_
);
97 void OffscreenBrowserCompositorOutputSurface::Reshape(const gfx::Size
& size
,
99 if (size
== surface_size_
)
102 surface_size_
= size
;
103 device_scale_factor_
= scale_factor
;
108 void OffscreenBrowserCompositorOutputSurface::BindFramebuffer() {
109 bool need_to_bind
= !!reflector_texture_
.get();
111 DCHECK(reflector_texture_
.get());
114 GLES2Interface
* gl
= context_provider_
->ContextGL();
115 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
119 void OffscreenBrowserCompositorOutputSurface::SwapBuffers(
120 cc::CompositorFrame
* frame
) {
122 if (frame
->gl_frame_data
->sub_buffer_rect
==
123 gfx::Rect(frame
->gl_frame_data
->size
))
124 reflector_
->OnSourceSwapBuffers();
126 reflector_
->OnSourcePostSubBuffer(frame
->gl_frame_data
->sub_buffer_rect
);
129 client_
->DidSwapBuffers();
132 void OffscreenBrowserCompositorOutputSurface::OnReflectorChanged() {
138 OffscreenBrowserCompositorOutputSurface::CreateCompositionStartedCallback() {
139 return base::Bind(&OutputSurface::OnSwapBuffersComplete
,
140 weak_ptr_factory_
.GetWeakPtr());
143 #if defined(OS_MACOSX)
145 bool OffscreenBrowserCompositorOutputSurface::
146 SurfaceShouldNotShowFramesAfterSuspendForRecycle() const {
152 } // namespace content