Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: content/browser/compositor/reflector_impl.cc

Issue 1123763003: Draw the offscreen texture to reflector's surface without extra copy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/reflector_impl.h" 5 #include "content/browser/compositor/reflector_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "content/browser/compositor/browser_compositor_output_surface.h" 9 #include "content/browser/compositor/browser_compositor_output_surface.h"
10 #include "content/browser/compositor/owned_mailbox.h" 10 #include "content/browser/compositor/owned_mailbox.h"
11 #include "content/common/gpu/client/gl_helper.h" 11 #include "content/common/gpu/client/gl_helper.h"
12 #include "ui/compositor/layer.h" 12 #include "ui/compositor/layer.h"
13 13
14 namespace content { 14 namespace content {
15 15
16 struct ReflectorImpl::LayerData { 16 struct ReflectorImpl::LayerData {
17 LayerData(ui::Layer* layer) : layer(layer) {} 17 LayerData(ui::Layer* layer) : layer(layer) {}
18 18
19 ui::Layer* layer; 19 ui::Layer* layer;
20 bool needs_set_mailbox = false; 20 bool needs_set_mailbox = false;
21 }; 21 };
22 22
23 ReflectorImpl::ReflectorImpl(ui::Compositor* mirrored_compositor, 23 ReflectorImpl::ReflectorImpl(ui::Compositor* mirrored_compositor,
24 ui::Layer* mirroring_layer) 24 ui::Layer* mirroring_layer)
25 : mirrored_compositor_(mirrored_compositor), 25 : mirrored_compositor_(mirrored_compositor),
26 mirrored_compositor_gl_helper_texture_id_(0), 26 mirrored_compositor_gl_helper_texture_id_(0),
27 flip_texture_(false), 27 flip_texture_(false),
28 composition_count_(0),
28 output_surface_(nullptr) { 29 output_surface_(nullptr) {
29 if (mirroring_layer) 30 if (mirroring_layer)
30 mirroring_layers_.push_back(new LayerData(mirroring_layer)); 31 AddMirroringLayer(mirroring_layer);
31 } 32 }
32 33
33 ReflectorImpl::~ReflectorImpl() { 34 ReflectorImpl::~ReflectorImpl() {
34 } 35 }
35 36
36 void ReflectorImpl::Shutdown() { 37 void ReflectorImpl::Shutdown() {
37 if (output_surface_) 38 if (output_surface_)
38 DetachFromOutputSurface(); 39 DetachFromOutputSurface();
39 // Prevent the ReflectorImpl from picking up a new output surface. 40 // Prevent the ReflectorImpl from picking up a new output surface.
40 mirroring_layers_.clear(); 41 mirroring_layers_.clear();
41 } 42 }
42 43
43 void ReflectorImpl::DetachFromOutputSurface() { 44 void ReflectorImpl::DetachFromOutputSurface() {
44 DCHECK(output_surface_); 45 DCHECK(output_surface_);
45 output_surface_->SetReflector(nullptr); 46 output_surface_->SetReflector(nullptr);
46 DCHECK(mailbox_.get()); 47 DCHECK(mailbox_.get());
47 mailbox_ = nullptr; 48 mailbox_ = nullptr;
48 output_surface_ = nullptr; 49 output_surface_ = nullptr;
49 mirrored_compositor_gl_helper_->DeleteTexture( 50 if (mirrored_compositor_gl_helper_.get()) {
50 mirrored_compositor_gl_helper_texture_id_); 51 mirrored_compositor_gl_helper_->DeleteTexture(
51 mirrored_compositor_gl_helper_texture_id_ = 0; 52 mirrored_compositor_gl_helper_texture_id_);
52 mirrored_compositor_gl_helper_ = nullptr; 53 mirrored_compositor_gl_helper_texture_id_ = 0;
54 mirrored_compositor_gl_helper_ = nullptr;
55 }
53 for (LayerData* layer_data : mirroring_layers_) 56 for (LayerData* layer_data : mirroring_layers_)
54 layer_data->layer->SetShowSolidColorContent(); 57 layer_data->layer->SetShowSolidColorContent();
55 } 58 }
56 59
57 void ReflectorImpl::OnSourceSurfaceReady( 60 void ReflectorImpl::OnSourceSurfaceReady(
58 BrowserCompositorOutputSurface* output_surface) { 61 BrowserCompositorOutputSurface* output_surface) {
59 if (mirroring_layers_.empty()) 62 if (mirroring_layers_.empty())
60 return; // Was already Shutdown(). 63 return; // Was already Shutdown().
61 if (output_surface == output_surface_) 64 if (output_surface == output_surface_)
62 return; // Is already attached. 65 return; // Is already attached.
63 if (output_surface_) 66 if (output_surface_)
64 DetachFromOutputSurface(); 67 DetachFromOutputSurface();
65 68
66 // Use the GLHelper from the ImageTransportFactory for our OwnedMailbox so we 69 output_surface_ = output_surface;
67 // don't have to manage the lifetime of the GLHelper relative to the lifetime
68 // of the mailbox.
69 GLHelper* shared_helper = ImageTransportFactory::GetInstance()->GetGLHelper();
70 mailbox_ = new OwnedMailbox(shared_helper);
71 for (LayerData* layer_data : mirroring_layers_)
72 layer_data->needs_set_mailbox = true;
73 70
74 // Create a GLHelper attached to the mirrored compositor's output surface for 71 composition_started_callback_ =
75 // copying the output of the mirrored compositor. 72 output_surface_->CreateCompositionStartedCallback();
76 mirrored_compositor_gl_helper_.reset( 73 OnSourceTextureMailboxUpdated();
77 new GLHelper(output_surface->context_provider()->ContextGL(), 74 // If the source output surface didn't have its own
78 output_surface->context_provider()->ContextSupport())); 75 // mailbox, create one for mirror mode.
79 // Create a texture id in the name space of the new GLHelper to update the 76 if (!mailbox_.get()) {
80 // mailbox being held by the |mirroring_layer_|. 77 // Use the GLHelper from the ImageTransportFactory for our OwnedMailbox
81 mirrored_compositor_gl_helper_texture_id_ = 78 // so we don't have to manage the lifetime of the GLHelper relative to
82 mirrored_compositor_gl_helper_->ConsumeMailboxToTexture( 79 // the lifetime of the mailbox.
83 mailbox_->mailbox(), mailbox_->sync_point()); 80 GLHelper* shared_helper =
81 ImageTransportFactory::GetInstance()->GetGLHelper();
82 mailbox_ = new OwnedMailbox(shared_helper);
83
84 // Create a GLHelper attached to the mirrored compositor's output
85 // surface for copying the output of the mirrored compositor.
86 mirrored_compositor_gl_helper_.reset(
87 new GLHelper(output_surface_->context_provider()->ContextGL(),
88 output_surface_->context_provider()->ContextSupport()));
89 // Create a texture id in the name space of the new GLHelper to update the
90 // mailbox being held by the |mirroring_layer_|.
91 mirrored_compositor_gl_helper_texture_id_ =
92 mirrored_compositor_gl_helper_->ConsumeMailboxToTexture(
93 mailbox_->mailbox(), mailbox_->sync_point());
94
95 for (LayerData* layer_data : mirroring_layers_)
96 layer_data->needs_set_mailbox = true;
97 }
84 98
85 flip_texture_ = !output_surface->capabilities().flipped_output_surface; 99 flip_texture_ = !output_surface->capabilities().flipped_output_surface;
86 100
87 // The texture doesn't have the data. Request full redraw on mirrored 101 // The texture doesn't have the data. Request full redraw on mirrored
88 // compositor so that the full content will be copied to mirroring compositor. 102 // compositor so that the full content will be copied to mirroring compositor.
89 // This full redraw should land us in OnSourceSwapBuffers() to resize the 103 // This full redraw should land us in OnSourceSwapBuffers() to resize the
90 // texture appropriately. 104 // texture appropriately.
91 mirrored_compositor_->ScheduleFullRedraw(); 105 mirrored_compositor_->ScheduleFullRedraw();
92 106
93 output_surface_ = output_surface;
94 output_surface_->SetReflector(this); 107 output_surface_->SetReflector(this);
95 } 108 }
96 109
97 void ReflectorImpl::OnMirroringCompositorResized() { 110 void ReflectorImpl::OnMirroringCompositorResized() {
98 for (LayerData* layer_data : mirroring_layers_) 111 for (LayerData* layer_data : mirroring_layers_)
99 layer_data->layer->SchedulePaint(layer_data->layer->bounds()); 112 layer_data->layer->SchedulePaint(layer_data->layer->bounds());
100 } 113 }
101 114
102 void ReflectorImpl::AddMirroringLayer(ui::Layer* layer) { 115 void ReflectorImpl::AddMirroringLayer(ui::Layer* layer) {
103 DCHECK(mirroring_layers_.end() == FindLayerData(layer)); 116 DCHECK(mirroring_layers_.end() == FindLayerData(layer));
104 LayerData* layer_data = new LayerData(layer); 117 LayerData* layer_data = new LayerData(layer);
105 if (mailbox_) 118 if (mailbox_)
106 layer_data->needs_set_mailbox = true; 119 layer_data->needs_set_mailbox = true;
107 mirroring_layers_.push_back(layer_data); 120 mirroring_layers_.push_back(layer_data);
108 mirrored_compositor_->ScheduleFullRedraw(); 121 mirrored_compositor_->ScheduleFullRedraw();
122
123 layer->GetCompositor()->AddObserver(this);
109 } 124 }
110 125
111 void ReflectorImpl::RemoveMirroringLayer(ui::Layer* layer) { 126 void ReflectorImpl::RemoveMirroringLayer(ui::Layer* layer) {
112 ScopedVector<LayerData>::iterator iter = FindLayerData(layer); 127 ScopedVector<LayerData>::iterator iter = FindLayerData(layer);
113 DCHECK(iter != mirroring_layers_.end()); 128 DCHECK(iter != mirroring_layers_.end());
114 (*iter)->layer->SetShowSolidColorContent(); 129 (*iter)->layer->SetShowSolidColorContent();
115 mirroring_layers_.erase(iter); 130 mirroring_layers_.erase(iter);
116 131
132 layer->GetCompositor()->RemoveObserver(this);
133 composition_count_--;
134
117 if (mirroring_layers_.empty() && output_surface_) 135 if (mirroring_layers_.empty() && output_surface_)
118 DetachFromOutputSurface(); 136 DetachFromOutputSurface();
119 } 137 }
120 138
139 void ReflectorImpl::OnCompositingStarted(ui::Compositor* compositor,
140 base::TimeTicks start_time) {
141 if (composition_count_ > 0 &&
142 --composition_count_ == 0 &&
143 !composition_started_callback_.is_null()) {
144 composition_started_callback_.Run();
145 }
146 }
147
148 void ReflectorImpl::OnSourceTextureMailboxUpdated() {
149 mailbox_ = output_surface_->GetTextureMailbox();
150 for (LayerData* layer_data : mirroring_layers_)
151 layer_data->needs_set_mailbox = true;
152 }
153
121 void ReflectorImpl::OnSourceSwapBuffers() { 154 void ReflectorImpl::OnSourceSwapBuffers() {
122 if (mirroring_layers_.empty()) 155 if (mirroring_layers_.empty()) {
156 if (!composition_started_callback_.is_null())
157 composition_started_callback_.Run();
123 return; 158 return;
159 }
160
124 // Should be attached to the source output surface already. 161 // Should be attached to the source output surface already.
125 DCHECK(mailbox_.get()); 162 DCHECK(mailbox_.get());
126 163
127 gfx::Size size = output_surface_->SurfaceSize(); 164 gfx::Size size = output_surface_->SurfaceSize();
128 mirrored_compositor_gl_helper_->CopyTextureFullImage( 165 if (mirrored_compositor_gl_helper_.get()) {
129 mirrored_compositor_gl_helper_texture_id_, size); 166 mirrored_compositor_gl_helper_->CopyTextureFullImage(
130 // Insert a barrier to make the copy show up in the mirroring compositor's 167 mirrored_compositor_gl_helper_texture_id_, size);
131 // mailbox. Since the the compositor contexts and the ImageTransportFactory's 168 // Insert a barrier to make the copy show up in the mirroring compositor's
132 // GLHelper are all on the same GPU channel, this is sufficient instead of 169 // mailbox. Since the the compositor contexts and the
133 // plumbing through a sync point. 170 // ImageTransportFactory's
134 mirrored_compositor_gl_helper_->InsertOrderingBarrier(); 171 // GLHelper are all on the same GPU channel, this is sufficient instead of
172 // plumbing through a sync point.
173 mirrored_compositor_gl_helper_->InsertOrderingBarrier();
174 }
135 175
136 // Request full redraw on mirroring compositor. 176 // Request full redraw on mirroring compositor.
137 for (LayerData* layer_data : mirroring_layers_) 177 for (LayerData* layer_data : mirroring_layers_)
138 UpdateTexture(layer_data, size, layer_data->layer->bounds()); 178 UpdateTexture(layer_data, size, layer_data->layer->bounds());
179 composition_count_ = mirroring_layers_.size();
139 } 180 }
140 181
141 void ReflectorImpl::OnSourcePostSubBuffer(const gfx::Rect& rect) { 182 void ReflectorImpl::OnSourcePostSubBuffer(const gfx::Rect& rect) {
142 if (mirroring_layers_.empty()) 183 if (mirroring_layers_.empty()) {
184 if (!composition_started_callback_.is_null())
185 composition_started_callback_.Run();
143 return; 186 return;
187 }
188
144 // Should be attached to the source output surface already. 189 // Should be attached to the source output surface already.
145 DCHECK(mailbox_.get()); 190 DCHECK(mailbox_.get());
146 191
147 gfx::Size size = output_surface_->SurfaceSize(); 192 gfx::Size size = output_surface_->SurfaceSize();
148 mirrored_compositor_gl_helper_->CopyTextureSubImage( 193 if (mirrored_compositor_gl_helper_.get()) {
149 mirrored_compositor_gl_helper_texture_id_, rect); 194 mirrored_compositor_gl_helper_->CopyTextureSubImage(
150 // Insert a barrier to make the copy show up in the mirroring compositor's 195 mirrored_compositor_gl_helper_texture_id_, rect);
151 // mailbox. Since the the compositor contexts and the ImageTransportFactory's 196 // Insert a barrier to make the copy show up in the mirroring compositor's
152 // GLHelper are all on the same GPU channel, this is sufficient instead of 197 // mailbox. Since the the compositor contexts and the
153 // plumbing through a sync point. 198 // ImageTransportFactory's
154 mirrored_compositor_gl_helper_->InsertOrderingBarrier(); 199 // GLHelper are all on the same GPU channel, this is sufficient instead of
200 // plumbing through a sync point.
201 mirrored_compositor_gl_helper_->InsertOrderingBarrier();
202 }
155 203
156 int y = rect.y(); 204 int y = rect.y();
157 // Flip the coordinates to compositor's one. 205 // Flip the coordinates to compositor's one.
158 if (flip_texture_) 206 if (flip_texture_)
159 y = size.height() - rect.y() - rect.height(); 207 y = size.height() - rect.y() - rect.height();
160 gfx::Rect mirroring_rect(rect.x(), y, rect.width(), rect.height()); 208 gfx::Rect mirroring_rect(rect.x(), y, rect.width(), rect.height());
161 209
162 // Request redraw of the dirty portion in mirroring compositor. 210 // Request redraw of the dirty portion in mirroring compositor.
163 for (LayerData* layer_data : mirroring_layers_) 211 for (LayerData* layer_data : mirroring_layers_)
164 UpdateTexture(layer_data, size, mirroring_rect); 212 UpdateTexture(layer_data, size, mirroring_rect);
213 composition_count_ = mirroring_layers_.size();
165 } 214 }
166 215
167 static void ReleaseMailbox(scoped_refptr<OwnedMailbox> mailbox, 216 static void ReleaseMailbox(scoped_refptr<OwnedMailbox> mailbox,
168 unsigned int sync_point, 217 unsigned int sync_point,
169 bool is_lost) { 218 bool is_lost) {
170 mailbox->UpdateSyncPoint(sync_point); 219 mailbox->UpdateSyncPoint(sync_point);
171 } 220 }
172 221
173 ScopedVector<ReflectorImpl::LayerData>::iterator ReflectorImpl::FindLayerData( 222 ScopedVector<ReflectorImpl::LayerData>::iterator ReflectorImpl::FindLayerData(
174 ui::Layer* layer) { 223 ui::Layer* layer) {
175 return std::find_if(mirroring_layers_.begin(), mirroring_layers_.end(), 224 return std::find_if(mirroring_layers_.begin(), mirroring_layers_.end(),
176 [layer](const LayerData* layer_data) { 225 [layer](const LayerData* layer_data) {
177 return layer_data->layer == layer; 226 return layer_data->layer == layer;
178 }); 227 });
179 } 228 }
180 229
181 void ReflectorImpl::UpdateTexture(ReflectorImpl::LayerData* layer_data, 230 void ReflectorImpl::UpdateTexture(ReflectorImpl::LayerData* layer_data,
182 const gfx::Size& source_size, 231 const gfx::Size& source_size,
183 const gfx::Rect& redraw_rect) { 232 const gfx::Rect& redraw_rect) {
184 if (layer_data->needs_set_mailbox) { 233 if (layer_data->needs_set_mailbox) {
185 layer_data->layer->SetTextureMailbox( 234 layer_data->layer->SetTextureMailbox(
186 cc::TextureMailbox(mailbox_->holder()), 235 cc::TextureMailbox(mailbox_->holder()),
187 cc::SingleReleaseCallback::Create(base::Bind(ReleaseMailbox, mailbox_)), 236 cc::SingleReleaseCallback::Create(base::Bind(ReleaseMailbox, mailbox_)),
188 source_size); 237 source_size);
189 layer_data->needs_set_mailbox = false; 238 layer_data->needs_set_mailbox = false;
190 } else { 239 } else {
191 layer_data->layer->SetTextureSize(source_size); 240 layer_data->layer->SetTextureSize(source_size);
192 } 241 }
193 layer_data->layer->SetBounds(gfx::Rect(source_size)); 242 layer_data->layer->SetBounds(gfx::Rect(source_size));
194 layer_data->layer->SetTextureFlipped(flip_texture_); 243 layer_data->layer->SetTextureFlipped(flip_texture_);
195 layer_data->layer->SchedulePaint(redraw_rect); 244 layer_data->layer->SchedulePaint(redraw_rect);
196 } 245 }
197 246
198 } // namespace content 247 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698