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

Side by Side Diff: android_webview/browser/browser_view_renderer.cc

Issue 1816283005: Move SharedRendererState ownership to AwContents (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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 "android_webview/browser/browser_view_renderer.h" 5 #include "android_webview/browser/browser_view_renderer.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "android_webview/browser/browser_view_renderer_client.h" 9 #include "android_webview/browser/browser_view_renderer_client.h"
10 #include "android_webview/browser/child_frame.h" 10 #include "android_webview/browser/child_frame.h"
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 BrowserViewRenderer* BrowserViewRenderer::FromWebContents( 81 BrowserViewRenderer* BrowserViewRenderer::FromWebContents(
82 content::WebContents* web_contents) { 82 content::WebContents* web_contents) {
83 return BrowserViewRendererUserData::GetBrowserViewRenderer(web_contents); 83 return BrowserViewRendererUserData::GetBrowserViewRenderer(web_contents);
84 } 84 }
85 85
86 BrowserViewRenderer::BrowserViewRenderer( 86 BrowserViewRenderer::BrowserViewRenderer(
87 BrowserViewRendererClient* client, 87 BrowserViewRendererClient* client,
88 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, 88 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner,
89 bool disable_page_visibility) 89 bool disable_page_visibility)
90 : client_(client), 90 : client_(client),
91 shared_renderer_state_(ui_task_runner, this), 91 shared_renderer_state_(nullptr),
92 ui_task_runner_(ui_task_runner), 92 ui_task_runner_(ui_task_runner),
93 disable_page_visibility_(disable_page_visibility), 93 disable_page_visibility_(disable_page_visibility),
94 compositor_(NULL), 94 compositor_(NULL),
95 is_paused_(false), 95 is_paused_(false),
96 view_visible_(false), 96 view_visible_(false),
97 window_visible_(false), 97 window_visible_(false),
98 attached_to_window_(false), 98 attached_to_window_(false),
99 hardware_enabled_(false), 99 hardware_enabled_(false),
100 dip_scale_(0.f), 100 dip_scale_(0.f),
101 page_scale_factor_(1.f), 101 page_scale_factor_(1.f),
102 min_page_scale_factor_(0.f), 102 min_page_scale_factor_(0.f),
103 max_page_scale_factor_(0.f), 103 max_page_scale_factor_(0.f),
104 on_new_picture_enable_(false), 104 on_new_picture_enable_(false),
105 clear_view_(false), 105 clear_view_(false),
106 offscreen_pre_raster_(false), 106 offscreen_pre_raster_(false),
107 next_compositor_id_(1) {} 107 next_compositor_id_(1) {}
108 108
109 BrowserViewRenderer::~BrowserViewRenderer() { 109 BrowserViewRenderer::~BrowserViewRenderer() {
110 DCHECK(compositor_map_.empty()); 110 DCHECK(compositor_map_.empty());
111 SetSharedRendererState(nullptr);
111 } 112 }
112 113
113 void BrowserViewRenderer::RegisterWithWebContents( 114 void BrowserViewRenderer::RegisterWithWebContents(
114 content::WebContents* web_contents) { 115 content::WebContents* web_contents) {
115 web_contents->SetUserData(kBrowserViewRendererUserDataKey, 116 web_contents->SetUserData(kBrowserViewRendererUserDataKey,
116 new BrowserViewRendererUserData(this)); 117 new BrowserViewRendererUserData(this));
117 } 118 }
118 119
120 void BrowserViewRenderer::SetSharedRendererState(
121 SharedRendererState* shared_renderer_state) {
122 if (shared_renderer_state_ == shared_renderer_state) return;
123
124 if (shared_renderer_state_) {
125 shared_renderer_state_->SetBrowserViewRendererOnUI(nullptr);
126 }
127 shared_renderer_state_ = shared_renderer_state;
128 if (shared_renderer_state_) {
129 shared_renderer_state_->SetBrowserViewRendererOnUI(this);
130 }
131 }
132
119 SharedRendererState* BrowserViewRenderer::GetAwDrawGLViewContext() { 133 SharedRendererState* BrowserViewRenderer::GetAwDrawGLViewContext() {
120 return &shared_renderer_state_; 134 return shared_renderer_state_;
121 } 135 }
122 136
123 bool BrowserViewRenderer::RequestDrawGL(bool wait_for_completion) { 137 bool BrowserViewRenderer::RequestDrawGL(bool wait_for_completion) {
124 return client_->RequestDrawGL(wait_for_completion); 138 return client_->RequestDrawGL(wait_for_completion);
125 } 139 }
126 140
127 void BrowserViewRenderer::TrimMemory(const int level, const bool visible) { 141 void BrowserViewRenderer::TrimMemory(const int level, const bool visible) {
128 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 142 DCHECK(ui_task_runner_->BelongsToCurrentThread());
129 // Constants from Android ComponentCallbacks2. 143 // Constants from Android ComponentCallbacks2.
130 enum { 144 enum {
(...skipping 14 matching lines...) Expand all
145 return; 159 return;
146 160
147 // Nothing to drop. 161 // Nothing to drop.
148 if (!compositor_ || !hardware_enabled_) 162 if (!compositor_ || !hardware_enabled_)
149 return; 163 return;
150 164
151 TRACE_EVENT0("android_webview", "BrowserViewRenderer::TrimMemory"); 165 TRACE_EVENT0("android_webview", "BrowserViewRenderer::TrimMemory");
152 166
153 // If offscreen pre-raster is disabled, drop everything in hardware. Otherwise 167 // If offscreen pre-raster is disabled, drop everything in hardware. Otherwise
154 // keep the tiles and just delete the HardwareRenderer. 168 // keep the tiles and just delete the HardwareRenderer.
155 if (level >= TRIM_MEMORY_MODERATE) { 169 if (level >= TRIM_MEMORY_MODERATE && shared_renderer_state_) {
boliu 2016/03/23 15:47:20 this should just be aw_contents -> SRS call, no ne
156 if (offscreen_pre_raster_) 170 if (offscreen_pre_raster_)
157 shared_renderer_state_.DeleteHardwareRendererOnUI(); 171 shared_renderer_state_->DeleteHardwareRendererOnUI();
158 else 172 else
159 shared_renderer_state_.ReleaseHardwareDrawIfNeededOnUI(); 173 shared_renderer_state_->ReleaseHardwareDrawIfNeededOnUI();
160 return; 174 return;
161 } 175 }
162 176
163 // Just set the memory limit to 0 and drop all tiles. This will be reset to 177 // Just set the memory limit to 0 and drop all tiles. This will be reset to
164 // normal levels in the next DrawGL call. 178 // normal levels in the next DrawGL call.
165 // TODO(hush): need to setMemoryPolicy to 0 for non-current compositors too. 179 // TODO(hush): need to setMemoryPolicy to 0 for non-current compositors too.
166 // But WebView only has non-current compositors temporarily. So don't have to 180 // But WebView only has non-current compositors temporarily. So don't have to
167 // do it now. 181 // do it now.
168 if (!offscreen_pre_raster_) 182 if (!offscreen_pre_raster_)
169 compositor_->SetMemoryPolicy(0u); 183 compositor_->SetMemoryPolicy(0u);
170 } 184 }
171 185
172 void BrowserViewRenderer::UpdateMemoryPolicy() { 186 void BrowserViewRenderer::UpdateMemoryPolicy() {
173 if (!hardware_enabled_) { 187 if (!hardware_enabled_ || !shared_renderer_state_) {
174 compositor_->SetMemoryPolicy(0u); 188 compositor_->SetMemoryPolicy(0u);
175 return; 189 return;
176 } 190 }
177 191
178 size_t bytes_limit = 0u; 192 size_t bytes_limit = 0u;
179 if (g_memory_override_in_bytes) { 193 if (g_memory_override_in_bytes) {
180 bytes_limit = static_cast<size_t>(g_memory_override_in_bytes); 194 bytes_limit = static_cast<size_t>(g_memory_override_in_bytes);
181 } else { 195 } else {
182 ParentCompositorDrawConstraints parent_draw_constraints = 196 ParentCompositorDrawConstraints parent_draw_constraints =
183 shared_renderer_state_.GetParentDrawConstraintsOnUI(); 197 shared_renderer_state_->GetParentDrawConstraintsOnUI();
184 gfx::Rect interest_rect = 198 gfx::Rect interest_rect =
185 offscreen_pre_raster_ || parent_draw_constraints.is_layer 199 offscreen_pre_raster_ || parent_draw_constraints.is_layer
186 ? gfx::Rect(size_) 200 ? gfx::Rect(size_)
187 : last_on_draw_global_visible_rect_; 201 : last_on_draw_global_visible_rect_;
188 size_t width = interest_rect.width(); 202 size_t width = interest_rect.width();
189 size_t height = interest_rect.height(); 203 size_t height = interest_rect.height();
190 bytes_limit = kMemoryMultiplier * kBytesPerPixel * width * height; 204 bytes_limit = kMemoryMultiplier * kBytesPerPixel * width * height;
191 // Round up to a multiple of kMemoryAllocationStep. 205 // Round up to a multiple of kMemoryAllocationStep.
192 bytes_limit = 206 bytes_limit =
193 (bytes_limit / kMemoryAllocationStep + 1) * kMemoryAllocationStep; 207 (bytes_limit / kMemoryAllocationStep + 1) * kMemoryAllocationStep;
(...skipping 17 matching lines...) Expand all
211 if (clear_view_) { 225 if (clear_view_) {
212 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_ClearView", 226 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_ClearView",
213 TRACE_EVENT_SCOPE_THREAD); 227 TRACE_EVENT_SCOPE_THREAD);
214 return false; 228 return false;
215 } 229 }
216 230
217 return true; 231 return true;
218 } 232 }
219 233
220 bool BrowserViewRenderer::OnDrawHardware() { 234 bool BrowserViewRenderer::OnDrawHardware() {
235 DCHECK(shared_renderer_state_);
221 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDrawHardware"); 236 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDrawHardware");
222 237 shared_renderer_state_->InitializeHardwareDrawIfNeededOnUI();
223 shared_renderer_state_.InitializeHardwareDrawIfNeededOnUI();
224 238
225 if (!CanOnDraw()) { 239 if (!CanOnDraw()) {
226 return false; 240 return false;
227 } 241 }
228 242
229 shared_renderer_state_.SetScrollOffsetOnUI(last_on_draw_scroll_offset_); 243 shared_renderer_state_->SetScrollOffsetOnUI(last_on_draw_scroll_offset_);
230 hardware_enabled_ = true; 244 hardware_enabled_ = true;
231 245
232 ReturnResourceFromParent(); 246 ReturnResourceFromParent();
233 UpdateMemoryPolicy(); 247 UpdateMemoryPolicy();
234 248
235 ParentCompositorDrawConstraints parent_draw_constraints = 249 ParentCompositorDrawConstraints parent_draw_constraints =
236 shared_renderer_state_.GetParentDrawConstraintsOnUI(); 250 shared_renderer_state_->GetParentDrawConstraintsOnUI();
237 gfx::Size surface_size(size_); 251 gfx::Size surface_size(size_);
238 gfx::Rect viewport(surface_size); 252 gfx::Rect viewport(surface_size);
239 gfx::Rect clip = viewport; 253 gfx::Rect clip = viewport;
240 gfx::Transform transform_for_tile_priority = 254 gfx::Transform transform_for_tile_priority =
241 parent_draw_constraints.transform; 255 parent_draw_constraints.transform;
242 256
243 // If the WebView is on a layer, WebView does not know what transform is 257 // If the WebView is on a layer, WebView does not know what transform is
244 // applied onto the layer so global visible rect does not make sense here. 258 // applied onto the layer so global visible rect does not make sense here.
245 // In this case, just use the surface rect for tiling. 259 // In this case, just use the surface rect for tiling.
246 gfx::Rect viewport_rect_for_tile_priority; 260 gfx::Rect viewport_rect_for_tile_priority;
247 261
248 // Leave viewport_rect_for_tile_priority empty if offscreen_pre_raster_ is on. 262 // Leave viewport_rect_for_tile_priority empty if offscreen_pre_raster_ is on.
249 if (!offscreen_pre_raster_ && !parent_draw_constraints.is_layer) { 263 if (!offscreen_pre_raster_ && !parent_draw_constraints.is_layer) {
250 viewport_rect_for_tile_priority = last_on_draw_global_visible_rect_; 264 viewport_rect_for_tile_priority = last_on_draw_global_visible_rect_;
251 } 265 }
252 266
253 content::SynchronousCompositor::Frame frame = 267 content::SynchronousCompositor::Frame frame =
254 compositor_->DemandDrawHw(surface_size, 268 compositor_->DemandDrawHw(surface_size,
255 gfx::Transform(), 269 gfx::Transform(),
256 viewport, 270 viewport,
257 clip, 271 clip,
258 viewport_rect_for_tile_priority, 272 viewport_rect_for_tile_priority,
259 transform_for_tile_priority); 273 transform_for_tile_priority);
260 if (!frame.frame.get()) { 274 if (!frame.frame.get()) {
261 TRACE_EVENT_INSTANT0("android_webview", "NoNewFrame", 275 TRACE_EVENT_INSTANT0("android_webview", "NoNewFrame",
262 TRACE_EVENT_SCOPE_THREAD); 276 TRACE_EVENT_SCOPE_THREAD);
263 return shared_renderer_state_.HasFrameOnUI(); 277 return shared_renderer_state_->HasFrameOnUI();
264 } 278 }
265 279
266 scoped_ptr<ChildFrame> child_frame = make_scoped_ptr(new ChildFrame( 280 scoped_ptr<ChildFrame> child_frame = make_scoped_ptr(new ChildFrame(
267 frame.output_surface_id, std::move(frame.frame), 281 frame.output_surface_id, std::move(frame.frame),
268 GetCompositorID(compositor_), viewport_rect_for_tile_priority.IsEmpty(), 282 GetCompositorID(compositor_), viewport_rect_for_tile_priority.IsEmpty(),
269 transform_for_tile_priority, offscreen_pre_raster_, 283 transform_for_tile_priority, offscreen_pre_raster_,
270 parent_draw_constraints.is_layer)); 284 parent_draw_constraints.is_layer));
271 285
272 ReturnUnusedResource(shared_renderer_state_.PassUncommittedFrameOnUI()); 286 ReturnUnusedResource(shared_renderer_state_->PassUncommittedFrameOnUI());
273 shared_renderer_state_.SetCompositorFrameOnUI(std::move(child_frame)); 287 shared_renderer_state_->SetCompositorFrameOnUI(std::move(child_frame));
274 return true; 288 return true;
275 } 289 }
276 290
277 void BrowserViewRenderer::UpdateParentDrawConstraints() { 291 void BrowserViewRenderer::UpdateParentDrawConstraints() {
292 DCHECK(shared_renderer_state_);
278 PostInvalidate(); 293 PostInvalidate();
279 ParentCompositorDrawConstraints parent_draw_constraints = 294 ParentCompositorDrawConstraints parent_draw_constraints =
280 shared_renderer_state_.GetParentDrawConstraintsOnUI(); 295 shared_renderer_state_->GetParentDrawConstraintsOnUI();
281 client_->ParentDrawConstraintsUpdated(parent_draw_constraints); 296 client_->ParentDrawConstraintsUpdated(parent_draw_constraints);
282 } 297 }
283 298
284 void BrowserViewRenderer::ReturnUnusedResource( 299 void BrowserViewRenderer::ReturnUnusedResource(
285 scoped_ptr<ChildFrame> child_frame) { 300 scoped_ptr<ChildFrame> child_frame) {
286 if (!child_frame.get() || !child_frame->frame.get()) 301 if (!child_frame.get() || !child_frame->frame.get())
287 return; 302 return;
288 303
289 cc::CompositorFrameAck frame_ack; 304 cc::CompositorFrameAck frame_ack;
290 cc::TransferableResource::ReturnResources( 305 cc::TransferableResource::ReturnResources(
291 child_frame->frame->delegated_frame_data->resource_list, 306 child_frame->frame->delegated_frame_data->resource_list,
292 &frame_ack.resources); 307 &frame_ack.resources);
293 content::SynchronousCompositor* compositor = 308 content::SynchronousCompositor* compositor =
294 compositor_map_[child_frame->compositor_id]; 309 compositor_map_[child_frame->compositor_id];
295 if (compositor && !frame_ack.resources.empty()) 310 if (compositor && !frame_ack.resources.empty())
296 compositor->ReturnResources(child_frame->output_surface_id, frame_ack); 311 compositor->ReturnResources(child_frame->output_surface_id, frame_ack);
297 } 312 }
298 313
299 void BrowserViewRenderer::ReturnResourceFromParent() { 314 void BrowserViewRenderer::ReturnResourceFromParent() {
315 DCHECK(shared_renderer_state_);
300 SharedRendererState::ReturnedResourcesMap returned_resource_map; 316 SharedRendererState::ReturnedResourcesMap returned_resource_map;
301 shared_renderer_state_.SwapReturnedResourcesOnUI(&returned_resource_map); 317 shared_renderer_state_->SwapReturnedResourcesOnUI(&returned_resource_map);
302 for (auto iterator = returned_resource_map.begin(); 318 for (auto iterator = returned_resource_map.begin();
303 iterator != returned_resource_map.end(); iterator++) { 319 iterator != returned_resource_map.end(); iterator++) {
304 uint32_t compositor_id = iterator->first; 320 uint32_t compositor_id = iterator->first;
305 content::SynchronousCompositor* compositor = compositor_map_[compositor_id]; 321 content::SynchronousCompositor* compositor = compositor_map_[compositor_id];
306 cc::CompositorFrameAck frame_ack; 322 cc::CompositorFrameAck frame_ack;
307 frame_ack.resources.swap(iterator->second.resources); 323 frame_ack.resources.swap(iterator->second.resources);
308 324
309 if (compositor && !frame_ack.resources.empty()) { 325 if (compositor && !frame_ack.resources.empty()) {
310 compositor->ReturnResources(iterator->second.output_surface_id, 326 compositor->ReturnResources(iterator->second.output_surface_id,
311 frame_ack); 327 frame_ack);
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 "width", 432 "width",
417 width, 433 width,
418 "height", 434 "height",
419 height); 435 height);
420 attached_to_window_ = true; 436 attached_to_window_ = true;
421 size_.SetSize(width, height); 437 size_.SetSize(width, height);
422 UpdateCompositorIsActive(); 438 UpdateCompositorIsActive();
423 } 439 }
424 440
425 void BrowserViewRenderer::OnDetachedFromWindow() { 441 void BrowserViewRenderer::OnDetachedFromWindow() {
442 DCHECK(shared_renderer_state_);
426 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDetachedFromWindow"); 443 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDetachedFromWindow");
427 shared_renderer_state_.ReleaseHardwareDrawIfNeededOnUI(); 444 shared_renderer_state_->ReleaseHardwareDrawIfNeededOnUI();
428 attached_to_window_ = false; 445 attached_to_window_ = false;
429 DCHECK(!hardware_enabled_); 446 DCHECK(!hardware_enabled_);
430 UpdateCompositorIsActive(); 447 UpdateCompositorIsActive();
431 } 448 }
432 449
433 void BrowserViewRenderer::OnComputeScroll(base::TimeTicks animation_time) { 450 void BrowserViewRenderer::OnComputeScroll(base::TimeTicks animation_time) {
434 if (!compositor_) 451 if (!compositor_)
435 return; 452 return;
436 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnComputeScroll"); 453 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnComputeScroll");
437 compositor_->OnComputeScroll(animation_time); 454 compositor_->OnComputeScroll(animation_time);
438 } 455 }
439 456
440 void BrowserViewRenderer::ReleaseHardware() { 457 void BrowserViewRenderer::ReleaseHardware() {
458 DCHECK(shared_renderer_state_);
441 DCHECK(hardware_enabled_); 459 DCHECK(hardware_enabled_);
442 ReturnUnusedResource(shared_renderer_state_.PassUncommittedFrameOnUI()); 460 ReturnUnusedResource(shared_renderer_state_->PassUncommittedFrameOnUI());
443 ReturnResourceFromParent(); 461 ReturnResourceFromParent();
444 DCHECK(shared_renderer_state_.ReturnedResourcesEmptyOnUI()); 462 DCHECK(shared_renderer_state_->ReturnedResourcesEmptyOnUI());
445 463
446 if (compositor_) { 464 if (compositor_) {
447 compositor_->SetMemoryPolicy(0u); 465 compositor_->SetMemoryPolicy(0u);
448 } 466 }
449 467
450 hardware_enabled_ = false; 468 hardware_enabled_ = false;
451 } 469 }
452 470
453 bool BrowserViewRenderer::IsVisible() const { 471 bool BrowserViewRenderer::IsVisible() const {
454 // Ignore |window_visible_| if |attached_to_window_| is false. 472 // Ignore |window_visible_| if |attached_to_window_| is false.
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
726 base::StringAppendF(&str, 744 base::StringAppendF(&str,
727 "overscroll_rounding_error_: %s ", 745 "overscroll_rounding_error_: %s ",
728 overscroll_rounding_error_.ToString().c_str()); 746 overscroll_rounding_error_.ToString().c_str());
729 base::StringAppendF( 747 base::StringAppendF(
730 &str, "on_new_picture_enable: %d ", on_new_picture_enable_); 748 &str, "on_new_picture_enable: %d ", on_new_picture_enable_);
731 base::StringAppendF(&str, "clear_view: %d ", clear_view_); 749 base::StringAppendF(&str, "clear_view: %d ", clear_view_);
732 return str; 750 return str;
733 } 751 }
734 752
735 } // namespace android_webview 753 } // namespace android_webview
OLDNEW
« no previous file with comments | « android_webview/browser/browser_view_renderer.h ('k') | android_webview/browser/shared_renderer_state.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698