| 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/hardware_renderer.h" | 5 #include "android_webview/browser/hardware_renderer.h" |
| 6 | 6 |
| 7 #include "android_webview/browser/aw_gl_surface.h" | 7 #include "android_webview/browser/aw_gl_surface.h" |
| 8 #include "android_webview/browser/browser_view_renderer_client.h" | 8 #include "android_webview/browser/browser_view_renderer_client.h" |
| 9 #include "android_webview/browser/gl_view_renderer_manager.h" | |
| 10 #include "android_webview/browser/scoped_app_gl_state_restore.h" | 9 #include "android_webview/browser/scoped_app_gl_state_restore.h" |
| 11 #include "android_webview/public/browser/draw_gl.h" | 10 #include "android_webview/public/browser/draw_gl.h" |
| 12 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 13 #include "base/debug/trace_event.h" | 12 #include "base/debug/trace_event.h" |
| 14 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "content/public/browser/android/synchronous_compositor.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
| 17 #include "gpu/command_buffer/service/shader_translator_cache.h" | 17 #include "gpu/command_buffer/service/shader_translator_cache.h" |
| 18 #include "ui/gfx/geometry/rect_conversions.h" | 18 #include "ui/gfx/geometry/rect_conversions.h" |
| 19 #include "ui/gfx/geometry/rect_f.h" | 19 #include "ui/gfx/geometry/rect_f.h" |
| 20 #include "ui/gfx/transform.h" | 20 #include "ui/gfx/transform.h" |
| 21 | 21 |
| 22 using content::BrowserThread; | |
| 23 | |
| 24 namespace android_webview { | 22 namespace android_webview { |
| 25 | 23 |
| 26 namespace { | 24 namespace { |
| 27 | 25 |
| 28 // Used to calculate memory and resource allocation. Determined experimentally. | |
| 29 const size_t g_memory_multiplier = 10; | |
| 30 const size_t g_num_gralloc_limit = 150; | |
| 31 const size_t kBytesPerPixel = 4; | |
| 32 const size_t kMemoryAllocationStep = 5 * 1024 * 1024; | |
| 33 | |
| 34 base::LazyInstance<scoped_refptr<internal::DeferredGpuCommandService> > | 26 base::LazyInstance<scoped_refptr<internal::DeferredGpuCommandService> > |
| 35 g_service = LAZY_INSTANCE_INITIALIZER; | 27 g_service = LAZY_INSTANCE_INITIALIZER; |
| 36 | 28 |
| 37 } // namespace | 29 } // namespace |
| 38 | 30 |
| 39 HardwareRenderer::HardwareRenderer(SharedRendererState* state) | 31 HardwareRenderer::HardwareRenderer(SharedRendererState* state) |
| 40 : shared_renderer_state_(state), | 32 : shared_renderer_state_(state), |
| 41 last_egl_context_(eglGetCurrentContext()), | 33 last_egl_context_(eglGetCurrentContext()), |
| 42 manager_key_(GLViewRendererManager::GetInstance()->PushBack( | 34 renderer_manager_key_(GLViewRendererManager::GetInstance()->PushBack( |
| 43 shared_renderer_state_)) { | 35 shared_renderer_state_)) { |
| 44 DCHECK(last_egl_context_); | 36 DCHECK(last_egl_context_); |
| 45 if (!g_service.Get()) { | 37 if (!g_service.Get()) { |
| 46 g_service.Get() = new internal::DeferredGpuCommandService; | 38 g_service.Get() = new internal::DeferredGpuCommandService; |
| 47 content::SynchronousCompositor::SetGpuService(g_service.Get()); | 39 content::SynchronousCompositor::SetGpuService(g_service.Get()); |
| 48 } | 40 } |
| 49 | 41 |
| 50 ScopedAppGLStateRestore state_restore( | 42 ScopedAppGLStateRestore state_restore( |
| 51 ScopedAppGLStateRestore::MODE_RESOURCE_MANAGEMENT); | 43 ScopedAppGLStateRestore::MODE_RESOURCE_MANAGEMENT); |
| 52 internal::ScopedAllowGL allow_gl; | 44 internal::ScopedAllowGL allow_gl; |
| 53 | 45 |
| 54 gl_surface_ = new AwGLSurface; | 46 gl_surface_ = new AwGLSurface; |
| 55 bool success = | 47 bool success = |
| 56 shared_renderer_state_->GetCompositor()-> | 48 shared_renderer_state_->GetCompositor()-> |
| 57 InitializeHwDraw(gl_surface_); | 49 InitializeHwDraw(gl_surface_); |
| 58 DCHECK(success); | 50 DCHECK(success); |
| 59 } | 51 } |
| 60 | 52 |
| 61 HardwareRenderer::~HardwareRenderer() { | 53 HardwareRenderer::~HardwareRenderer() { |
| 62 GLViewRendererManager* mru = GLViewRendererManager::GetInstance(); | 54 GLViewRendererManager* render_manager = GLViewRendererManager::GetInstance(); |
| 63 mru->Remove(manager_key_); | 55 render_manager->Remove(renderer_manager_key_); |
| 64 | 56 |
| 65 ScopedAppGLStateRestore state_restore( | 57 ScopedAppGLStateRestore state_restore( |
| 66 ScopedAppGLStateRestore::MODE_RESOURCE_MANAGEMENT); | 58 ScopedAppGLStateRestore::MODE_RESOURCE_MANAGEMENT); |
| 67 internal::ScopedAllowGL allow_gl; | 59 internal::ScopedAllowGL allow_gl; |
| 68 | 60 |
| 69 shared_renderer_state_->GetCompositor()->ReleaseHwDraw(); | 61 shared_renderer_state_->GetCompositor()->ReleaseHwDraw(); |
| 70 gl_surface_ = NULL; | 62 gl_surface_ = NULL; |
| 71 } | 63 } |
| 72 | 64 |
| 73 bool HardwareRenderer::DrawGL(AwDrawGLInfo* draw_info, DrawGLResult* result) { | 65 bool HardwareRenderer::DrawGL(AwDrawGLInfo* draw_info, DrawGLResult* result) { |
| 74 TRACE_EVENT0("android_webview", "HardwareRenderer::DrawGL"); | 66 TRACE_EVENT0("android_webview", "HardwareRenderer::DrawGL"); |
| 75 GLViewRendererManager::GetInstance()->DidDrawGL(manager_key_); | 67 GLViewRendererManager::GetInstance()->DidDrawGL(renderer_manager_key_); |
| 76 | 68 |
| 77 // We need to watch if the current Android context has changed and enforce | 69 // We need to watch if the current Android context has changed and enforce |
| 78 // a clean-up in the compositor. | 70 // a clean-up in the compositor. |
| 79 EGLContext current_context = eglGetCurrentContext(); | 71 EGLContext current_context = eglGetCurrentContext(); |
| 80 if (!current_context) { | 72 if (!current_context) { |
| 81 DLOG(ERROR) << "DrawGL called without EGLContext"; | 73 DLOG(ERROR) << "DrawGL called without EGLContext"; |
| 82 return false; | 74 return false; |
| 83 } | 75 } |
| 84 | 76 |
| 85 // TODO(boliu): Handle context loss. | 77 // TODO(boliu): Handle context loss. |
| 86 if (last_egl_context_ != current_context) | 78 if (last_egl_context_ != current_context) |
| 87 DLOG(WARNING) << "EGLContextChanged"; | 79 DLOG(WARNING) << "EGLContextChanged"; |
| 88 | 80 |
| 89 ScopedAppGLStateRestore state_restore(ScopedAppGLStateRestore::MODE_DRAW); | 81 ScopedAppGLStateRestore state_restore(ScopedAppGLStateRestore::MODE_DRAW); |
| 90 internal::ScopedAllowGL allow_gl; | 82 internal::ScopedAllowGL allow_gl; |
| 91 | 83 |
| 92 if (draw_info->mode != AwDrawGLInfo::kModeDraw) | 84 if (draw_info->mode != AwDrawGLInfo::kModeDraw) |
| 93 return false; | 85 return false; |
| 94 | 86 |
| 95 // Should only need to access SharedRendererState in kModeDraw and kModeSync. | 87 // Should only need to access SharedRendererState in kModeDraw and kModeSync. |
| 96 const DrawGLInput input = shared_renderer_state_->GetDrawGLInput(); | 88 const DrawGLInput input = shared_renderer_state_->GetDrawGLInput(); |
| 97 | 89 SetCompositorMemoryPolicy(); |
| 98 // Update memory budget. This will no-op in compositor if the policy has not | |
| 99 // changed since last draw. | |
| 100 content::SynchronousCompositorMemoryPolicy policy; | |
| 101 policy.bytes_limit = g_memory_multiplier * kBytesPerPixel * | |
| 102 input.global_visible_rect.width() * | |
| 103 input.global_visible_rect.height(); | |
| 104 // Round up to a multiple of kMemoryAllocationStep. | |
| 105 policy.bytes_limit = | |
| 106 (policy.bytes_limit / kMemoryAllocationStep + 1) * kMemoryAllocationStep; | |
| 107 policy.num_resources_limit = g_num_gralloc_limit; | |
| 108 SetMemoryPolicy(policy); | |
| 109 | 90 |
| 110 gl_surface_->SetBackingFrameBufferObject( | 91 gl_surface_->SetBackingFrameBufferObject( |
| 111 state_restore.framebuffer_binding_ext()); | 92 state_restore.framebuffer_binding_ext()); |
| 112 | 93 |
| 113 gfx::Transform transform; | 94 gfx::Transform transform; |
| 114 transform.matrix().setColMajorf(draw_info->transform); | 95 transform.matrix().setColMajorf(draw_info->transform); |
| 115 transform.Translate(input.scroll_offset.x(), input.scroll_offset.y()); | 96 transform.Translate(input.scroll_offset.x(), input.scroll_offset.y()); |
| 116 gfx::Rect clip_rect(draw_info->clip_left, | 97 gfx::Rect clip_rect(draw_info->clip_left, |
| 117 draw_info->clip_top, | 98 draw_info->clip_top, |
| 118 draw_info->clip_right - draw_info->clip_left, | 99 draw_info->clip_right - draw_info->clip_left, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 135 gl_surface_->ResetBackingFrameBufferObject(); | 116 gl_surface_->ResetBackingFrameBufferObject(); |
| 136 | 117 |
| 137 if (did_draw) { | 118 if (did_draw) { |
| 138 result->frame_id = input.frame_id; | 119 result->frame_id = input.frame_id; |
| 139 result->clip_contains_visible_rect = | 120 result->clip_contains_visible_rect = |
| 140 clip_rect.Contains(input.global_visible_rect); | 121 clip_rect.Contains(input.global_visible_rect); |
| 141 } | 122 } |
| 142 return did_draw; | 123 return did_draw; |
| 143 } | 124 } |
| 144 | 125 |
| 145 bool HardwareRenderer::TrimMemory(int level, bool visible) { | 126 void HardwareRenderer::SetCompositorMemoryPolicy() { |
| 146 // Constants from Android ComponentCallbacks2. | 127 if (shared_renderer_state_->IsMemoryPolicyDirty()) { |
| 147 enum { | 128 content::SynchronousCompositorMemoryPolicy policy = |
| 148 TRIM_MEMORY_RUNNING_LOW = 10, | 129 shared_renderer_state_->GetMemoryPolicy(); |
| 149 TRIM_MEMORY_UI_HIDDEN = 20, | 130 // Memory policy is set by BrowserViewRenderer on UI thread. |
| 150 TRIM_MEMORY_BACKGROUND = 40, | 131 shared_renderer_state_->GetCompositor()->SetMemoryPolicy(policy); |
| 151 }; | 132 shared_renderer_state_->SetMemoryPolicyDirty(false); |
| 152 | |
| 153 // Not urgent enough. TRIM_MEMORY_UI_HIDDEN is treated specially because | |
| 154 // it does not indicate memory pressure, but merely that the app is | |
| 155 // backgrounded. | |
| 156 if (level < TRIM_MEMORY_RUNNING_LOW || level == TRIM_MEMORY_UI_HIDDEN) | |
| 157 return false; | |
| 158 | |
| 159 // Do not release resources on view we expect to get DrawGL soon. | |
| 160 if (level < TRIM_MEMORY_BACKGROUND && visible) | |
| 161 return false; | |
| 162 | |
| 163 if (!eglGetCurrentContext()) { | |
| 164 NOTREACHED(); | |
| 165 return false; | |
| 166 } | 133 } |
| 167 | |
| 168 DCHECK_EQ(last_egl_context_, eglGetCurrentContext()); | |
| 169 | |
| 170 // Just set the memory limit to 0 and drop all tiles. This will be reset to | |
| 171 // normal levels in the next DrawGL call. | |
| 172 content::SynchronousCompositorMemoryPolicy policy; | |
| 173 policy.bytes_limit = 0; | |
| 174 policy.num_resources_limit = 0; | |
| 175 if (memory_policy_ == policy) | |
| 176 return false; | |
| 177 | |
| 178 TRACE_EVENT0("android_webview", "HardwareRenderer::TrimMemory"); | |
| 179 ScopedAppGLStateRestore state_restore( | |
| 180 ScopedAppGLStateRestore::MODE_RESOURCE_MANAGEMENT); | |
| 181 internal::ScopedAllowGL allow_gl; | |
| 182 | |
| 183 SetMemoryPolicy(policy); | |
| 184 return true; | |
| 185 } | |
| 186 | |
| 187 void HardwareRenderer::SetMemoryPolicy( | |
| 188 content::SynchronousCompositorMemoryPolicy& new_policy) { | |
| 189 if (memory_policy_ == new_policy) | |
| 190 return; | |
| 191 | |
| 192 memory_policy_ = new_policy; | |
| 193 shared_renderer_state_->GetCompositor()-> | |
| 194 SetMemoryPolicy(memory_policy_); | |
| 195 } | 134 } |
| 196 | 135 |
| 197 // static | 136 // static |
| 198 void HardwareRenderer::CalculateTileMemoryPolicy() { | 137 void HardwareRenderer::CalculateTileMemoryPolicy() { |
| 199 CommandLine* cl = CommandLine::ForCurrentProcess(); | 138 CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 200 | 139 |
| 201 const char kDefaultTileSize[] = "384"; | 140 const char kDefaultTileSize[] = "384"; |
| 202 if (!cl->HasSwitch(switches::kDefaultTileWidth)) | 141 if (!cl->HasSwitch(switches::kDefaultTileWidth)) |
| 203 cl->AppendSwitchASCII(switches::kDefaultTileWidth, kDefaultTileSize); | 142 cl->AppendSwitchASCII(switches::kDefaultTileWidth, kDefaultTileSize); |
| 204 | 143 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 base::RefCountedThreadSafe<DeferredGpuCommandService>::AddRef(); | 235 base::RefCountedThreadSafe<DeferredGpuCommandService>::AddRef(); |
| 297 } | 236 } |
| 298 | 237 |
| 299 void DeferredGpuCommandService::Release() const { | 238 void DeferredGpuCommandService::Release() const { |
| 300 base::RefCountedThreadSafe<DeferredGpuCommandService>::Release(); | 239 base::RefCountedThreadSafe<DeferredGpuCommandService>::Release(); |
| 301 } | 240 } |
| 302 | 241 |
| 303 } // namespace internal | 242 } // namespace internal |
| 304 | 243 |
| 305 } // namespace android_webview | 244 } // namespace android_webview |
| OLD | NEW |