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

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: moved texture code to ReflectorTexture 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),
27 flip_texture_(false), 26 flip_texture_(false),
27 composition_count_(0),
28 output_surface_(nullptr) { 28 output_surface_(nullptr) {
29 if (mirroring_layer) 29 if (mirroring_layer)
30 mirroring_layers_.push_back(new LayerData(mirroring_layer)); 30 AddMirroringLayer(mirroring_layer);
31 } 31 }
32 32
33 ReflectorImpl::~ReflectorImpl() { 33 ReflectorImpl::~ReflectorImpl() {
34 } 34 }
35 35
36 void ReflectorImpl::Shutdown() { 36 void ReflectorImpl::Shutdown() {
37 if (output_surface_) 37 if (output_surface_)
38 DetachFromOutputSurface(); 38 DetachFromOutputSurface();
39 // Prevent the ReflectorImpl from picking up a new output surface. 39 // Prevent the ReflectorImpl from picking up a new output surface.
40 mirroring_layers_.clear(); 40 mirroring_layers_.clear();
41 } 41 }
42 42
43 void ReflectorImpl::DetachFromOutputSurface() { 43 void ReflectorImpl::DetachFromOutputSurface() {
44 DCHECK(output_surface_); 44 DCHECK(output_surface_);
45 output_surface_->SetReflector(nullptr); 45 output_surface_->SetReflector(nullptr);
46 DCHECK(mailbox_.get()); 46 DCHECK(mailbox_.get());
47 mailbox_ = nullptr; 47 mailbox_ = nullptr;
48 output_surface_ = nullptr; 48 output_surface_ = nullptr;
49 mirrored_compositor_gl_helper_->DeleteTexture(
50 mirrored_compositor_gl_helper_texture_id_);
51 mirrored_compositor_gl_helper_texture_id_ = 0;
52 mirrored_compositor_gl_helper_ = nullptr;
53 for (LayerData* layer_data : mirroring_layers_) 49 for (LayerData* layer_data : mirroring_layers_)
54 layer_data->layer->SetShowSolidColorContent(); 50 layer_data->layer->SetShowSolidColorContent();
55 } 51 }
56 52
57 void ReflectorImpl::OnSourceSurfaceReady( 53 void ReflectorImpl::OnSourceSurfaceReady(
58 BrowserCompositorOutputSurface* output_surface) { 54 BrowserCompositorOutputSurface* output_surface) {
59 if (mirroring_layers_.empty()) 55 if (mirroring_layers_.empty())
60 return; // Was already Shutdown(). 56 return; // Was already Shutdown().
61 if (output_surface == output_surface_) 57 if (output_surface == output_surface_)
62 return; // Is already attached. 58 return; // Is already attached.
63 if (output_surface_) 59 if (output_surface_)
64 DetachFromOutputSurface(); 60 DetachFromOutputSurface();
65 61
66 // Use the GLHelper from the ImageTransportFactory for our OwnedMailbox so we 62 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 63
74 // Create a GLHelper attached to the mirrored compositor's output surface for 64 composition_started_callback_ =
75 // copying the output of the mirrored compositor. 65 output_surface_->CreateCompositionStartedCallback();
76 mirrored_compositor_gl_helper_.reset(
77 new GLHelper(output_surface->context_provider()->ContextGL(),
78 output_surface->context_provider()->ContextSupport()));
79 // Create a texture id in the name space of the new GLHelper to update the
80 // mailbox being held by the |mirroring_layer_|.
81 mirrored_compositor_gl_helper_texture_id_ =
82 mirrored_compositor_gl_helper_->ConsumeMailboxToTexture(
83 mailbox_->mailbox(), mailbox_->sync_point());
84 66
85 flip_texture_ = !output_surface->capabilities().flipped_output_surface; 67 flip_texture_ = !output_surface->capabilities().flipped_output_surface;
86 68
87 // 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.
89 // This full redraw should land us in OnSourceSwapBuffers() to resize the
90 // texture appropriately.
91 mirrored_compositor_->ScheduleFullRedraw();
92
93 output_surface_ = output_surface;
94 output_surface_->SetReflector(this); 69 output_surface_->SetReflector(this);
95 } 70 }
96 71
97 void ReflectorImpl::OnMirroringCompositorResized() { 72 void ReflectorImpl::OnMirroringCompositorResized() {
98 for (LayerData* layer_data : mirroring_layers_) 73 for (LayerData* layer_data : mirroring_layers_)
99 layer_data->layer->SchedulePaint(layer_data->layer->bounds()); 74 layer_data->layer->SchedulePaint(layer_data->layer->bounds());
100 } 75 }
101 76
102 void ReflectorImpl::AddMirroringLayer(ui::Layer* layer) { 77 void ReflectorImpl::AddMirroringLayer(ui::Layer* layer) {
78 DCHECK(layer->GetCompositor());
103 DCHECK(mirroring_layers_.end() == FindLayerData(layer)); 79 DCHECK(mirroring_layers_.end() == FindLayerData(layer));
80
104 LayerData* layer_data = new LayerData(layer); 81 LayerData* layer_data = new LayerData(layer);
105 if (mailbox_) 82 if (mailbox_)
106 layer_data->needs_set_mailbox = true; 83 layer_data->needs_set_mailbox = true;
107 mirroring_layers_.push_back(layer_data); 84 mirroring_layers_.push_back(layer_data);
108 mirrored_compositor_->ScheduleFullRedraw(); 85 mirrored_compositor_->ScheduleFullRedraw();
86
87 layer->GetCompositor()->AddObserver(this);
109 } 88 }
110 89
111 void ReflectorImpl::RemoveMirroringLayer(ui::Layer* layer) { 90 void ReflectorImpl::RemoveMirroringLayer(ui::Layer* layer) {
91 DCHECK(layer->GetCompositor());
92
112 ScopedVector<LayerData>::iterator iter = FindLayerData(layer); 93 ScopedVector<LayerData>::iterator iter = FindLayerData(layer);
113 DCHECK(iter != mirroring_layers_.end()); 94 DCHECK(iter != mirroring_layers_.end());
114 (*iter)->layer->SetShowSolidColorContent(); 95 (*iter)->layer->SetShowSolidColorContent();
115 mirroring_layers_.erase(iter); 96 mirroring_layers_.erase(iter);
116 97
98 layer->GetCompositor()->RemoveObserver(this);
99 composition_count_--;
100 if (composition_count_ == 0 && !composition_started_callback_.is_null())
101 composition_started_callback_.Run();
102
117 if (mirroring_layers_.empty() && output_surface_) 103 if (mirroring_layers_.empty() && output_surface_)
118 DetachFromOutputSurface(); 104 DetachFromOutputSurface();
119 } 105 }
120 106
107 void ReflectorImpl::OnCompositingStarted(ui::Compositor* compositor,
108 base::TimeTicks start_time) {
109 if (composition_count_ > 0 && --composition_count_ == 0 &&
110 !composition_started_callback_.is_null()) {
111 composition_started_callback_.Run();
112 }
113 }
114
115 void ReflectorImpl::OnSourceTextureMailboxUpdated(
116 scoped_refptr<OwnedMailbox> mailbox) {
117 mailbox_ = mailbox;
118 if (mailbox_.get()) {
119 for (LayerData* layer_data : mirroring_layers_)
120 layer_data->needs_set_mailbox = true;
121
122 // The texture doesn't have the data. Request full redraw on mirrored
123 // compositor so that the full content will be copied to mirroring
124 // compositor. This full redraw should land us in OnSourceSwapBuffers() to
125 // resize the texture appropriately.
126 mirrored_compositor_->ScheduleFullRedraw();
127 }
128 }
129
121 void ReflectorImpl::OnSourceSwapBuffers() { 130 void ReflectorImpl::OnSourceSwapBuffers() {
122 if (mirroring_layers_.empty()) 131 if (mirroring_layers_.empty()) {
132 if (!composition_started_callback_.is_null())
133 composition_started_callback_.Run();
123 return; 134 return;
135 }
136
124 // Should be attached to the source output surface already. 137 // Should be attached to the source output surface already.
125 DCHECK(mailbox_.get()); 138 DCHECK(mailbox_.get());
126 139
127 gfx::Size size = output_surface_->SurfaceSize(); 140 gfx::Size size = output_surface_->SurfaceSize();
128 mirrored_compositor_gl_helper_->CopyTextureFullImage(
129 mirrored_compositor_gl_helper_texture_id_, size);
130 // Insert a barrier to make the copy show up in the mirroring compositor's
131 // mailbox. Since the the compositor contexts and the ImageTransportFactory's
132 // GLHelper are all on the same GPU channel, this is sufficient instead of
133 // plumbing through a sync point.
134 mirrored_compositor_gl_helper_->InsertOrderingBarrier();
135 141
136 // Request full redraw on mirroring compositor. 142 // Request full redraw on mirroring compositor.
137 for (LayerData* layer_data : mirroring_layers_) 143 for (LayerData* layer_data : mirroring_layers_)
138 UpdateTexture(layer_data, size, layer_data->layer->bounds()); 144 UpdateTexture(layer_data, size, layer_data->layer->bounds());
145 composition_count_ = mirroring_layers_.size();
139 } 146 }
140 147
141 void ReflectorImpl::OnSourcePostSubBuffer(const gfx::Rect& rect) { 148 void ReflectorImpl::OnSourcePostSubBuffer(const gfx::Rect& rect) {
142 if (mirroring_layers_.empty()) 149 if (mirroring_layers_.empty()) {
150 if (!composition_started_callback_.is_null())
151 composition_started_callback_.Run();
143 return; 152 return;
153 }
154
144 // Should be attached to the source output surface already. 155 // Should be attached to the source output surface already.
145 DCHECK(mailbox_.get()); 156 DCHECK(mailbox_.get());
146 157
147 gfx::Size size = output_surface_->SurfaceSize(); 158 gfx::Size size = output_surface_->SurfaceSize();
148 mirrored_compositor_gl_helper_->CopyTextureSubImage(
149 mirrored_compositor_gl_helper_texture_id_, rect);
150 // Insert a barrier to make the copy show up in the mirroring compositor's
151 // mailbox. Since the the compositor contexts and the ImageTransportFactory's
152 // GLHelper are all on the same GPU channel, this is sufficient instead of
153 // plumbing through a sync point.
154 mirrored_compositor_gl_helper_->InsertOrderingBarrier();
155 159
156 int y = rect.y(); 160 int y = rect.y();
157 // Flip the coordinates to compositor's one. 161 // Flip the coordinates to compositor's one.
158 if (flip_texture_) 162 if (flip_texture_)
159 y = size.height() - rect.y() - rect.height(); 163 y = size.height() - rect.y() - rect.height();
160 gfx::Rect mirroring_rect(rect.x(), y, rect.width(), rect.height()); 164 gfx::Rect mirroring_rect(rect.x(), y, rect.width(), rect.height());
161 165
162 // Request redraw of the dirty portion in mirroring compositor. 166 // Request redraw of the dirty portion in mirroring compositor.
163 for (LayerData* layer_data : mirroring_layers_) 167 for (LayerData* layer_data : mirroring_layers_)
164 UpdateTexture(layer_data, size, mirroring_rect); 168 UpdateTexture(layer_data, size, mirroring_rect);
169 composition_count_ = mirroring_layers_.size();
165 } 170 }
166 171
167 static void ReleaseMailbox(scoped_refptr<OwnedMailbox> mailbox, 172 static void ReleaseMailbox(scoped_refptr<OwnedMailbox> mailbox,
168 unsigned int sync_point, 173 unsigned int sync_point,
169 bool is_lost) { 174 bool is_lost) {
170 mailbox->UpdateSyncPoint(sync_point); 175 mailbox->UpdateSyncPoint(sync_point);
171 } 176 }
172 177
173 ScopedVector<ReflectorImpl::LayerData>::iterator ReflectorImpl::FindLayerData( 178 ScopedVector<ReflectorImpl::LayerData>::iterator ReflectorImpl::FindLayerData(
174 ui::Layer* layer) { 179 ui::Layer* layer) {
(...skipping 14 matching lines...) Expand all
189 layer_data->needs_set_mailbox = false; 194 layer_data->needs_set_mailbox = false;
190 } else { 195 } else {
191 layer_data->layer->SetTextureSize(source_size); 196 layer_data->layer->SetTextureSize(source_size);
192 } 197 }
193 layer_data->layer->SetBounds(gfx::Rect(source_size)); 198 layer_data->layer->SetBounds(gfx::Rect(source_size));
194 layer_data->layer->SetTextureFlipped(flip_texture_); 199 layer_data->layer->SetTextureFlipped(flip_texture_);
195 layer_data->layer->SchedulePaint(redraw_rect); 200 layer_data->layer->SchedulePaint(redraw_rect);
196 } 201 }
197 202
198 } // namespace content 203 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/compositor/reflector_impl.h ('k') | content/browser/compositor/reflector_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698