OLD | NEW |
---|---|
1 // Copyright 2013 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/in_process_view_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.h" | |
9 #include "android_webview/browser/gl_view_renderer_manager.h" | |
8 #include "android_webview/browser/scoped_app_gl_state_restore.h" | 10 #include "android_webview/browser/scoped_app_gl_state_restore.h" |
9 #include "android_webview/common/aw_switches.h" | 11 #include "android_webview/common/aw_switches.h" |
10 #include "android_webview/public/browser/draw_gl.h" | 12 #include "android_webview/public/browser/draw_gl.h" |
11 #include "base/android/jni_android.h" | |
12 #include "base/auto_reset.h" | |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/debug/trace_event.h" | 14 #include "base/debug/trace_event.h" |
15 #include "base/lazy_instance.h" | |
16 #include "base/logging.h" | |
17 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/stringprintf.h" | |
19 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/web_contents.h" | |
21 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
22 #include "gpu/command_buffer/service/in_process_command_buffer.h" | |
23 #include "third_party/skia/include/core/SkBitmap.h" | |
24 #include "third_party/skia/include/core/SkBitmapDevice.h" | |
25 #include "third_party/skia/include/core/SkCanvas.h" | |
26 #include "third_party/skia/include/core/SkPicture.h" | |
27 #include "ui/gfx/transform.h" | 18 #include "ui/gfx/transform.h" |
28 #include "ui/gfx/vector2d_conversions.h" | |
29 | 19 |
30 using base::android::AttachCurrentThread; | |
31 using base::android::JavaRef; | |
32 using base::android::ScopedJavaLocalRef; | |
33 using content::BrowserThread; | 20 using content::BrowserThread; |
34 | 21 |
35 namespace android_webview { | 22 namespace android_webview { |
36 | 23 |
37 namespace { | 24 namespace { |
38 | 25 |
39 const void* kUserDataKey = &kUserDataKey; | |
40 | |
41 class UserData : public content::WebContents::Data { | |
42 public: | |
43 UserData(InProcessViewRenderer* ptr) : instance_(ptr) {} | |
44 virtual ~UserData() { | |
45 instance_->WebContentsGone(); | |
46 } | |
47 | |
48 static InProcessViewRenderer* GetInstance(content::WebContents* contents) { | |
49 if (!contents) | |
50 return NULL; | |
51 UserData* data = reinterpret_cast<UserData*>( | |
52 contents->GetUserData(kUserDataKey)); | |
53 return data ? data->instance_ : NULL; | |
54 } | |
55 | |
56 private: | |
57 InProcessViewRenderer* instance_; | |
58 }; | |
59 | |
60 bool HardwareEnabled() { | |
61 static bool g_hw_enabled = !CommandLine::ForCurrentProcess()->HasSwitch( | |
62 switches::kDisableWebViewGLMode); | |
63 return g_hw_enabled; | |
64 } | |
65 | |
66 const int64 kFallbackTickTimeoutInMilliseconds = 20; | |
67 | |
68 // Used to calculate memory and resource allocation. Determined experimentally. | 26 // Used to calculate memory and resource allocation. Determined experimentally. |
69 size_t g_memory_multiplier = 10; | 27 size_t g_memory_multiplier = 10; |
benm (inactive)
2014/02/25 12:48:01
can be overridden with switches::kTileMemoryMultip
| |
70 size_t g_num_gralloc_limit = 150; | 28 size_t g_num_gralloc_limit = 150; |
71 const size_t kBytesPerPixel = 4; | 29 const size_t kBytesPerPixel = 4; |
72 const size_t kMemoryAllocationStep = 5 * 1024 * 1024; | 30 const size_t kMemoryAllocationStep = 5 * 1024 * 1024; |
73 | 31 |
74 base::LazyInstance<GLViewRendererManager>::Leaky g_view_renderer_manager = | 32 base::LazyInstance<scoped_refptr<internal::DeferredGpuCommandService> > |
75 LAZY_INSTANCE_INITIALIZER; | 33 g_service = LAZY_INSTANCE_INITIALIZER; |
76 | 34 |
77 class ScopedAllowGL { | 35 } // namespace |
78 public: | |
79 ScopedAllowGL(); | |
80 ~ScopedAllowGL(); | |
81 | 36 |
82 static bool IsAllowed() { | 37 DrawGLResult::DrawGLResult() |
83 return g_view_renderer_manager.Get().OnRenderThread() && allow_gl; | 38 : did_draw(false), clip_contains_visible_rect(false) {} |
39 | |
40 HardwareRenderer::HardwareRenderer(content::SynchronousCompositor* compositor, | |
41 BrowserViewRendererClient* client) | |
42 : compositor_(compositor), | |
43 client_(client), | |
44 last_egl_context_(eglGetCurrentContext()), | |
45 manager_key_(GLViewRendererManager::GetInstance()->NullKey()) { | |
46 DCHECK(compositor_); | |
47 DCHECK(last_egl_context_); | |
48 } | |
49 | |
50 HardwareRenderer::~HardwareRenderer() { | |
51 ReleaseHardwareDraw(); | |
52 DCHECK(manager_key_ == GLViewRendererManager::GetInstance()->NullKey()); | |
53 } | |
54 | |
55 bool HardwareRenderer::InitializeHardwareDraw() { | |
56 TRACE_EVENT0("android_webview", "InitializeHardwareDraw"); | |
57 if (!g_service.Get()) { | |
58 g_service.Get() = new internal::DeferredGpuCommandService; | |
59 content::SynchronousCompositor::SetGpuService(g_service.Get()); | |
84 } | 60 } |
85 | 61 |
86 private: | 62 bool success = true; |
87 static bool allow_gl; | 63 if (!gl_surface_) { |
64 scoped_refptr<AwGLSurface> gl_surface = new AwGLSurface; | |
65 success = compositor_->InitializeHwDraw(gl_surface); | |
66 if (success) | |
67 gl_surface_ = gl_surface; | |
68 } | |
88 | 69 |
89 DISALLOW_COPY_AND_ASSIGN(ScopedAllowGL); | 70 return success; |
90 }; | |
91 | |
92 ScopedAllowGL::ScopedAllowGL() { | |
93 DCHECK(g_view_renderer_manager.Get().OnRenderThread()); | |
94 DCHECK(!allow_gl); | |
95 allow_gl = true; | |
96 } | 71 } |
97 | 72 |
98 ScopedAllowGL::~ScopedAllowGL() { | 73 void HardwareRenderer::ReleaseHardwareDraw() { |
sgurun-gerrit only
2014/02/25 06:56:36
consider moving this code to destructor - not much
| |
99 allow_gl = false; | 74 GLViewRendererManager* mru = GLViewRendererManager::GetInstance(); |
100 } | 75 if (manager_key_ != mru->NullKey()) { |
101 | 76 mru->NoLongerExpectsDrawGL(manager_key_); |
102 bool ScopedAllowGL::allow_gl = false; | 77 manager_key_ = mru->NullKey(); |
103 | |
104 void RequestProcessGLOnUIThread() { | |
105 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
106 BrowserThread::PostTask( | |
107 BrowserThread::UI, FROM_HERE, base::Bind(&RequestProcessGLOnUIThread)); | |
108 return; | |
109 } | 78 } |
110 | 79 |
111 InProcessViewRenderer* renderer = static_cast<InProcessViewRenderer*>( | 80 if (gl_surface_) { |
112 g_view_renderer_manager.Get().GetMostRecentlyDrawn()); | 81 ScopedAppGLStateRestore state_restore( |
113 if (!renderer || !renderer->RequestProcessGL()) { | 82 ScopedAppGLStateRestore::MODE_RESOURCE_MANAGEMENT); |
114 LOG(ERROR) << "Failed to request GL process. Deadlock likely: " | 83 internal::ScopedAllowGL allow_gl; |
115 << !!renderer; | 84 |
sgurun-gerrit only
2014/02/25 06:56:36
Original code does a RunTasks() here. not needed a
boliu
2014/02/25 19:02:22
Moved RunTasks to constructor of ScopedAllowGL.
| |
85 compositor_->ReleaseHwDraw(); | |
86 gl_surface_ = NULL; | |
116 } | 87 } |
117 } | 88 } |
118 | 89 |
119 class DeferredGpuCommandService | 90 DrawGLResult HardwareRenderer::DrawGL(AwDrawGLInfo* draw_info, |
120 : public gpu::InProcessCommandBuffer::Service, | 91 const DrawGLInput& input) { |
121 public base::RefCountedThreadSafe<DeferredGpuCommandService> { | 92 TRACE_EVENT0("android_webview", "HardwareRenderer::DrawGL"); |
122 public: | 93 manager_key_ = |
123 DeferredGpuCommandService(); | 94 GLViewRendererManager::GetInstance()->DidDrawGL(manager_key_, this); |
95 DrawGLResult result; | |
124 | 96 |
125 virtual void ScheduleTask(const base::Closure& task) OVERRIDE; | 97 // We need to watch if the current Android context has changed and enforce |
126 virtual void ScheduleIdleWork(const base::Closure& task) OVERRIDE; | 98 // a clean-up in the compositor. |
127 virtual bool UseVirtualizedGLContexts() OVERRIDE; | 99 EGLContext current_context = eglGetCurrentContext(); |
128 | 100 if (!current_context) { |
129 void RunTasks(); | 101 DLOG(ERROR) << "DrawGL called without EGLContext"; |
130 | 102 return result; |
131 virtual void AddRef() const OVERRIDE { | |
132 base::RefCountedThreadSafe<DeferredGpuCommandService>::AddRef(); | |
133 } | |
134 virtual void Release() const OVERRIDE { | |
135 base::RefCountedThreadSafe<DeferredGpuCommandService>::Release(); | |
136 } | 103 } |
137 | 104 |
138 protected: | 105 // TODO(boliu): Handle context loss. |
139 virtual ~DeferredGpuCommandService(); | 106 if (last_egl_context_ != current_context) |
140 friend class base::RefCountedThreadSafe<DeferredGpuCommandService>; | 107 DLOG(WARNING) << "EGLContextChanged"; |
141 | 108 |
142 private: | 109 ScopedAppGLStateRestore state_restore(ScopedAppGLStateRestore::MODE_DRAW); |
143 base::Lock tasks_lock_; | 110 internal::ScopedAllowGL allow_gl; |
144 std::queue<base::Closure> tasks_; | |
145 DISALLOW_COPY_AND_ASSIGN(DeferredGpuCommandService); | |
146 }; | |
147 | 111 |
148 DeferredGpuCommandService::DeferredGpuCommandService() {} | 112 if (draw_info->mode == AwDrawGLInfo::kModeProcess) |
113 return result; | |
149 | 114 |
150 DeferredGpuCommandService::~DeferredGpuCommandService() { | 115 if (!InitializeHardwareDraw()) { |
151 base::AutoLock lock(tasks_lock_); | 116 DLOG(ERROR) << "WebView hardware initialization failed"; |
152 DCHECK(tasks_.empty()); | 117 return result; |
118 } | |
119 | |
120 // Update memory budget. This will no-op in compositor if the policy has not | |
121 // changed since last draw. | |
122 content::SynchronousCompositorMemoryPolicy policy; | |
123 policy.bytes_limit = g_memory_multiplier * kBytesPerPixel * | |
124 input.global_visible_rect.width() * | |
125 input.global_visible_rect.height(); | |
126 // Round up to a multiple of kMemoryAllocationStep. | |
127 policy.bytes_limit = | |
128 (policy.bytes_limit / kMemoryAllocationStep + 1) * kMemoryAllocationStep; | |
129 policy.num_resources_limit = g_num_gralloc_limit; | |
130 SetMemoryPolicy(policy); | |
131 | |
132 gl_surface_->SetBackingFrameBufferObject( | |
133 state_restore.framebuffer_binding_ext()); | |
134 | |
135 gfx::Transform transform; | |
136 transform.matrix().setColMajorf(draw_info->transform); | |
137 transform.Translate(input.scroll.x(), input.scroll.y()); | |
138 gfx::Rect clip_rect(draw_info->clip_left, | |
139 draw_info->clip_top, | |
140 draw_info->clip_right - draw_info->clip_left, | |
141 draw_info->clip_bottom - draw_info->clip_top); | |
142 | |
143 gfx::Rect viewport_rect; | |
144 if (draw_info->is_layer) { | |
145 viewport_rect = clip_rect; | |
146 } else { | |
147 viewport_rect = input.global_visible_rect; | |
148 clip_rect.Intersect(viewport_rect); | |
149 } | |
150 result.clip_contains_visible_rect = clip_rect.Contains(viewport_rect); | |
151 | |
152 result.did_draw = | |
153 compositor_->DemandDrawHw(gfx::Size(draw_info->width, draw_info->height), | |
154 transform, | |
155 viewport_rect, | |
156 clip_rect, | |
157 state_restore.stencil_enabled()); | |
158 gl_surface_->ResetBackingFrameBufferObject(); | |
159 | |
160 return result; | |
153 } | 161 } |
154 | 162 |
155 // Called from different threads! | 163 bool HardwareRenderer::TrimMemory(int level, bool visible) { |
156 void DeferredGpuCommandService::ScheduleTask(const base::Closure& task) { | |
157 { | |
158 base::AutoLock lock(tasks_lock_); | |
159 tasks_.push(task); | |
160 } | |
161 if (ScopedAllowGL::IsAllowed()) { | |
162 RunTasks(); | |
163 } else { | |
164 RequestProcessGLOnUIThread(); | |
165 } | |
166 } | |
167 | |
168 void DeferredGpuCommandService::ScheduleIdleWork( | |
169 const base::Closure& callback) { | |
170 // TODO(sievers): Should this do anything? | |
171 } | |
172 | |
173 bool DeferredGpuCommandService::UseVirtualizedGLContexts() { return true; } | |
174 | |
175 void DeferredGpuCommandService::RunTasks() { | |
176 bool has_more_tasks; | |
177 { | |
178 base::AutoLock lock(tasks_lock_); | |
179 has_more_tasks = tasks_.size() > 0; | |
180 } | |
181 | |
182 while (has_more_tasks) { | |
183 base::Closure task; | |
184 { | |
185 base::AutoLock lock(tasks_lock_); | |
186 task = tasks_.front(); | |
187 tasks_.pop(); | |
188 } | |
189 task.Run(); | |
190 { | |
191 base::AutoLock lock(tasks_lock_); | |
192 has_more_tasks = tasks_.size() > 0; | |
193 } | |
194 | |
195 } | |
196 } | |
197 | |
198 base::LazyInstance<scoped_refptr<DeferredGpuCommandService> > g_service = | |
199 LAZY_INSTANCE_INITIALIZER; | |
200 | |
201 } // namespace | |
202 | |
203 InProcessViewRenderer::InProcessViewRenderer( | |
204 BrowserViewRenderer::Client* client, | |
205 content::WebContents* web_contents) | |
206 : client_(client), | |
207 web_contents_(web_contents), | |
208 compositor_(NULL), | |
209 is_paused_(false), | |
210 view_visible_(false), | |
211 window_visible_(false), | |
212 attached_to_window_(false), | |
213 dip_scale_(0.0), | |
214 page_scale_factor_(1.0), | |
215 on_new_picture_enable_(false), | |
216 clear_view_(false), | |
217 compositor_needs_continuous_invalidate_(false), | |
218 block_invalidates_(false), | |
219 width_(0), | |
220 height_(0), | |
221 hardware_initialized_(false), | |
222 hardware_failed_(false), | |
223 last_egl_context_(NULL), | |
224 manager_key_(g_view_renderer_manager.Get().NullKey()) { | |
225 CHECK(web_contents_); | |
226 web_contents_->SetUserData(kUserDataKey, new UserData(this)); | |
227 content::SynchronousCompositor::SetClientForWebContents(web_contents_, this); | |
228 | |
229 // Currently the logic in this class relies on |compositor_| remaining NULL | |
230 // until the DidInitializeCompositor() call, hence it is not set here. | |
231 } | |
232 | |
233 InProcessViewRenderer::~InProcessViewRenderer() { | |
234 CHECK(web_contents_); | |
235 content::SynchronousCompositor::SetClientForWebContents(web_contents_, NULL); | |
236 web_contents_->SetUserData(kUserDataKey, NULL); | |
237 NoLongerExpectsDrawGL(); | |
238 DCHECK(web_contents_ == NULL); // WebContentsGone should have been called. | |
239 } | |
240 | |
241 void InProcessViewRenderer::NoLongerExpectsDrawGL() { | |
242 GLViewRendererManager& mru = g_view_renderer_manager.Get(); | |
243 if (manager_key_ != mru.NullKey()) { | |
244 mru.NoLongerExpectsDrawGL(manager_key_); | |
245 manager_key_ = mru.NullKey(); | |
246 } | |
247 } | |
248 | |
249 // static | |
250 InProcessViewRenderer* InProcessViewRenderer::FromWebContents( | |
251 content::WebContents* contents) { | |
252 return UserData::GetInstance(contents); | |
253 } | |
254 | |
255 void InProcessViewRenderer::WebContentsGone() { | |
256 web_contents_ = NULL; | |
257 compositor_ = NULL; | |
258 } | |
259 | |
260 // static | |
261 void InProcessViewRenderer::CalculateTileMemoryPolicy() { | |
262 CommandLine* cl = CommandLine::ForCurrentProcess(); | |
263 if (cl->HasSwitch(switches::kTileMemoryMultiplier)) { | |
264 std::string string_value = | |
265 cl->GetSwitchValueASCII(switches::kTileMemoryMultiplier); | |
266 int int_value = 0; | |
267 if (base::StringToInt(string_value, &int_value) && | |
268 int_value >= 2 && int_value <= 50) { | |
269 g_memory_multiplier = int_value; | |
270 } | |
271 } | |
272 | |
273 if (cl->HasSwitch(switches::kNumGrallocBuffersPerWebview)) { | |
274 std::string string_value = | |
275 cl->GetSwitchValueASCII(switches::kNumGrallocBuffersPerWebview); | |
276 int int_value = 0; | |
277 if (base::StringToInt(string_value, &int_value) && | |
278 int_value >= 50 && int_value <= 500) { | |
279 g_num_gralloc_limit = int_value; | |
280 } | |
281 } | |
282 | |
283 const char kDefaultTileSize[] = "384"; | |
284 if (!cl->HasSwitch(switches::kDefaultTileWidth)) | |
285 cl->AppendSwitchASCII(switches::kDefaultTileWidth, kDefaultTileSize); | |
286 | |
287 if (!cl->HasSwitch(switches::kDefaultTileHeight)) | |
288 cl->AppendSwitchASCII(switches::kDefaultTileHeight, kDefaultTileSize); | |
289 } | |
290 | |
291 bool InProcessViewRenderer::RequestProcessGL() { | |
292 return client_->RequestDrawGL(NULL); | |
293 } | |
294 | |
295 void InProcessViewRenderer::TrimMemory(int level) { | |
296 // Constants from Android ComponentCallbacks2. | 164 // Constants from Android ComponentCallbacks2. |
297 enum { | 165 enum { |
298 TRIM_MEMORY_RUNNING_LOW = 10, | 166 TRIM_MEMORY_RUNNING_LOW = 10, |
299 TRIM_MEMORY_UI_HIDDEN = 20, | 167 TRIM_MEMORY_UI_HIDDEN = 20, |
300 TRIM_MEMORY_BACKGROUND = 40, | 168 TRIM_MEMORY_BACKGROUND = 40, |
301 }; | 169 }; |
302 | 170 |
303 // Not urgent enough. TRIM_MEMORY_UI_HIDDEN is treated specially because | 171 // Not urgent enough. TRIM_MEMORY_UI_HIDDEN is treated specially because |
304 // it does not indicate memory pressure, but merely that the app is | 172 // it does not indicate memory pressure, but merely that the app is |
305 // backgrounded. | 173 // backgrounded. |
306 if (level < TRIM_MEMORY_RUNNING_LOW || level == TRIM_MEMORY_UI_HIDDEN) | 174 if (level < TRIM_MEMORY_RUNNING_LOW || level == TRIM_MEMORY_UI_HIDDEN) |
307 return; | 175 return false; |
308 | |
309 // Nothing to drop. | |
310 if (!attached_to_window_ || !hardware_initialized_ || !compositor_) | |
311 return; | |
312 | 176 |
313 // Do not release resources on view we expect to get DrawGL soon. | 177 // Do not release resources on view we expect to get DrawGL soon. |
314 if (level < TRIM_MEMORY_BACKGROUND) { | 178 if (level < TRIM_MEMORY_BACKGROUND && visible) |
315 client_->UpdateGlobalVisibleRect(); | 179 return false; |
316 if (view_visible_ && window_visible_ && | |
317 !cached_global_visible_rect_.IsEmpty()) { | |
318 return; | |
319 } | |
320 } | |
321 | 180 |
322 if (!eglGetCurrentContext()) { | 181 if (!eglGetCurrentContext()) { |
323 NOTREACHED(); | 182 NOTREACHED(); |
324 return; | 183 return false; |
325 } | 184 } |
326 | 185 |
186 DCHECK_EQ(last_egl_context_, eglGetCurrentContext()); | |
187 | |
327 // Just set the memory limit to 0 and drop all tiles. This will be reset to | 188 // Just set the memory limit to 0 and drop all tiles. This will be reset to |
328 // normal levels in the next DrawGL call. | 189 // normal levels in the next DrawGL call. |
329 content::SynchronousCompositorMemoryPolicy policy; | 190 content::SynchronousCompositorMemoryPolicy policy; |
330 policy.bytes_limit = 0; | 191 policy.bytes_limit = 0; |
331 policy.num_resources_limit = 0; | 192 policy.num_resources_limit = 0; |
332 if (memory_policy_ == policy) | 193 if (memory_policy_ == policy) |
333 return; | 194 return false; |
334 | 195 |
335 TRACE_EVENT0("android_webview", "InProcessViewRenderer::TrimMemory"); | 196 TRACE_EVENT0("android_webview", "BrowserViewRenderer::TrimMemory"); |
336 ScopedAppGLStateRestore state_restore( | 197 ScopedAppGLStateRestore state_restore( |
337 ScopedAppGLStateRestore::MODE_RESOURCE_MANAGEMENT); | 198 ScopedAppGLStateRestore::MODE_RESOURCE_MANAGEMENT); |
338 g_service.Get()->RunTasks(); | 199 internal::ScopedAllowGL allow_gl; |
339 ScopedAllowGL allow_gl; | |
340 | 200 |
341 SetMemoryPolicy(policy); | 201 SetMemoryPolicy(policy); |
342 ForceFakeCompositeSW(); | 202 return true; |
343 } | 203 } |
344 | 204 |
345 void InProcessViewRenderer::SetMemoryPolicy( | 205 void HardwareRenderer::SetMemoryPolicy( |
346 content::SynchronousCompositorMemoryPolicy& new_policy) { | 206 content::SynchronousCompositorMemoryPolicy& new_policy) { |
347 if (memory_policy_ == new_policy) | 207 if (memory_policy_ == new_policy) |
348 return; | 208 return; |
349 | 209 |
350 memory_policy_ = new_policy; | 210 memory_policy_ = new_policy; |
351 compositor_->SetMemoryPolicy(memory_policy_); | 211 compositor_->SetMemoryPolicy(memory_policy_); |
352 } | 212 } |
353 | 213 |
354 void InProcessViewRenderer::UpdateCachedGlobalVisibleRect() { | 214 // static |
355 client_->UpdateGlobalVisibleRect(); | 215 void HardwareRenderer::CalculateTileMemoryPolicy() { |
216 CommandLine* cl = CommandLine::ForCurrentProcess(); | |
217 if (cl->HasSwitch(switches::kTileMemoryMultiplier)) { | |
218 std::string string_value = | |
219 cl->GetSwitchValueASCII(switches::kTileMemoryMultiplier); | |
220 int int_value = 0; | |
221 if (base::StringToInt(string_value, &int_value) && int_value >= 2 && | |
222 int_value <= 50) { | |
223 g_memory_multiplier = int_value; | |
224 } | |
225 } | |
226 | |
227 if (cl->HasSwitch(switches::kNumGrallocBuffersPerWebview)) { | |
228 std::string string_value = | |
229 cl->GetSwitchValueASCII(switches::kNumGrallocBuffersPerWebview); | |
230 int int_value = 0; | |
231 if (base::StringToInt(string_value, &int_value) && int_value >= 50 && | |
232 int_value <= 500) { | |
benm (inactive)
2014/02/25 12:48:01
nit:consider making the magic numbers consts? here
boliu
2014/02/25 19:02:22
Screw this. Even I'm not using these anymore. Remo
| |
233 g_num_gralloc_limit = int_value; | |
234 } | |
235 } | |
236 | |
237 const char kDefaultTileSize[] = "384"; | |
238 if (!cl->HasSwitch(switches::kDefaultTileWidth)) | |
239 cl->AppendSwitchASCII(switches::kDefaultTileWidth, kDefaultTileSize); | |
240 | |
241 if (!cl->HasSwitch(switches::kDefaultTileHeight)) | |
242 cl->AppendSwitchASCII(switches::kDefaultTileHeight, kDefaultTileSize); | |
356 } | 243 } |
357 | 244 |
358 bool InProcessViewRenderer::OnDraw(jobject java_canvas, | 245 namespace internal { |
359 bool is_hardware_canvas, | 246 |
360 const gfx::Vector2d& scroll, | 247 bool ScopedAllowGL::allow_gl = false; |
361 const gfx::Rect& clip) { | 248 |
362 scroll_at_start_of_frame_ = scroll; | 249 // static |
363 if (clear_view_) | 250 bool ScopedAllowGL::IsAllowed() { |
364 return false; | 251 return GLViewRendererManager::GetInstance()->OnRenderThread() && allow_gl; |
365 if (is_hardware_canvas && attached_to_window_ && HardwareEnabled()) { | |
366 // We should be performing a hardware draw here. If we don't have the | |
367 // comositor yet or if RequestDrawGL fails, it means we failed this draw and | |
368 // thus return false here to clear to background color for this draw. | |
369 return compositor_ && client_->RequestDrawGL(java_canvas); | |
370 } | |
371 // Perform a software draw | |
372 return DrawSWInternal(java_canvas, clip); | |
373 } | 252 } |
374 | 253 |
375 bool InProcessViewRenderer::InitializeHwDraw() { | 254 ScopedAllowGL::ScopedAllowGL() { |
376 TRACE_EVENT0("android_webview", "InitializeHwDraw"); | 255 DCHECK(GLViewRendererManager::GetInstance()->OnRenderThread()); |
377 DCHECK(!gl_surface_); | 256 DCHECK(!allow_gl); |
378 gl_surface_ = new AwGLSurface; | 257 allow_gl = true; |
379 if (!g_service.Get()) { | |
380 g_service.Get() = new DeferredGpuCommandService; | |
381 content::SynchronousCompositor::SetGpuService(g_service.Get()); | |
382 } | |
383 hardware_failed_ = !compositor_->InitializeHwDraw(gl_surface_); | |
384 hardware_initialized_ = true; | |
385 | 258 |
386 if (hardware_failed_) | 259 if (g_service.Get()) |
387 gl_surface_ = NULL; | 260 g_service.Get()->RunTasks(); |
388 | |
389 return !hardware_failed_; | |
390 } | 261 } |
391 | 262 |
392 void InProcessViewRenderer::DrawGL(AwDrawGLInfo* draw_info) { | 263 ScopedAllowGL::~ScopedAllowGL() { allow_gl = false; } |
393 TRACE_EVENT0("android_webview", "InProcessViewRenderer::DrawGL"); | |
394 | 264 |
395 manager_key_ = g_view_renderer_manager.Get().DidDrawGL(manager_key_, this); | 265 DeferredGpuCommandService::DeferredGpuCommandService() {} |
396 | 266 |
397 // We need to watch if the current Android context has changed and enforce | 267 DeferredGpuCommandService::~DeferredGpuCommandService() { |
398 // a clean-up in the compositor. | 268 base::AutoLock lock(tasks_lock_); |
399 EGLContext current_context = eglGetCurrentContext(); | 269 DCHECK(tasks_.empty()); |
400 if (!current_context) { | 270 } |
401 TRACE_EVENT_INSTANT0( | 271 |
402 "android_webview", "EarlyOut_NullEGLContext", TRACE_EVENT_SCOPE_THREAD); | 272 // static |
273 void DeferredGpuCommandService::RequestProcessGLOnUIThread() { | |
274 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
275 BrowserThread::PostTask( | |
276 BrowserThread::UI, FROM_HERE, base::Bind(&RequestProcessGLOnUIThread)); | |
403 return; | 277 return; |
404 } | 278 } |
sgurun-gerrit only
2014/02/25 06:56:36
slightly confusing. It is probably better to have
| |
405 | 279 |
406 ScopedAppGLStateRestore state_restore(ScopedAppGLStateRestore::MODE_DRAW); | 280 HardwareRenderer* renderer = |
407 if (g_service.Get()) | 281 GLViewRendererManager::GetInstance()->GetMostRecentlyDrawn(); |
408 g_service.Get()->RunTasks(); | 282 if (!renderer || !renderer->client_->RequestDrawGL(NULL)) { |
409 ScopedAllowGL allow_gl; | 283 LOG(ERROR) << "Failed to request GL process. Deadlock likely: " |
410 | 284 << !!renderer; |
411 if (!attached_to_window_) { | |
412 TRACE_EVENT_INSTANT0( | |
413 "android_webview", "EarlyOut_NotAttached", TRACE_EVENT_SCOPE_THREAD); | |
414 return; | |
415 } | |
416 | |
417 if (draw_info->mode == AwDrawGLInfo::kModeProcess) { | |
418 TRACE_EVENT_INSTANT0( | |
419 "android_webview", "EarlyOut_ModeProcess", TRACE_EVENT_SCOPE_THREAD); | |
420 return; | |
421 } | |
422 | |
423 if (compositor_ && !hardware_initialized_) { | |
424 if (InitializeHwDraw()) { | |
425 last_egl_context_ = current_context; | |
426 } else { | |
427 TRACE_EVENT_INSTANT0( | |
428 "android_webview", "EarlyOut_HwInitFail", TRACE_EVENT_SCOPE_THREAD); | |
429 LOG(ERROR) << "WebView hardware initialization failed"; | |
430 return; | |
431 } | |
432 } | |
433 | |
434 UpdateCachedGlobalVisibleRect(); | |
435 if (cached_global_visible_rect_.IsEmpty()) { | |
436 TRACE_EVENT_INSTANT0("android_webview", | |
437 "EarlyOut_EmptyVisibleRect", | |
438 TRACE_EVENT_SCOPE_THREAD); | |
439 return; | |
440 } | |
441 | |
442 if (last_egl_context_ != current_context) { | |
443 // TODO(boliu): Handle context lost | |
444 TRACE_EVENT_INSTANT0( | |
445 "android_webview", "EGLContextChanged", TRACE_EVENT_SCOPE_THREAD); | |
446 } | |
447 | |
448 if (!compositor_) { | |
449 TRACE_EVENT_INSTANT0( | |
450 "android_webview", "EarlyOut_NoCompositor", TRACE_EVENT_SCOPE_THREAD); | |
451 return; | |
452 } | |
453 | |
454 // DrawGL may be called without OnDraw, so cancel |fallback_tick_| here as | |
455 // well just to be safe. | |
456 fallback_tick_.Cancel(); | |
457 | |
458 // Update memory budget. This will no-op in compositor if the policy has not | |
459 // changed since last draw. | |
460 content::SynchronousCompositorMemoryPolicy policy; | |
461 policy.bytes_limit = g_memory_multiplier * kBytesPerPixel * | |
462 cached_global_visible_rect_.width() * | |
463 cached_global_visible_rect_.height(); | |
464 // Round up to a multiple of kMemoryAllocationStep. | |
465 policy.bytes_limit = | |
466 (policy.bytes_limit / kMemoryAllocationStep + 1) * kMemoryAllocationStep; | |
467 policy.num_resources_limit = g_num_gralloc_limit; | |
468 SetMemoryPolicy(policy); | |
469 | |
470 DCHECK(gl_surface_); | |
471 gl_surface_->SetBackingFrameBufferObject( | |
472 state_restore.framebuffer_binding_ext()); | |
473 | |
474 gfx::Transform transform; | |
475 transform.matrix().setColMajorf(draw_info->transform); | |
476 transform.Translate(scroll_at_start_of_frame_.x(), | |
477 scroll_at_start_of_frame_.y()); | |
478 gfx::Rect clip_rect(draw_info->clip_left, | |
479 draw_info->clip_top, | |
480 draw_info->clip_right - draw_info->clip_left, | |
481 draw_info->clip_bottom - draw_info->clip_top); | |
482 | |
483 // Assume we always draw the full visible rect if we are drawing into a layer. | |
484 bool drew_full_visible_rect = true; | |
485 | |
486 gfx::Rect viewport_rect; | |
487 if (!draw_info->is_layer) { | |
488 viewport_rect = cached_global_visible_rect_; | |
489 clip_rect.Intersect(viewport_rect); | |
490 drew_full_visible_rect = clip_rect.Contains(viewport_rect); | |
491 } else { | |
492 viewport_rect = clip_rect; | |
493 } | |
494 | |
495 block_invalidates_ = true; | |
496 // TODO(joth): Check return value. | |
497 compositor_->DemandDrawHw(gfx::Size(draw_info->width, draw_info->height), | |
498 transform, | |
499 viewport_rect, | |
500 clip_rect, | |
501 state_restore.stencil_enabled()); | |
502 block_invalidates_ = false; | |
503 gl_surface_->ResetBackingFrameBufferObject(); | |
504 | |
505 EnsureContinuousInvalidation(draw_info, !drew_full_visible_rect); | |
506 } | |
507 | |
508 void InProcessViewRenderer::SetGlobalVisibleRect( | |
509 const gfx::Rect& visible_rect) { | |
510 cached_global_visible_rect_ = visible_rect; | |
511 } | |
512 | |
513 bool InProcessViewRenderer::DrawSWInternal(jobject java_canvas, | |
514 const gfx::Rect& clip) { | |
515 if (clip.IsEmpty()) { | |
516 TRACE_EVENT_INSTANT0( | |
517 "android_webview", "EarlyOut_EmptyClip", TRACE_EVENT_SCOPE_THREAD); | |
518 return true; | |
519 } | |
520 | |
521 if (!compositor_) { | |
522 TRACE_EVENT_INSTANT0( | |
523 "android_webview", "EarlyOut_NoCompositor", TRACE_EVENT_SCOPE_THREAD); | |
524 return false; | |
525 } | |
526 | |
527 return JavaHelper::GetInstance()->RenderViaAuxilaryBitmapIfNeeded( | |
528 java_canvas, | |
529 scroll_at_start_of_frame_, | |
530 clip, | |
531 base::Bind(&InProcessViewRenderer::CompositeSW, base::Unretained(this))); | |
532 } | |
533 | |
534 skia::RefPtr<SkPicture> InProcessViewRenderer::CapturePicture(int width, | |
535 int height) { | |
536 TRACE_EVENT0("android_webview", "InProcessViewRenderer::CapturePicture"); | |
537 | |
538 // Return empty Picture objects for empty SkPictures. | |
539 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); | |
540 if (width <= 0 || height <= 0) { | |
541 return picture; | |
542 } | |
543 | |
544 // Reset scroll back to the origin, will go back to the old | |
545 // value when scroll_reset is out of scope. | |
546 base::AutoReset<gfx::Vector2dF> scroll_reset(&scroll_offset_dip_, | |
547 gfx::Vector2d()); | |
548 | |
549 SkCanvas* rec_canvas = picture->beginRecording(width, height, 0); | |
550 if (compositor_) | |
551 CompositeSW(rec_canvas); | |
552 picture->endRecording(); | |
553 return picture; | |
554 } | |
555 | |
556 void InProcessViewRenderer::EnableOnNewPicture(bool enabled) { | |
557 on_new_picture_enable_ = enabled; | |
558 EnsureContinuousInvalidation(NULL, false); | |
559 } | |
560 | |
561 void InProcessViewRenderer::ClearView() { | |
562 TRACE_EVENT_INSTANT0("android_webview", | |
563 "InProcessViewRenderer::ClearView", | |
564 TRACE_EVENT_SCOPE_THREAD); | |
565 if (clear_view_) | |
566 return; | |
567 | |
568 clear_view_ = true; | |
569 // Always invalidate ignoring the compositor to actually clear the webview. | |
570 EnsureContinuousInvalidation(NULL, true); | |
571 } | |
572 | |
573 void InProcessViewRenderer::SetIsPaused(bool paused) { | |
574 TRACE_EVENT_INSTANT1("android_webview", | |
575 "InProcessViewRenderer::SetIsPaused", | |
576 TRACE_EVENT_SCOPE_THREAD, | |
577 "paused", | |
578 paused); | |
579 is_paused_ = paused; | |
580 EnsureContinuousInvalidation(NULL, false); | |
581 } | |
582 | |
583 void InProcessViewRenderer::SetViewVisibility(bool view_visible) { | |
584 TRACE_EVENT_INSTANT1("android_webview", | |
585 "InProcessViewRenderer::SetViewVisibility", | |
586 TRACE_EVENT_SCOPE_THREAD, | |
587 "view_visible", | |
588 view_visible); | |
589 view_visible_ = view_visible; | |
590 } | |
591 | |
592 void InProcessViewRenderer::SetWindowVisibility(bool window_visible) { | |
593 TRACE_EVENT_INSTANT1("android_webview", | |
594 "InProcessViewRenderer::SetWindowVisibility", | |
595 TRACE_EVENT_SCOPE_THREAD, | |
596 "window_visible", | |
597 window_visible); | |
598 window_visible_ = window_visible; | |
599 EnsureContinuousInvalidation(NULL, false); | |
600 } | |
601 | |
602 void InProcessViewRenderer::OnSizeChanged(int width, int height) { | |
603 TRACE_EVENT_INSTANT2("android_webview", | |
604 "InProcessViewRenderer::OnSizeChanged", | |
605 TRACE_EVENT_SCOPE_THREAD, | |
606 "width", | |
607 width, | |
608 "height", | |
609 height); | |
610 width_ = width; | |
611 height_ = height; | |
612 } | |
613 | |
614 void InProcessViewRenderer::OnAttachedToWindow(int width, int height) { | |
615 TRACE_EVENT2("android_webview", | |
616 "InProcessViewRenderer::OnAttachedToWindow", | |
617 "width", | |
618 width, | |
619 "height", | |
620 height); | |
621 attached_to_window_ = true; | |
622 width_ = width; | |
623 height_ = height; | |
624 } | |
625 | |
626 void InProcessViewRenderer::OnDetachedFromWindow() { | |
627 TRACE_EVENT0("android_webview", | |
628 "InProcessViewRenderer::OnDetachedFromWindow"); | |
629 | |
630 NoLongerExpectsDrawGL(); | |
631 if (hardware_initialized_) { | |
632 DCHECK(compositor_); | |
633 | |
634 ScopedAppGLStateRestore state_restore( | |
635 ScopedAppGLStateRestore::MODE_RESOURCE_MANAGEMENT); | |
636 g_service.Get()->RunTasks(); | |
637 ScopedAllowGL allow_gl; | |
638 compositor_->ReleaseHwDraw(); | |
639 hardware_initialized_ = false; | |
640 } | |
641 | |
642 gl_surface_ = NULL; | |
643 attached_to_window_ = false; | |
644 } | |
645 | |
646 bool InProcessViewRenderer::IsAttachedToWindow() { | |
647 return attached_to_window_; | |
648 } | |
649 | |
650 bool InProcessViewRenderer::IsVisible() { | |
651 // Ignore |window_visible_| if |attached_to_window_| is false. | |
652 return view_visible_ && (!attached_to_window_ || window_visible_); | |
653 } | |
654 | |
655 gfx::Rect InProcessViewRenderer::GetScreenRect() { | |
656 return gfx::Rect(client_->GetLocationOnScreen(), gfx::Size(width_, height_)); | |
657 } | |
658 | |
659 void InProcessViewRenderer::DidInitializeCompositor( | |
660 content::SynchronousCompositor* compositor) { | |
661 TRACE_EVENT0("android_webview", | |
662 "InProcessViewRenderer::DidInitializeCompositor"); | |
663 DCHECK(compositor && compositor_ == NULL); | |
664 compositor_ = compositor; | |
665 hardware_initialized_ = false; | |
666 hardware_failed_ = false; | |
667 } | |
668 | |
669 void InProcessViewRenderer::DidDestroyCompositor( | |
670 content::SynchronousCompositor* compositor) { | |
671 TRACE_EVENT0("android_webview", | |
672 "InProcessViewRenderer::DidDestroyCompositor"); | |
673 DCHECK(compositor_ == compositor); | |
674 | |
675 // This can fail if Apps call destroy while the webview is still attached | |
676 // to the view tree. This is an illegal operation that will lead to leaks. | |
677 // Log for now. Consider a proper fix if this becomes a problem. | |
678 LOG_IF(ERROR, hardware_initialized_) | |
679 << "Destroy called before OnDetachedFromWindow. May Leak GL resources"; | |
680 compositor_ = NULL; | |
681 } | |
682 | |
683 void InProcessViewRenderer::SetContinuousInvalidate(bool invalidate) { | |
684 if (compositor_needs_continuous_invalidate_ == invalidate) | |
685 return; | |
686 | |
687 TRACE_EVENT_INSTANT1("android_webview", | |
688 "InProcessViewRenderer::SetContinuousInvalidate", | |
689 TRACE_EVENT_SCOPE_THREAD, | |
690 "invalidate", | |
691 invalidate); | |
692 compositor_needs_continuous_invalidate_ = invalidate; | |
693 EnsureContinuousInvalidation(NULL, false); | |
694 } | |
695 | |
696 void InProcessViewRenderer::SetDipScale(float dip_scale) { | |
697 dip_scale_ = dip_scale; | |
698 CHECK(dip_scale_ > 0); | |
699 } | |
700 | |
701 gfx::Vector2d InProcessViewRenderer::max_scroll_offset() const { | |
702 DCHECK_GT(dip_scale_, 0); | |
703 return gfx::ToCeiledVector2d(gfx::ScaleVector2d( | |
704 max_scroll_offset_dip_, dip_scale_ * page_scale_factor_)); | |
705 } | |
706 | |
707 void InProcessViewRenderer::ScrollTo(gfx::Vector2d scroll_offset) { | |
708 gfx::Vector2d max_offset = max_scroll_offset(); | |
709 gfx::Vector2dF scroll_offset_dip; | |
710 // To preserve the invariant that scrolling to the maximum physical pixel | |
711 // value also scrolls to the maximum dip pixel value we transform the physical | |
712 // offset into the dip offset by using a proportion (instead of dividing by | |
713 // dip_scale * page_scale_factor). | |
714 if (max_offset.x()) { | |
715 scroll_offset_dip.set_x((scroll_offset.x() * max_scroll_offset_dip_.x()) / | |
716 max_offset.x()); | |
717 } | |
718 if (max_offset.y()) { | |
719 scroll_offset_dip.set_y((scroll_offset.y() * max_scroll_offset_dip_.y()) / | |
720 max_offset.y()); | |
721 } | |
722 | |
723 DCHECK_LE(0, scroll_offset_dip.x()); | |
724 DCHECK_LE(0, scroll_offset_dip.y()); | |
725 DCHECK_LE(scroll_offset_dip.x(), max_scroll_offset_dip_.x()); | |
726 DCHECK_LE(scroll_offset_dip.y(), max_scroll_offset_dip_.y()); | |
727 | |
728 if (scroll_offset_dip_ == scroll_offset_dip) | |
729 return; | |
730 | |
731 scroll_offset_dip_ = scroll_offset_dip; | |
732 | |
733 if (compositor_) | |
734 compositor_->DidChangeRootLayerScrollOffset(); | |
735 } | |
736 | |
737 void InProcessViewRenderer::DidUpdateContent() { | |
738 TRACE_EVENT_INSTANT0("android_webview", | |
739 "InProcessViewRenderer::DidUpdateContent", | |
740 TRACE_EVENT_SCOPE_THREAD); | |
741 clear_view_ = false; | |
742 EnsureContinuousInvalidation(NULL, false); | |
743 if (on_new_picture_enable_) | |
744 client_->OnNewPicture(); | |
745 } | |
746 | |
747 void InProcessViewRenderer::SetMaxRootLayerScrollOffset( | |
748 gfx::Vector2dF new_value_dip) { | |
749 DCHECK_GT(dip_scale_, 0); | |
750 | |
751 max_scroll_offset_dip_ = new_value_dip; | |
752 DCHECK_LE(0, max_scroll_offset_dip_.x()); | |
753 DCHECK_LE(0, max_scroll_offset_dip_.y()); | |
754 | |
755 client_->SetMaxContainerViewScrollOffset(max_scroll_offset()); | |
756 } | |
757 | |
758 void InProcessViewRenderer::SetTotalRootLayerScrollOffset( | |
759 gfx::Vector2dF scroll_offset_dip) { | |
760 // TOOD(mkosiba): Add a DCHECK to say that this does _not_ get called during | |
761 // DrawGl when http://crbug.com/249972 is fixed. | |
762 if (scroll_offset_dip_ == scroll_offset_dip) | |
763 return; | |
764 | |
765 scroll_offset_dip_ = scroll_offset_dip; | |
766 | |
767 gfx::Vector2d max_offset = max_scroll_offset(); | |
768 gfx::Vector2d scroll_offset; | |
769 // For an explanation as to why this is done this way see the comment in | |
770 // InProcessViewRenderer::ScrollTo. | |
771 if (max_scroll_offset_dip_.x()) { | |
772 scroll_offset.set_x((scroll_offset_dip.x() * max_offset.x()) / | |
773 max_scroll_offset_dip_.x()); | |
774 } | |
775 | |
776 if (max_scroll_offset_dip_.y()) { | |
777 scroll_offset.set_y((scroll_offset_dip.y() * max_offset.y()) / | |
778 max_scroll_offset_dip_.y()); | |
779 } | |
780 | |
781 DCHECK(0 <= scroll_offset.x()); | |
782 DCHECK(0 <= scroll_offset.y()); | |
783 // Disabled because the conditions are being violated while running | |
784 // AwZoomTest.testMagnification, see http://crbug.com/340648 | |
785 // DCHECK(scroll_offset.x() <= max_offset.x()); | |
786 // DCHECK(scroll_offset.y() <= max_offset.y()); | |
787 | |
788 client_->ScrollContainerViewTo(scroll_offset); | |
789 } | |
790 | |
791 gfx::Vector2dF InProcessViewRenderer::GetTotalRootLayerScrollOffset() { | |
792 return scroll_offset_dip_; | |
793 } | |
794 | |
795 bool InProcessViewRenderer::IsExternalFlingActive() const { | |
796 return client_->IsFlingActive(); | |
797 } | |
798 | |
799 void InProcessViewRenderer::SetRootLayerPageScaleFactorAndLimits( | |
800 float page_scale_factor, | |
801 float min_page_scale_factor, | |
802 float max_page_scale_factor) { | |
803 page_scale_factor_ = page_scale_factor; | |
804 DCHECK_GT(page_scale_factor_, 0); | |
805 client_->SetPageScaleFactorAndLimits( | |
806 page_scale_factor, min_page_scale_factor, max_page_scale_factor); | |
807 } | |
808 | |
809 void InProcessViewRenderer::SetRootLayerScrollableSize( | |
810 gfx::SizeF scrollable_size) { | |
811 client_->SetContentsSize(scrollable_size); | |
812 } | |
813 | |
814 void InProcessViewRenderer::DidOverscroll( | |
815 gfx::Vector2dF accumulated_overscroll, | |
816 gfx::Vector2dF latest_overscroll_delta, | |
817 gfx::Vector2dF current_fling_velocity) { | |
818 const float physical_pixel_scale = dip_scale_ * page_scale_factor_; | |
819 if (accumulated_overscroll == latest_overscroll_delta) | |
820 overscroll_rounding_error_ = gfx::Vector2dF(); | |
821 gfx::Vector2dF scaled_overscroll_delta = | |
822 gfx::ScaleVector2d(latest_overscroll_delta, physical_pixel_scale); | |
823 gfx::Vector2d rounded_overscroll_delta = gfx::ToRoundedVector2d( | |
824 scaled_overscroll_delta + overscroll_rounding_error_); | |
825 overscroll_rounding_error_ = | |
826 scaled_overscroll_delta - rounded_overscroll_delta; | |
827 client_->DidOverscroll(rounded_overscroll_delta); | |
828 } | |
829 | |
830 void InProcessViewRenderer::EnsureContinuousInvalidation( | |
831 AwDrawGLInfo* draw_info, | |
832 bool invalidate_ignore_compositor) { | |
833 // This method should be called again when any of these conditions change. | |
834 bool need_invalidate = | |
835 compositor_needs_continuous_invalidate_ || invalidate_ignore_compositor; | |
836 if (!need_invalidate || block_invalidates_) | |
837 return; | |
838 | |
839 // Always call view invalidate. We rely the Android framework to ignore the | |
840 // invalidate when it's not needed such as when view is not visible. | |
841 if (draw_info) { | |
842 draw_info->dirty_left = cached_global_visible_rect_.x(); | |
843 draw_info->dirty_top = cached_global_visible_rect_.y(); | |
844 draw_info->dirty_right = cached_global_visible_rect_.right(); | |
845 draw_info->dirty_bottom = cached_global_visible_rect_.bottom(); | |
846 draw_info->status_mask |= AwDrawGLInfo::kStatusMaskDraw; | |
847 } else { | |
848 client_->PostInvalidate(); | |
849 } | |
850 | |
851 // Stop fallback ticks when one of these is true. | |
852 // 1) Webview is paused. Also need to check we are not in clear view since | |
853 // paused, offscreen still expect clear view to recover. | |
854 // 2) If we are attached to window and the window is not visible (eg when | |
855 // app is in the background). We are sure in this case the webview is used | |
856 // "on-screen" but that updates are not needed when in the background. | |
857 bool throttle_fallback_tick = | |
858 (is_paused_ && !clear_view_) || (attached_to_window_ && !window_visible_); | |
859 if (throttle_fallback_tick) | |
860 return; | |
861 | |
862 block_invalidates_ = compositor_needs_continuous_invalidate_; | |
863 | |
864 // Unretained here is safe because the callback is cancelled when | |
865 // |fallback_tick_| is destroyed. | |
866 fallback_tick_.Reset(base::Bind(&InProcessViewRenderer::FallbackTickFired, | |
867 base::Unretained(this))); | |
868 | |
869 // No need to reschedule fallback tick if compositor does not need to be | |
870 // ticked. This can happen if this is reached because | |
871 // invalidate_ignore_compositor is true. | |
872 if (compositor_needs_continuous_invalidate_) { | |
873 BrowserThread::PostDelayedTask( | |
874 BrowserThread::UI, | |
875 FROM_HERE, | |
876 fallback_tick_.callback(), | |
877 base::TimeDelta::FromMilliseconds( | |
878 kFallbackTickTimeoutInMilliseconds)); | |
879 } | 285 } |
880 } | 286 } |
881 | 287 |
882 void InProcessViewRenderer::FallbackTickFired() { | 288 // Called from different threads! |
883 TRACE_EVENT1("android_webview", | 289 void DeferredGpuCommandService::ScheduleTask(const base::Closure& task) { |
884 "InProcessViewRenderer::FallbackTickFired", | 290 { |
885 "compositor_needs_continuous_invalidate_", | 291 base::AutoLock lock(tasks_lock_); |
886 compositor_needs_continuous_invalidate_); | 292 tasks_.push(task); |
887 | 293 } |
888 // This should only be called if OnDraw or DrawGL did not come in time, which | 294 if (ScopedAllowGL::IsAllowed()) { |
889 // means block_invalidates_ must still be true. | 295 RunTasks(); |
890 DCHECK(block_invalidates_); | 296 } else { |
891 if (compositor_needs_continuous_invalidate_ && compositor_) | 297 RequestProcessGLOnUIThread(); |
892 ForceFakeCompositeSW(); | 298 } |
893 } | 299 } |
894 | 300 |
895 void InProcessViewRenderer::ForceFakeCompositeSW() { | 301 void DeferredGpuCommandService::ScheduleIdleWork( |
896 DCHECK(compositor_); | 302 const base::Closure& callback) { |
897 SkBitmapDevice device(SkBitmap::kARGB_8888_Config, 1, 1); | 303 // TODO(sievers): Should this do anything? |
898 SkCanvas canvas(&device); | |
899 CompositeSW(&canvas); | |
900 } | 304 } |
901 | 305 |
902 bool InProcessViewRenderer::CompositeSW(SkCanvas* canvas) { | 306 bool DeferredGpuCommandService::UseVirtualizedGLContexts() { return true; } |
903 DCHECK(compositor_); | |
904 | 307 |
905 fallback_tick_.Cancel(); | 308 void DeferredGpuCommandService::RunTasks() { |
906 block_invalidates_ = true; | 309 bool has_more_tasks; |
907 bool result = compositor_->DemandDrawSw(canvas); | 310 { |
908 block_invalidates_ = false; | 311 base::AutoLock lock(tasks_lock_); |
909 EnsureContinuousInvalidation(NULL, false); | 312 has_more_tasks = tasks_.size() > 0; |
910 return result; | 313 } |
314 | |
315 while (has_more_tasks) { | |
316 base::Closure task; | |
317 { | |
318 base::AutoLock lock(tasks_lock_); | |
319 task = tasks_.front(); | |
320 tasks_.pop(); | |
321 } | |
322 task.Run(); | |
323 { | |
324 base::AutoLock lock(tasks_lock_); | |
325 has_more_tasks = tasks_.size() > 0; | |
326 } | |
327 } | |
911 } | 328 } |
912 | 329 |
913 std::string InProcessViewRenderer::ToString(AwDrawGLInfo* draw_info) const { | 330 void DeferredGpuCommandService::AddRef() const { |
914 std::string str; | 331 base::RefCountedThreadSafe<DeferredGpuCommandService>::AddRef(); |
915 base::StringAppendF(&str, "is_paused: %d ", is_paused_); | |
916 base::StringAppendF(&str, "view_visible: %d ", view_visible_); | |
917 base::StringAppendF(&str, "window_visible: %d ", window_visible_); | |
918 base::StringAppendF(&str, "dip_scale: %f ", dip_scale_); | |
919 base::StringAppendF(&str, "page_scale_factor: %f ", page_scale_factor_); | |
920 base::StringAppendF(&str, | |
921 "compositor_needs_continuous_invalidate: %d ", | |
922 compositor_needs_continuous_invalidate_); | |
923 base::StringAppendF(&str, "block_invalidates: %d ", block_invalidates_); | |
924 base::StringAppendF(&str, "view width height: [%d %d] ", width_, height_); | |
925 base::StringAppendF(&str, "attached_to_window: %d ", attached_to_window_); | |
926 base::StringAppendF(&str, "hardware_initialized: %d ", hardware_initialized_); | |
927 base::StringAppendF(&str, "hardware_failed: %d ", hardware_failed_); | |
928 base::StringAppendF(&str, | |
929 "global visible rect: %s ", | |
930 cached_global_visible_rect_.ToString().c_str()); | |
931 base::StringAppendF(&str, | |
932 "scroll_at_start_of_frame: %s ", | |
933 scroll_at_start_of_frame_.ToString().c_str()); | |
934 base::StringAppendF( | |
935 &str, "scroll_offset_dip: %s ", scroll_offset_dip_.ToString().c_str()); | |
936 base::StringAppendF(&str, | |
937 "overscroll_rounding_error_: %s ", | |
938 overscroll_rounding_error_.ToString().c_str()); | |
939 base::StringAppendF( | |
940 &str, "on_new_picture_enable: %d ", on_new_picture_enable_); | |
941 base::StringAppendF(&str, "clear_view: %d ", clear_view_); | |
942 if (draw_info) { | |
943 base::StringAppendF(&str, | |
944 "clip left top right bottom: [%d %d %d %d] ", | |
945 draw_info->clip_left, | |
946 draw_info->clip_top, | |
947 draw_info->clip_right, | |
948 draw_info->clip_bottom); | |
949 base::StringAppendF(&str, | |
950 "surface width height: [%d %d] ", | |
951 draw_info->width, | |
952 draw_info->height); | |
953 base::StringAppendF(&str, "is_layer: %d ", draw_info->is_layer); | |
954 } | |
955 return str; | |
956 } | 332 } |
957 | 333 |
334 void DeferredGpuCommandService::Release() const { | |
335 base::RefCountedThreadSafe<DeferredGpuCommandService>::Release(); | |
336 } | |
337 | |
338 } // namespace internal | |
339 | |
958 } // namespace android_webview | 340 } // namespace android_webview |
OLD | NEW |