summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/graphics/android/TiledTexture.cpp
blob: 4b872a3bf42fcfe670c190f13ea5dae3fd3781ec (plain)
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * Copyright 2011, 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 "TiledTexture.h"

#include "TilesManager.h"
#include "TilesTracker.h"

#include "PaintedSurface.h"
#include "PaintTileOperation.h"
#include "SkCanvas.h"
#include "SkPicture.h"

#include <cutils/log.h>
#include <wtf/CurrentTime.h>
#include <wtf/text/CString.h>

#undef XLOGC
#define XLOGC(...) android_printLog(ANDROID_LOG_DEBUG, "TiledTexture", __VA_ARGS__)

#ifdef DEBUG

#undef XLOG
#define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "TiledTexture", __VA_ARGS__)

#else

#undef XLOG
#define XLOG(...)

#endif // DEBUG

namespace WebCore {

TiledTexture::~TiledTexture()
{
    SkSafeUnref(m_paintingPicture);
#ifdef DEBUG_COUNT
    ClassTracker::instance()->decrement("TiledTexture");
#endif
    removeTiles();
}

bool TiledTexture::ready()
{
    bool tilesAllReady = true;
    bool tilesVisible = false;
    for (unsigned int i = 0; i < m_tiles.size(); i++) {
        BaseTile* tile = m_tiles[i];
        if (tile->isTileVisible(m_area)) {
            tilesVisible = true;
            if (!tile->isTileReady()) {
                tilesAllReady = false;
                break;
            }
        }
    }
    // For now, if no textures are available, consider ourselves as ready
    // in order to unblock the zooming process.
    // FIXME: have a better system -- maybe keeping the last scale factor
    // able to fully render everything
    XLOG("TT %p, ready %d, visible %d, texturesRemain %d",
         this, tilesAllReady, tilesVisible,
         TilesManager::instance()->layerTexturesRemain());

    return !TilesManager::instance()->layerTexturesRemain()
            || !tilesVisible || tilesAllReady;
}

void TiledTexture::swapTiles()
{
    int swaps = 0;
    for (unsigned int i = 0; i < m_tiles.size(); i++)
        if (m_tiles[i]->swapTexturesIfNeeded())
            swaps++;
    XLOG("TT %p swapping, swaps = %d", this, swaps);
}

IntRect TiledTexture::computeTilesArea(IntRect& contentArea, float scale)
{
    IntRect computedArea;
    IntRect area(contentArea.x() * scale,
                 contentArea.y() * scale,
                 ceilf(contentArea.width() * scale),
                 ceilf(contentArea.height() * scale));

    XLOG("TT %p prepare, scale %f, area %d x %d", this, scale, area.width(), area.height());

    if (area.width() == 0 && area.height() == 0) {
        computedArea.setWidth(0);
        computedArea.setHeight(0);
        return computedArea;
    }

    int tileWidth = TilesManager::instance()->layerTileWidth();
    int tileHeight = TilesManager::instance()->layerTileHeight();

    computedArea.setX(area.x() / tileWidth);
    computedArea.setY(area.y() / tileHeight);
    float right = (area.x() + area.width()) / (float) tileWidth;
    float bottom = (area.y() + area.height()) / (float) tileHeight;
    computedArea.setWidth(ceilf(right) - computedArea.x());
    computedArea.setHeight(ceilf(bottom) - computedArea.y());
    return computedArea;
}

void TiledTexture::prepare(GLWebViewState* state, float scale, bool repaint,
                           bool startFastSwap, IntRect& prepareArea)
{
    if (!m_surface)
        return;

    // first, how many tiles do we need
    m_area = computeTilesArea(prepareArea, scale);
    if (m_area.isEmpty())
        return;

    XLOG("for TiledTexture %p, we prepare with scale %.2f, have a prepare area of "
         " %d, %d - %d x %d, corresponding to %d, %d x - %d x %d tiles",
         this, scale,
         prepareArea.x(), prepareArea.y(),
         prepareArea.width(), prepareArea.height(),
         m_area.x(), m_area.y(),
         m_area.width(), m_area.height());

    bool goingDown = m_prevTileY < m_area.y();
    m_prevTileY = m_area.y();

    if (scale != m_scale)
        TilesManager::instance()->removeOperationsForFilter(new ScaleFilter(this, scale));

    m_scale = scale;

    // apply dirty region to affected tiles
    if (!m_dirtyRegion.isEmpty()) {
        for (unsigned int i = 0; i < m_tiles.size(); i++) {
            // TODO: don't mark all tiles dirty
            m_tiles[i]->markAsDirty(1, m_dirtyRegion);
        }
    }
    m_dirtyRegion.setEmpty();

    for (int i = 0; i < m_area.width(); i++) {
        if (goingDown) {
            for (int j = 0; j < m_area.height(); j++) {
                prepareTile(repaint, m_area.x() + i, m_area.y() + j);
            }
        } else {
            for (int j = m_area.height() - 1; j >= 0; j--) {
                prepareTile(repaint, m_area.x() + i, m_area.y() + j);
            }
        }
    }
}

void TiledTexture::update(const SkRegion& invalRegion, SkPicture* picture)
{
    XLOG("TT %p update, current region empty %d, new empty %d, painting picture %p",
          this, m_dirtyRegion.isEmpty(), invalRegion.isEmpty(), picture);
    m_dirtyRegion.op(invalRegion, SkRegion::kUnion_Op);

    android::Mutex::Autolock lock(m_paintingPictureSync);
    SkSafeRef(picture);
    SkSafeUnref(m_paintingPicture);
    m_paintingPicture = picture;
}

void TiledTexture::prepareTile(bool repaint, int x, int y)
{
    BaseTile* tile = getTile(x, y);
    if (!tile) {
        tile = new BaseTile(true);
        m_tiles.append(tile);
    }

    XLOG("preparing tile %p at %d, %d, painter is this %p", tile, x, y, this);
    tile->setContents(this, x, y, m_scale);

    // TODO: move below (which is largely the same for layers / tiled page) into
    // prepare() function

    if (tile->isDirty() || !tile->frontTexture())
        tile->reserveTexture();

    bool hasPicture = m_paintingPicture != 0; // safely read on UI thread, since only UI thread writes
    if (tile->backTexture() && tile->isDirty() && !tile->isRepaintPending() && hasPicture) {
        PaintTileOperation *operation = new PaintTileOperation(tile, m_surface);
        TilesManager::instance()->scheduleOperation(operation);
    }
}

BaseTile* TiledTexture::getTile(int x, int y)
{
    for (unsigned int i = 0; i <m_tiles.size(); i++) {
        BaseTile* tile = m_tiles[i];
        if (tile->x() == x && tile->y() == y)
            return tile;
    }
    return 0;
}

int TiledTexture::nbTextures(IntRect& area, float scale)
{
    IntRect tileBounds = computeTilesArea(area, scale);
    int numberTextures = tileBounds.width() * tileBounds.height();

    // add the number of dirty tiles in the bounds, as they take up double
    // textures for double buffering
    for (unsigned int i = 0; i <m_tiles.size(); i++) {
        BaseTile* tile = m_tiles[i];
        if (tile->isDirty()
                && tile->x() >= tileBounds.x() && tile->x() <= tileBounds.maxX()
                && tile->y() >= tileBounds.y() && tile->y() <= tileBounds.maxY())
            numberTextures++;
    }
    return numberTextures;
}

bool TiledTexture::draw(IntRect& visibleArea)
{
    if (!m_surface)
        return true;

    XLOG("TT %p draw", this);

#ifdef DEBUG
    TilesManager::instance()->getTilesTracker()->trackLayer();
#endif

    m_area = computeTilesArea(visibleArea, m_scale);
    if (m_area.width() == 0 || m_area.height() == 0)
        return false;

#ifdef DEBUG
    TilesManager::instance()->getTilesTracker()->trackVisibleLayer();
#endif

    float m_invScale = 1 / m_scale;
    const float tileWidth = TilesManager::layerTileWidth() * m_invScale;
    const float tileHeight = TilesManager::layerTileHeight() * m_invScale;

    bool askRedraw = false;
    for (unsigned int i = 0; i < m_tiles.size(); i++) {
        BaseTile* tile = m_tiles[i];

        if (tile->isTileVisible(m_area)) {
            askRedraw |= !tile->isTileReady();
            SkRect rect;
            rect.fLeft = tile->x() * tileWidth;
            rect.fTop = tile->y() * tileHeight;
            rect.fRight = rect.fLeft + tileWidth;
            rect.fBottom = rect.fTop + tileHeight;
            XLOG("- [%d], { painter %x vs %x }, tile %x (layer tile: %d) %d,%d at scale %.2f vs %.2f [ready: %d] dirty: %d",
                 i, this, tile->painter(), tile, tile->isLayerTile(), tile->x(), tile->y(),
                 tile->scale(), m_scale, tile->isTileReady(), tile->isDirty());
            tile->draw(m_surface->opacity(), rect, m_scale);
#ifdef DEBUG
            TilesManager::instance()->getTilesTracker()->track(tile->isTileReady(), tile->backTexture());
#endif
        }
    }

    // need to redraw if some visible tile wasn't ready
    return askRedraw;
}

bool TiledTexture::paint(BaseTile* tile, SkCanvas* canvas, unsigned int* pictureUsed)
{
    m_paintingPictureSync.lock();
    SkPicture* picture = m_paintingPicture;
    SkSafeRef(picture);
    m_paintingPictureSync.unlock();

    if (!picture) {
        XLOG("TT %p COULDNT PAINT, NO PICTURE", this);
        return false;
    }

    XLOG("TT %p painting tile %d, %d with picture %p", this, tile->x(), tile->y(), picture);

    canvas->drawPicture(*picture);

    SkSafeUnref(picture);

    return true;
}

const TransformationMatrix* TiledTexture::transform()
{
    if (!m_surface)
        return 0;
    return m_surface->transform();
}

void TiledTexture::removeTiles()
{
    for (unsigned int i = 0; i < m_tiles.size(); i++)
        SkSafeUnref(m_tiles[i]);
    m_tiles.clear();
}

void TiledTexture::discardTextures()
{
    for (unsigned int i = 0; i < m_tiles.size(); i++)
        m_tiles[i]->discardTextures();
}

bool TiledTexture::owns(BaseTileTexture* texture)
{
    for (unsigned int i = 0; i < m_tiles.size(); i++) {
        BaseTile* tile = m_tiles[i];
        if (tile->frontTexture() == texture)
            return true;
        if (tile->backTexture() == texture)
            return true;
    }
    return false;
}

DualTiledTexture::DualTiledTexture(SurfacePainter* surface)
{
    m_textureA = new TiledTexture(surface);
    m_textureB = new TiledTexture(surface);
    m_frontTexture = m_textureA;
    m_backTexture = m_textureB;
    m_scale = -1;
    m_futureScale = -1;
    m_zooming = false;
}

DualTiledTexture::~DualTiledTexture()
{
    delete m_textureA;
    delete m_textureB;
}

void DualTiledTexture::prepare(GLWebViewState* state, float scale, bool repaint,
                               bool startFastSwap, IntRect& prepareArea)
{
    // If we are zooming, we will use the previously used area, to prevent the
    // frontTexture to try to allocate more tiles than what it has already
    if (!m_zooming)
        m_preZoomPrepareArea = prepareArea;

    if (m_scale == -1) {
        m_scale = scale;
        m_futureScale = scale;
    }

    if (m_futureScale != scale) {
        m_futureScale = scale;
        m_zoomUpdateTime = WTF::currentTime() + DualTiledTexture::s_zoomUpdateDelay;
        m_zooming = true;
    }

    XLOG("Preparing DTT %p with scale %.2f, m_scale %.2f, futureScale: %.2f, zooming: %d",
          this, scale, m_scale, m_futureScale, m_zooming);

    if (m_scale > 0)
        m_frontTexture->prepare(state, m_scale, repaint, startFastSwap, m_preZoomPrepareArea);

    // If we had a scheduled update
    if (m_zooming && m_zoomUpdateTime < WTF::currentTime()) {
        m_backTexture->prepare(state, m_futureScale, repaint, startFastSwap, prepareArea);
        if (m_backTexture->ready()) {
            m_backTexture->swapTiles();
            swap();
            m_zooming = false;
        }
    }
}

void DualTiledTexture::swap()
{
    m_frontTexture = m_frontTexture == m_textureA ? m_textureB : m_textureA;
    m_backTexture = m_backTexture == m_textureA ? m_textureB : m_textureA;
    m_scale = m_futureScale;
    m_backTexture->discardTextures();
}

bool DualTiledTexture::draw(IntRect& visibleArea)
{
    bool needsRepaint = m_frontTexture->draw(visibleArea);
    needsRepaint |= m_zooming;
    needsRepaint |= (m_scale <= 0);
    return needsRepaint;
}

void DualTiledTexture::update(const SkRegion& dirtyArea, SkPicture* picture)
{
    m_backTexture->update(dirtyArea, picture);
    m_frontTexture->update(dirtyArea, picture);
}

void DualTiledTexture::swapTiles()
{
    m_backTexture->swapTiles();
    m_frontTexture->swapTiles();
}

bool DualTiledTexture::owns(BaseTileTexture* texture)
{
    bool owns = m_textureA->owns(texture);
    owns |= m_textureB->owns(texture);
    return owns;
}

} // namespace WebCore