OLD | NEW |
---|---|
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 "android_webview/public/browser/draw_gl.h" | |
11 #include "base/android/jni_android.h" | |
12 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
13 #include "base/command_line.h" | 11 #include "base/command_line.h" |
14 #include "base/debug/trace_event.h" | 12 #include "base/debug/trace_event_argument.h" |
15 #include "base/json/json_writer.h" | |
16 #include "base/logging.h" | 13 #include "base/logging.h" |
17 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
19 #include "cc/output/compositor_frame.h" | 16 #include "cc/output/compositor_frame.h" |
20 #include "content/public/browser/browser_thread.h" | |
21 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
22 #include "content/public/common/content_switches.h" | 18 #include "content/public/common/content_switches.h" |
23 #include "gpu/command_buffer/service/gpu_switches.h" | 19 #include "gpu/command_buffer/service/gpu_switches.h" |
24 #include "third_party/skia/include/core/SkBitmap.h" | 20 #include "third_party/skia/include/core/SkBitmap.h" |
25 #include "third_party/skia/include/core/SkCanvas.h" | 21 #include "third_party/skia/include/core/SkCanvas.h" |
26 #include "third_party/skia/include/core/SkPicture.h" | 22 #include "third_party/skia/include/core/SkPicture.h" |
27 #include "third_party/skia/include/core/SkPictureRecorder.h" | 23 #include "third_party/skia/include/core/SkPictureRecorder.h" |
28 #include "ui/gfx/vector2d_conversions.h" | 24 #include "ui/gfx/vector2d_conversions.h" |
29 | 25 |
30 using base::android::AttachCurrentThread; | |
31 using base::android::JavaRef; | |
32 using base::android::ScopedJavaLocalRef; | |
33 using content::BrowserThread; | |
34 using content::SynchronousCompositorMemoryPolicy; | 26 using content::SynchronousCompositorMemoryPolicy; |
35 | 27 |
36 namespace android_webview { | 28 namespace android_webview { |
37 | 29 |
38 namespace { | 30 namespace { |
39 | 31 |
40 const int64 kFallbackTickTimeoutInMilliseconds = 100; | 32 const int64 kFallbackTickTimeoutInMilliseconds = 100; |
41 | 33 |
42 // Used to calculate memory allocation. Determined experimentally. | 34 // Used to calculate memory allocation. Determined experimentally. |
43 const size_t kMemoryMultiplier = 20; | 35 const size_t kMemoryMultiplier = 20; |
44 const size_t kBytesPerPixel = 4; | 36 const size_t kBytesPerPixel = 4; |
45 const size_t kMemoryAllocationStep = 5 * 1024 * 1024; | 37 const size_t kMemoryAllocationStep = 5 * 1024 * 1024; |
46 uint64 g_memory_override_in_bytes = 0u; | 38 uint64 g_memory_override_in_bytes = 0u; |
47 | 39 |
48 // Used to calculate tile allocation. Determined experimentally. | 40 // Used to calculate tile allocation. Determined experimentally. |
49 const size_t kTileMultiplier = 12; | 41 const size_t kTileMultiplier = 12; |
50 const size_t kTileAllocationStep = 20; | 42 const size_t kTileAllocationStep = 20; |
51 // This will be set by static function CalculateTileMemoryPolicy() during init. | 43 // Use chrome's default tile size, which varies from 256 to 512. |
52 // See AwMainDelegate::BasicStartupComplete. | 44 // Be conservative here and use the smallest tile size possible. |
53 size_t g_tile_area; | 45 const size_t g_tile_area = 256 * 256; |
hush (inactive)
2014/10/14 01:01:10
since this is a const, please rename it to kTileAr
boliu
2014/10/14 01:04:20
Done.
| |
54 | |
55 class TracedValue : public base::debug::ConvertableToTraceFormat { | |
56 public: | |
57 explicit TracedValue(base::Value* value) : value_(value) {} | |
58 static scoped_refptr<base::debug::ConvertableToTraceFormat> FromValue( | |
59 base::Value* value) { | |
60 return scoped_refptr<base::debug::ConvertableToTraceFormat>( | |
61 new TracedValue(value)); | |
62 } | |
63 virtual void AppendAsTraceFormat(std::string* out) const override { | |
64 std::string tmp; | |
65 base::JSONWriter::Write(value_.get(), &tmp); | |
66 *out += tmp; | |
67 } | |
68 | |
69 private: | |
70 virtual ~TracedValue() {} | |
71 scoped_ptr<base::Value> value_; | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(TracedValue); | |
74 }; | |
75 | 46 |
76 } // namespace | 47 } // namespace |
77 | 48 |
78 // static | 49 // static |
79 void BrowserViewRenderer::CalculateTileMemoryPolicy() { | 50 void BrowserViewRenderer::CalculateTileMemoryPolicy() { |
80 CommandLine* cl = CommandLine::ForCurrentProcess(); | 51 CommandLine* cl = CommandLine::ForCurrentProcess(); |
81 | 52 |
82 // If the value was overridden on the command line, use the specified value. | 53 // If the value was overridden on the command line, use the specified value. |
83 bool client_hard_limit_bytes_overridden = | 54 bool client_hard_limit_bytes_overridden = |
84 cl->HasSwitch(switches::kForceGpuMemAvailableMb); | 55 cl->HasSwitch(switches::kForceGpuMemAvailableMb); |
85 if (client_hard_limit_bytes_overridden) { | 56 if (client_hard_limit_bytes_overridden) { |
86 base::StringToUint64( | 57 base::StringToUint64( |
87 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 58 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
88 switches::kForceGpuMemAvailableMb), | 59 switches::kForceGpuMemAvailableMb), |
89 &g_memory_override_in_bytes); | 60 &g_memory_override_in_bytes); |
90 g_memory_override_in_bytes *= 1024 * 1024; | 61 g_memory_override_in_bytes *= 1024 * 1024; |
91 } | 62 } |
92 | 63 |
93 // Use chrome's default tile size, which varies from 256 to 512. | |
94 // Be conservative here and use the smallest tile size possible. | |
95 g_tile_area = 256 * 256; | |
96 | |
97 // Also use a high tile limit since there are no file descriptor issues. | 64 // Also use a high tile limit since there are no file descriptor issues. |
98 GlobalTileManager::GetInstance()->SetTileLimit(1000); | 65 GlobalTileManager::GetInstance()->SetTileLimit(1000); |
99 } | 66 } |
100 | 67 |
101 BrowserViewRenderer::BrowserViewRenderer( | 68 BrowserViewRenderer::BrowserViewRenderer( |
102 BrowserViewRendererClient* client, | 69 BrowserViewRendererClient* client, |
103 SharedRendererState* shared_renderer_state, | 70 SharedRendererState* shared_renderer_state, |
104 content::WebContents* web_contents, | 71 content::WebContents* web_contents, |
105 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) | 72 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) |
106 : client_(client), | 73 : client_(client), |
(...skipping 23 matching lines...) Expand all Loading... | |
130 } | 97 } |
131 | 98 |
132 BrowserViewRenderer::~BrowserViewRenderer() { | 99 BrowserViewRenderer::~BrowserViewRenderer() { |
133 content::SynchronousCompositor::SetClientForWebContents(web_contents_, NULL); | 100 content::SynchronousCompositor::SetClientForWebContents(web_contents_, NULL); |
134 // OnDetachedFromWindow should be called before the destructor, so the memory | 101 // OnDetachedFromWindow should be called before the destructor, so the memory |
135 // policy should have already been updated. | 102 // policy should have already been updated. |
136 } | 103 } |
137 | 104 |
138 // This function updates the resource allocation in GlobalTileManager. | 105 // This function updates the resource allocation in GlobalTileManager. |
139 void BrowserViewRenderer::TrimMemory(const int level, const bool visible) { | 106 void BrowserViewRenderer::TrimMemory(const int level, const bool visible) { |
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 107 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
141 // Constants from Android ComponentCallbacks2. | 108 // Constants from Android ComponentCallbacks2. |
142 enum { | 109 enum { |
143 TRIM_MEMORY_RUNNING_LOW = 10, | 110 TRIM_MEMORY_RUNNING_LOW = 10, |
144 TRIM_MEMORY_UI_HIDDEN = 20, | 111 TRIM_MEMORY_UI_HIDDEN = 20, |
145 TRIM_MEMORY_BACKGROUND = 40, | 112 TRIM_MEMORY_BACKGROUND = 40, |
146 }; | 113 }; |
147 | 114 |
148 // Not urgent enough. TRIM_MEMORY_UI_HIDDEN is treated specially because | 115 // Not urgent enough. TRIM_MEMORY_UI_HIDDEN is treated specially because |
149 // it does not indicate memory pressure, but merely that the app is | 116 // it does not indicate memory pressure, but merely that the app is |
150 // backgrounded. | 117 // backgrounded. |
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
612 const gfx::Vector2dF& max_scroll_offset_dip, | 579 const gfx::Vector2dF& max_scroll_offset_dip, |
613 const gfx::SizeF& scrollable_size_dip, | 580 const gfx::SizeF& scrollable_size_dip, |
614 float page_scale_factor, | 581 float page_scale_factor, |
615 float min_page_scale_factor, | 582 float min_page_scale_factor, |
616 float max_page_scale_factor) { | 583 float max_page_scale_factor) { |
617 TRACE_EVENT_INSTANT1( | 584 TRACE_EVENT_INSTANT1( |
618 "android_webview", | 585 "android_webview", |
619 "BrowserViewRenderer::UpdateRootLayerState", | 586 "BrowserViewRenderer::UpdateRootLayerState", |
620 TRACE_EVENT_SCOPE_THREAD, | 587 TRACE_EVENT_SCOPE_THREAD, |
621 "state", | 588 "state", |
622 TracedValue::FromValue( | 589 RootLayerStateAsValue(total_scroll_offset_dip, scrollable_size_dip)); |
623 RootLayerStateAsValue(total_scroll_offset_dip, scrollable_size_dip) | |
624 .release())); | |
625 | 590 |
626 DCHECK_GT(dip_scale_, 0); | 591 DCHECK_GT(dip_scale_, 0); |
627 | 592 |
628 max_scroll_offset_dip_ = max_scroll_offset_dip; | 593 max_scroll_offset_dip_ = max_scroll_offset_dip; |
629 DCHECK_LE(0, max_scroll_offset_dip_.x()); | 594 DCHECK_LE(0, max_scroll_offset_dip_.x()); |
630 DCHECK_LE(0, max_scroll_offset_dip_.y()); | 595 DCHECK_LE(0, max_scroll_offset_dip_.y()); |
631 | 596 |
632 page_scale_factor_ = page_scale_factor; | 597 page_scale_factor_ = page_scale_factor; |
633 DCHECK_GT(page_scale_factor_, 0); | 598 DCHECK_GT(page_scale_factor_, 0); |
634 | 599 |
635 client_->UpdateScrollState(max_scroll_offset(), | 600 client_->UpdateScrollState(max_scroll_offset(), |
636 scrollable_size_dip, | 601 scrollable_size_dip, |
637 page_scale_factor, | 602 page_scale_factor, |
638 min_page_scale_factor, | 603 min_page_scale_factor, |
639 max_page_scale_factor); | 604 max_page_scale_factor); |
640 SetTotalRootLayerScrollOffset(total_scroll_offset_dip); | 605 SetTotalRootLayerScrollOffset(total_scroll_offset_dip); |
641 } | 606 } |
642 | 607 |
643 scoped_ptr<base::Value> BrowserViewRenderer::RootLayerStateAsValue( | 608 scoped_refptr<base::debug::ConvertableToTraceFormat> |
609 BrowserViewRenderer::RootLayerStateAsValue( | |
644 const gfx::Vector2dF& total_scroll_offset_dip, | 610 const gfx::Vector2dF& total_scroll_offset_dip, |
645 const gfx::SizeF& scrollable_size_dip) { | 611 const gfx::SizeF& scrollable_size_dip) { |
646 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue); | 612 scoped_refptr<base::debug::TracedValue> state = |
613 new base::debug::TracedValue(); | |
647 | 614 |
648 state->SetDouble("total_scroll_offset_dip.x", total_scroll_offset_dip.x()); | 615 state->SetDouble("total_scroll_offset_dip.x", total_scroll_offset_dip.x()); |
649 state->SetDouble("total_scroll_offset_dip.y", total_scroll_offset_dip.y()); | 616 state->SetDouble("total_scroll_offset_dip.y", total_scroll_offset_dip.y()); |
650 | 617 |
651 state->SetDouble("max_scroll_offset_dip.x", max_scroll_offset_dip_.x()); | 618 state->SetDouble("max_scroll_offset_dip.x", max_scroll_offset_dip_.x()); |
652 state->SetDouble("max_scroll_offset_dip.y", max_scroll_offset_dip_.y()); | 619 state->SetDouble("max_scroll_offset_dip.y", max_scroll_offset_dip_.y()); |
653 | 620 |
654 state->SetDouble("scrollable_size_dip.width", scrollable_size_dip.width()); | 621 state->SetDouble("scrollable_size_dip.width", scrollable_size_dip.width()); |
655 state->SetDouble("scrollable_size_dip.height", scrollable_size_dip.height()); | 622 state->SetDouble("scrollable_size_dip.height", scrollable_size_dip.height()); |
656 | 623 |
657 state->SetDouble("page_scale_factor", page_scale_factor_); | 624 state->SetDouble("page_scale_factor", page_scale_factor_); |
658 return state.PassAs<base::Value>(); | 625 return state; |
659 } | 626 } |
660 | 627 |
661 void BrowserViewRenderer::DidOverscroll(gfx::Vector2dF accumulated_overscroll, | 628 void BrowserViewRenderer::DidOverscroll(gfx::Vector2dF accumulated_overscroll, |
662 gfx::Vector2dF latest_overscroll_delta, | 629 gfx::Vector2dF latest_overscroll_delta, |
663 gfx::Vector2dF current_fling_velocity) { | 630 gfx::Vector2dF current_fling_velocity) { |
664 const float physical_pixel_scale = dip_scale_ * page_scale_factor_; | 631 const float physical_pixel_scale = dip_scale_ * page_scale_factor_; |
665 if (accumulated_overscroll == latest_overscroll_delta) | 632 if (accumulated_overscroll == latest_overscroll_delta) |
666 overscroll_rounding_error_ = gfx::Vector2dF(); | 633 overscroll_rounding_error_ = gfx::Vector2dF(); |
667 gfx::Vector2dF scaled_overscroll_delta = | 634 gfx::Vector2dF scaled_overscroll_delta = |
668 gfx::ScaleVector2d(latest_overscroll_delta, physical_pixel_scale); | 635 gfx::ScaleVector2d(latest_overscroll_delta, physical_pixel_scale); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
773 fallback_tick_fired_.Cancel(); | 740 fallback_tick_fired_.Cancel(); |
774 fallback_tick_pending_ = false; | 741 fallback_tick_pending_ = false; |
775 EnsureContinuousInvalidation(false, false); | 742 EnsureContinuousInvalidation(false, false); |
776 } | 743 } |
777 | 744 |
778 void BrowserViewRenderer::SkippedCompositeInDraw() { | 745 void BrowserViewRenderer::SkippedCompositeInDraw() { |
779 block_invalidates_ = false; | 746 block_invalidates_ = false; |
780 EnsureContinuousInvalidation(false, true); | 747 EnsureContinuousInvalidation(false, true); |
781 } | 748 } |
782 | 749 |
783 std::string BrowserViewRenderer::ToString(AwDrawGLInfo* draw_info) const { | 750 std::string BrowserViewRenderer::ToString() const { |
784 std::string str; | 751 std::string str; |
785 base::StringAppendF(&str, "is_paused: %d ", is_paused_); | 752 base::StringAppendF(&str, "is_paused: %d ", is_paused_); |
786 base::StringAppendF(&str, "view_visible: %d ", view_visible_); | 753 base::StringAppendF(&str, "view_visible: %d ", view_visible_); |
787 base::StringAppendF(&str, "window_visible: %d ", window_visible_); | 754 base::StringAppendF(&str, "window_visible: %d ", window_visible_); |
788 base::StringAppendF(&str, "dip_scale: %f ", dip_scale_); | 755 base::StringAppendF(&str, "dip_scale: %f ", dip_scale_); |
789 base::StringAppendF(&str, "page_scale_factor: %f ", page_scale_factor_); | 756 base::StringAppendF(&str, "page_scale_factor: %f ", page_scale_factor_); |
790 base::StringAppendF(&str, | 757 base::StringAppendF(&str, |
791 "compositor_needs_continuous_invalidate: %d ", | 758 "compositor_needs_continuous_invalidate: %d ", |
792 compositor_needs_continuous_invalidate_); | 759 compositor_needs_continuous_invalidate_); |
793 base::StringAppendF(&str, "block_invalidates: %d ", block_invalidates_); | 760 base::StringAppendF(&str, "block_invalidates: %d ", block_invalidates_); |
794 base::StringAppendF(&str, "view width height: [%d %d] ", width_, height_); | 761 base::StringAppendF(&str, "view width height: [%d %d] ", width_, height_); |
795 base::StringAppendF(&str, "attached_to_window: %d ", attached_to_window_); | 762 base::StringAppendF(&str, "attached_to_window: %d ", attached_to_window_); |
796 base::StringAppendF(&str, | 763 base::StringAppendF(&str, |
797 "global visible rect: %s ", | 764 "global visible rect: %s ", |
798 last_on_draw_global_visible_rect_.ToString().c_str()); | 765 last_on_draw_global_visible_rect_.ToString().c_str()); |
799 base::StringAppendF( | 766 base::StringAppendF( |
800 &str, "scroll_offset_dip: %s ", scroll_offset_dip_.ToString().c_str()); | 767 &str, "scroll_offset_dip: %s ", scroll_offset_dip_.ToString().c_str()); |
801 base::StringAppendF(&str, | 768 base::StringAppendF(&str, |
802 "overscroll_rounding_error_: %s ", | 769 "overscroll_rounding_error_: %s ", |
803 overscroll_rounding_error_.ToString().c_str()); | 770 overscroll_rounding_error_.ToString().c_str()); |
804 base::StringAppendF( | 771 base::StringAppendF( |
805 &str, "on_new_picture_enable: %d ", on_new_picture_enable_); | 772 &str, "on_new_picture_enable: %d ", on_new_picture_enable_); |
806 base::StringAppendF(&str, "clear_view: %d ", clear_view_); | 773 base::StringAppendF(&str, "clear_view: %d ", clear_view_); |
807 if (draw_info) { | |
808 base::StringAppendF(&str, | |
809 "clip left top right bottom: [%d %d %d %d] ", | |
810 draw_info->clip_left, | |
811 draw_info->clip_top, | |
812 draw_info->clip_right, | |
813 draw_info->clip_bottom); | |
814 base::StringAppendF(&str, | |
815 "surface width height: [%d %d] ", | |
816 draw_info->width, | |
817 draw_info->height); | |
818 base::StringAppendF(&str, "is_layer: %d ", draw_info->is_layer); | |
819 } | |
820 return str; | 774 return str; |
821 } | 775 } |
822 | 776 |
823 } // namespace android_webview | 777 } // namespace android_webview |
OLD | NEW |