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

#define LOG_TAG "BaseLayerAndroid"
#define LOG_NDEBUG 1

#include "config.h"
#include "BaseLayerAndroid.h"

#include "AndroidLog.h"
#include "ClassTracker.h"
#include "GLUtils.h"
#include "LayerGroup.h"
#include "ShaderProgram.h"
#include "SkCanvas.h"
#include "TilesManager.h"
#include <GLES2/gl2.h>

// TODO: dynamically determine based on DPI
#define PREFETCH_SCALE_MODIFIER 0.3
#define PREFETCH_OPACITY 1
#define PREFETCH_X_DIST 0
#define PREFETCH_Y_DIST 1

namespace WebCore {

using namespace android;

BaseLayerAndroid::BaseLayerAndroid()
#if USE(ACCELERATED_COMPOSITING)
    : m_color(Color::white)
    , m_content(0)
    , m_scrollState(NotScrolling)
#endif
{
#ifdef DEBUG_COUNT
    ClassTracker::instance()->increment("BaseLayerAndroid");
#endif
}

BaseLayerAndroid::~BaseLayerAndroid()
{
    SkSafeUnref(m_content);
#ifdef DEBUG_COUNT
    ClassTracker::instance()->decrement("BaseLayerAndroid");
#endif
}

void BaseLayerAndroid::setContent(LayerContent* content)
{
    SkSafeRef(content);
    SkSafeUnref(m_content);
    m_content = content;
    // FIXME: We cannot set the size of the base layer because it will screw up
    // the matrix used.  We need to fix matrix computation for the base layer
    // and then we can set the size.
    // setSize(src.width(), src.height());
}

bool BaseLayerAndroid::drawCanvas(SkCanvas* canvas)
{
    android::Mutex::Autolock lock(m_drawLock);
    if (m_content && !m_content->isEmpty())
        m_content->draw(canvas);
    return true;
}

#if USE(ACCELERATED_COMPOSITING)

void BaseLayerAndroid::prefetchBasePicture(const SkRect& viewport, float currentScale,
                                           TiledPage* prefetchTiledPage, bool draw)
{
    SkIRect bounds;
    float prefetchScale = currentScale * PREFETCH_SCALE_MODIFIER;

    float invTileWidth = (prefetchScale)
        / TilesManager::instance()->tileWidth();
    float invTileHeight = (prefetchScale)
        / TilesManager::instance()->tileHeight();
    bool goingDown = m_state->goingDown();
    bool goingLeft = m_state->goingLeft();


    ALOGV("fetch rect %f %f %f %f, scale %f",
          viewport.fLeft,
          viewport.fTop,
          viewport.fRight,
          viewport.fBottom,
          currentScale);

    bounds.fLeft = static_cast<int>(floorf(viewport.fLeft * invTileWidth)) - PREFETCH_X_DIST;
    bounds.fTop = static_cast<int>(floorf(viewport.fTop * invTileHeight)) - PREFETCH_Y_DIST;
    bounds.fRight = static_cast<int>(ceilf(viewport.fRight * invTileWidth)) + PREFETCH_X_DIST;
    bounds.fBottom = static_cast<int>(ceilf(viewport.fBottom * invTileHeight)) + PREFETCH_Y_DIST;

    ALOGV("prefetch rect %d %d %d %d, scale %f, preparing page %p",
          bounds.fLeft, bounds.fTop,
          bounds.fRight, bounds.fBottom,
          prefetchScale,
          prefetchTiledPage);

    prefetchTiledPage->setScale(prefetchScale);
    prefetchTiledPage->updateTileDirtiness();
    prefetchTiledPage->prepare(goingDown, goingLeft, bounds,
                               TiledPage::ExpandedBounds);
    prefetchTiledPage->swapBuffersIfReady(bounds,
                                          prefetchScale);
    if (draw)
        prefetchTiledPage->prepareForDrawGL(PREFETCH_OPACITY, bounds);
}

bool BaseLayerAndroid::isReady()
{
    ZoomManager* zoomManager = m_state->zoomManager();
    if (ZoomManager::kNoScaleRequest != zoomManager->scaleRequestState()) {
        ALOGV("base layer not ready, still zooming");
        return false; // still zooming
    }

    if (!m_state->frontPage()->isReady(m_state->preZoomBounds())) {
        ALOGV("base layer not ready, front page not done painting");
        return false;
    }

    return true;
}

void BaseLayerAndroid::swapTiles()
{
    m_state->frontPage()->swapBuffersIfReady(m_state->preZoomBounds(),
                                             m_state->zoomManager()->currentScale());

    m_state->backPage()->swapBuffersIfReady(m_state->preZoomBounds(),
                                            m_state->zoomManager()->currentScale());
}

void BaseLayerAndroid::setIsPainting()
{
    ALOGV("BLA %p setIsPainting, dirty %d", this, isDirty());
    m_state->invalRegion(m_dirtyRegion);
    m_dirtyRegion.setEmpty();
}

void BaseLayerAndroid::mergeInvalsInto(BaseLayerAndroid* replacementLayer)
{
    replacementLayer->markAsDirty(m_dirtyRegion);
}

void BaseLayerAndroid::prepareGL(const SkRect& viewport, float scale, double currentTime)
{
    ALOGV("prepareGL BLA %p, m_state %p", this, m_state);

    ZoomManager* zoomManager = m_state->zoomManager();

    bool goingDown = m_state->goingDown();
    bool goingLeft = m_state->goingLeft();

    const SkIRect& viewportTileBounds = m_state->viewportTileBounds();
    ALOGV("drawBasePicture, TX: %d, TY: %d scale %.2f", viewportTileBounds.fLeft,
          viewportTileBounds.fTop, scale);

    // Query the resulting state from the zoom manager
    bool prepareNextTiledPage = zoomManager->needPrepareNextTiledPage();

    // Display the current page
    TiledPage* tiledPage = m_state->frontPage();
    TiledPage* nextTiledPage = m_state->backPage();
    tiledPage->setScale(zoomManager->currentScale());

    // Let's prepare the page if needed so that it will start painting
    if (prepareNextTiledPage) {
        nextTiledPage->setScale(scale);
        m_state->setFutureViewport(viewportTileBounds);

        nextTiledPage->updateTileDirtiness();

        nextTiledPage->prepare(goingDown, goingLeft, viewportTileBounds,
                               TiledPage::VisibleBounds);
        // Cancel pending paints for the foreground page
        TilesManager::instance()->removePaintOperationsForPage(tiledPage, false);
    }

    // If we fired a request, let's check if it's ready to use
    if (zoomManager->didFireRequest()) {
        if (nextTiledPage->swapBuffersIfReady(viewportTileBounds,
                                              zoomManager->futureScale()))
            zoomManager->setReceivedRequest(); // transition to received request state
    }

    float transparency = 1;
    bool doZoomPageSwap = false;

    // If the page is ready, display it. We do a short transition between
    // the two pages (current one and future one with the new scale factor)
    if (zoomManager->didReceivedRequest()) {
        float nextTiledPageTransparency = 1;
        m_state->resetFrameworkInval();
        zoomManager->processTransition(currentTime, scale, &doZoomPageSwap,
                                       &nextTiledPageTransparency, &transparency);
        nextTiledPage->prepareForDrawGL(nextTiledPageTransparency, viewportTileBounds);
    }

    const SkIRect& preZoomBounds = m_state->preZoomBounds();

    bool zooming = ZoomManager::kNoScaleRequest != zoomManager->scaleRequestState();

    if (doZoomPageSwap) {
        zoomManager->setCurrentScale(scale);
        m_state->swapPages();
    }

    tiledPage->updateTileDirtiness();

    // paint what's needed unless we're zooming, since the new tiles won't
    // be relevant soon anyway
    if (!zooming)
        tiledPage->prepare(goingDown, goingLeft, preZoomBounds,
                           TiledPage::ExpandedBounds);

    ALOGV("scrollState %d, zooming %d", m_scrollState, zooming);

    // prefetch in the nextTiledPage if unused by zooming (even if not scrolling
    // since we want the tiles to be ready before they're needed)
    bool usePrefetchPage = !zooming;
    nextTiledPage->setIsPrefetchPage(usePrefetchPage);
    if (usePrefetchPage) {
        // if the non-prefetch page isn't missing tiles, don't bother drawing
        // prefetch page
        bool drawPrefetchPage = tiledPage->hasMissingContent(preZoomBounds);
        prefetchBasePicture(viewport, scale, nextTiledPage, drawPrefetchPage);
    }

    tiledPage->prepareForDrawGL(transparency, preZoomBounds);
}

void BaseLayerAndroid::drawBasePictureInGL()
{
    m_state->backPage()->drawGL();
    m_state->frontPage()->drawGL();
}

void BaseLayerAndroid::updateLayerPositions(const SkRect& visibleRect)
{
    LayerAndroid* compositedRoot = static_cast<LayerAndroid*>(getChild(0));
    if (!compositedRoot)
        return;
    TransformationMatrix ident;
    compositedRoot->updateLayerPositions(visibleRect);
    FloatRect clip(0, 0, content()->width(), content()->height());

    // Note that this function may be called (and should still work) with no m_state in SW mode
    // TODO: is this the best thing to do in software rendering
    float scale = m_state ? m_state->scale() : 1.0f;
    compositedRoot->updateGLPositionsAndScale(ident, clip, 1, scale);

#ifdef DEBUG
    compositedRoot->showLayer(0);
    ALOGV("We have %d layers, %d textured",
          compositedRoot->nbLayers(),
          compositedRoot->nbTexturedLayers());
#endif
}

#endif // USE(ACCELERATED_COMPOSITING)

void BaseLayerAndroid::drawGL(float scale)
{
    ALOGV("drawGL BLA %p", this);

    // TODO: consider moving drawBackground outside of prepare (into tree manager)
    m_state->drawBackground(m_color);
    drawBasePictureInGL();
    m_state->glExtras()->drawGL(0);
}

} // namespace WebCore