2 * Copyright 2010, The Android Open Source Project
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef GLWebViewState_h
27 #define GLWebViewState_h
29 #if USE(ACCELERATED_COMPOSITING)
32 #include "DrawExtra.h"
37 #include "TiledPage.h"
38 #include "ZoomManager.h"
39 #include <utils/threads.h>
41 // Performance measurements probe
42 // To use it, enable the visual indicators in debug mode.
43 // turning off the visual indicators will flush the measures.
44 // #define MEASURES_PERF
45 #define MAX_MEASURES_PERF 2000
47 // Prefetch and render 1 tiles ahead of the scroll
48 // TODO: We should either dynamically change the outer bound by detecting the
49 // HW limit or save further in the GPU memory consumption.
50 #define TILE_PREFETCH_DISTANCE 1
52 // ratio of content to view required for prefetching to enable
53 #define TILE_PREFETCH_RATIO 1.2
57 class BaseLayerAndroid;
60 /////////////////////////////////////////////////////////////////////////////////
62 /////////////////////////////////////////////////////////////////////////////////
64 // To draw things, WebView use a tree of layers. The root of that tree is a
65 // BaseLayerAndroid, which may have numerous LayerAndroid over it. The content
66 // of those layers are SkPicture, the content of the BaseLayer is an PictureSet.
68 // When drawing, we therefore have one large "surface" that is the BaseLayer,
69 // and (possibly) additional surfaces (usually smaller), which are the
70 // LayerAndroids. The BaseLayer usually corresponds to the normal web page
71 // content, the Layers are used for some parts such as specific divs (e.g. fixed
72 // position divs, or elements using CSS3D transforms, or containing video,
75 // *** NOTE: The GL drawing architecture only paints the BaseLayer for now.
77 // The rendering model is to use tiles to display the BaseLayer (as obviously a
78 // BaseLayer's area can be arbitrarly large). The idea is to compute a set of
79 // tiles covering the viewport's area, paint those tiles using the webview's
80 // content (i.e. the BaseLayer's PictureSet), then display those tiles.
81 // We check which tile we should use at every frame.
86 // The tiles are grouped into a TiledPage -- basically a map of tiles covering
87 // the BaseLayer's surface. When drawing, we ask the TiledPage to prepare()
88 // itself then draw itself on screen. The prepare() function is the one
89 // that schedules tiles to be painted -- i.e. the subset of tiles that intersect
90 // with the current viewport. When they are ready, we can display
93 // Note that BaseLayerAndroid::drawGL() will return true to the java side if
94 // there is a need to be called again (i.e. if we do not have up to date
95 // textures or a transition is going on).
97 // Tiles are implemented as a BaseTile. It knows how to paint itself with the
98 // PictureSet, and to display itself. A GL texture is usually associated to it.
100 // We also works with two TiledPages -- one to display the page at the
101 // current scale factor, and another we use to paint the page at a different
102 // scale factor. I.e. when we zoom, we use TiledPage A, with its tiles scaled
103 // accordingly (and therefore possible loss of quality): this is fast as it's
104 // purely a hardware operation. When the user is done zooming, we ask for
105 // TiledPage B to be painted at the new scale factor, covering the
106 // viewport's area. When B is ready, we swap it with A.
108 // Texture allocation
109 // ------------------
111 // Obviously we cannot have every BaseTile having a GL texture -- we need to
112 // get the GL textures from an existing pool, and reuse them.
114 // The way we do it is that when we call TiledPage::prepare(), we group the
115 // tiles we need (i.e. in the viewport and dirty) into a TilesSet and call
116 // BaseTile::reserveTexture() for each tile (which ensures there is a specific
117 // GL textures backing the BaseTiles).
119 // reserveTexture() will ask the TilesManager for a texture. The allocation
120 // mechanism goal is to (in order):
121 // - prefers to allocate the same texture as the previous time
122 // - prefers to allocate textures that are as far from the viewport as possible
123 // - prefers to allocate textures that are used by different TiledPages
125 // Note that to compute the distance of each tile from the viewport, each time
126 // we prepare() a TiledPage. Also during each prepare() we compute which tiles
127 // are dirty based on the info we have received from webkit.
129 // BaseTile Invalidation
130 // ------------------
132 // We do not want to redraw a tile if the tile is up-to-date. A tile is
133 // considered to be dirty an in need of redrawing in the following cases
134 // - the tile has acquires a new texture
135 // - webkit invalidates all or part of the tiles contents
137 // To handle the case of webkit invalidation we store two ids (counters) of the
138 // pictureSets in the tile. The first id (A) represents the pictureSet used to
139 // paint the tile and the second id (B) represents the pictureSet in which the
140 // tile was invalidated by webkit. Thus, if A < B then tile is dirty.
142 // Since invalidates can occur faster than a full tiled page update, the tiled
143 // page is protected by a 'lock' (m_baseLayerUpdate) that is set to true to
144 // defer updates to the background layer, giving the foreground time to render
145 // content instead of constantly flushing with invalidates. See
146 // lockBaseLayerUpdate() & unlockBaseLayerUpdate().
148 // Painting scheduling
149 // -------------------
151 // The next operation is to schedule this TilesSet to be painted
152 // (TilesManager::schedulePaintForTilesSet()). TexturesGenerator
153 // will get the TilesSet and ask the BaseTiles in it to be painted.
155 // BaseTile::paintBitmap() will paint the texture using the BaseLayer's
156 // PictureSet (calling TiledPage::paintBaseLayerContent() which in turns
157 // calls GLWebViewState::paintBaseLayerContent()).
159 // Note that TexturesGenerator is running in a separate thread, the textures
160 // are shared using EGLImages (this is necessary to not slow down the rendering
161 // speed -- updating GL textures in the main GL thread would slow things down).
163 /////////////////////////////////////////////////////////////////////////////////
165 class GLWebViewState {
167 GLWebViewState(android::Mutex* globalButtonMutex);
170 ZoomManager* zoomManager() { return &m_zoomManager; }
171 const SkIRect& futureViewport() const { return m_futureViewportTileBounds; }
172 void setFutureViewport(const SkIRect& viewport) { m_futureViewportTileBounds = viewport; }
174 unsigned int paintBaseLayerContent(SkCanvas* canvas);
175 void setBaseLayer(BaseLayerAndroid* layer, const SkRegion& inval, bool showVisualIndicator,
176 bool isPictureAfterFirstLayout);
177 void setExtra(BaseLayerAndroid*, SkPicture&, const IntRect&, bool allowSame);
180 void setRings(Vector<IntRect>& rings, bool isPressed);
182 void drawFocusRing(SkRect& rect);
184 TiledPage* sibling(TiledPage* page);
185 TiledPage* frontPage();
186 TiledPage* backPage();
189 // dimensions of the current base layer
190 int baseContentWidth();
191 int baseContentHeight();
193 void setViewport(SkRect& viewport, float scale);
195 // a rect containing the coordinates of all tiles in the current viewport
196 const SkIRect& viewportTileBounds() const { return m_viewportTileBounds; }
197 // a rect containing the viewportTileBounds before there was a scale change
198 const SkIRect& preZoomBounds() const { return m_preZoomBounds; }
199 void setPreZoomBounds(const SkIRect& bounds) { m_preZoomBounds = bounds; }
201 unsigned int currentPictureCounter() const { return m_currentPictureCounter; }
203 void lockBaseLayerUpdate() { m_baseLayerUpdate = false; }
204 void unlockBaseLayerUpdate();
206 void setIsScrolling(bool isScrolling) { m_isScrolling = isScrolling; }
207 bool isScrolling() { return m_isScrolling; }
209 double setupDrawing(IntRect& rect, SkRect& viewport, IntRect& webViewRect,
210 int titleBarHeight, IntRect& screenClip,
213 bool drawGL(IntRect& rect, SkRect& viewport, IntRect* invalRect,
214 IntRect& webViewRect, int titleBarHeight,
215 IntRect& clip, float scale, bool* buffersSwappedPtr);
221 void resetFrameworkInval();
222 void addDirtyArea(const IntRect& rect);
223 void resetLayersDirtyArea();
224 LayerAndroid* previouslyUsedRoot() { return m_previouslyUsedRoot; }
226 bool goingDown() { return m_goingDown; }
227 bool goingLeft() { return m_goingLeft; }
228 void setDirection(bool goingDown, bool goingLeft) {
229 m_goingDown = goingDown;
230 m_goingLeft = goingLeft;
233 int expandedTileBoundsX() { return m_expandedTileBoundsX; }
234 int expandedTileBoundsY() { return m_expandedTileBoundsY; }
237 void inval(const IntRect& rect); // caller must hold m_baseLayerLock
238 void invalRegion(const SkRegion& region);
240 ZoomManager m_zoomManager;
241 android::Mutex m_tiledPageLock;
243 SkIRect m_viewportTileBounds;
244 SkIRect m_futureViewportTileBounds;
245 SkIRect m_preZoomBounds;
246 android::Mutex m_baseLayerLock;
247 BaseLayerAndroid* m_baseLayer;
248 BaseLayerAndroid* m_currentBaseLayer;
249 LayerAndroid* m_previouslyUsedRoot;
251 unsigned int m_currentPictureCounter;
253 TiledPage* m_tiledPageA;
254 TiledPage* m_tiledPageB;
256 IntRect m_frameworkInval;
257 IntRect m_frameworkLayersInval;
258 android::Mutex* m_globalButtonMutex;
260 bool m_baseLayerUpdate;
261 SkRegion m_invalidateRegion;
263 Color m_backgroundColor;
266 unsigned int m_totalTimeCounter;
268 double m_delayTimes[MAX_MEASURES_PERF];
273 bool m_ringsIsPressed;
274 int m_focusRingTexture;
280 int m_expandedTileBoundsX;
281 int m_expandedTileBoundsY;
284 } // namespace WebCore
286 #endif // USE(ACCELERATED_COMPOSITING)
287 #endif // GLWebViewState_h