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
|
/*
* 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 "PaintTileSetOperation.h"
#include "TilesManager.h"
#ifdef DEBUG
#include <cutils/log.h>
#include <wtf/CurrentTime.h>
#include <wtf/text/CString.h>
#undef XLOG
#define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "TiledPage", __VA_ARGS__)
#else
#undef XLOG
#define XLOG(...)
#endif // DEBUG
#define MAX_TILES 256
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_baseTiles = new BaseTile[MAX_TILES];
#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;
XLOG("Allocate %d tiles", m_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
{
for (int j = 0; j < m_baseTileSize; j++) {
BaseTile& tile = m_baseTiles[j];
if (tile.x() == x && tile.y() == y && !tile.isAvailable())
return &tile;
}
return 0;
}
void TiledPage::setUsable(bool usable)
{
for (int j = 0; j < m_baseTileSize; j++) {
BaseTile& tile = m_baseTiles[j];
tile.setUsable(usable);
}
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.right() * invTileContentWidth));
const int lastDirtyTileY = static_cast<int>(ceilf(inval.bottom() * invTileContentHeight));
// 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_latestPictureInval = pictureCount;
}
void TiledPage::prepareRow(bool goingLeft, int tilesInRow, int firstTileX, int y, TileSet* set)
{
if (y < 0)
return;
if (!set)
return;
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.isAvailable())
availableTile = &tile;
}
if (!currentTile && availableTile) {
currentTile = availableTile;
currentTile->setContents(this, x, y);
}
if (currentTile) {
currentTile->setScale(m_scale);
// ensure there is a texture associated with the tile and then check to
// see if the texture is dirty and in need of repainting
currentTile->reserveTexture();
if (currentTile->isDirty())
set->add(currentTile);
}
}
}
void TiledPage::updateTileState(const SkIRect& tileBounds)
{
if (!m_glWebViewState || tileBounds.isEmpty()) {
m_invalRegion.setEmpty();
return;
}
const int nbTilesWidth = tileBounds.width();
const int nbTilesHeight = tileBounds.height();
const int lastTileX = tileBounds.fRight - 1;
const int lastTileY = tileBounds.fBottom - 1;
for (int x = 0; x < m_baseTileSize; x++) {
BaseTile& tile = m_baseTiles[x];
// if the tile no longer has a texture then proceed to the next tile
if (tile.isAvailable())
continue;
// 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);
// set the used level of the tile (e.g. distance from the viewport)
int dx = 0;
int dy = 0;
if (tileBounds.fLeft > tile.x())
dx = tileBounds.fLeft - tile.x();
else if (lastTileX < tile.x())
dx = tile.x() - lastTileX;
if (tileBounds.fTop > tile.y())
dy = tileBounds.fTop - tile.y();
else if (lastTileY < tile.y())
dy = tile.y() - lastTileY;
int d = std::max(dx, dy);
tile.setUsedLevel(d);
}
// clear the invalidated region as all tiles within that region have now
// been marked as dirty.
m_invalRegion.setEmpty();
}
void TiledPage::prepare(bool goingDown, bool goingLeft, const SkIRect& tileBounds)
{
if (!m_glWebViewState)
return;
// update the tiles distance from the viewport
updateTileState(tileBounds);
m_prepare = true;
int firstTileX = tileBounds.fLeft;
int firstTileY = tileBounds.fTop;
int nbTilesWidth = tileBounds.width();
int nbTilesHeight = tileBounds.height();
int lastTileX = tileBounds.fRight - 1;
int lastTileY = tileBounds.fBottom - 1;
const int baseContentHeight = m_glWebViewState->baseContentHeight();
const int baseContentWidth = m_glWebViewState->baseContentWidth();
TileSet* highResSet = new TileSet(this, nbTilesHeight, nbTilesWidth);
// 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;
int expandX = TilesManager::instance()->expandedTileBoundsX();
int expandY = TilesManager::instance()->expandedTileBoundsY();
if (nTilesToPrepare + (nbTilesHeight * expandX * 2) <= nMaxTilesPerPage) {
firstTileX -= expandX;
lastTileX += expandX;
nbTilesWidth += expandX * 2;
}
if (nTilesToPrepare + (nbTilesWidth * expandY * 2) <= nMaxTilesPerPage) {
firstTileY -= expandY;
lastTileY += expandY;
nbTilesHeight += expandY * 2;
}
// We chose to prepare tiles depending on the scroll direction. Tiles are
// appended to the list and the texture uploader goes through the list front
// to back. So we append tiles in reverse order because the last additions
// to the are processed first.
if (goingDown) {
for (int i = 0; i < nbTilesHeight; i++)
prepareRow(goingLeft, nbTilesWidth, firstTileX, lastTileY - i, highResSet);
} else {
for (int i = 0; i < nbTilesHeight; i++)
prepareRow(goingLeft, nbTilesWidth, firstTileX, firstTileY + i, highResSet);
}
// The paint operation will take ownership of the tileSet here, so no delete
// is necessary.
PaintTileSetOperation* operation = new PaintTileSetOperation(highResSet);
TilesManager::instance()->scheduleOperation(operation);
}
bool TiledPage::ready(const SkIRect& tileBounds, float scale)
{
if (!m_glWebViewState)
return false;
if (!m_invalRegion.isEmpty() && !m_prepare)
return false;
if (m_scale != scale)
return false;
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())
return false;
}
}
m_prepare = false;
return true;
}
void TiledPage::draw(float transparency, const SkIRect& tileBounds)
{
if (!m_glWebViewState)
return;
const float tileWidth = TilesManager::tileWidth() * m_invScale;
const float tileHeight = TilesManager::tileHeight() * m_invScale;
SkIRect actualTileBounds = tileBounds;
actualTileBounds.fTop -= TilesManager::instance()->expandedTileBoundsY();
actualTileBounds.fBottom += TilesManager::instance()->expandedTileBoundsY();
actualTileBounds.fLeft -= TilesManager::instance()->expandedTileBoundsX();
actualTileBounds.fRight += TilesManager::instance()->expandedTileBoundsX();
XLOG("WE DRAW %x (%.2f) with transparency %.2f", this, scale(), transparency);
for (int j = 0; j < m_baseTileSize; j++) {
BaseTile& tile = m_baseTiles[j];
if (actualTileBounds.contains(tile.x(), tile.y())) {
SkRect rect;
rect.fLeft = tile.x() * tileWidth;
rect.fTop = tile.y() * tileHeight;
rect.fRight = rect.fLeft + tileWidth;
rect.fBottom = rect.fTop + tileHeight;
tile.draw(transparency, rect);
}
}
#ifdef DEBUG
XLOG("FINISHED WE DRAW %x (%.2f) with transparency %.2f", this, scale(), transparency);
TilesManager::instance()->printTextures();
#endif // DEBUG
}
unsigned int TiledPage::paintBaseLayerContent(SkCanvas* canvas)
{
if (m_glWebViewState)
return m_glWebViewState->paintBaseLayerContent(canvas);
return 0;
}
TiledPage* TiledPage::sibling()
{
if (!m_glWebViewState)
return 0;
return m_glWebViewState->sibling(this);
}
} // namespace WebCore
#endif // USE(ACCELERATED_COMPOSITING)
|