| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/compositor/buffer_queue.h" | 5 #include "content/browser/compositor/buffer_queue.h" |
| 6 | 6 |
| 7 #include "content/browser/compositor/image_transport_factory.h" |
| 7 #include "content/common/gpu/client/context_provider_command_buffer.h" | 8 #include "content/common/gpu/client/context_provider_command_buffer.h" |
| 9 #include "content/common/gpu/client/gl_helper.h" |
| 8 #include "gpu/GLES2/gl2extchromium.h" | 10 #include "gpu/GLES2/gl2extchromium.h" |
| 9 #include "gpu/command_buffer/client/gles2_interface.h" | 11 #include "gpu/command_buffer/client/gles2_interface.h" |
| 12 #include "third_party/skia/include/core/SkRect.h" |
| 13 #include "third_party/skia/include/core/SkRegion.h" |
| 10 | 14 |
| 11 namespace content { | 15 namespace content { |
| 12 | 16 |
| 13 BufferQueue::BufferQueue(scoped_refptr<cc::ContextProvider> context_provider, | 17 BufferQueue::BufferQueue(scoped_refptr<cc::ContextProvider> context_provider, |
| 14 unsigned int internalformat) | 18 unsigned int internalformat) |
| 15 : context_provider_(context_provider), | 19 : context_provider_(context_provider), |
| 16 fbo_(0), | 20 fbo_(0), |
| 17 allocated_count_(0), | 21 allocated_count_(0), |
| 18 internalformat_(internalformat) { | 22 internalformat_(internalformat) { |
| 19 } | 23 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 39 if (!current_surface_.texture) { | 43 if (!current_surface_.texture) { |
| 40 current_surface_ = GetNextSurface(); | 44 current_surface_ = GetNextSurface(); |
| 41 gl->FramebufferTexture2D(GL_FRAMEBUFFER, | 45 gl->FramebufferTexture2D(GL_FRAMEBUFFER, |
| 42 GL_COLOR_ATTACHMENT0, | 46 GL_COLOR_ATTACHMENT0, |
| 43 GL_TEXTURE_2D, | 47 GL_TEXTURE_2D, |
| 44 current_surface_.texture, | 48 current_surface_.texture, |
| 45 0); | 49 0); |
| 46 } | 50 } |
| 47 } | 51 } |
| 48 | 52 |
| 49 void BufferQueue::SwapBuffers() { | 53 void BufferQueue::CopyBufferDamage(int texture, |
| 50 in_flight_surfaces_.push(current_surface_); | 54 int source_texture, |
| 55 const gfx::Rect& new_damage, |
| 56 const gfx::Rect& old_damage) { |
| 57 ImageTransportFactory::GetInstance()->GetGLHelper()->CopySubBufferDamage( |
| 58 texture, |
| 59 source_texture, |
| 60 SkRegion(SkIRect::MakeXYWH(new_damage.x(), |
| 61 new_damage.y(), |
| 62 new_damage.width(), |
| 63 new_damage.height())), |
| 64 SkRegion(SkIRect::MakeXYWH(old_damage.x(), |
| 65 old_damage.y(), |
| 66 old_damage.width(), |
| 67 old_damage.height()))); |
| 68 } |
| 69 |
| 70 void BufferQueue::SwapBuffers(const gfx::Rect& damage) { |
| 71 if (damage != gfx::Rect(size_)) { |
| 72 // We must have a frame available to copy from. |
| 73 DCHECK(!in_flight_surfaces_.empty()); |
| 74 |
| 75 if (!damage.Contains(current_surface_.damage)) { |
| 76 CopyBufferDamage(current_surface_.texture, |
| 77 in_flight_surfaces_.back().texture, |
| 78 damage, |
| 79 current_surface_.damage); |
| 80 // Because we copied everything except |damage| to this texture, |
| 81 // the cumulative damage for this frame is only |damage| now. |
| 82 in_flight_surfaces_.back().damage = damage; |
| 83 } |
| 84 |
| 85 // Compute cumulative damage for all frames. |
| 86 for (size_t i = 0; i < available_surfaces_.size(); i++) |
| 87 available_surfaces_[i].damage.Union(damage); |
| 88 for (std::deque<AllocatedSurface>::iterator it = |
| 89 in_flight_surfaces_.begin(); |
| 90 it != in_flight_surfaces_.end(); |
| 91 ++it) |
| 92 it->damage.Union(damage); |
| 93 } else { |
| 94 for (size_t i = 0; i < available_surfaces_.size(); i++) |
| 95 available_surfaces_[i].damage = damage; |
| 96 for (std::deque<AllocatedSurface>::iterator it = |
| 97 in_flight_surfaces_.begin(); |
| 98 it != in_flight_surfaces_.end(); |
| 99 ++it) |
| 100 it->damage = damage; |
| 101 } |
| 102 current_surface_.damage = gfx::Rect(); |
| 103 in_flight_surfaces_.push_back(current_surface_); |
| 51 current_surface_.texture = 0; | 104 current_surface_.texture = 0; |
| 52 current_surface_.image = 0; | 105 current_surface_.image = 0; |
| 53 } | 106 } |
| 54 | 107 |
| 55 void BufferQueue::Reshape(const gfx::Size& size, float scale_factor) { | 108 void BufferQueue::Reshape(const gfx::Size& size, float scale_factor) { |
| 56 DCHECK(!current_surface_.texture); | 109 DCHECK(!current_surface_.texture); |
| 57 if (size == size_) | 110 if (size == size_) |
| 58 return; | 111 return; |
| 59 size_ = size; | 112 size_ = size; |
| 60 | 113 |
| 61 // TODO: add stencil buffer when needed. | 114 // TODO: add stencil buffer when needed. |
| 62 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | 115 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); |
| 63 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_); | 116 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_); |
| 64 gl->FramebufferTexture2D( | 117 gl->FramebufferTexture2D( |
| 65 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); | 118 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); |
| 66 | 119 |
| 67 FreeAllSurfaces(); | 120 FreeAllSurfaces(); |
| 68 } | 121 } |
| 69 | 122 |
| 70 void BufferQueue::PageFlipComplete() { | 123 void BufferQueue::PageFlipComplete() { |
| 71 if (in_flight_surfaces_.size() > 1) { | 124 if (in_flight_surfaces_.size() > 1) { |
| 72 available_surfaces_.push_back(in_flight_surfaces_.front()); | 125 available_surfaces_.push_back(in_flight_surfaces_.front()); |
| 73 in_flight_surfaces_.pop(); | 126 in_flight_surfaces_.pop_front(); |
| 74 } | 127 } |
| 75 } | 128 } |
| 76 | 129 |
| 77 void BufferQueue::FreeAllSurfaces() { | 130 void BufferQueue::FreeAllSurfaces() { |
| 78 FreeSurface(¤t_surface_); | 131 FreeSurface(¤t_surface_); |
| 79 while (!in_flight_surfaces_.empty()) { | 132 while (!in_flight_surfaces_.empty()) { |
| 80 FreeSurface(&in_flight_surfaces_.front()); | 133 FreeSurface(&in_flight_surfaces_.front()); |
| 81 in_flight_surfaces_.pop(); | 134 in_flight_surfaces_.pop_front(); |
| 82 } | 135 } |
| 83 for (size_t i = 0; i < available_surfaces_.size(); i++) | 136 for (size_t i = 0; i < available_surfaces_.size(); i++) |
| 84 FreeSurface(&available_surfaces_[i]); | 137 FreeSurface(&available_surfaces_[i]); |
| 85 available_surfaces_.clear(); | 138 available_surfaces_.clear(); |
| 86 } | 139 } |
| 87 | 140 |
| 88 void BufferQueue::FreeSurface(AllocatedSurface* surface) { | 141 void BufferQueue::FreeSurface(AllocatedSurface* surface) { |
| 89 if (surface->texture) { | 142 if (surface->texture) { |
| 90 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | 143 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); |
| 91 gl->BindTexture(GL_TEXTURE_2D, surface->texture); | 144 gl->BindTexture(GL_TEXTURE_2D, surface->texture); |
| 92 gl->ReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, surface->image); | 145 gl->ReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, surface->image); |
| 93 gl->DeleteTextures(1, &surface->texture); | 146 gl->DeleteTextures(1, &surface->texture); |
| 94 gl->DestroyImageCHROMIUM(surface->image); | 147 gl->DestroyImageCHROMIUM(surface->image); |
| 95 surface->image = 0; | 148 surface->image = 0; |
| 96 surface->texture = 0; | 149 surface->texture = 0; |
| 97 allocated_count_--; | 150 allocated_count_--; |
| 98 } | 151 } |
| 99 } | 152 } |
| 100 | 153 |
| 101 BufferQueue::AllocatedSurface BufferQueue::GetNextSurface() { | 154 BufferQueue::AllocatedSurface BufferQueue::GetNextSurface() { |
| 102 if (!available_surfaces_.empty()) { | 155 if (!available_surfaces_.empty()) { |
| 103 AllocatedSurface id = available_surfaces_.back(); | 156 AllocatedSurface surface = available_surfaces_.back(); |
| 104 available_surfaces_.pop_back(); | 157 available_surfaces_.pop_back(); |
| 105 return id; | 158 return surface; |
| 106 } | 159 } |
| 107 | 160 |
| 108 unsigned int texture = 0; | 161 unsigned int texture = 0; |
| 109 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | 162 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); |
| 110 gl->GenTextures(1, &texture); | 163 gl->GenTextures(1, &texture); |
| 111 if (!texture) | 164 if (!texture) |
| 112 return AllocatedSurface(); | 165 return AllocatedSurface(); |
| 113 | 166 |
| 114 // We don't want to allow anything more than triple buffering. | 167 // We don't want to allow anything more than triple buffering. |
| 115 DCHECK_LT(allocated_count_, 4U); | 168 DCHECK_LT(allocated_count_, 4U); |
| 116 | 169 |
| 117 unsigned int id = context_provider_->ContextGL()->CreateImageCHROMIUM( | 170 unsigned int id = context_provider_->ContextGL()->CreateImageCHROMIUM( |
| 118 size_.width(), | 171 size_.width(), |
| 119 size_.height(), | 172 size_.height(), |
| 120 internalformat_, | 173 internalformat_, |
| 121 GL_IMAGE_SCANOUT_CHROMIUM); | 174 GL_IMAGE_SCANOUT_CHROMIUM); |
| 122 allocated_count_++; | 175 allocated_count_++; |
| 123 gl->BindTexture(GL_TEXTURE_2D, texture); | 176 gl->BindTexture(GL_TEXTURE_2D, texture); |
| 124 gl->BindTexImage2DCHROMIUM(GL_TEXTURE_2D, id); | 177 gl->BindTexImage2DCHROMIUM(GL_TEXTURE_2D, id); |
| 125 return AllocatedSurface(texture, id); | 178 return AllocatedSurface(texture, id, gfx::Rect(size_)); |
| 126 } | 179 } |
| 127 | 180 |
| 128 } // namespace content | 181 } // namespace content |
| OLD | NEW |