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

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

Issue 655813004: Part 1: Refactor Android WebView graphics code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactor
Patch Set: rewrite, without renames Created 6 years, 2 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 "android_webview/browser/browser_view_renderer_client.h" 7 #include "android_webview/browser/browser_view_renderer_client.h"
8 #include "android_webview/browser/shared_renderer_state.h" 8 #include "android_webview/browser/shared_renderer_state.h"
9 #include "android_webview/common/aw_switches.h" 9 #include "android_webview/common/aw_switches.h"
10 #include "base/auto_reset.h" 10 #include "base/auto_reset.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/debug/trace_event_argument.h" 12 #include "base/debug/trace_event_argument.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "cc/output/compositor_frame.h" 16 #include "cc/output/compositor_frame.h"
17 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/content_switches.h" 19 #include "content/public/common/content_switches.h"
19 #include "gpu/command_buffer/service/gpu_switches.h" 20 #include "gpu/command_buffer/service/gpu_switches.h"
20 #include "third_party/skia/include/core/SkBitmap.h" 21 #include "third_party/skia/include/core/SkBitmap.h"
21 #include "third_party/skia/include/core/SkCanvas.h" 22 #include "third_party/skia/include/core/SkCanvas.h"
22 #include "third_party/skia/include/core/SkPicture.h" 23 #include "third_party/skia/include/core/SkPicture.h"
23 #include "third_party/skia/include/core/SkPictureRecorder.h" 24 #include "third_party/skia/include/core/SkPictureRecorder.h"
24 #include "ui/gfx/vector2d_conversions.h" 25 #include "ui/gfx/vector2d_conversions.h"
25 26
26 using content::SynchronousCompositorMemoryPolicy; 27 using content::SynchronousCompositorMemoryPolicy;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 g_memory_override_in_bytes *= 1024 * 1024; 62 g_memory_override_in_bytes *= 1024 * 1024;
62 } 63 }
63 64
64 // There is no need to limit number of tiles, so use an effectively unlimited 65 // There is no need to limit number of tiles, so use an effectively unlimited
65 // value as the limit. 66 // value as the limit.
66 GlobalTileManager::GetInstance()->SetTileLimit(10 * 1000 * 1000); 67 GlobalTileManager::GetInstance()->SetTileLimit(10 * 1000 * 1000);
67 } 68 }
68 69
69 BrowserViewRenderer::BrowserViewRenderer( 70 BrowserViewRenderer::BrowserViewRenderer(
70 BrowserViewRendererClient* client, 71 BrowserViewRendererClient* client,
71 SharedRendererState* shared_renderer_state,
72 content::WebContents* web_contents, 72 content::WebContents* web_contents,
73 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) 73 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner)
74 : client_(client), 74 : client_(client),
75 shared_renderer_state_(shared_renderer_state), 75 shared_renderer_state_(
76 content::BrowserThread::GetMessageLoopProxyForThread(
boliu 2014/10/24 22:12:01 pass in ui_task_runner
hush (inactive) 2014/10/24 22:30:15 SRS needs a base::MessageLoopProxy, which is a chi
77 content::BrowserThread::UI),
78 this),
76 web_contents_(web_contents), 79 web_contents_(web_contents),
77 ui_task_runner_(ui_task_runner), 80 ui_task_runner_(ui_task_runner),
78 compositor_(NULL), 81 compositor_(NULL),
79 is_paused_(false), 82 is_paused_(false),
80 view_visible_(false), 83 view_visible_(false),
81 window_visible_(false), 84 window_visible_(false),
82 attached_to_window_(false), 85 attached_to_window_(false),
83 hardware_enabled_(false), 86 hardware_enabled_(false),
84 dip_scale_(0.0), 87 dip_scale_(0.0),
85 page_scale_factor_(1.0), 88 page_scale_factor_(1.0),
(...skipping 11 matching lines...) Expand all
97 // Currently the logic in this class relies on |compositor_| remaining 100 // Currently the logic in this class relies on |compositor_| remaining
98 // NULL until the DidInitializeCompositor() call, hence it is not set here. 101 // NULL until the DidInitializeCompositor() call, hence it is not set here.
99 } 102 }
100 103
101 BrowserViewRenderer::~BrowserViewRenderer() { 104 BrowserViewRenderer::~BrowserViewRenderer() {
102 content::SynchronousCompositor::SetClientForWebContents(web_contents_, NULL); 105 content::SynchronousCompositor::SetClientForWebContents(web_contents_, NULL);
103 // OnDetachedFromWindow should be called before the destructor, so the memory 106 // OnDetachedFromWindow should be called before the destructor, so the memory
104 // policy should have already been updated. 107 // policy should have already been updated.
105 } 108 }
106 109
110 SharedRendererState* BrowserViewRenderer::GetSharedRendererState() {
111 return &shared_renderer_state_;
112 }
113
114 bool BrowserViewRenderer::RequestDrawGL(jobject canvas,
115 bool wait_for_completion) {
116 return client_->RequestDrawGL(canvas, wait_for_completion);
117 }
118
107 // This function updates the resource allocation in GlobalTileManager. 119 // This function updates the resource allocation in GlobalTileManager.
108 void BrowserViewRenderer::TrimMemory(const int level, const bool visible) { 120 void BrowserViewRenderer::TrimMemory(const int level, const bool visible) {
109 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 121 DCHECK(ui_task_runner_->BelongsToCurrentThread());
110 // Constants from Android ComponentCallbacks2. 122 // Constants from Android ComponentCallbacks2.
111 enum { 123 enum {
112 TRIM_MEMORY_RUNNING_LOW = 10, 124 TRIM_MEMORY_RUNNING_LOW = 10,
113 TRIM_MEMORY_UI_HIDDEN = 20, 125 TRIM_MEMORY_UI_HIDDEN = 20,
114 TRIM_MEMORY_BACKGROUND = 40, 126 TRIM_MEMORY_BACKGROUND = 40,
115 }; 127 };
116 128
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 217
206 // Perform a software draw 218 // Perform a software draw
207 return OnDrawSoftware(java_canvas); 219 return OnDrawSoftware(java_canvas);
208 } 220 }
209 221
210 bool BrowserViewRenderer::OnDrawHardware(jobject java_canvas) { 222 bool BrowserViewRenderer::OnDrawHardware(jobject java_canvas) {
211 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDrawHardware"); 223 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDrawHardware");
212 if (!compositor_) 224 if (!compositor_)
213 return false; 225 return false;
214 226
215 shared_renderer_state_->SetScrollOffset(last_on_draw_scroll_offset_); 227 shared_renderer_state_.SetScrollOffsetOnUI(last_on_draw_scroll_offset_);
216 228
217 if (!hardware_enabled_) { 229 if (!hardware_enabled_) {
218 hardware_enabled_ = compositor_->InitializeHwDraw(); 230 hardware_enabled_ = compositor_->InitializeHwDraw();
219 if (hardware_enabled_) { 231 if (hardware_enabled_) {
220 tile_manager_key_ = GlobalTileManager::GetInstance()->PushBack(this); 232 tile_manager_key_ = GlobalTileManager::GetInstance()->PushBack(this);
221 } 233 }
222 } 234 }
223 if (!hardware_enabled_) 235 if (!hardware_enabled_)
224 return false; 236 return false;
225 237
226 if (last_on_draw_global_visible_rect_.IsEmpty() && 238 if (last_on_draw_global_visible_rect_.IsEmpty() &&
227 parent_draw_constraints_.surface_rect.IsEmpty()) { 239 parent_draw_constraints_.surface_rect.IsEmpty()) {
228 TRACE_EVENT_INSTANT0("android_webview", 240 TRACE_EVENT_INSTANT0("android_webview",
229 "EarlyOut_EmptyVisibleRect", 241 "EarlyOut_EmptyVisibleRect",
230 TRACE_EVENT_SCOPE_THREAD); 242 TRACE_EVENT_SCOPE_THREAD);
231 shared_renderer_state_->SetForceInvalidateOnNextDrawGL(true); 243 shared_renderer_state_.SetForceInvalidateOnNextDrawGLOnUI(true);
232 return client_->RequestDrawGL(java_canvas, false); 244 return client_->RequestDrawGL(java_canvas, false);
233 } 245 }
234 246
235 ReturnResourceFromParent(); 247 ReturnResourceFromParent();
236 if (shared_renderer_state_->HasCompositorFrame()) { 248 if (shared_renderer_state_.HasCompositorFrameOnUI()) {
237 TRACE_EVENT_INSTANT0("android_webview", 249 TRACE_EVENT_INSTANT0("android_webview",
238 "EarlyOut_PreviousFrameUnconsumed", 250 "EarlyOut_PreviousFrameUnconsumed",
239 TRACE_EVENT_SCOPE_THREAD); 251 TRACE_EVENT_SCOPE_THREAD);
240 DidSkipCompositeInDraw(); 252 DidSkipCompositeInDraw();
241 return client_->RequestDrawGL(java_canvas, false); 253 return client_->RequestDrawGL(java_canvas, false);
242 } 254 }
243 255
244 scoped_ptr<cc::CompositorFrame> frame = CompositeHw(); 256 scoped_ptr<cc::CompositorFrame> frame = CompositeHw();
245 if (!frame.get()) 257 if (!frame.get())
246 return false; 258 return false;
247 259
248 shared_renderer_state_->SetCompositorFrame(frame.Pass(), false); 260 shared_renderer_state_.SetCompositorFrameOnUI(frame.Pass(), false);
249 GlobalTileManager::GetInstance()->DidUse(tile_manager_key_); 261 GlobalTileManager::GetInstance()->DidUse(tile_manager_key_);
250 return client_->RequestDrawGL(java_canvas, false); 262 return client_->RequestDrawGL(java_canvas, false);
251 } 263 }
252 264
253 scoped_ptr<cc::CompositorFrame> BrowserViewRenderer::CompositeHw() { 265 scoped_ptr<cc::CompositorFrame> BrowserViewRenderer::CompositeHw() {
254 SynchronousCompositorMemoryPolicy new_policy = CalculateDesiredMemoryPolicy(); 266 SynchronousCompositorMemoryPolicy new_policy = CalculateDesiredMemoryPolicy();
255 RequestMemoryPolicy(new_policy); 267 RequestMemoryPolicy(new_policy);
256 compositor_->SetMemoryPolicy(memory_policy_); 268 compositor_->SetMemoryPolicy(memory_policy_);
257 269
258 parent_draw_constraints_ = shared_renderer_state_->ParentDrawConstraints(); 270 parent_draw_constraints_ =
271 shared_renderer_state_.GetParentDrawConstraintsOnUI();
259 gfx::Size surface_size(width_, height_); 272 gfx::Size surface_size(width_, height_);
260 gfx::Rect viewport(surface_size); 273 gfx::Rect viewport(surface_size);
261 gfx::Rect clip = viewport; 274 gfx::Rect clip = viewport;
262 gfx::Transform transform_for_tile_priority = 275 gfx::Transform transform_for_tile_priority =
263 parent_draw_constraints_.transform; 276 parent_draw_constraints_.transform;
264 277
265 // If the WebView is on a layer, WebView does not know what transform is 278 // If the WebView is on a layer, WebView does not know what transform is
266 // applied onto the layer so global visible rect does not make sense here. 279 // applied onto the layer so global visible rect does not make sense here.
267 // In this case, just use the surface rect for tiling. 280 // In this case, just use the surface rect for tiling.
268 gfx::Rect viewport_rect_for_tile_priority; 281 gfx::Rect viewport_rect_for_tile_priority;
(...skipping 13 matching lines...) Expand all
282 transform_for_tile_priority); 295 transform_for_tile_priority);
283 if (frame.get()) 296 if (frame.get())
284 DidComposite(); 297 DidComposite();
285 return frame.Pass(); 298 return frame.Pass();
286 } 299 }
287 300
288 void BrowserViewRenderer::UpdateParentDrawConstraints() { 301 void BrowserViewRenderer::UpdateParentDrawConstraints() {
289 // Post an invalidate if the parent draw constraints are stale and there is 302 // Post an invalidate if the parent draw constraints are stale and there is
290 // no pending invalidate. 303 // no pending invalidate.
291 bool needs_force_invalidate = 304 bool needs_force_invalidate =
292 shared_renderer_state_->NeedsForceInvalidateOnNextDrawGL(); 305 shared_renderer_state_.NeedsForceInvalidateOnNextDrawGLOnUI();
293 if (needs_force_invalidate || 306 if (needs_force_invalidate ||
294 !parent_draw_constraints_.Equals( 307 !parent_draw_constraints_.Equals(
295 shared_renderer_state_->ParentDrawConstraints())) { 308 shared_renderer_state_.GetParentDrawConstraintsOnUI())) {
296 shared_renderer_state_->SetForceInvalidateOnNextDrawGL(false); 309 shared_renderer_state_.SetForceInvalidateOnNextDrawGLOnUI(false);
297 EnsureContinuousInvalidation(true, needs_force_invalidate); 310 EnsureContinuousInvalidation(true, needs_force_invalidate);
298 } 311 }
299 } 312 }
300 313
301 void BrowserViewRenderer::ReturnUnusedResource( 314 void BrowserViewRenderer::ReturnUnusedResource(
302 scoped_ptr<cc::CompositorFrame> frame) { 315 scoped_ptr<cc::CompositorFrame> frame) {
303 if (!frame.get()) 316 if (!frame.get())
304 return; 317 return;
305 318
306 cc::CompositorFrameAck frame_ack; 319 cc::CompositorFrameAck frame_ack;
307 cc::TransferableResource::ReturnResources( 320 cc::TransferableResource::ReturnResources(
308 frame->delegated_frame_data->resource_list, &frame_ack.resources); 321 frame->delegated_frame_data->resource_list, &frame_ack.resources);
309 if (compositor_ && !frame_ack.resources.empty()) 322 if (compositor_ && !frame_ack.resources.empty())
310 compositor_->ReturnResources(frame_ack); 323 compositor_->ReturnResources(frame_ack);
311 } 324 }
312 325
313 void BrowserViewRenderer::ReturnResourceFromParent() { 326 void BrowserViewRenderer::ReturnResourceFromParent() {
314 cc::CompositorFrameAck frame_ack; 327 cc::CompositorFrameAck frame_ack;
315 shared_renderer_state_->SwapReturnedResources(&frame_ack.resources); 328 shared_renderer_state_.SwapReturnedResourcesOnUI(&frame_ack.resources);
316 if (compositor_ && !frame_ack.resources.empty()) { 329 if (compositor_ && !frame_ack.resources.empty()) {
317 compositor_->ReturnResources(frame_ack); 330 compositor_->ReturnResources(frame_ack);
318 } 331 }
319 } 332 }
320 333
321 void BrowserViewRenderer::DidSkipCommitFrame() { 334 void BrowserViewRenderer::DidSkipCommitFrame() {
322 // Treat it the same way as skipping onDraw. 335 // Treat it the same way as skipping onDraw.
323 DidSkipCompositeInDraw(); 336 DidSkipCompositeInDraw();
324 } 337 }
325 338
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 } 451 }
439 452
440 void BrowserViewRenderer::OnDetachedFromWindow() { 453 void BrowserViewRenderer::OnDetachedFromWindow() {
441 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDetachedFromWindow"); 454 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDetachedFromWindow");
442 attached_to_window_ = false; 455 attached_to_window_ = false;
443 DCHECK(!hardware_enabled_); 456 DCHECK(!hardware_enabled_);
444 } 457 }
445 458
446 void BrowserViewRenderer::ReleaseHardware() { 459 void BrowserViewRenderer::ReleaseHardware() {
447 DCHECK(hardware_enabled_); 460 DCHECK(hardware_enabled_);
448 ReturnUnusedResource(shared_renderer_state_->PassCompositorFrame()); 461 // TODO(hush): do this in somewhere else. Either in hardware render or in
462 // shared renderer state.
463 ReturnUnusedResource(shared_renderer_state_.PassCompositorFrame());
449 ReturnResourceFromParent(); 464 ReturnResourceFromParent();
450 DCHECK(shared_renderer_state_->ReturnedResourcesEmpty());
hush (inactive) 2014/10/24 21:37:41 We don't need this because we have an equivalent D
boliu 2014/10/24 22:12:01 Keep this for now. It's not quite the same. What
hush (inactive) 2014/10/24 22:30:15 ok
451 465
452 if (compositor_) { 466 if (compositor_) {
453 compositor_->ReleaseHwDraw(); 467 compositor_->ReleaseHwDraw();
454 SynchronousCompositorMemoryPolicy zero_policy; 468 SynchronousCompositorMemoryPolicy zero_policy;
455 RequestMemoryPolicy(zero_policy); 469 RequestMemoryPolicy(zero_policy);
456 } 470 }
457 471
458 hardware_enabled_ = false; 472 hardware_enabled_ = false;
459 GlobalTileManager::GetInstance()->Remove(tile_manager_key_); 473 GlobalTileManager::GetInstance()->Remove(tile_manager_key_);
460 } 474 }
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
737 "compositor_needs_continuous_invalidate_", 751 "compositor_needs_continuous_invalidate_",
738 compositor_needs_continuous_invalidate_); 752 compositor_needs_continuous_invalidate_);
739 753
740 // This should only be called if OnDraw or DrawGL did not come in time, which 754 // This should only be called if OnDraw or DrawGL did not come in time, which
741 // means block_invalidates_ must still be true. 755 // means block_invalidates_ must still be true.
742 DCHECK(block_invalidates_); 756 DCHECK(block_invalidates_);
743 fallback_tick_pending_ = false; 757 fallback_tick_pending_ = false;
744 if (compositor_needs_continuous_invalidate_ && compositor_) { 758 if (compositor_needs_continuous_invalidate_ && compositor_) {
745 if (hardware_enabled_) { 759 if (hardware_enabled_) {
746 ReturnResourceFromParent(); 760 ReturnResourceFromParent();
747 ReturnUnusedResource(shared_renderer_state_->PassCompositorFrame()); 761 ReturnUnusedResource(shared_renderer_state_.PassCompositorFrame());
748 scoped_ptr<cc::CompositorFrame> frame = CompositeHw(); 762 scoped_ptr<cc::CompositorFrame> frame = CompositeHw();
749 if (frame.get()) { 763 if (frame.get()) {
750 shared_renderer_state_->SetCompositorFrame(frame.Pass(), true); 764 shared_renderer_state_.SetCompositorFrameOnUI(frame.Pass(), true);
751 } 765 }
752 } else { 766 } else {
753 ForceFakeCompositeSW(); 767 ForceFakeCompositeSW();
754 } 768 }
755 } else { 769 } else {
756 // Pretend we just composited to unblock further invalidates. 770 // Pretend we just composited to unblock further invalidates.
757 DidComposite(); 771 DidComposite();
758 } 772 }
759 } 773 }
760 774
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 base::StringAppendF(&str, 823 base::StringAppendF(&str,
810 "overscroll_rounding_error_: %s ", 824 "overscroll_rounding_error_: %s ",
811 overscroll_rounding_error_.ToString().c_str()); 825 overscroll_rounding_error_.ToString().c_str());
812 base::StringAppendF( 826 base::StringAppendF(
813 &str, "on_new_picture_enable: %d ", on_new_picture_enable_); 827 &str, "on_new_picture_enable: %d ", on_new_picture_enable_);
814 base::StringAppendF(&str, "clear_view: %d ", clear_view_); 828 base::StringAppendF(&str, "clear_view: %d ", clear_view_);
815 return str; 829 return str;
816 } 830 }
817 831
818 } // namespace android_webview 832 } // namespace android_webview
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698