Change next_proto member type.
[chromium-blink-merge.git] / content / browser / compositor / buffer_queue.cc
blobc58e6bc16ad251e0e2936fe8be00fda66eef68ee
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"
18 namespace content {
20 BufferQueue::BufferQueue(
21 scoped_refptr<cc::ContextProvider> context_provider,
22 unsigned int internalformat,
23 GLHelper* gl_helper,
24 BrowserGpuMemoryBufferManager* gpu_memory_buffer_manager,
25 int surface_id)
26 : context_provider_(context_provider),
27 fbo_(0),
28 allocated_count_(0),
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() {
36 FreeAllSurfaces();
38 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL();
39 if (fbo_)
40 gl->DeleteFramebuffers(1, &fbo_);
43 bool BufferQueue::Initialize() {
44 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL();
45 gl->GenFramebuffers(1, &fbo_);
46 return fbo_ != 0;
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,
56 GL_COLOR_ATTACHMENT0,
57 GL_TEXTURE_2D,
58 current_surface_.texture,
59 0);
63 void BufferQueue::CopyBufferDamage(int texture,
64 int source_texture,
65 const gfx::Rect& new_damage,
66 const gfx::Rect& old_damage) {
67 gl_helper_->CopySubBufferDamage(
68 texture,
69 source_texture,
70 SkRegion(SkIRect::MakeXYWH(new_damage.x(),
71 new_damage.y(),
72 new_damage.width(),
73 new_damage.height())),
74 SkRegion(SkIRect::MakeXYWH(old_damage.x(),
75 old_damage.y(),
76 old_damage.width(),
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();
86 ++it)
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,
96 damage,
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);
108 if (size == size_)
109 return;
110 size_ = size;
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);
118 FreeAllSurfaces();
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(&current_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);
146 surface->image = 0;
147 surface->texture = 0;
148 allocated_count_--;
152 BufferQueue::AllocatedSurface BufferQueue::GetNextSurface() {
153 if (!available_surfaces_.empty()) {
154 AllocatedSurface surface = available_surfaces_.back();
155 available_surfaces_.pop_back();
156 return surface;
159 unsigned int texture = 0;
160 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL();
161 gl->GenTextures(1, &texture);
162 if (!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(
171 internalformat_),
172 surface_id_));
174 unsigned int id = gl->CreateImageCHROMIUM(
175 buffer->AsClientBuffer(), size_.width(), size_.height(), internalformat_);
177 if (!id) {
178 LOG(ERROR) << "Failed to allocate backing image surface";
179 gl->DeleteTextures(1, &texture);
180 return AllocatedSurface();
182 allocated_count_++;
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