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" |
15 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/common/content_switches.h" | 15 #include "content/public/common/content_switches.h" |
17 #include "gpu/command_buffer/service/shader_translator_cache.h" | 16 #include "gpu/command_buffer/service/shader_translator_cache.h" |
18 #include "ui/gfx/geometry/rect_conversions.h" | 17 #include "ui/gfx/geometry/rect_conversions.h" |
19 #include "ui/gfx/geometry/rect_f.h" | 18 #include "ui/gfx/geometry/rect_f.h" |
20 #include "ui/gfx/transform.h" | 19 #include "ui/gfx/transform.h" |
21 | 20 |
22 using content::BrowserThread; | |
23 | |
24 namespace android_webview { | 21 namespace android_webview { |
25 | 22 |
26 namespace { | 23 namespace { |
27 | 24 |
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> > | 25 base::LazyInstance<scoped_refptr<internal::DeferredGpuCommandService> > |
35 g_service = LAZY_INSTANCE_INITIALIZER; | 26 g_service = LAZY_INSTANCE_INITIALIZER; |
36 | 27 |
37 } // namespace | 28 } // namespace |
38 | 29 |
39 HardwareRenderer::HardwareRenderer(SharedRendererState* state) | 30 HardwareRenderer::HardwareRenderer(SharedRendererState* state) |
40 : shared_renderer_state_(state), | 31 : shared_renderer_state_(state), |
41 last_egl_context_(eglGetCurrentContext()), | 32 last_egl_context_(eglGetCurrentContext()), |
42 manager_key_(GLViewRendererManager::GetInstance()->PushBack( | 33 renderer_manager_key_(GLViewRendererManager::GetInstance()->PushBack( |
43 shared_renderer_state_)) { | 34 shared_renderer_state_)) { |
44 DCHECK(last_egl_context_); | 35 DCHECK(last_egl_context_); |
36 | |
boliu
2014/04/28 22:22:15
spurious new line
hush (inactive)
2014/04/30 20:50:17
Done.
| |
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 const DrawGLInput input = shared_renderer_state_->GetDrawGLInput(); | 68 const DrawGLInput input = shared_renderer_state_->GetDrawGLInput(); |
77 | 69 |
78 // We need to watch if the current Android context has changed and enforce | 70 // We need to watch if the current Android context has changed and enforce |
79 // a clean-up in the compositor. | 71 // a clean-up in the compositor. |
80 EGLContext current_context = eglGetCurrentContext(); | 72 EGLContext current_context = eglGetCurrentContext(); |
81 if (!current_context) { | 73 if (!current_context) { |
82 DLOG(ERROR) << "DrawGL called without EGLContext"; | 74 DLOG(ERROR) << "DrawGL called without EGLContext"; |
83 return false; | 75 return false; |
84 } | 76 } |
85 | 77 |
86 // TODO(boliu): Handle context loss. | 78 // TODO(boliu): Handle context loss. |
87 if (last_egl_context_ != current_context) | 79 if (last_egl_context_ != current_context) |
88 DLOG(WARNING) << "EGLContextChanged"; | 80 DLOG(WARNING) << "EGLContextChanged"; |
89 | 81 |
90 ScopedAppGLStateRestore state_restore(ScopedAppGLStateRestore::MODE_DRAW); | 82 ScopedAppGLStateRestore state_restore(ScopedAppGLStateRestore::MODE_DRAW); |
91 internal::ScopedAllowGL allow_gl; | 83 internal::ScopedAllowGL allow_gl; |
92 | 84 |
93 if (draw_info->mode == AwDrawGLInfo::kModeProcess) | 85 if (draw_info->mode == AwDrawGLInfo::kModeProcess) |
94 return false; | 86 return false; |
95 | 87 |
96 // Update memory budget. This will no-op in compositor if the policy has not | 88 // Memory policy is set by BrowserViewRenderer on UI thread. |
97 // changed since last draw. | 89 content::SynchronousCompositorMemoryPolicy policy = |
98 content::SynchronousCompositorMemoryPolicy policy; | 90 shared_renderer_state_->GetMemoryPolicy(); |
99 policy.bytes_limit = g_memory_multiplier * kBytesPerPixel * | 91 |
100 input.global_visible_rect.width() * | |
101 input.global_visible_rect.height(); | |
102 // Round up to a multiple of kMemoryAllocationStep. | |
103 policy.bytes_limit = | |
104 (policy.bytes_limit / kMemoryAllocationStep + 1) * kMemoryAllocationStep; | |
105 policy.num_resources_limit = g_num_gralloc_limit; | |
106 SetMemoryPolicy(policy); | 92 SetMemoryPolicy(policy); |
107 | 93 |
108 gl_surface_->SetBackingFrameBufferObject( | 94 gl_surface_->SetBackingFrameBufferObject( |
109 state_restore.framebuffer_binding_ext()); | 95 state_restore.framebuffer_binding_ext()); |
110 | 96 |
111 gfx::Transform transform; | 97 gfx::Transform transform; |
112 transform.matrix().setColMajorf(draw_info->transform); | 98 transform.matrix().setColMajorf(draw_info->transform); |
113 transform.Translate(input.scroll_offset.x(), input.scroll_offset.y()); | 99 transform.Translate(input.scroll_offset.x(), input.scroll_offset.y()); |
114 gfx::Rect clip_rect(draw_info->clip_left, | 100 gfx::Rect clip_rect(draw_info->clip_left, |
115 draw_info->clip_top, | 101 draw_info->clip_top, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 | 146 |
161 if (!eglGetCurrentContext()) { | 147 if (!eglGetCurrentContext()) { |
162 NOTREACHED(); | 148 NOTREACHED(); |
163 return false; | 149 return false; |
164 } | 150 } |
165 | 151 |
166 DCHECK_EQ(last_egl_context_, eglGetCurrentContext()); | 152 DCHECK_EQ(last_egl_context_, eglGetCurrentContext()); |
167 | 153 |
168 // Just set the memory limit to 0 and drop all tiles. This will be reset to | 154 // Just set the memory limit to 0 and drop all tiles. This will be reset to |
169 // normal levels in the next DrawGL call. | 155 // normal levels in the next DrawGL call. |
170 content::SynchronousCompositorMemoryPolicy policy; | 156 content::SynchronousCompositorMemoryPolicy zero_policy; |
171 policy.bytes_limit = 0; | 157 if (shared_renderer_state_->GetMemoryPolicy() == zero_policy) |
172 policy.num_resources_limit = 0; | |
173 if (memory_policy_ == policy) | |
174 return false; | 158 return false; |
175 | 159 |
176 TRACE_EVENT0("android_webview", "HardwareRenderer::TrimMemory"); | 160 TRACE_EVENT0("android_webview", "HardwareRenderer::TrimMemory"); |
177 ScopedAppGLStateRestore state_restore( | 161 ScopedAppGLStateRestore state_restore( |
178 ScopedAppGLStateRestore::MODE_RESOURCE_MANAGEMENT); | 162 ScopedAppGLStateRestore::MODE_RESOURCE_MANAGEMENT); |
179 internal::ScopedAllowGL allow_gl; | 163 internal::ScopedAllowGL allow_gl; |
180 | 164 |
181 SetMemoryPolicy(policy); | 165 SetMemoryPolicy(zero_policy); |
182 return true; | 166 return true; |
183 } | 167 } |
184 | 168 |
185 void HardwareRenderer::SetMemoryPolicy( | 169 void HardwareRenderer::SetMemoryPolicy( |
186 content::SynchronousCompositorMemoryPolicy& new_policy) { | 170 content::SynchronousCompositorMemoryPolicy& policy) { |
187 if (memory_policy_ == new_policy) | 171 shared_renderer_state_->GetCompositor()->SetMemoryPolicy(policy); |
boliu
2014/04/28 22:22:15
This check is still important, but I understand th
hush (inactive)
2014/04/30 20:50:17
I add the boolean in shared renderer state and har
| |
188 return; | |
189 | |
190 memory_policy_ = new_policy; | |
191 shared_renderer_state_->GetCompositor()-> | |
192 SetMemoryPolicy(memory_policy_); | |
193 } | 172 } |
194 | 173 |
195 // static | 174 // static |
196 void HardwareRenderer::CalculateTileMemoryPolicy() { | 175 void HardwareRenderer::CalculateTileMemoryPolicy() { |
197 CommandLine* cl = CommandLine::ForCurrentProcess(); | 176 CommandLine* cl = CommandLine::ForCurrentProcess(); |
198 | 177 |
199 const char kDefaultTileSize[] = "384"; | 178 const char kDefaultTileSize[] = "384"; |
200 if (!cl->HasSwitch(switches::kDefaultTileWidth)) | 179 if (!cl->HasSwitch(switches::kDefaultTileWidth)) |
201 cl->AppendSwitchASCII(switches::kDefaultTileWidth, kDefaultTileSize); | 180 cl->AppendSwitchASCII(switches::kDefaultTileWidth, kDefaultTileSize); |
202 | 181 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
295 base::RefCountedThreadSafe<DeferredGpuCommandService>::AddRef(); | 274 base::RefCountedThreadSafe<DeferredGpuCommandService>::AddRef(); |
296 } | 275 } |
297 | 276 |
298 void DeferredGpuCommandService::Release() const { | 277 void DeferredGpuCommandService::Release() const { |
299 base::RefCountedThreadSafe<DeferredGpuCommandService>::Release(); | 278 base::RefCountedThreadSafe<DeferredGpuCommandService>::Release(); |
300 } | 279 } |
301 | 280 |
302 } // namespace internal | 281 } // namespace internal |
303 | 282 |
304 } // namespace android_webview | 283 } // namespace android_webview |
OLD | NEW |