blob: 8bb2c60e7dbe5fa407be73c173b6405a83e1cd77 [file] [log] [blame]
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01001// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +01005#ifndef ANDROID_WEBVIEW_BROWSER_IN_PROCESS_VIEW_RENDERER_H_
6#define ANDROID_WEBVIEW_BROWSER_IN_PROCESS_VIEW_RENDERER_H_
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01007
Ben Murdochca12bfa2013-07-23 11:17:05 +01008#include <string>
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01009
Ben Murdochca12bfa2013-07-23 11:17:05 +010010#include "android_webview/browser/browser_view_renderer.h"
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010011#include "android_webview/browser/gl_view_renderer_manager.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010012#include "base/cancelable_callback.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010013#include "content/public/browser/android/synchronous_compositor_client.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010014#include "ui/gfx/vector2d_f.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010015
16namespace content {
17class SynchronousCompositor;
18class WebContents;
19}
20
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010021typedef void* EGLContext;
22class SkCanvas;
23
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010024namespace android_webview {
25
Ben Murdochbb1529c2013-08-08 10:24:53 +010026class AwGLSurface;
27
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010028// Provides RenderViewHost wrapper functionality for sending WebView-specific
29// IPC messages to the renderer and from there to WebKit.
30class InProcessViewRenderer : public BrowserViewRenderer,
31 public content::SynchronousCompositorClient {
32 public:
33 InProcessViewRenderer(BrowserViewRenderer::Client* client,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010034 JavaHelper* java_helper,
35 content::WebContents* web_contents);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010036 virtual ~InProcessViewRenderer();
37
38 static InProcessViewRenderer* FromWebContents(
39 content::WebContents* contents);
40
41 // BrowserViewRenderer overrides
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010042 virtual bool OnDraw(jobject java_canvas,
43 bool is_hardware_canvas,
Ben Murdocheb525c52013-07-10 11:40:50 +010044 const gfx::Vector2d& scroll_,
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010045 const gfx::Rect& clip) OVERRIDE;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010046 virtual void DrawGL(AwDrawGLInfo* draw_info) OVERRIDE;
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010047 virtual void SetGlobalVisibleRect(const gfx::Rect& visible_rect) OVERRIDE;
Ben Murdoch558790d2013-07-30 15:19:42 +010048 virtual base::android::ScopedJavaLocalRef<jobject> CapturePicture(
49 int width,
50 int height) OVERRIDE;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010051 virtual void EnableOnNewPicture(bool enabled) OVERRIDE;
Ben Murdocheb525c52013-07-10 11:40:50 +010052 virtual void OnVisibilityChanged(bool visible) OVERRIDE;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010053 virtual void OnSizeChanged(int width, int height) OVERRIDE;
Ben Murdocheb525c52013-07-10 11:40:50 +010054 virtual void ScrollTo(gfx::Vector2d new_value) OVERRIDE;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010055 virtual void SetPageScaleFactor(float page_scale_factor) OVERRIDE;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010056 virtual void OnAttachedToWindow(int width, int height) OVERRIDE;
57 virtual void OnDetachedFromWindow() OVERRIDE;
Ben Murdocheb525c52013-07-10 11:40:50 +010058 virtual void SetDipScale(float dip_scale) OVERRIDE;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010059 virtual bool IsAttachedToWindow() OVERRIDE;
60 virtual bool IsViewVisible() OVERRIDE;
61 virtual gfx::Rect GetScreenRect() OVERRIDE;
62
63 // SynchronousCompositorClient overrides
64 virtual void DidInitializeCompositor(
65 content::SynchronousCompositor* compositor) OVERRIDE;
66 virtual void DidDestroyCompositor(
67 content::SynchronousCompositor* compositor) OVERRIDE;
68 virtual void SetContinuousInvalidate(bool invalidate) OVERRIDE;
Ben Murdocheb525c52013-07-10 11:40:50 +010069 virtual void SetTotalRootLayerScrollOffset(
70 gfx::Vector2dF new_value_css) OVERRIDE;
Ben Murdochca12bfa2013-07-23 11:17:05 +010071 virtual void DidUpdateContent() OVERRIDE;
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010072 virtual gfx::Vector2dF GetTotalRootLayerScrollOffset() OVERRIDE;
Ben Murdoch9ab55632013-07-18 11:57:30 +010073 virtual void DidOverscroll(gfx::Vector2dF latest_overscroll_delta,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010074 gfx::Vector2dF current_fling_velocity) OVERRIDE;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010075
76 void WebContentsGone();
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010077 bool RequestProcessGL();
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010078
79 private:
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010080 // Checks the continuous invalidate and block invalidate state, and schedule
81 // invalidates appropriately. If |invalidate_ignore_compositor| is true,
82 // then send a view invalidate regardless of compositor expectation.
83 void EnsureContinuousInvalidation(
84 AwDrawGLInfo* draw_info,
85 bool invalidate_ignore_compositor);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010086 bool DrawSWInternal(jobject java_canvas,
87 const gfx::Rect& clip_bounds);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010088 bool CompositeSW(SkCanvas* canvas);
89
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010090 void UpdateCachedGlobalVisibleRect();
91
Ben Murdocheb525c52013-07-10 11:40:50 +010092 // If we call up view invalidate and OnDraw is not called before a deadline,
93 // then we keep ticking the SynchronousCompositor so it can make progress.
94 void FallbackTickFired();
95
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010096 void NoLongerExpectsDrawGL();
97
Ben Murdochbb1529c2013-08-08 10:24:53 +010098 bool InitializeHwDraw();
99
Ben Murdochca12bfa2013-07-23 11:17:05 +0100100 // For debug tracing or logging. Return the string representation of this
101 // view renderer's state and the |draw_info| if provided.
102 std::string ToString(AwDrawGLInfo* draw_info) const;
103
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100104 BrowserViewRenderer::Client* client_;
105 BrowserViewRenderer::JavaHelper* java_helper_;
106 content::WebContents* web_contents_;
107 content::SynchronousCompositor* compositor_;
108
Ben Murdocheb525c52013-07-10 11:40:50 +0100109 bool visible_;
110 float dip_scale_;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100111 float page_scale_factor_;
Ben Murdochca12bfa2013-07-23 11:17:05 +0100112 bool on_new_picture_enable_;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100113
114 // When true, we should continuously invalidate and keep drawing, for example
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100115 // to drive animation. This value is set by the compositor and should always
116 // reflect the expectation of the compositor and not be reused for other
117 // states.
118 bool compositor_needs_continuous_invalidate_;
119
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100120 // Used to block additional invalidates while one is already pending or before
121 // compositor draw which may switch continuous_invalidate on and off in the
122 // process.
123 bool block_invalidates_;
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100124
Ben Murdocheb525c52013-07-10 11:40:50 +0100125 // Holds a callback to FallbackTickFired while it is pending.
126 base::CancelableClosure fallback_tick_;
127
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100128 int width_;
129 int height_;
130
131 bool attached_to_window_;
132 bool hardware_initialized_;
133 bool hardware_failed_;
Ben Murdochbb1529c2013-08-08 10:24:53 +0100134 scoped_refptr<AwGLSurface> gl_surface_;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100135
136 // Used only for detecting Android View System context changes.
137 // Not to be used between draw calls.
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100138 EGLContext last_egl_context_;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100139
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100140 // Should always call UpdateCachedGlobalVisibleRect before using this.
141 gfx::Rect cached_global_visible_rect_;
Ben Murdoch58e6fbe2013-07-26 10:20:38 +0100142
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100143 // Last View scroll when View.onDraw() was called.
Ben Murdocheb525c52013-07-10 11:40:50 +0100144 gfx::Vector2d scroll_at_start_of_frame_;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100145
Ben Murdocheb525c52013-07-10 11:40:50 +0100146 // Current scroll offset in CSS pixels.
147 gfx::Vector2dF scroll_offset_css_;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100148
Ben Murdoch9ab55632013-07-18 11:57:30 +0100149 // Used to prevent rounding errors from accumulating enough to generate
150 // visible skew (especially noticeable when scrolling up and down in the same
151 // spot over a period of time).
152 gfx::Vector2dF overscroll_rounding_error_;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100153
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100154 GLViewRendererManager::Key manager_key_;
155
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100156 DISALLOW_COPY_AND_ASSIGN(InProcessViewRenderer);
157};
158
159} // namespace android_webview
160
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100161#endif // ANDROID_WEBVIEW_BROWSER_IN_PROCESS_VIEW_RENDERER_H_