OSDN Git Service

Merge "Fine-tune the number of buckets in PictureSet add some clearing logic as well"
[android-x86/external-webkit.git] / Source / WebCore / platform / graphics / android / GLWebViewState.h
1 /*
2  * Copyright 2010, The Android Open Source Project
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #ifndef GLWebViewState_h
27 #define GLWebViewState_h
28
29 #if USE(ACCELERATED_COMPOSITING)
30
31 #include "Color.h"
32 #include "DrawExtra.h"
33 #include "IntRect.h"
34 #include "SkCanvas.h"
35 #include "SkRect.h"
36 #include "SkRegion.h"
37 #include "TiledPage.h"
38 #include "ZoomManager.h"
39 #include <utils/threads.h>
40
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
46
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
51
52 // ratio of content to view required for prefetching to enable
53 #define TILE_PREFETCH_RATIO 1.2
54
55 namespace WebCore {
56
57 class BaseLayerAndroid;
58 class LayerAndroid;
59
60 /////////////////////////////////////////////////////////////////////////////////
61 // GL Architecture
62 /////////////////////////////////////////////////////////////////////////////////
63 //
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.
67 //
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,
73 // plugins, etc.).
74 //
75 // *** NOTE: The GL drawing architecture only paints the BaseLayer for now.
76 //
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.
82 //
83 // Overview
84 // ---------
85 //
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
91 // the TiledPage.
92 //
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).
96 //
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.
99 //
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.
107 //
108 // Texture allocation
109 // ------------------
110 //
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.
113 //
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).
118 //
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
124 //
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.
128 //
129 // BaseTile Invalidation
130 // ------------------
131 //
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
136 //
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.
141 //
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().
147 //
148 // Painting scheduling
149 // -------------------
150 //
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.
154 //
155 // BaseTile::paintBitmap() will paint the texture using the BaseLayer's
156 // PictureSet (calling TiledPage::paintBaseLayerContent() which in turns
157 // calls GLWebViewState::paintBaseLayerContent()).
158 //
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).
162 //
163 /////////////////////////////////////////////////////////////////////////////////
164
165 class GLWebViewState {
166 public:
167     GLWebViewState(android::Mutex* globalButtonMutex);
168     ~GLWebViewState();
169
170     ZoomManager* zoomManager() { return &m_zoomManager; }
171     const SkIRect& futureViewport() const { return m_futureViewportTileBounds; }
172     void setFutureViewport(const SkIRect& viewport) { m_futureViewportTileBounds = viewport; }
173
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);
178     void paintExtras();
179
180     void setRings(Vector<IntRect>& rings, bool isPressed);
181     void resetRings();
182     void drawFocusRing(SkRect& rect);
183
184     TiledPage* sibling(TiledPage* page);
185     TiledPage* frontPage();
186     TiledPage* backPage();
187     void swapPages();
188
189     // dimensions of the current base layer
190     int baseContentWidth();
191     int baseContentHeight();
192
193     void setViewport(SkRect& viewport, float scale);
194
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; }
200
201     unsigned int currentPictureCounter() const { return m_currentPictureCounter; }
202
203     void lockBaseLayerUpdate() { m_baseLayerUpdate = false; }
204     void unlockBaseLayerUpdate();
205
206     void setIsScrolling(bool isScrolling) { m_isScrolling = isScrolling; }
207     bool isScrolling() { return m_isScrolling; }
208
209     double setupDrawing(IntRect& rect, SkRect& viewport, IntRect& webViewRect,
210                 int titleBarHeight, IntRect& screenClip,
211                 float scale);
212
213     bool drawGL(IntRect& rect, SkRect& viewport, IntRect* invalRect,
214                 IntRect& webViewRect, int titleBarHeight,
215                 IntRect& clip, float scale, bool* buffersSwappedPtr);
216
217 #ifdef MEASURES_PERF
218     void dumpMeasures();
219 #endif
220
221     void resetFrameworkInval();
222     void addDirtyArea(const IntRect& rect);
223     void resetLayersDirtyArea();
224     LayerAndroid* previouslyUsedRoot() { return m_previouslyUsedRoot; }
225
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;
231     }
232
233     int expandedTileBoundsX() { return m_expandedTileBoundsX; }
234     int expandedTileBoundsY() { return m_expandedTileBoundsY; }
235
236 private:
237     void inval(const IntRect& rect); // caller must hold m_baseLayerLock
238     void invalRegion(const SkRegion& region);
239
240     ZoomManager m_zoomManager;
241     android::Mutex m_tiledPageLock;
242     SkRect m_viewport;
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;
250
251     unsigned int m_currentPictureCounter;
252     bool m_usePageA;
253     TiledPage* m_tiledPageA;
254     TiledPage* m_tiledPageB;
255     IntRect m_lastInval;
256     IntRect m_frameworkInval;
257     IntRect m_frameworkLayersInval;
258     android::Mutex* m_globalButtonMutex;
259
260     bool m_baseLayerUpdate;
261     SkRegion m_invalidateRegion;
262
263     Color m_backgroundColor;
264
265 #ifdef MEASURES_PERF
266     unsigned int m_totalTimeCounter;
267     int m_timeCounter;
268     double m_delayTimes[MAX_MEASURES_PERF];
269     bool m_measurePerfs;
270 #endif
271     bool m_displayRings;
272     SkRegion m_rings;
273     bool m_ringsIsPressed;
274     int m_focusRingTexture;
275
276     bool m_isScrolling;
277     bool m_goingDown;
278     bool m_goingLeft;
279
280     int m_expandedTileBoundsX;
281     int m_expandedTileBoundsY;
282 };
283
284 } // namespace WebCore
285
286 #endif // USE(ACCELERATED_COMPOSITING)
287 #endif // GLWebViewState_h