summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/graphics/android/rendering/SurfaceCollectionManager.cpp
blob: f3ff0cffa506545cea38cd50e1fee7c37e7e959e (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
/*
 * 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.
 */

#define LOG_TAG "SurfaceCollectionManager"
#define LOG_NDEBUG 1

#include "config.h"
#include "SurfaceCollectionManager.h"

#include "AndroidLog.h"
#include "private/hwui/DrawGlInfo.h"
#include "TilesManager.h"
#include "SurfaceCollection.h"

namespace WebCore {

using namespace android::uirenderer;

// Tag used to display current number of SurfaceCollections.
// Note: this will only work if one webview is actively drawing at a time.
static const char* COLLECTION_COUNT_TAG = "CollectionCount";

SurfaceCollectionManager::SurfaceCollectionManager()
    : m_drawingCollection(0)
    , m_paintingCollection(0)
    , m_queuedCollection(0)
    , m_fastSwapMode(false)
    , m_previouslyScrolling(false)
    , m_newPaintingCollection(false)
{
}

SurfaceCollectionManager::~SurfaceCollectionManager()
{
    clearCollections();
}

// the painting collection has finished painting:
//   discard the drawing collection
//   swap the painting collection in place of the drawing collection
//   and start painting the queued collection
void SurfaceCollectionManager::swap()
{
    // swap can't be called unless painting just finished
    ASSERT(m_paintingCollection);

    ALOGV("SWAPPING, D %p, P %p, Q %p",
          m_drawingCollection, m_paintingCollection, m_queuedCollection);

    // if we have a drawing collection, discard it since the painting collection is done
    if (m_drawingCollection) {
        ALOGV("destroying drawing collection %p", m_drawingCollection);
        m_drawingCollection->addFrameworkInvals();
        m_drawingCollection->removePainterOperations();
        SkSafeUnref(m_drawingCollection);
    }

    // painting collection becomes the drawing collection
    ALOGV("drawing collection %p", m_paintingCollection);
    m_paintingCollection->setIsDrawing(); // initialize animations
    m_paintingCollection->addFrameworkInvals();

    if (m_queuedCollection) {
        // start painting with the queued collection
        ALOGV("now painting collection %p", m_queuedCollection);
        m_queuedCollection->setIsPainting(m_paintingCollection);
    }
    m_drawingCollection = m_paintingCollection;
    m_paintingCollection = m_queuedCollection;
    m_queuedCollection = 0;

    if (ATRACE_ENABLED()) {
        ATRACE_INT(COLLECTION_COUNT_TAG,
                   (m_drawingCollection ? 1 : 0)
                   + (m_paintingCollection ? 1 : 0));
    }

    ALOGV("SWAPPING COMPLETE, D %p, P %p, Q %p",
         m_drawingCollection, m_paintingCollection, m_queuedCollection);
}

// clear all of the content in the three collections held by the collection manager
void SurfaceCollectionManager::clearCollections()
{
    // remove all painting operations, since they're no longer relevant
    if (m_drawingCollection)
        m_drawingCollection->removePainterOperations();
    if (m_paintingCollection)
        m_paintingCollection->removePainterOperations();

    SkSafeUnref(m_drawingCollection);
    m_drawingCollection = 0;
    SkSafeUnref(m_paintingCollection);
    m_paintingCollection = 0;
    SkSafeUnref(m_queuedCollection);
    m_queuedCollection = 0;

    ATRACE_INT(COLLECTION_COUNT_TAG, 0);
}

void SurfaceCollectionManager::updatePaintingCollection(SurfaceCollection* newCollection)
{
    m_paintingCollection = newCollection;
    m_paintingCollection->setIsPainting(m_drawingCollection);
    m_newPaintingCollection = true;
}

// a new layer collection has arrived, queue it if we're painting something already,
// or start painting it if we aren't. Returns true if the manager has two collections
// already queued.
bool SurfaceCollectionManager::updateWithSurfaceCollection(SurfaceCollection* newCollection,
                                                           bool brandNew)
{
    // can't have a queued collection unless have a painting collection too
    ASSERT(m_paintingCollection || !m_queuedCollection);

    if (!newCollection || brandNew) {
        clearCollections();
        if (brandNew) {
            updatePaintingCollection(newCollection);
            ATRACE_INT(COLLECTION_COUNT_TAG, 1);
        }
        return false;
    }

    ALOGV("updateWithSurfaceCollection - %p, has children %d, has animations %d",
          newCollection, newCollection->hasCompositedLayers(),
          newCollection->hasCompositedAnimations());

    if (m_queuedCollection || m_paintingCollection) {
        // currently painting, so defer this new collection
        if (m_queuedCollection) {
            // already have a queued collection, copy over invals so the regions are
            // eventually repainted and let the old queued collection be discarded
            m_queuedCollection->mergeInvalsInto(newCollection);

            if (!TilesManager::instance()->useDoubleBuffering()) {
                // not double buffering, count discarded collection/webkit paint as an update
                TilesManager::instance()->incContentUpdates();
            }

            ALOGV("DISCARDING collection - %p, has children %d, has animations %d",
                  newCollection, newCollection->hasCompositedLayers(),
                  newCollection->hasCompositedAnimations());
        }
        SkSafeUnref(m_queuedCollection);
        m_queuedCollection = newCollection;
    } else {
        // don't have painting collection, paint this one!
        updatePaintingCollection(newCollection);
    }

    if (ATRACE_ENABLED()) {
        ATRACE_INT(COLLECTION_COUNT_TAG,
                   (m_drawingCollection ? 1 : 0)
                   + (m_paintingCollection ? 1 : 0)
                   + (m_queuedCollection ? 1 : 0));
    }
    return m_drawingCollection && TilesManager::instance()->useDoubleBuffering();
}

void SurfaceCollectionManager::updateScrollableLayer(int layerId, int x, int y)
{
    if (m_queuedCollection)
        m_queuedCollection->updateScrollableLayer(layerId, x, y);
    if (m_paintingCollection)
        m_paintingCollection->updateScrollableLayer(layerId, x, y);
    if (m_drawingCollection)
        m_drawingCollection->updateScrollableLayer(layerId, x, y);
}


int SurfaceCollectionManager::singleSurfaceModeInvalidation(bool hasRunningAnimation,
                                                            bool scrolling,
                                                            bool shouldDraw)
{
    int returnFlags = 0;
    // In single surface mode, we need to dirty all the tiles when we are finishing
    // scrolling or have an incoming painting tree.
    bool requireDirtyAll = (m_previouslyScrolling && !scrolling)
                           || m_newPaintingCollection;

    // We also need to tell the framework to continue to invoke until
    // the base layer is ready.
    bool drawingBaseSurfaceReady = m_drawingCollection
                                   && m_drawingCollection->isReady();

    // When the base layer is ready, we can ask the framework to draw. And if
    // animation is running, dirty all the tiles, otherwise the animation will
    // be paused.
    if (drawingBaseSurfaceReady) {
        if (!shouldDraw)
            returnFlags |= DrawGlInfo::kStatusDraw;
        else
            requireDirtyAll |= hasRunningAnimation;
    }
    if (requireDirtyAll)
        TilesManager::instance()->dirtyAllTiles();

    bool requireInvoke = requireDirtyAll || !drawingBaseSurfaceReady;
    if (requireInvoke)
        returnFlags |= DrawGlInfo::kStatusInvoke;

    m_newPaintingCollection = false;
    m_previouslyScrolling = scrolling;

    return returnFlags;
}

int SurfaceCollectionManager::drawGL(double currentTime, IntRect& viewRect,
                            SkRect& visibleContentRect, float scale,
                            bool scrolling, bool singleSurfaceMode,
                            bool* collectionsSwappedPtr, bool* newCollectionHasAnimPtr,
                            TexturesResult* texturesResultPtr, bool shouldDraw)
{
    m_fastSwapMode |= scrolling || singleSurfaceMode;

    ALOGV("drawGL, D %p, P %p, Q %p, fastSwap %d shouldDraw %d",
          m_drawingCollection, m_paintingCollection,
          m_queuedCollection, m_fastSwapMode, shouldDraw);

    // ask for kStatusInvoke while painting, kStatusDraw if we have content to be redrawn next frame
    // returning 0 indicates all painting complete, no framework inval needed.
    int returnFlags = 0;

    bool didCollectionSwap = false;
    bool tryFastBlit = !m_fastSwapMode;
    if (m_paintingCollection) {
        ALOGV("preparing painting collection %p", m_paintingCollection);

        m_paintingCollection->evaluateAnimations(currentTime);

        m_paintingCollection->prepareGL(visibleContentRect, tryFastBlit);
        m_paintingCollection->computeTexturesAmount(texturesResultPtr);

        if (!TilesManager::instance()->useDoubleBuffering() || m_paintingCollection->isReady()) {
            ALOGV("have painting collection %p ready, swapping!", m_paintingCollection);
            didCollectionSwap = true;
            m_fastSwapMode = false;
            TilesManager::instance()->incContentUpdates();
            if (collectionsSwappedPtr)
                *collectionsSwappedPtr = true;
            if (newCollectionHasAnimPtr)
                *newCollectionHasAnimPtr = m_paintingCollection->hasCompositedAnimations();
            swap();
            returnFlags |= uirenderer::DrawGlInfo::kStatusDraw;
        }
    } else if (m_drawingCollection) {
        ALOGV("preparing drawing collection %p", m_drawingCollection);
        m_drawingCollection->prepareGL(visibleContentRect);
        m_drawingCollection->computeTexturesAmount(texturesResultPtr);
    }

    if (m_paintingCollection)
        returnFlags |= DrawGlInfo::kStatusInvoke;

    if (!shouldDraw) {
        if (didCollectionSwap
            || (!m_paintingCollection
                && m_drawingCollection
                && m_drawingCollection->isReady())) {
            // either a swap just occurred, or there is no more work to be done: do a full draw
            m_drawingCollection->swapTiles();

            if (didCollectionSwap && m_paintingCollection)
                m_paintingCollection->prepareGL(visibleContentRect, tryFastBlit);
            returnFlags |= DrawGlInfo::kStatusDraw;
        } else {
            // current collection not ready - invoke functor in process mode
            // until either drawing or painting collection is ready
            returnFlags |= DrawGlInfo::kStatusInvoke;
        }

        return returnFlags;
    }

    // ===========================================================================
    // Don't have a drawing collection, draw white background
    Color background = Color::white;
    bool drawBackground = true;
    bool hasRunningAnimation = false;
    if (m_drawingCollection) {
        bool drawingReady = didCollectionSwap || m_drawingCollection->isReady();

        if (didCollectionSwap || m_fastSwapMode || (drawingReady && !m_paintingCollection))
            m_drawingCollection->swapTiles();

        if (didCollectionSwap && m_paintingCollection)
            m_paintingCollection->prepareGL(visibleContentRect, tryFastBlit);

        if (drawingReady) {
            // exit fast swap mode, as content is up to date
            m_fastSwapMode = false;
        } else {
            // drawing isn't ready, must redraw
            returnFlags |= DrawGlInfo::kStatusInvoke;
        }

        hasRunningAnimation = m_drawingCollection->evaluateAnimations(currentTime);

        ALOGV("drawing collection %p", m_drawingCollection);
        background = m_drawingCollection->getBackgroundColor();
        drawBackground = m_drawingCollection->isMissingBackgroundContent();
    } else if (m_paintingCollection) {
        // Use paintingCollection background color while tiles are not done painting.
        background = m_paintingCollection->getBackgroundColor();
    }

    if (singleSurfaceMode)
        returnFlags |= singleSurfaceModeInvalidation(hasRunningAnimation,
                                                     scrolling, shouldDraw);
    // Start doing the actual GL drawing.
    if (drawBackground) {
        ALOGV("background is %x", background.rgb());
        // If background is opaque, we can safely and efficiently clear it here.
        // Otherwise, we have to calculate all the missing tiles and blend the background.
        GLUtils::clearBackgroundIfOpaque(&background);
    }

#ifdef DEBUG
    ALOGV("Drawing %d / %d surfaces",
        m_drawingCollection ? m_drawingCollection->backedSize() : -1,
        m_drawingCollection ? m_drawingCollection->size() : -1);
#endif

    if (m_drawingCollection && m_drawingCollection->drawGL(visibleContentRect))
        returnFlags |= DrawGlInfo::kStatusDraw;

    ALOGV("returnFlags %d,  m_paintingCollection %d ", returnFlags, m_paintingCollection);
    return returnFlags;
}

} // namespace WebCore