OSDN Git Service

Mark layers as ready to swap if invisible
[android-x86/external-webkit.git] / Source / WebCore / platform / graphics / android / TiledTexture.cpp
1 /*
2  * Copyright 2011, 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 #include "config.h"
27 #include "TiledTexture.h"
28
29 #include "TilesManager.h"
30 #include "TilesTracker.h"
31
32 #include "PaintedSurface.h"
33 #include "PaintTileOperation.h"
34 #include "SkCanvas.h"
35
36 #include <cutils/log.h>
37 #include <wtf/CurrentTime.h>
38 #include <wtf/text/CString.h>
39
40 #undef XLOGC
41 #define XLOGC(...) android_printLog(ANDROID_LOG_DEBUG, "TiledTexture", __VA_ARGS__)
42
43 #ifdef DEBUG
44
45 #undef XLOG
46 #define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "TiledTexture", __VA_ARGS__)
47
48 #else
49
50 #undef XLOG
51 #define XLOG(...)
52
53 #endif // DEBUG
54
55 namespace WebCore {
56
57 bool TiledTexture::ready() {
58     bool tilesAllReady = true;
59     bool tilesVisible = false;
60     for (unsigned int i = 0; i < m_tiles.size(); i++) {
61         BaseTile* tile = m_tiles[i];
62         if (tile->isTileVisible(m_area) && !tile->isTileReady()) {
63             tilesAllReady = false;
64             break;
65         }
66         if (tile->isTileVisible(m_area))
67             tilesVisible = true;
68     }
69     // For now, if no textures are available, consider ourselves as ready
70     // in order to unblock the zooming process.
71     // FIXME: have a better system -- maybe keeping the last scale factor
72     // able to fully render everything
73     return !TilesManager::instance()->layerTexturesRemain()
74             || !tilesVisible || tilesAllReady;
75 }
76
77 void TiledTexture::prepare(GLWebViewState* state, float scale, bool repaint,
78                            bool startFastSwap, IntRect& visibleArea)
79 {
80     if (!m_surface)
81         return;
82
83     if (!m_surface->layer())
84         return;
85
86     // first, how many tiles do we need
87     IntRect area(visibleArea.x() * scale,
88                  visibleArea.y() * scale,
89                  ceilf(visibleArea.width() * scale),
90                  ceilf(visibleArea.height() * scale));
91
92     if (area.width() == 0 && area.height() == 0) {
93         m_area.setWidth(0);
94         m_area.setHeight(0);
95         return;
96     }
97
98     int tileWidth = TilesManager::instance()->layerTileWidth();
99     int tileHeight = TilesManager::instance()->layerTileHeight();
100
101     m_area.setX(area.x() / tileWidth);
102     m_area.setY(area.y() / tileHeight);
103     float right = (area.x() + area.width()) / (float) tileWidth;
104     float bottom = (area.y() + area.height()) / (float) tileHeight;
105     m_area.setWidth(ceilf(right) - m_area.x());
106     m_area.setHeight(ceilf(bottom) - m_area.y());
107
108     XLOG("for TiledTexture %p, we prepare with scale %.2f, have a visible area of %d, %d - %d x %d, corresponding to %d, %d x - %d x %d tiles",
109          this, scale,
110          visibleArea.x(), visibleArea.y(),
111          visibleArea.width(), visibleArea.height(),
112          m_area.x(), m_area.y(),
113          m_area.width(), m_area.height());
114
115     bool goingDown = m_prevTileY < m_area.y();
116     m_prevTileY = m_area.y();
117
118     if (scale != m_scale)
119         TilesManager::instance()->removeOperationsForFilter(new ScaleFilter(this, scale));
120
121     m_scale = scale;
122
123     // unlock if tiles all ready
124     bool tilesAllReady = ready();
125
126     // startFastSwap=true will swap all ready tiles each
127     // frame until all visible tiles are up to date
128     if (tilesAllReady)
129         m_swapWhateverIsReady = false;
130     else if (startFastSwap)
131         m_swapWhateverIsReady = true;
132
133     // swap as appropriate
134     for (unsigned int i = 0; i < m_tiles.size(); i++) {
135         BaseTile* tile = m_tiles[i];
136         if (tilesAllReady || m_swapWhateverIsReady)
137             tile->swapTexturesIfNeeded();
138     }
139
140     if (tilesAllReady) {
141         m_updateManager.swap();
142         m_dirtyRegion.op(m_updateManager.getPaintingInval(), SkRegion::kUnion_Op);
143         XLOG("TT %p swapping, now painting with picture %p"
144              this, m_updateManager.getPaintingPicture());
145         m_updateManager.clearPaintingInval();
146     }
147
148     // apply dirty region to affected tiles
149     if (!m_dirtyRegion.isEmpty()) {
150         for (unsigned int i = 0; i < m_tiles.size(); i++) {
151             // TODO: don't mark all tiles dirty
152             m_tiles[i]->markAsDirty(1, m_dirtyRegion);
153         }
154     }
155     m_dirtyRegion.setEmpty();
156
157     for (int i = 0; i < m_area.width(); i++) {
158         if (goingDown) {
159             for (int j = 0; j < m_area.height(); j++) {
160                 prepareTile(repaint, m_area.x() + i, m_area.y() + j);
161             }
162         } else {
163             for (int j = m_area.height() - 1; j >= 0; j--) {
164                 prepareTile(repaint, m_area.x() + i, m_area.y() + j);
165             }
166         }
167     }
168 }
169
170 void TiledTexture::update(const SkRegion& invalRegion, SkPicture* picture)
171 {
172     XLOG("TT %p, update manager %p updated with picture %p, region empty %d",
173           this, &m_updateManager, picture, invalRegion.isEmpty());
174     // attempt to update inval and picture. these may be deferred below instead
175     // of used immediately.
176     m_updateManager.updateInval(invalRegion);
177     m_updateManager.updatePicture(picture);
178 }
179
180 void TiledTexture::prepareTile(bool repaint, int x, int y)
181 {
182     BaseTile* tile = getTile(x, y);
183     if (!tile) {
184         tile = new BaseTile(true);
185         m_tiles.append(tile);
186     }
187
188     XLOG("preparing tile %p, painter is this %p", tile, this);
189     tile->setContents(this, x, y, m_scale);
190
191     // TODO: move below (which is largely the same for layers / tiled page) into
192     // prepare() function
193
194     if (tile->isDirty() || !tile->frontTexture())
195         tile->reserveTexture();
196     LayerAndroid* layer = m_surface->layer();
197     if (tile->backTexture() && tile->isDirty() && !tile->isRepaintPending() && layer) {
198         PaintTileOperation *operation = new PaintTileOperation(tile, m_surface);
199         TilesManager::instance()->scheduleOperation(operation);
200     }
201 }
202
203 BaseTile* TiledTexture::getTile(int x, int y)
204 {
205     for (unsigned int i = 0; i <m_tiles.size(); i++) {
206         BaseTile* tile = m_tiles[i];
207         if (tile->x() == x && tile->y() == y)
208             return tile;
209     }
210     return 0;
211 }
212
213 bool TiledTexture::draw()
214 {
215 #ifdef DEBUG
216     TilesManager::instance()->getTilesTracker()->trackLayer();
217 #endif
218
219     if (m_area.width() == 0 || m_area.height() == 0)
220         return false;
221
222 #ifdef DEBUG
223     TilesManager::instance()->getTilesTracker()->trackVisibleLayer();
224 #endif
225
226     float m_invScale = 1 / m_scale;
227     const float tileWidth = TilesManager::layerTileWidth() * m_invScale;
228     const float tileHeight = TilesManager::layerTileHeight() * m_invScale;
229
230     bool askRedraw = false;
231     for (unsigned int i = 0; i < m_tiles.size(); i++) {
232         BaseTile* tile = m_tiles[i];
233
234         if (tile->isTileVisible(m_area)) {
235             askRedraw |= !tile->isTileReady();
236             SkRect rect;
237             rect.fLeft = tile->x() * tileWidth;
238             rect.fTop = tile->y() * tileHeight;
239             rect.fRight = rect.fLeft + tileWidth;
240             rect.fBottom = rect.fTop + tileHeight;
241             XLOG(" - [%d], { painter %x vs %x }, tile %x %d,%d at scale %.2f vs %.2f [ready: %d] dirty: %d",
242                  i, this, tile->painter(), tile, tile->x(), tile->y(),
243                  tile->scale(), m_scale, tile->isTileReady(), tile->isDirty());
244             tile->draw(m_surface->opacity(), rect, m_scale);
245 #ifdef DEBUG
246             TilesManager::instance()->getTilesTracker()->track(tile->isTileReady(), tile->backTexture());
247 #endif
248         }
249     }
250
251     // need to redraw if some visible tile wasn't ready
252     return askRedraw;
253 }
254
255 bool TiledTexture::paint(BaseTile* tile, SkCanvas* canvas, unsigned int* pictureUsed)
256 {
257     return m_updateManager.paint(tile, canvas, pictureUsed);
258 }
259
260 void TiledTexture::paintExtra(SkCanvas* canvas)
261 {
262     m_surface->paintExtra(canvas);
263 }
264
265 const TransformationMatrix* TiledTexture::transform()
266 {
267     return m_surface->transform();
268 }
269
270 void TiledTexture::removeTiles()
271 {
272     for (unsigned int i = 0; i < m_tiles.size(); i++) {
273         delete m_tiles[i];
274     }
275     m_tiles.clear();
276 }
277
278 void TiledTexture::discardTextures()
279 {
280     for (unsigned int i = 0; i < m_tiles.size(); i++)
281         m_tiles[i]->discardTextures();
282 }
283
284 bool TiledTexture::owns(BaseTileTexture* texture)
285 {
286     for (unsigned int i = 0; i < m_tiles.size(); i++) {
287         BaseTile* tile = m_tiles[i];
288         if (tile->frontTexture() == texture)
289             return true;
290         if (tile->backTexture() == texture)
291             return true;
292     }
293     return false;
294 }
295
296 DualTiledTexture::DualTiledTexture(PaintedSurface* surface)
297 {
298     m_textureA = new TiledTexture(surface);
299     m_textureB = new TiledTexture(surface);
300     m_frontTexture = m_textureA;
301     m_backTexture = m_textureB;
302     m_scale = -1;
303     m_futureScale = -1;
304     m_zooming = false;
305 }
306
307 DualTiledTexture::~DualTiledTexture()
308 {
309     delete m_textureA;
310     delete m_textureB;
311 }
312
313 void DualTiledTexture::prepare(GLWebViewState* state, float scale, bool repaint,
314                                bool startFastSwap, IntRect& visibleArea)
315 {
316     // If we are zooming, we will use the previously used area, to prevent the
317     // frontTexture to try to allocate more tiles than what it has already
318     if (!m_zooming)
319         m_preZoomVisibleArea = visibleArea;
320
321     if (m_futureScale != scale) {
322         m_futureScale = scale;
323         m_zoomUpdateTime = WTF::currentTime() + DualTiledTexture::s_zoomUpdateDelay;
324         m_zooming = true;
325     }
326
327     XLOG("\n*** %x Drawing with scale %.2f, futureScale: %.2f, zooming: %d",
328           this, scale, m_futureScale, m_zooming);
329
330     if (m_scale > 0)
331         m_frontTexture->prepare(state, m_scale, repaint, startFastSwap, m_preZoomVisibleArea);
332
333     // If we had a scheduled update
334     if (m_zooming && m_zoomUpdateTime < WTF::currentTime()) {
335         m_backTexture->prepare(state, m_futureScale, repaint, startFastSwap, visibleArea);
336         if (m_backTexture->ready()) {
337             swap();
338             m_zooming = false;
339         }
340     }
341 }
342
343 void DualTiledTexture::swap()
344 {
345     m_frontTexture = m_frontTexture == m_textureA ? m_textureB : m_textureA;
346     m_backTexture = m_backTexture == m_textureA ? m_textureB : m_textureA;
347     m_scale = m_futureScale;
348     m_backTexture->discardTextures();
349 }
350
351 bool DualTiledTexture::draw()
352 {
353     bool needsRepaint = m_frontTexture->draw();
354     needsRepaint |= m_zooming;
355     needsRepaint |= (m_scale <= 0);
356     return needsRepaint;
357 }
358
359 void DualTiledTexture::update(const SkRegion& dirtyArea, SkPicture* picture)
360 {
361     m_backTexture->update(dirtyArea, picture);
362     m_frontTexture->update(dirtyArea, picture);
363 }
364
365 bool DualTiledTexture::owns(BaseTileTexture* texture)
366 {
367     bool owns = m_textureA->owns(texture);
368     owns |= m_textureB->owns(texture);
369     return owns;
370 }
371
372 } // namespace WebCore