Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / compositor / offscreen_browser_compositor_output_surface.cc
blob9fa502684c74517388c4566f60e116647be7f92f
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/common/gpu/client/context_provider_command_buffer.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "gpu/command_buffer/client/context_support.h"
18 #include "gpu/command_buffer/client/gles2_interface.h"
19 #include "third_party/khronos/GLES2/gl2.h"
20 #include "third_party/khronos/GLES2/gl2ext.h"
22 using cc::CompositorFrame;
23 using cc::GLFrameData;
24 using cc::ResourceProvider;
25 using gpu::gles2::GLES2Interface;
27 namespace content {
29 OffscreenBrowserCompositorOutputSurface::
30 OffscreenBrowserCompositorOutputSurface(
31 const scoped_refptr<ContextProviderCommandBuffer>& context,
32 const scoped_refptr<ui::CompositorVSyncManager>& vsync_manager,
33 scoped_ptr<BrowserCompositorOverlayCandidateValidator>
34 overlay_candidate_validator)
35 : BrowserCompositorOutputSurface(context,
36 vsync_manager,
37 overlay_candidate_validator.Pass()),
38 fbo_(0),
39 is_backbuffer_discarded_(false),
40 backing_texture_id_(0),
41 weak_ptr_factory_(this) {
42 capabilities_.max_frames_pending = 1;
43 capabilities_.uses_default_gl_framebuffer = false;
46 OffscreenBrowserCompositorOutputSurface::
47 ~OffscreenBrowserCompositorOutputSurface() {
48 DiscardBackbuffer();
51 void OffscreenBrowserCompositorOutputSurface::EnsureBackbuffer() {
52 is_backbuffer_discarded_ = false;
54 if (!backing_texture_id_) {
55 GLES2Interface* gl = context_provider_->ContextGL();
56 cc::ResourceFormat format = cc::RGBA_8888;
58 gl->GenTextures(1, &backing_texture_id_);
59 gl->BindTexture(GL_TEXTURE_2D, backing_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);
70 void OffscreenBrowserCompositorOutputSurface::DiscardBackbuffer() {
71 is_backbuffer_discarded_ = true;
73 GLES2Interface* gl = context_provider_->ContextGL();
75 if (backing_texture_id_) {
76 gl->DeleteTextures(1, &backing_texture_id_);
77 backing_texture_id_ = 0;
80 if (fbo_) {
81 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_);
82 gl->DeleteFramebuffers(1, &fbo_);
83 fbo_ = 0;
87 void OffscreenBrowserCompositorOutputSurface::Reshape(const gfx::Size& size,
88 float scale_factor) {
89 if (size == surface_size_)
90 return;
92 surface_size_ = size;
93 device_scale_factor_ = scale_factor;
94 DiscardBackbuffer();
95 EnsureBackbuffer();
98 void OffscreenBrowserCompositorOutputSurface::BindFramebuffer() {
99 EnsureBackbuffer();
100 DCHECK(backing_texture_id_);
102 GLES2Interface* gl = context_provider_->ContextGL();
104 if (!fbo_)
105 gl->GenFramebuffers(1, &fbo_);
106 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_);
107 gl->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
108 backing_texture_id_, 0);
111 void OffscreenBrowserCompositorOutputSurface::SwapBuffers(
112 cc::CompositorFrame* frame) {
113 // TODO(oshima): Pass the texture to the reflector so that the
114 // reflector can simply use and draw it on their surface instead of
115 // copying the texture.
116 if (reflector_) {
117 if (frame->gl_frame_data->sub_buffer_rect ==
118 gfx::Rect(frame->gl_frame_data->size))
119 reflector_->OnSourceSwapBuffers();
120 else
121 reflector_->OnSourcePostSubBuffer(frame->gl_frame_data->sub_buffer_rect);
124 client_->DidSwapBuffers();
126 // TODO(oshima): sync with the reflector's SwapBuffersComplete.
127 uint32_t sync_point =
128 context_provider_->ContextGL()->InsertSyncPointCHROMIUM();
129 context_provider_->ContextSupport()->SignalSyncPoint(
130 sync_point, base::Bind(&OutputSurface::OnSwapBuffersComplete,
131 weak_ptr_factory_.GetWeakPtr()));
134 #if defined(OS_MACOSX)
136 bool OffscreenBrowserCompositorOutputSurface::
137 SurfaceShouldNotShowFramesAfterSuspendForRecycle() const {
138 return true;
141 #endif
143 } // namespace content