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