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/buffer_queue.h"
7 #include "content/browser/compositor/image_transport_factory.h"
8 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h"
9 #include "content/common/gpu/client/context_provider_command_buffer.h"
10 #include "content/common/gpu/client/gl_helper.h"
11 #include "gpu/GLES2/gl2extchromium.h"
12 #include "gpu/command_buffer/client/gles2_interface.h"
13 #include "gpu/command_buffer/service/image_factory.h"
14 #include "third_party/skia/include/core/SkRect.h"
15 #include "third_party/skia/include/core/SkRegion.h"
16 #include "ui/gfx/gpu_memory_buffer.h"
20 BufferQueue::BufferQueue(
21 scoped_refptr
<cc::ContextProvider
> context_provider
,
22 unsigned int texture_target
,
23 unsigned int internalformat
,
25 BrowserGpuMemoryBufferManager
* gpu_memory_buffer_manager
,
27 : context_provider_(context_provider
),
30 texture_target_(texture_target
),
31 internal_format_(internalformat
),
32 gl_helper_(gl_helper
),
33 gpu_memory_buffer_manager_(gpu_memory_buffer_manager
),
34 surface_id_(surface_id
) {}
36 BufferQueue::~BufferQueue() {
39 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
41 gl
->DeleteFramebuffers(1, &fbo_
);
44 void BufferQueue::Initialize() {
45 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
46 gl
->GenFramebuffers(1, &fbo_
);
49 void BufferQueue::BindFramebuffer() {
50 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
51 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
53 if (!current_surface_
.texture
) {
54 current_surface_
= GetNextSurface();
55 gl
->FramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
56 texture_target_
, current_surface_
.texture
, 0);
60 void BufferQueue::CopyBufferDamage(int texture
,
62 const gfx::Rect
& new_damage
,
63 const gfx::Rect
& old_damage
) {
64 gl_helper_
->CopySubBufferDamage(
65 texture_target_
, texture
, source_texture
,
66 SkRegion(SkIRect::MakeXYWH(new_damage
.x(), new_damage
.y(),
67 new_damage
.width(), new_damage
.height())),
68 SkRegion(SkIRect::MakeXYWH(old_damage
.x(), old_damage
.y(),
69 old_damage
.width(), old_damage
.height())));
72 void BufferQueue::UpdateBufferDamage(const gfx::Rect
& damage
) {
73 displayed_surface_
.damage
.Union(damage
);
74 for (size_t i
= 0; i
< available_surfaces_
.size(); i
++)
75 available_surfaces_
[i
].damage
.Union(damage
);
77 for (std::deque
<AllocatedSurface
>::iterator it
=
78 in_flight_surfaces_
.begin();
79 it
!= in_flight_surfaces_
.end();
81 it
->damage
.Union(damage
);
84 void BufferQueue::SwapBuffers(const gfx::Rect
& damage
) {
85 if (damage
!= gfx::Rect(size_
)) {
86 // We must have a frame available to copy from.
87 DCHECK(!in_flight_surfaces_
.empty() || displayed_surface_
.texture
);
88 unsigned int texture_id
= !in_flight_surfaces_
.empty()
89 ? in_flight_surfaces_
.back().texture
90 : displayed_surface_
.texture
;
92 CopyBufferDamage(current_surface_
.texture
, texture_id
, damage
,
93 current_surface_
.damage
);
95 UpdateBufferDamage(damage
);
96 current_surface_
.damage
= gfx::Rect();
97 in_flight_surfaces_
.push_back(current_surface_
);
98 current_surface_
.texture
= 0;
99 current_surface_
.image
= 0;
100 // Some things reset the framebuffer (CopySubBufferDamage, some GLRenderer
101 // paths), so ensure we restore it here.
102 context_provider_
->ContextGL()->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
105 void BufferQueue::Reshape(const gfx::Size
& size
, float scale_factor
) {
108 DCHECK(!current_surface_
.texture
);
111 // TODO: add stencil buffer when needed.
112 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
113 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
114 gl
->FramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
115 texture_target_
, 0, 0);
120 void BufferQueue::RecreateBuffers() {
121 // We need to recreate the buffers, for whatever reason the old ones are not
122 // presentable on the device anymore.
123 // Unused buffers can be freed directly, they will be re-allocated as needed.
124 // Any in flight, current or displayed surface must be replaced.
125 for (size_t i
= 0; i
< available_surfaces_
.size(); i
++)
126 FreeSurface(&available_surfaces_
[i
]);
127 available_surfaces_
.clear();
129 for (auto& surface
: in_flight_surfaces_
)
130 surface
= RecreateBuffer(&surface
);
132 current_surface_
= RecreateBuffer(¤t_surface_
);
133 displayed_surface_
= RecreateBuffer(&displayed_surface_
);
135 if (current_surface_
.texture
) {
136 // If we have a texture bound, we will need to re-bind it.
137 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
138 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
139 gl
->FramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
140 texture_target_
, current_surface_
.texture
, 0);
144 BufferQueue::AllocatedSurface
BufferQueue::RecreateBuffer(
145 AllocatedSurface
* surface
) {
146 if (!surface
->texture
)
148 AllocatedSurface new_surface
= GetNextSurface();
149 new_surface
.damage
= surface
->damage
;
151 // Copy the entire texture.
152 CopyBufferDamage(new_surface
.texture
, surface
->texture
, gfx::Rect(),
155 FreeSurface(surface
);
159 void BufferQueue::PageFlipComplete() {
160 DCHECK(!in_flight_surfaces_
.empty());
162 if (displayed_surface_
.texture
)
163 available_surfaces_
.push_back(displayed_surface_
);
165 displayed_surface_
= in_flight_surfaces_
.front();
166 in_flight_surfaces_
.pop_front();
169 void BufferQueue::FreeAllSurfaces() {
170 FreeSurface(&displayed_surface_
);
171 FreeSurface(¤t_surface_
);
172 // This is intentionally not emptied since the swap buffers acks are still
173 // expected to arrive.
174 for (auto& surface
: in_flight_surfaces_
)
175 FreeSurface(&surface
);
177 for (size_t i
= 0; i
< available_surfaces_
.size(); i
++)
178 FreeSurface(&available_surfaces_
[i
]);
180 available_surfaces_
.clear();
183 void BufferQueue::FreeSurface(AllocatedSurface
* surface
) {
184 if (surface
->texture
) {
185 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
186 gl
->BindTexture(texture_target_
, surface
->texture
);
187 gl
->ReleaseTexImage2DCHROMIUM(texture_target_
, surface
->image
);
188 gl
->DeleteTextures(1, &surface
->texture
);
189 gl
->DestroyImageCHROMIUM(surface
->image
);
191 surface
->texture
= 0;
196 BufferQueue::AllocatedSurface
BufferQueue::GetNextSurface() {
197 if (!available_surfaces_
.empty()) {
198 AllocatedSurface surface
= available_surfaces_
.back();
199 available_surfaces_
.pop_back();
203 unsigned int texture
= 0;
204 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
205 gl
->GenTextures(1, &texture
);
207 return AllocatedSurface();
209 // We don't want to allow anything more than triple buffering.
210 DCHECK_LT(allocated_count_
, 4U);
212 scoped_ptr
<gfx::GpuMemoryBuffer
> buffer(
213 gpu_memory_buffer_manager_
->AllocateGpuMemoryBufferForScanout(
214 size_
, gpu::ImageFactory::ImageFormatToGpuMemoryBufferFormat(
218 gl
->DeleteTextures(1, &texture
);
219 DLOG(ERROR
) << "Failed to allocate GPU memory buffer";
220 return AllocatedSurface();
223 unsigned int id
= gl
->CreateImageCHROMIUM(
224 buffer
->AsClientBuffer(), size_
.width(), size_
.height(),
228 LOG(ERROR
) << "Failed to allocate backing image surface";
229 gl
->DeleteTextures(1, &texture
);
230 return AllocatedSurface();
233 gl
->BindTexture(texture_target_
, texture
);
234 gl
->BindTexImage2DCHROMIUM(texture_target_
, id
);
235 return AllocatedSurface(texture
, id
, gfx::Rect(size_
));
238 } // namespace content