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

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

Issue 25082006: [Android WebView] OnMemoryPressure to drop tile memory (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: indent Created 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/in_process_view_renderer.h" 5 #include "android_webview/browser/in_process_view_renderer.h"
6 6
7 #include <android/bitmap.h> 7 #include <android/bitmap.h>
8 8
9 #include "android_webview/browser/aw_gl_surface.h" 9 #include "android_webview/browser/aw_gl_surface.h"
10 #include "android_webview/browser/scoped_app_gl_state_restore.h" 10 #include "android_webview/browser/scoped_app_gl_state_restore.h"
11 #include "android_webview/common/aw_switches.h" 11 #include "android_webview/common/aw_switches.h"
12 #include "android_webview/public/browser/draw_gl.h" 12 #include "android_webview/public/browser/draw_gl.h"
13 #include "android_webview/public/browser/draw_sw.h" 13 #include "android_webview/public/browser/draw_sw.h"
14 #include "base/android/jni_android.h" 14 #include "base/android/jni_android.h"
15 #include "base/auto_reset.h" 15 #include "base/auto_reset.h"
16 #include "base/command_line.h" 16 #include "base/command_line.h"
17 #include "base/debug/trace_event.h" 17 #include "base/debug/trace_event.h"
18 #include "base/lazy_instance.h" 18 #include "base/lazy_instance.h"
19 #include "base/logging.h" 19 #include "base/logging.h"
20 #include "base/strings/string_number_conversions.h" 20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/stringprintf.h" 21 #include "base/strings/stringprintf.h"
22 #include "content/public/browser/android/synchronous_compositor.h"
23 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/web_contents.h" 23 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/content_switches.h" 24 #include "content/public/common/content_switches.h"
26 #include "gpu/command_buffer/service/in_process_command_buffer.h" 25 #include "gpu/command_buffer/service/in_process_command_buffer.h"
27 #include "third_party/skia/include/core/SkBitmap.h" 26 #include "third_party/skia/include/core/SkBitmap.h"
28 #include "third_party/skia/include/core/SkBitmapDevice.h" 27 #include "third_party/skia/include/core/SkBitmapDevice.h"
29 #include "third_party/skia/include/core/SkCanvas.h" 28 #include "third_party/skia/include/core/SkCanvas.h"
30 #include "third_party/skia/include/core/SkGraphics.h" 29 #include "third_party/skia/include/core/SkGraphics.h"
31 #include "third_party/skia/include/core/SkPicture.h" 30 #include "third_party/skia/include/core/SkPicture.h"
32 #include "third_party/skia/include/utils/SkCanvasStateUtils.h" 31 #include "third_party/skia/include/utils/SkCanvasStateUtils.h"
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 std::stringstream size; 308 std::stringstream size;
310 size << default_tile_size; 309 size << default_tile_size;
311 cl->AppendSwitchASCII(switches::kDefaultTileWidth, size.str()); 310 cl->AppendSwitchASCII(switches::kDefaultTileWidth, size.str());
312 cl->AppendSwitchASCII(switches::kDefaultTileHeight, size.str()); 311 cl->AppendSwitchASCII(switches::kDefaultTileHeight, size.str());
313 } 312 }
314 313
315 bool InProcessViewRenderer::RequestProcessGL() { 314 bool InProcessViewRenderer::RequestProcessGL() {
316 return client_->RequestDrawGL(NULL); 315 return client_->RequestDrawGL(NULL);
317 } 316 }
318 317
318 void InProcessViewRenderer::TrimMemory(int level) {
319 // Constants from Android ComponentCallbacks2.
320 enum {
321 TRIM_MEMORY_RUNNING_LOW = 10,
322 TRIM_MEMORY_UI_HIDDEN = 20,
323 TRIM_MEMORY_BACKGROUND = 40,
324 };
325
326 // Not urgent enough. TRIM_MEMORY_UI_HIDDEN is treated specially because
327 // it does not indicate memory pressure, but merely that the app is
328 // backgrounded.
329 if (level < TRIM_MEMORY_RUNNING_LOW || level == TRIM_MEMORY_UI_HIDDEN)
330 return;
331
332 // Nothing to drop.
333 if (!attached_to_window_ || !hardware_initialized_ || !compositor_)
334 return;
335
336 // Do not release resources on view we expect to get DrawGL soon.
337 if (level < TRIM_MEMORY_BACKGROUND) {
338 client_->UpdateGlobalVisibleRect();
339 if (view_visible_ && window_visible_ &&
340 !cached_global_visible_rect_.IsEmpty()) {
341 return;
342 }
343 }
344
345 if (!eglGetCurrentContext()) {
346 NOTREACHED();
347 return;
348 }
349
350 // Just set the memory limit to 0 and drop all tiles. This will be reset to
351 // normal levels in the next DrawGL call.
352 content::SynchronousCompositorMemoryPolicy policy;
353 policy.bytes_limit = 0;
354 policy.num_resources_limit = 0;
355 if (memory_policy_ == policy)
356 return;
357
358 TRACE_EVENT0("android_webview", "InProcessViewRenderer::TrimMemory");
359 ScopedAppGLStateRestore state_restore(
360 ScopedAppGLStateRestore::MODE_RESOURCE_MANAGEMENT);
361 gpu::InProcessCommandBuffer::ProcessGpuWorkOnCurrentThread();
362 ScopedAllowGL allow_gl;
363
364 SetMemoryPolicy(policy);
365 ForceFakeCompositeSW();
366 }
367
368 void InProcessViewRenderer::SetMemoryPolicy(
369 content::SynchronousCompositorMemoryPolicy& new_policy) {
370 if (memory_policy_ == new_policy)
371 return;
372
373 memory_policy_ = new_policy;
374 compositor_->SetMemoryPolicy(memory_policy_);
375 }
376
319 void InProcessViewRenderer::UpdateCachedGlobalVisibleRect() { 377 void InProcessViewRenderer::UpdateCachedGlobalVisibleRect() {
320 client_->UpdateGlobalVisibleRect(); 378 client_->UpdateGlobalVisibleRect();
321 } 379 }
322 380
323 bool InProcessViewRenderer::OnDraw(jobject java_canvas, 381 bool InProcessViewRenderer::OnDraw(jobject java_canvas,
324 bool is_hardware_canvas, 382 bool is_hardware_canvas,
325 const gfx::Vector2d& scroll, 383 const gfx::Vector2d& scroll,
326 const gfx::Rect& clip) { 384 const gfx::Rect& clip) {
327 scroll_at_start_of_frame_ = scroll; 385 scroll_at_start_of_frame_ = scroll;
328 if (is_hardware_canvas && attached_to_window_ && HardwareEnabled()) { 386 if (is_hardware_canvas && attached_to_window_ && HardwareEnabled()) {
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 // Update memory budget. This will no-op in compositor if the policy has not 474 // Update memory budget. This will no-op in compositor if the policy has not
417 // changed since last draw. 475 // changed since last draw.
418 content::SynchronousCompositorMemoryPolicy policy; 476 content::SynchronousCompositorMemoryPolicy policy;
419 policy.bytes_limit = g_memory_multiplier * kBytesPerPixel * 477 policy.bytes_limit = g_memory_multiplier * kBytesPerPixel *
420 cached_global_visible_rect_.width() * 478 cached_global_visible_rect_.width() *
421 cached_global_visible_rect_.height(); 479 cached_global_visible_rect_.height();
422 // Round up to a multiple of kMemoryAllocationStep. 480 // Round up to a multiple of kMemoryAllocationStep.
423 policy.bytes_limit = 481 policy.bytes_limit =
424 (policy.bytes_limit / kMemoryAllocationStep + 1) * kMemoryAllocationStep; 482 (policy.bytes_limit / kMemoryAllocationStep + 1) * kMemoryAllocationStep;
425 policy.num_resources_limit = kMaxNumTilesToFillDisplay * g_memory_multiplier; 483 policy.num_resources_limit = kMaxNumTilesToFillDisplay * g_memory_multiplier;
426 compositor_->SetMemoryPolicy(policy); 484 SetMemoryPolicy(policy);
427 485
428 DCHECK(gl_surface_); 486 DCHECK(gl_surface_);
429 gl_surface_->SetBackingFrameBufferObject( 487 gl_surface_->SetBackingFrameBufferObject(
430 state_restore.framebuffer_binding_ext()); 488 state_restore.framebuffer_binding_ext());
431 489
432 gfx::Transform transform; 490 gfx::Transform transform;
433 transform.matrix().setColMajorf(draw_info->transform); 491 transform.matrix().setColMajorf(draw_info->transform);
434 transform.Translate(scroll_at_start_of_frame_.x(), 492 transform.Translate(scroll_at_start_of_frame_.x(),
435 scroll_at_start_of_frame_.y()); 493 scroll_at_start_of_frame_.y());
436 gfx::Rect clip_rect(draw_info->clip_left, 494 gfx::Rect clip_rect(draw_info->clip_left,
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 689
632 void InProcessViewRenderer::OnDetachedFromWindow() { 690 void InProcessViewRenderer::OnDetachedFromWindow() {
633 TRACE_EVENT0("android_webview", 691 TRACE_EVENT0("android_webview",
634 "InProcessViewRenderer::OnDetachedFromWindow"); 692 "InProcessViewRenderer::OnDetachedFromWindow");
635 693
636 NoLongerExpectsDrawGL(); 694 NoLongerExpectsDrawGL();
637 if (hardware_initialized_) { 695 if (hardware_initialized_) {
638 DCHECK(compositor_); 696 DCHECK(compositor_);
639 697
640 ScopedAppGLStateRestore state_restore( 698 ScopedAppGLStateRestore state_restore(
641 ScopedAppGLStateRestore::MODE_DETACH_FROM_WINDOW); 699 ScopedAppGLStateRestore::MODE_RESOURCE_MANAGEMENT);
642 gpu::InProcessCommandBuffer::ProcessGpuWorkOnCurrentThread(); 700 gpu::InProcessCommandBuffer::ProcessGpuWorkOnCurrentThread();
643 ScopedAllowGL allow_gl; 701 ScopedAllowGL allow_gl;
644 compositor_->ReleaseHwDraw(); 702 compositor_->ReleaseHwDraw();
645 hardware_initialized_ = false; 703 hardware_initialized_ = false;
646 } 704 }
647 705
648 gl_surface_ = NULL; 706 gl_surface_ = NULL;
649 attached_to_window_ = false; 707 attached_to_window_ = false;
650 } 708 }
651 709
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
862 920
863 void InProcessViewRenderer::FallbackTickFired() { 921 void InProcessViewRenderer::FallbackTickFired() {
864 TRACE_EVENT1("android_webview", 922 TRACE_EVENT1("android_webview",
865 "InProcessViewRenderer::FallbackTickFired", 923 "InProcessViewRenderer::FallbackTickFired",
866 "compositor_needs_continuous_invalidate_", 924 "compositor_needs_continuous_invalidate_",
867 compositor_needs_continuous_invalidate_); 925 compositor_needs_continuous_invalidate_);
868 926
869 // This should only be called if OnDraw or DrawGL did not come in time, which 927 // This should only be called if OnDraw or DrawGL did not come in time, which
870 // means block_invalidates_ must still be true. 928 // means block_invalidates_ must still be true.
871 DCHECK(block_invalidates_); 929 DCHECK(block_invalidates_);
872 if (compositor_needs_continuous_invalidate_ && compositor_) { 930 if (compositor_needs_continuous_invalidate_ && compositor_)
873 SkBitmapDevice device(SkBitmap::kARGB_8888_Config, 1, 1); 931 ForceFakeCompositeSW();
874 SkCanvas canvas(&device); 932 }
875 CompositeSW(&canvas); 933
876 } 934 void InProcessViewRenderer::ForceFakeCompositeSW() {
935 DCHECK(compositor_);
936 SkBitmapDevice device(SkBitmap::kARGB_8888_Config, 1, 1);
937 SkCanvas canvas(&device);
938 CompositeSW(&canvas);
877 } 939 }
878 940
879 bool InProcessViewRenderer::CompositeSW(SkCanvas* canvas) { 941 bool InProcessViewRenderer::CompositeSW(SkCanvas* canvas) {
880 DCHECK(compositor_); 942 DCHECK(compositor_);
881 943
882 fallback_tick_.Cancel(); 944 fallback_tick_.Cancel();
883 block_invalidates_ = true; 945 block_invalidates_ = true;
884 bool result = compositor_->DemandDrawSw(canvas); 946 bool result = compositor_->DemandDrawSw(canvas);
885 block_invalidates_ = false; 947 block_invalidates_ = false;
886 EnsureContinuousInvalidation(NULL, false); 948 EnsureContinuousInvalidation(NULL, false);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
925 base::StringAppendF(&str, 987 base::StringAppendF(&str,
926 "surface width height: [%d %d] ", 988 "surface width height: [%d %d] ",
927 draw_info->width, 989 draw_info->width,
928 draw_info->height); 990 draw_info->height);
929 base::StringAppendF(&str, "is_layer: %d ", draw_info->is_layer); 991 base::StringAppendF(&str, "is_layer: %d ", draw_info->is_layer);
930 } 992 }
931 return str; 993 return str;
932 } 994 }
933 995
934 } // namespace android_webview 996 } // namespace android_webview
OLDNEW
« no previous file with comments | « android_webview/browser/in_process_view_renderer.h ('k') | android_webview/browser/scoped_app_gl_state_restore.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698