1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
/*
* Copyright 2010, The Android Open Source Project
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "TiledPage.h"
#if USE(ACCELERATED_COMPOSITING)
#include "GLUtils.h"
#include "IntRect.h"
#include "PaintTileOperation.h"
#include "SkPaint.h"
#include "SkPaintFlagsDrawFilter.h"
#include "TilesManager.h"
#include <cutils/log.h>
#include <wtf/CurrentTime.h>
#include <wtf/text/CString.h>
#undef XLOGC
#define XLOGC(...) android_printLog(ANDROID_LOG_DEBUG, "TiledPage", __VA_ARGS__)
#ifdef DEBUG
#undef XLOG
#define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "TiledPage", __VA_ARGS__)
#else
#undef XLOG
#define XLOG(...)
#endif // DEBUG
namespace WebCore {
using namespace android;
TiledPage::TiledPage(int id, GLWebViewState* state)
: m_baseTiles(0)
, m_baseTileSize(0)
, m_id(id)
, m_scale(1)
, m_invScale(1)
, m_glWebViewState(state)
, m_latestPictureInval(0)
, m_prepare(false)
, m_isPrefetchPage(false)
, m_willDraw(false)
{
m_baseTiles = new BaseTile[TilesManager::getMaxTextureAllocation() + 1];
#ifdef DEBUG_COUNT
ClassTracker::instance()->increment("TiledPage");
#endif
}
void TiledPage::updateBaseTileSize()
{
// This value must be at least 1 greater than the max number of allowed
// textures. This is because prepare() asks for a tile before it reserves
// a texture for that tile. If all textures are currently in use by the
// page then there will be no available tile and having the extra tile
// ensures that this does not happen. After claiming the extra tile the call
// to reserveTexture() will cause some other tile in the page to lose it's
// texture and become available, thus ensuring that we always have at least
// one tile that is available.
int baseTileSize = TilesManager::instance()->maxTextureCount() + 1;
if (baseTileSize > m_baseTileSize)
m_baseTileSize = baseTileSize;
}
TiledPage::~TiledPage()
{
// In order to delete the page we must ensure that none of its BaseTiles are
// currently painting or scheduled to be painted by the TextureGenerator
TilesManager::instance()->removeOperationsForPage(this);
delete[] m_baseTiles;
#ifdef DEBUG_COUNT
ClassTracker::instance()->decrement("TiledPage");
#endif
}
BaseTile* TiledPage::getBaseTile(int x, int y) const
{
// TODO: replace loop over array with HashMap indexing
for (int j = 0; j < m_baseTileSize; j++) {
BaseTile& tile = m_baseTiles[j];
if (tile.x() == x && tile.y() == y)
return &tile;
}
return 0;
}
void TiledPage::discardTextures()
{
for (int j = 0; j < m_baseTileSize; j++) {
BaseTile& tile = m_baseTiles[j];
tile.discardTextures();
}
return;
}
void TiledPage::invalidateRect(const IntRect& inval, const unsigned int pictureCount)
{
// Given the current scale level we need to mark the appropriate tiles as dirty
const float invTileContentWidth = m_scale / TilesManager::tileWidth();
const float invTileContentHeight = m_scale / TilesManager::tileHeight();
const int firstDirtyTileX = static_cast<int>(floorf(inval.x() * invTileContentWidth));
const int firstDirtyTileY = static_cast<int>(floorf(inval.y() * invTileContentHeight));
const int lastDirtyTileX = static_cast<int>(ceilf(inval.maxX() * invTileContentWidth));
const int lastDirtyTileY = static_cast<int>(ceilf(inval.maxY() * invTileContentHeight));
XLOG("Marking X %d-%d and Y %d-%d dirty", firstDirtyTileX, lastDirtyTileX, firstDirtyTileY, lastDirtyTileY);
// We defer marking the tile as dirty until the next time we need to prepare
// to draw.
m_invalRegion.op(firstDirtyTileX, firstDirtyTileY, lastDirtyTileX, lastDirtyTileY, SkRegion::kUnion_Op);
m_invalTilesRegion.op(inval.x(), inval.y(), inval.maxX(), inval.maxY(), SkRegion::kUnion_Op);
m_latestPictureInval = pictureCount;
}
void TiledPage::prepareRow(bool goingLeft, int tilesInRow, int firstTileX, int y, const SkIRect& tileBounds)
{
for (int i = 0; i < tilesInRow; i++) {
int x = firstTileX;
// If we are goingLeft, we want to schedule the tiles starting from the
// right (and to the left if not). This is because tiles are appended to
// the list and the texture uploader goes through the set front to back.
if (goingLeft)
x += (tilesInRow - 1) - i;
else
x += i;
BaseTile* currentTile = 0;
BaseTile* availableTile = 0;
for (int j = 0; j < m_baseTileSize; j++) {
BaseTile& tile = m_baseTiles[j];
if (tile.x() == x && tile.y() == y) {
currentTile = &tile;
break;
}
if (!availableTile || (tile.drawCount() < availableTile->drawCount()))
availableTile = &tile;
}
if (!currentTile && availableTile) {
XLOG("STEALING tile %d, %d (draw count %llu) for tile %d, %d",
availableTile->x(), availableTile->y(), availableTile->drawCount(), x, y);
availableTile->discardTextures(); // don't wait for textures to be stolen
currentTile = availableTile;
}
if (!currentTile) {
XLOG("ERROR: No tile available for tile %d %d", x, y);
}
if (currentTile) {
currentTile->setGLWebViewState(m_glWebViewState);
currentTile->setPage(this);
currentTile->setContents(this, x, y, m_scale);
// TODO: move below (which is largely the same for layers / tiled
// page) into prepare() function
// ensure there is a texture associated with the tile and then check to
// see if the texture is dirty and in need of repainting
if (currentTile->isDirty() || !currentTile->frontTexture())
currentTile->reserveTexture();
if (currentTile->backTexture()
&& currentTile->isDirty()
&& !currentTile->isRepaintPending()) {
PaintTileOperation *operation = new PaintTileOperation(currentTile);
TilesManager::instance()->scheduleOperation(operation);
}
}
}
}
bool TiledPage::updateTileDirtiness(const SkIRect& tileBounds)
{
if (!m_glWebViewState || tileBounds.isEmpty()) {
m_invalRegion.setEmpty();
m_invalTilesRegion.setEmpty();
return false;
}
bool visibleTileIsDirty = false;
for (int x = 0; x < m_baseTileSize; x++) {
BaseTile& tile = m_baseTiles[x];
// if the tile is in the dirty region then we must invalidate it
if (m_invalRegion.contains(tile.x(), tile.y())) {
tile.markAsDirty(m_latestPictureInval, m_invalTilesRegion);
if (tileBounds.contains(tile.x(), tile.y()))
visibleTileIsDirty = true;
}
}
// clear the invalidated region as all tiles within that region have now
// been marked as dirty.
m_invalRegion.setEmpty();
m_invalTilesRegion.setEmpty();
return visibleTileIsDirty;
}
void TiledPage::prepare(bool goingDown, bool goingLeft, const SkIRect& tileBounds, PrepareBounds bounds)
{
if (!m_glWebViewState)
return;
TilesManager::instance()->gatherTextures();
m_scrollingDown = goingDown;
int firstTileX = tileBounds.fLeft;
int firstTileY = tileBounds.fTop;
int nbTilesWidth = tileBounds.width();
int nbTilesHeight = tileBounds.height();
// Expand number of tiles to allow tiles outside of viewport to be prepared for
// smoother scrolling.
int nTilesToPrepare = nbTilesWidth * nbTilesHeight;
int nMaxTilesPerPage = m_baseTileSize / 2;
if (bounds == ExpandedBounds) {
// prepare tiles outside of the visible bounds
int expandX = m_glWebViewState->expandedTileBoundsX();
int expandY = m_glWebViewState->expandedTileBoundsY();
firstTileX -= expandX;
nbTilesWidth += expandX * 2;
firstTileY -= expandY;
nbTilesHeight += expandY * 2;
}
// crop the prepared region to the contents of the base layer
float maxWidthTiles = m_glWebViewState->baseContentWidth() * m_scale / TilesManager::tileWidth();
float maxHeightTiles = m_glWebViewState->baseContentHeight() * m_scale / TilesManager::tileHeight();
// adjust perimeter to not go outside base content bounds
if (firstTileX < 0) {
nbTilesWidth += firstTileX;
firstTileX = 0;
}
if (firstTileY < 0) {
nbTilesHeight += firstTileY;
firstTileY = 0;
}
nbTilesWidth = std::min(nbTilesWidth, static_cast<int>(ceilf(maxWidthTiles)) - firstTileX);
nbTilesHeight = std::min(nbTilesHeight, static_cast<int>(ceilf(maxHeightTiles)) - firstTileY);
// check against corrupted scale values giving bad height/width (use float to avoid overflow)
float numTiles = static_cast<float>(nbTilesHeight) * static_cast<float>(nbTilesWidth);
if (numTiles > TilesManager::getMaxTextureAllocation() || nbTilesHeight < 1 || nbTilesWidth < 1)
{
XLOGC("ERROR: We don't have enough tiles for this page!"
" nbTilesHeight %d nbTilesWidth %d", nbTilesHeight, nbTilesWidth);
return;
}
for (int i = 0; i < nbTilesHeight; i++)
prepareRow(goingLeft, nbTilesWidth, firstTileX, firstTileY + i, tileBounds);
m_prepare = true;
}
bool TiledPage::hasMissingContent(const SkIRect& tileBounds)
{
int neededTiles = tileBounds.width() * tileBounds.height();
for (int j = 0; j < m_baseTileSize; j++) {
BaseTile& tile = m_baseTiles[j];
if (tileBounds.contains(tile.x(), tile.y())) {
if (tile.frontTexture())
neededTiles--;
}
}
return neededTiles > 0;
}
bool TiledPage::isReady(const SkIRect& tileBounds)
{
int neededTiles = tileBounds.width() * tileBounds.height();
XLOG("tiled page %p needs %d ready tiles", this, neededTiles);
for (int j = 0; j < m_baseTileSize; j++) {
BaseTile& tile = m_baseTiles[j];
if (tileBounds.contains(tile.x(), tile.y())) {
if (tile.isTileReady())
neededTiles--;
}
}
XLOG("tiled page %p still needs %d ready tiles", this, neededTiles);
return neededTiles == 0;
}
bool TiledPage::swapBuffersIfReady(const SkIRect& tileBounds, float scale)
{
if (!m_glWebViewState)
return false;
if (!m_invalRegion.isEmpty() && !m_prepare)
return false;
if (m_scale != scale)
return false;
int swaps = 0;
bool fullSwap = true;
for (int x = tileBounds.fLeft; x < tileBounds.fRight; x++) {
for (int y = tileBounds.fTop; y < tileBounds.fBottom; y++) {
BaseTile* t = getBaseTile(x, y);
if (!t || !t->isTileReady())
fullSwap = false;
}
}
// swap every tile on page (even if off screen)
for (int j = 0; j < m_baseTileSize; j++) {
BaseTile& tile = m_baseTiles[j];
if (tile.swapTexturesIfNeeded())
swaps++;
}
XLOG("%p greedy swapped %d textures, returning true", this, swaps);
return fullSwap;
}
void TiledPage::prepareForDrawGL(float transparency, const SkIRect& tileBounds)
{
m_willDraw = true;
m_transparency = transparency;
m_tileBounds = tileBounds;
}
void TiledPage::drawGL()
{
if (!m_glWebViewState || m_transparency == 0 || !m_willDraw)
return;
const float tileWidth = TilesManager::tileWidth() * m_invScale;
const float tileHeight = TilesManager::tileHeight() * m_invScale;
for (int j = 0; j < m_baseTileSize; j++) {
BaseTile& tile = m_baseTiles[j];
bool tileInView = m_tileBounds.contains(tile.x(), tile.y());
if (tileInView) {
SkRect rect;
rect.fLeft = tile.x() * tileWidth;
rect.fTop = tile.y() * tileHeight;
rect.fRight = rect.fLeft + tileWidth;
rect.fBottom = rect.fTop + tileHeight;
tile.draw(m_transparency, rect, m_scale);
}
TilesManager::instance()->getProfiler()->nextTile(tile, m_invScale, tileInView);
}
m_willDraw = false; // don't redraw until re-prepared
}
bool TiledPage::paint(BaseTile* tile, SkCanvas* canvas, unsigned int* pictureUsed)
{
static SkPaintFlagsDrawFilter prefetchFilter(SkPaint::kAllFlags,
SkPaint::kAntiAlias_Flag);
if (!m_glWebViewState)
return false;
if (isPrefetchPage())
canvas->setDrawFilter(&prefetchFilter);
*pictureUsed = m_glWebViewState->paintBaseLayerContent(canvas);
return true;
}
TiledPage* TiledPage::sibling()
{
if (!m_glWebViewState)
return 0;
return m_glWebViewState->sibling(this);
}
} // namespace WebCore
#endif // USE(ACCELERATED_COMPOSITING)
|