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::UpdateBufferDamage(const gfx::Rect& damage) { |
| 71 for (size_t i = 0; i < available_surfaces_.size(); i++) |
| 72 available_surfaces_[i].damage.Union(damage); |
| 73 for (std::deque<AllocatedSurface>::iterator it = |
| 74 in_flight_surfaces_.begin(); |
| 75 it != in_flight_surfaces_.end(); |
| 76 ++it) |
| 77 it->damage.Union(damage); |
| 78 } |
| 79 |
| 80 void BufferQueue::SwapBuffers(const gfx::Rect& damage) { |
| 81 if (damage != gfx::Rect(size_)) { |
| 82 // We must have a frame available to copy from. |
| 83 DCHECK(!in_flight_surfaces_.empty()); |
| 84 CopyBufferDamage(current_surface_.texture, |
| 85 in_flight_surfaces_.back().texture, |
| 86 damage, |
| 87 current_surface_.damage); |
| 88 } |
| 89 UpdateBufferDamage(damage); |
| 90 current_surface_.damage = gfx::Rect(); |
| 91 in_flight_surfaces_.push_back(current_surface_); |
51 current_surface_.texture = 0; | 92 current_surface_.texture = 0; |
52 current_surface_.image = 0; | 93 current_surface_.image = 0; |
53 } | 94 } |
54 | 95 |
55 void BufferQueue::Reshape(const gfx::Size& size, float scale_factor) { | 96 void BufferQueue::Reshape(const gfx::Size& size, float scale_factor) { |
56 DCHECK(!current_surface_.texture); | 97 DCHECK(!current_surface_.texture); |
57 if (size == size_) | 98 if (size == size_) |
58 return; | 99 return; |
59 size_ = size; | 100 size_ = size; |
60 | 101 |
61 // TODO: add stencil buffer when needed. | 102 // TODO: add stencil buffer when needed. |
62 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | 103 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); |
63 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_); | 104 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo_); |
64 gl->FramebufferTexture2D( | 105 gl->FramebufferTexture2D( |
65 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); | 106 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); |
66 | 107 |
67 FreeAllSurfaces(); | 108 FreeAllSurfaces(); |
68 } | 109 } |
69 | 110 |
70 void BufferQueue::PageFlipComplete() { | 111 void BufferQueue::PageFlipComplete() { |
71 if (in_flight_surfaces_.size() > 1) { | 112 if (in_flight_surfaces_.size() > 1) { |
72 available_surfaces_.push_back(in_flight_surfaces_.front()); | 113 available_surfaces_.push_back(in_flight_surfaces_.front()); |
73 in_flight_surfaces_.pop(); | 114 in_flight_surfaces_.pop_front(); |
74 } | 115 } |
75 } | 116 } |
76 | 117 |
77 void BufferQueue::FreeAllSurfaces() { | 118 void BufferQueue::FreeAllSurfaces() { |
78 FreeSurface(¤t_surface_); | 119 FreeSurface(¤t_surface_); |
79 while (!in_flight_surfaces_.empty()) { | 120 while (!in_flight_surfaces_.empty()) { |
80 FreeSurface(&in_flight_surfaces_.front()); | 121 FreeSurface(&in_flight_surfaces_.front()); |
81 in_flight_surfaces_.pop(); | 122 in_flight_surfaces_.pop_front(); |
82 } | 123 } |
83 for (size_t i = 0; i < available_surfaces_.size(); i++) | 124 for (size_t i = 0; i < available_surfaces_.size(); i++) |
84 FreeSurface(&available_surfaces_[i]); | 125 FreeSurface(&available_surfaces_[i]); |
85 available_surfaces_.clear(); | 126 available_surfaces_.clear(); |
86 } | 127 } |
87 | 128 |
88 void BufferQueue::FreeSurface(AllocatedSurface* surface) { | 129 void BufferQueue::FreeSurface(AllocatedSurface* surface) { |
89 if (surface->texture) { | 130 if (surface->texture) { |
90 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | 131 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); |
91 gl->BindTexture(GL_TEXTURE_2D, surface->texture); | 132 gl->BindTexture(GL_TEXTURE_2D, surface->texture); |
92 gl->ReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, surface->image); | 133 gl->ReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, surface->image); |
93 gl->DeleteTextures(1, &surface->texture); | 134 gl->DeleteTextures(1, &surface->texture); |
94 gl->DestroyImageCHROMIUM(surface->image); | 135 gl->DestroyImageCHROMIUM(surface->image); |
95 surface->image = 0; | 136 surface->image = 0; |
96 surface->texture = 0; | 137 surface->texture = 0; |
97 allocated_count_--; | 138 allocated_count_--; |
98 } | 139 } |
99 } | 140 } |
100 | 141 |
101 BufferQueue::AllocatedSurface BufferQueue::GetNextSurface() { | 142 BufferQueue::AllocatedSurface BufferQueue::GetNextSurface() { |
102 if (!available_surfaces_.empty()) { | 143 if (!available_surfaces_.empty()) { |
103 AllocatedSurface id = available_surfaces_.back(); | 144 AllocatedSurface surface = available_surfaces_.back(); |
104 available_surfaces_.pop_back(); | 145 available_surfaces_.pop_back(); |
105 return id; | 146 return surface; |
106 } | 147 } |
107 | 148 |
108 unsigned int texture = 0; | 149 unsigned int texture = 0; |
109 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | 150 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); |
110 gl->GenTextures(1, &texture); | 151 gl->GenTextures(1, &texture); |
111 if (!texture) | 152 if (!texture) |
112 return AllocatedSurface(); | 153 return AllocatedSurface(); |
113 | 154 |
114 // We don't want to allow anything more than triple buffering. | 155 // We don't want to allow anything more than triple buffering. |
115 DCHECK_LT(allocated_count_, 4U); | 156 DCHECK_LT(allocated_count_, 4U); |
116 | 157 |
117 unsigned int id = context_provider_->ContextGL()->CreateImageCHROMIUM( | 158 unsigned int id = gl->CreateImageCHROMIUM( |
118 size_.width(), | 159 size_.width(), |
119 size_.height(), | 160 size_.height(), |
120 internalformat_, | 161 internalformat_, |
121 GL_IMAGE_SCANOUT_CHROMIUM); | 162 GL_IMAGE_SCANOUT_CHROMIUM); |
| 163 if (!id) { |
| 164 LOG(ERROR) << "Failed to allocate backing CreateImageCHROMIUM surface"; |
| 165 gl->DeleteTextures(1, &texture); |
| 166 return AllocatedSurface(); |
| 167 } |
122 allocated_count_++; | 168 allocated_count_++; |
123 gl->BindTexture(GL_TEXTURE_2D, texture); | 169 gl->BindTexture(GL_TEXTURE_2D, texture); |
124 gl->BindTexImage2DCHROMIUM(GL_TEXTURE_2D, id); | 170 gl->BindTexImage2DCHROMIUM(GL_TEXTURE_2D, id); |
125 return AllocatedSurface(texture, id); | 171 return AllocatedSurface(texture, id, gfx::Rect(size_)); |
126 } | 172 } |
127 | 173 |
128 } // namespace content | 174 } // namespace content |
OLD | NEW |