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 internalformat
,
24 BrowserGpuMemoryBufferManager
* gpu_memory_buffer_manager
,
26 : context_provider_(context_provider
),
29 internalformat_(internalformat
),
30 gl_helper_(gl_helper
),
31 gpu_memory_buffer_manager_(gpu_memory_buffer_manager
),
32 surface_id_(surface_id
) {
35 BufferQueue::~BufferQueue() {
38 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
40 gl
->DeleteFramebuffers(1, &fbo_
);
43 bool BufferQueue::Initialize() {
44 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
45 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
,
58 current_surface_
.texture
,
63 void BufferQueue::CopyBufferDamage(int texture
,
65 const gfx::Rect
& new_damage
,
66 const gfx::Rect
& old_damage
) {
67 gl_helper_
->CopySubBufferDamage(
70 SkRegion(SkIRect::MakeXYWH(new_damage
.x(),
73 new_damage
.height())),
74 SkRegion(SkIRect::MakeXYWH(old_damage
.x(),
77 old_damage
.height())));
80 void BufferQueue::UpdateBufferDamage(const gfx::Rect
& damage
) {
81 for (size_t i
= 0; i
< available_surfaces_
.size(); i
++)
82 available_surfaces_
[i
].damage
.Union(damage
);
83 for (std::deque
<AllocatedSurface
>::iterator it
=
84 in_flight_surfaces_
.begin();
85 it
!= in_flight_surfaces_
.end();
87 it
->damage
.Union(damage
);
90 void BufferQueue::SwapBuffers(const gfx::Rect
& damage
) {
91 if (damage
!= gfx::Rect(size_
)) {
92 // We must have a frame available to copy from.
93 DCHECK(!in_flight_surfaces_
.empty());
94 CopyBufferDamage(current_surface_
.texture
,
95 in_flight_surfaces_
.back().texture
,
97 current_surface_
.damage
);
99 UpdateBufferDamage(damage
);
100 current_surface_
.damage
= gfx::Rect();
101 in_flight_surfaces_
.push_back(current_surface_
);
102 current_surface_
.texture
= 0;
103 current_surface_
.image
= 0;
106 void BufferQueue::Reshape(const gfx::Size
& size
, float scale_factor
) {
107 DCHECK(!current_surface_
.texture
);
112 // TODO: add stencil buffer when needed.
113 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
114 gl
->BindFramebuffer(GL_FRAMEBUFFER
, fbo_
);
115 gl
->FramebufferTexture2D(
116 GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, 0, 0);
121 void BufferQueue::PageFlipComplete() {
122 if (in_flight_surfaces_
.size() > 1) {
123 available_surfaces_
.push_back(in_flight_surfaces_
.front());
124 in_flight_surfaces_
.pop_front();
128 void BufferQueue::FreeAllSurfaces() {
129 FreeSurface(¤t_surface_
);
130 while (!in_flight_surfaces_
.empty()) {
131 FreeSurface(&in_flight_surfaces_
.front());
132 in_flight_surfaces_
.pop_front();
134 for (size_t i
= 0; i
< available_surfaces_
.size(); i
++)
135 FreeSurface(&available_surfaces_
[i
]);
136 available_surfaces_
.clear();
139 void BufferQueue::FreeSurface(AllocatedSurface
* surface
) {
140 if (surface
->texture
) {
141 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
142 gl
->BindTexture(GL_TEXTURE_2D
, surface
->texture
);
143 gl
->ReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D
, surface
->image
);
144 gl
->DeleteTextures(1, &surface
->texture
);
145 gl
->DestroyImageCHROMIUM(surface
->image
);
147 surface
->texture
= 0;
152 BufferQueue::AllocatedSurface
BufferQueue::GetNextSurface() {
153 if (!available_surfaces_
.empty()) {
154 AllocatedSurface surface
= available_surfaces_
.back();
155 available_surfaces_
.pop_back();
159 unsigned int texture
= 0;
160 gpu::gles2::GLES2Interface
* gl
= context_provider_
->ContextGL();
161 gl
->GenTextures(1, &texture
);
163 return AllocatedSurface();
165 // We don't want to allow anything more than triple buffering.
166 DCHECK_LT(allocated_count_
, 4U);
168 scoped_ptr
<gfx::GpuMemoryBuffer
> buffer(
169 gpu_memory_buffer_manager_
->AllocateGpuMemoryBufferForScanout(
170 size_
, gpu::ImageFactory::ImageFormatToGpuMemoryBufferFormat(
174 unsigned int id
= gl
->CreateImageCHROMIUM(
175 buffer
->AsClientBuffer(), size_
.width(), size_
.height(), internalformat_
);
178 LOG(ERROR
) << "Failed to allocate backing image surface";
179 gl
->DeleteTextures(1, &texture
);
180 return AllocatedSurface();
183 gl
->BindTexture(GL_TEXTURE_2D
, texture
);
184 gl
->BindTexImage2DCHROMIUM(GL_TEXTURE_2D
, id
);
185 return AllocatedSurface(texture
, id
, gfx::Rect(size_
));
188 } // namespace content