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/public/browser/draw_gl.h" | 9 #include "android_webview/public/browser/draw_gl.h" |
10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
11 #include "base/auto_reset.h" | 11 #include "base/auto_reset.h" |
| 12 #include "base/command_line.h" |
12 #include "base/debug/trace_event.h" | 13 #include "base/debug/trace_event.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
15 #include "content/public/browser/android/synchronous_compositor.h" | 17 #include "content/public/browser/android/synchronous_compositor.h" |
| 18 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/common/content_switches.h" |
17 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
18 #include "third_party/skia/include/core/SkCanvas.h" | 22 #include "third_party/skia/include/core/SkCanvas.h" |
19 #include "third_party/skia/include/core/SkPicture.h" | 23 #include "third_party/skia/include/core/SkPicture.h" |
20 #include "third_party/skia/include/core/SkPictureRecorder.h" | 24 #include "third_party/skia/include/core/SkPictureRecorder.h" |
21 #include "ui/gfx/vector2d_conversions.h" | 25 #include "ui/gfx/vector2d_conversions.h" |
22 | 26 |
23 using base::android::AttachCurrentThread; | 27 using base::android::AttachCurrentThread; |
24 using base::android::JavaRef; | 28 using base::android::JavaRef; |
25 using base::android::ScopedJavaLocalRef; | 29 using base::android::ScopedJavaLocalRef; |
| 30 using content::BrowserThread; |
| 31 using content::SynchronousCompositorMemoryPolicy; |
26 | 32 |
27 namespace android_webview { | 33 namespace android_webview { |
28 | 34 |
29 namespace { | 35 namespace { |
30 | 36 |
31 const int64 kFallbackTickTimeoutInMilliseconds = 20; | 37 const int64 kFallbackTickTimeoutInMilliseconds = 20; |
32 | 38 |
| 39 // Used to calculate memory allocation. Determined experimentally. |
| 40 const size_t kMemoryMultiplier = 10; |
| 41 const size_t kBytesPerPixel = 4; |
| 42 const size_t kMemoryAllocationStep = 5 * 1024 * 1024; |
| 43 |
| 44 // Used to calculate tile allocation. Determined experimentally. |
| 45 const size_t kTileMultiplier = 12; |
| 46 const size_t kTileAllocationStep = 20; |
| 47 // This will be set by static function CalculateTileMemoryPolicy() during init. |
| 48 // See AwMainDelegate::BasicStartupComplete. |
| 49 size_t g_tile_area; |
| 50 |
33 class AutoResetWithLock { | 51 class AutoResetWithLock { |
34 public: | 52 public: |
35 AutoResetWithLock(gfx::Vector2dF* scoped_variable, | 53 AutoResetWithLock(gfx::Vector2dF* scoped_variable, |
36 gfx::Vector2dF new_value, | 54 gfx::Vector2dF new_value, |
37 base::Lock& lock) | 55 base::Lock& lock) |
38 : scoped_variable_(scoped_variable), | 56 : scoped_variable_(scoped_variable), |
39 original_value_(*scoped_variable), | 57 original_value_(*scoped_variable), |
40 lock_(lock) { | 58 lock_(lock) { |
41 base::AutoLock auto_lock(lock_); | 59 base::AutoLock auto_lock(lock_); |
42 *scoped_variable_ = new_value; | 60 *scoped_variable_ = new_value; |
43 } | 61 } |
44 | 62 |
45 ~AutoResetWithLock() { | 63 ~AutoResetWithLock() { |
46 base::AutoLock auto_lock(lock_); | 64 base::AutoLock auto_lock(lock_); |
47 *scoped_variable_ = original_value_; | 65 *scoped_variable_ = original_value_; |
48 } | 66 } |
49 | 67 |
50 private: | 68 private: |
51 gfx::Vector2dF* scoped_variable_; | 69 gfx::Vector2dF* scoped_variable_; |
52 gfx::Vector2dF original_value_; | 70 gfx::Vector2dF original_value_; |
53 base::Lock& lock_; | 71 base::Lock& lock_; |
54 | 72 |
55 DISALLOW_COPY_AND_ASSIGN(AutoResetWithLock); | 73 DISALLOW_COPY_AND_ASSIGN(AutoResetWithLock); |
56 }; | 74 }; |
57 | 75 |
58 } // namespace | 76 } // namespace |
59 | 77 |
| 78 // static |
| 79 void BrowserViewRenderer::CalculateTileMemoryPolicy() { |
| 80 CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 81 const char kDefaultTileSize[] = "384"; |
| 82 |
| 83 if (!cl->HasSwitch(switches::kDefaultTileWidth)) |
| 84 cl->AppendSwitchASCII(switches::kDefaultTileWidth, kDefaultTileSize); |
| 85 |
| 86 if (!cl->HasSwitch(switches::kDefaultTileHeight)) |
| 87 cl->AppendSwitchASCII(switches::kDefaultTileHeight, kDefaultTileSize); |
| 88 |
| 89 size_t tile_size; |
| 90 base::StringToSizeT(kDefaultTileSize, &tile_size); |
| 91 g_tile_area = tile_size * tile_size; |
| 92 } |
| 93 |
60 BrowserViewRenderer::BrowserViewRenderer( | 94 BrowserViewRenderer::BrowserViewRenderer( |
61 BrowserViewRendererClient* client, | 95 BrowserViewRendererClient* client, |
62 SharedRendererState* shared_renderer_state, | 96 SharedRendererState* shared_renderer_state, |
63 content::WebContents* web_contents, | 97 content::WebContents* web_contents, |
64 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) | 98 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) |
65 : client_(client), | 99 : client_(client), |
66 shared_renderer_state_(shared_renderer_state), | 100 shared_renderer_state_(shared_renderer_state), |
67 web_contents_(web_contents), | 101 web_contents_(web_contents), |
68 weak_factory_on_ui_thread_(this), | 102 weak_factory_on_ui_thread_(this), |
69 ui_thread_weak_ptr_(weak_factory_on_ui_thread_.GetWeakPtr()), | 103 ui_thread_weak_ptr_(weak_factory_on_ui_thread_.GetWeakPtr()), |
(...skipping 13 matching lines...) Expand all Loading... |
83 height_(0) { | 117 height_(0) { |
84 CHECK(web_contents_); | 118 CHECK(web_contents_); |
85 content::SynchronousCompositor::SetClientForWebContents(web_contents_, this); | 119 content::SynchronousCompositor::SetClientForWebContents(web_contents_, this); |
86 | 120 |
87 // Currently the logic in this class relies on |has_compositor_| remaining | 121 // Currently the logic in this class relies on |has_compositor_| remaining |
88 // false until the DidInitializeCompositor() call, hence it is not set here. | 122 // false until the DidInitializeCompositor() call, hence it is not set here. |
89 } | 123 } |
90 | 124 |
91 BrowserViewRenderer::~BrowserViewRenderer() { | 125 BrowserViewRenderer::~BrowserViewRenderer() { |
92 content::SynchronousCompositor::SetClientForWebContents(web_contents_, NULL); | 126 content::SynchronousCompositor::SetClientForWebContents(web_contents_, NULL); |
| 127 // OnDetachedFromWindow should be called before the destructor, so the memory |
| 128 // policy should have already been updated. |
| 129 } |
| 130 |
| 131 // This function updates the cached memory policy in shared renderer state, as |
| 132 // well as the tile resource allocation in GlobalTileManager. |
| 133 void BrowserViewRenderer::TrimMemory(const int level, const bool visible) { |
| 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 135 // Constants from Android ComponentCallbacks2. |
| 136 enum { |
| 137 TRIM_MEMORY_RUNNING_LOW = 10, |
| 138 TRIM_MEMORY_UI_HIDDEN = 20, |
| 139 TRIM_MEMORY_BACKGROUND = 40, |
| 140 }; |
| 141 |
| 142 // Not urgent enough. TRIM_MEMORY_UI_HIDDEN is treated specially because |
| 143 // it does not indicate memory pressure, but merely that the app is |
| 144 // backgrounded. |
| 145 if (level < TRIM_MEMORY_RUNNING_LOW || level == TRIM_MEMORY_UI_HIDDEN) |
| 146 return; |
| 147 |
| 148 // Do not release resources on view we expect to get DrawGL soon. |
| 149 if (level < TRIM_MEMORY_BACKGROUND && visible) |
| 150 return; |
| 151 |
| 152 // Just set the memory limit to 0 and drop all tiles. This will be reset to |
| 153 // normal levels in the next DrawGL call. |
| 154 SynchronousCompositorMemoryPolicy zero_policy; |
| 155 if (shared_renderer_state_->GetMemoryPolicy() == zero_policy) |
| 156 return; |
| 157 |
| 158 TRACE_EVENT0("android_webview", "BrowserViewRenderer::TrimMemory"); |
| 159 |
| 160 RequestMemoryPolicy(zero_policy); |
| 161 EnforceMemoryPolicyImmediately(zero_policy); |
| 162 } |
| 163 |
| 164 SynchronousCompositorMemoryPolicy |
| 165 BrowserViewRenderer::CalculateDesiredMemoryPolicy() { |
| 166 SynchronousCompositorMemoryPolicy policy; |
| 167 size_t width = draw_gl_input_.global_visible_rect.width(); |
| 168 size_t height = draw_gl_input_.global_visible_rect.height(); |
| 169 policy.bytes_limit = kMemoryMultiplier * kBytesPerPixel * width * height; |
| 170 // Round up to a multiple of kMemoryAllocationStep. |
| 171 policy.bytes_limit = |
| 172 (policy.bytes_limit / kMemoryAllocationStep + 1) * kMemoryAllocationStep; |
| 173 |
| 174 size_t tiles = std::max(width * height * kTileMultiplier / g_tile_area, 1u); |
| 175 // Round up to a multiple of kTileAllocationStep. The minimum number of tiles |
| 176 // is also kTileAllocationStep. |
| 177 tiles = (tiles + kTileAllocationStep - 1) / kTileAllocationStep * |
| 178 kTileAllocationStep; |
| 179 policy.num_resources_limit = tiles; |
| 180 return policy; |
| 181 } |
| 182 |
| 183 // This function updates the cached memory policy in shared renderer state, as |
| 184 // well as the tile resource allocation in GlobalTileManager. |
| 185 void BrowserViewRenderer::RequestMemoryPolicy( |
| 186 SynchronousCompositorMemoryPolicy& new_policy) { |
| 187 // This will be used in SetNumTiles. |
| 188 num_bytes_ = new_policy.bytes_limit; |
| 189 |
| 190 GlobalTileManager* manager = GlobalTileManager::GetInstance(); |
| 191 |
| 192 // The following line will call BrowserViewRenderer::SetTilesNum(). |
| 193 manager->RequestTiles(new_policy.num_resources_limit, tile_manager_key_); |
| 194 } |
| 195 |
| 196 void BrowserViewRenderer::SetNumTiles(size_t num_tiles, |
| 197 bool effective_immediately) { |
| 198 if (num_tiles == num_tiles_) |
| 199 return; |
| 200 num_tiles_ = num_tiles; |
| 201 |
| 202 SynchronousCompositorMemoryPolicy new_policy; |
| 203 new_policy.num_resources_limit = num_tiles_; |
| 204 new_policy.bytes_limit = num_bytes_; |
| 205 shared_renderer_state_->SetMemoryPolicy(new_policy); |
| 206 |
| 207 if (effective_immediately) |
| 208 EnforceMemoryPolicyImmediately(new_policy); |
| 209 } |
| 210 |
| 211 void BrowserViewRenderer::EnforceMemoryPolicyImmediately( |
| 212 SynchronousCompositorMemoryPolicy new_policy) { |
| 213 shared_renderer_state_->GetCompositor()->SetMemoryPolicy(new_policy); |
| 214 ForceFakeCompositeSW(); |
| 215 shared_renderer_state_->SetMemoryPolicyDirty(false); |
| 216 } |
| 217 |
| 218 size_t BrowserViewRenderer::GetNumTiles() const { |
| 219 return shared_renderer_state_->GetMemoryPolicy().num_resources_limit; |
93 } | 220 } |
94 | 221 |
95 bool BrowserViewRenderer::OnDraw(jobject java_canvas, | 222 bool BrowserViewRenderer::OnDraw(jobject java_canvas, |
96 bool is_hardware_canvas, | 223 bool is_hardware_canvas, |
97 const gfx::Vector2d& scroll, | 224 const gfx::Vector2d& scroll, |
98 const gfx::Rect& global_visible_rect, | 225 const gfx::Rect& global_visible_rect, |
99 const gfx::Rect& clip) { | 226 const gfx::Rect& clip) { |
100 draw_gl_input_.frame_id++; | 227 draw_gl_input_.frame_id++; |
101 draw_gl_input_.scroll_offset = scroll; | 228 draw_gl_input_.scroll_offset = scroll; |
102 draw_gl_input_.global_visible_rect = global_visible_rect; | 229 draw_gl_input_.global_visible_rect = global_visible_rect; |
103 draw_gl_input_.width = width_; | 230 draw_gl_input_.width = width_; |
104 draw_gl_input_.height = height_; | 231 draw_gl_input_.height = height_; |
105 if (clear_view_) | 232 if (clear_view_) |
106 return false; | 233 return false; |
107 if (is_hardware_canvas && attached_to_window_) { | 234 if (is_hardware_canvas && attached_to_window_) { |
108 shared_renderer_state_->SetDrawGLInput(draw_gl_input_); | 235 shared_renderer_state_->SetDrawGLInput(draw_gl_input_); |
| 236 |
| 237 SynchronousCompositorMemoryPolicy old_policy = |
| 238 shared_renderer_state_->GetMemoryPolicy(); |
| 239 SynchronousCompositorMemoryPolicy new_policy = |
| 240 CalculateDesiredMemoryPolicy(); |
| 241 RequestMemoryPolicy(new_policy); |
109 // We should be performing a hardware draw here. If we don't have the | 242 // We should be performing a hardware draw here. If we don't have the |
110 // compositor yet or if RequestDrawGL fails, it means we failed this draw | 243 // compositor yet or if RequestDrawGL fails, it means we failed this draw |
111 // and thus return false here to clear to background color for this draw. | 244 // and thus return false here to clear to background color for this draw. |
112 return has_compositor_ && client_->RequestDrawGL(java_canvas, false); | 245 bool did_draw_gl = |
| 246 has_compositor_ && client_->RequestDrawGL(java_canvas, false); |
| 247 if (did_draw_gl) |
| 248 GlobalTileManager::GetInstance()->DidUse(tile_manager_key_); |
| 249 else |
| 250 RequestMemoryPolicy(old_policy); |
| 251 |
| 252 return did_draw_gl; |
113 } | 253 } |
114 // Perform a software draw | 254 // Perform a software draw |
115 return DrawSWInternal(java_canvas, clip); | 255 return DrawSWInternal(java_canvas, clip); |
116 } | 256 } |
117 | 257 |
118 void BrowserViewRenderer::DidDrawGL(const DrawGLResult& result) { | 258 void BrowserViewRenderer::DidDrawGL(const DrawGLResult& result) { |
119 DidComposite(!result.clip_contains_visible_rect); | 259 DidComposite(!result.clip_contains_visible_rect); |
120 } | 260 } |
121 | 261 |
122 bool BrowserViewRenderer::DrawSWInternal(jobject java_canvas, | 262 bool BrowserViewRenderer::DrawSWInternal(jobject java_canvas, |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 void BrowserViewRenderer::OnAttachedToWindow(int width, int height) { | 363 void BrowserViewRenderer::OnAttachedToWindow(int width, int height) { |
224 TRACE_EVENT2("android_webview", | 364 TRACE_EVENT2("android_webview", |
225 "BrowserViewRenderer::OnAttachedToWindow", | 365 "BrowserViewRenderer::OnAttachedToWindow", |
226 "width", | 366 "width", |
227 width, | 367 width, |
228 "height", | 368 "height", |
229 height); | 369 height); |
230 attached_to_window_ = true; | 370 attached_to_window_ = true; |
231 width_ = width; | 371 width_ = width; |
232 height_ = height; | 372 height_ = height; |
| 373 tile_manager_key_ = GlobalTileManager::GetInstance()->PushBack(this); |
233 } | 374 } |
234 | 375 |
235 void BrowserViewRenderer::OnDetachedFromWindow() { | 376 void BrowserViewRenderer::OnDetachedFromWindow() { |
236 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDetachedFromWindow"); | 377 TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDetachedFromWindow"); |
237 attached_to_window_ = false; | 378 attached_to_window_ = false; |
| 379 SynchronousCompositorMemoryPolicy zero_policy; |
| 380 RequestMemoryPolicy(zero_policy); |
| 381 GlobalTileManager::GetInstance()->Remove(tile_manager_key_); |
| 382 // The hardware resources are released in the destructor of hardware renderer, |
| 383 // so we don't need to do it here. |
| 384 // See AwContents::ReleaseHardwareDrawOnRenderThread(JNIEnv*, jobject). |
238 } | 385 } |
239 | 386 |
240 bool BrowserViewRenderer::IsAttachedToWindow() const { | 387 bool BrowserViewRenderer::IsAttachedToWindow() const { |
241 return attached_to_window_; | 388 return attached_to_window_; |
242 } | 389 } |
243 | 390 |
244 bool BrowserViewRenderer::IsVisible() const { | 391 bool BrowserViewRenderer::IsVisible() const { |
245 // Ignore |window_visible_| if |attached_to_window_| is false. | 392 // Ignore |window_visible_| if |attached_to_window_| is false. |
246 return view_visible_ && (!attached_to_window_ || window_visible_); | 393 return view_visible_ && (!attached_to_window_ || window_visible_); |
247 } | 394 } |
(...skipping 13 matching lines...) Expand all Loading... |
261 shared_renderer_state_->SetCompositorOnUiThread(compositor); | 408 shared_renderer_state_->SetCompositorOnUiThread(compositor); |
262 } | 409 } |
263 | 410 |
264 void BrowserViewRenderer::DidDestroyCompositor( | 411 void BrowserViewRenderer::DidDestroyCompositor( |
265 content::SynchronousCompositor* compositor) { | 412 content::SynchronousCompositor* compositor) { |
266 TRACE_EVENT0("android_webview", "BrowserViewRenderer::DidDestroyCompositor"); | 413 TRACE_EVENT0("android_webview", "BrowserViewRenderer::DidDestroyCompositor"); |
267 DCHECK(has_compositor_); | 414 DCHECK(has_compositor_); |
268 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 415 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
269 has_compositor_ = false; | 416 has_compositor_ = false; |
270 shared_renderer_state_->SetCompositorOnUiThread(NULL); | 417 shared_renderer_state_->SetCompositorOnUiThread(NULL); |
| 418 SynchronousCompositorMemoryPolicy zero_policy; |
| 419 DCHECK(shared_renderer_state_->GetMemoryPolicy() == zero_policy); |
271 } | 420 } |
272 | 421 |
273 void BrowserViewRenderer::SetContinuousInvalidate(bool invalidate) { | 422 void BrowserViewRenderer::SetContinuousInvalidate(bool invalidate) { |
274 if (!ui_task_runner_->BelongsToCurrentThread()) { | 423 if (!ui_task_runner_->BelongsToCurrentThread()) { |
275 ui_task_runner_->PostTask( | 424 ui_task_runner_->PostTask( |
276 FROM_HERE, | 425 FROM_HERE, |
277 base::Bind(&BrowserViewRenderer::SetContinuousInvalidate, | 426 base::Bind(&BrowserViewRenderer::SetContinuousInvalidate, |
278 ui_thread_weak_ptr_, | 427 ui_thread_weak_ptr_, |
279 invalidate)); | 428 invalidate)); |
280 return; | 429 return; |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 base::StringAppendF(&str, | 747 base::StringAppendF(&str, |
599 "surface width height: [%d %d] ", | 748 "surface width height: [%d %d] ", |
600 draw_info->width, | 749 draw_info->width, |
601 draw_info->height); | 750 draw_info->height); |
602 base::StringAppendF(&str, "is_layer: %d ", draw_info->is_layer); | 751 base::StringAppendF(&str, "is_layer: %d ", draw_info->is_layer); |
603 } | 752 } |
604 return str; | 753 return str; |
605 } | 754 } |
606 | 755 |
607 } // namespace android_webview | 756 } // namespace android_webview |
OLD | NEW |