summaryrefslogtreecommitdiffstats
path: root/WebCore/platform/graphics/android/BaseLayerAndroid.cpp
blob: 1a08b3ca352d56136b4fdc03019320e76cbdc86c (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
/*
 * 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 "BaseLayerAndroid.h"

#if USE(ACCELERATED_COMPOSITING)
#include "GLUtils.h"
#include "ShaderProgram.h"
#include "SkCanvas.h"
#include "TilesManager.h"
#include <GLES2/gl2.h>
#include <wtf/CurrentTime.h>
#endif // USE(ACCELERATED_COMPOSITING)

#ifdef DEBUG

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

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

#else

#undef XLOG
#define XLOG(...)

#endif // DEBUG

namespace WebCore {

using namespace android;

#ifdef DEBUG_COUNT
static int gBaseLayerAndroidCount = 0;
int BaseLayerAndroid::count()
{
    return gBaseLayerAndroidCount;
}
#endif

BaseLayerAndroid::BaseLayerAndroid()
#if USE(ACCELERATED_COMPOSITING)
    : m_glWebViewState(0),
      m_color(Color::white)
#endif
{
#ifdef DEBUG_COUNT
    gBaseLayerAndroidCount++;
#endif
}

BaseLayerAndroid::~BaseLayerAndroid()
{
#if USE(ACCELERATED_COMPOSITING)
    TilesManager::instance()->removeOperationsForBaseLayer(this);
#endif
    m_content.clear();
#ifdef DEBUG_COUNT
    gBaseLayerAndroidCount--;
#endif
}

void BaseLayerAndroid::setContent(const PictureSet& src)
{
#if USE(ACCELERATED_COMPOSITING)
    // FIXME: We lock here because we do not want
    // to paint and change the m_content concurrently.
    // We should instead refactor PictureSet to use
    // an atomic refcounting scheme and use atomic operations
    // to swap PictureSets.
    android::Mutex::Autolock lock(m_drawLock);
#endif
    m_content.set(src);
    // 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());
}

void BaseLayerAndroid::setExtra(SkPicture& src)
{
#if USE(ACCELERATED_COMPOSITING)
    android::Mutex::Autolock lock(m_drawLock);
#endif
    m_extra.swap(src);
}

void BaseLayerAndroid::drawCanvas(SkCanvas* canvas)
{
#if USE(ACCELERATED_COMPOSITING)
    android::Mutex::Autolock lock(m_drawLock);
#endif
    if (!m_content.isEmpty())
        m_content.draw(canvas);
    // TODO : replace with !m_extra.isEmpty() once such a call exists
    if (m_extra.width() > 0)
        m_extra.draw(canvas);
}

#if USE(ACCELERATED_COMPOSITING)
bool BaseLayerAndroid::drawBasePictureInGL(SkRect& viewport, float scale)
{
    if (m_content.isEmpty())
        return false;
    if (!m_glWebViewState)
        return false;

    double currentTime = WTF::currentTime();
    bool goingDown = m_previousVisible.fTop - viewport.fTop <= 0;
    bool goingLeft = m_previousVisible.fLeft - viewport.fLeft >= 0;

    m_glWebViewState->setViewport(viewport, scale);

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

    if (scale == m_glWebViewState->currentScale()
        || m_glWebViewState->preZoomBounds().isEmpty())
        m_glWebViewState->setPreZoomBounds(viewportTileBounds);

    bool prepareNextTiledPage = false;
    // If we have a different scale than the current one, we have to
    // decide what to do. The current behaviour is to delay an update,
    // so that we do not slow down zooming unnecessarily.
    if (m_glWebViewState->currentScale() != scale
        && (m_glWebViewState->scaleRequestState() == GLWebViewState::kNoScaleRequest
            || m_glWebViewState->scaleRequestState() == GLWebViewState::kWillScheduleRequest
            || m_glWebViewState->futureScale() != scale)) {

        // schedule the new request
        m_glWebViewState->scheduleUpdate(currentTime, viewportTileBounds, scale);

        // If it's a new request, we will have to prepare the page.
        if (m_glWebViewState->scaleRequestState() == GLWebViewState::kRequestNewScale)
            prepareNextTiledPage = true;
    }

    // If the viewport has changed since we scheduled the request, we also need to prepare.
    if (((m_glWebViewState->scaleRequestState() == GLWebViewState::kRequestNewScale)
         || (m_glWebViewState->scaleRequestState() == GLWebViewState::kReceivedNewScale))
         && (m_glWebViewState->futureViewport() != viewportTileBounds))
        prepareNextTiledPage = true;

    // Let's prepare the page if needed
    if (prepareNextTiledPage) {
        TiledPage* nextTiledPage = m_glWebViewState->backPage();
        nextTiledPage->setScale(scale);
        m_glWebViewState->setFutureViewport(viewportTileBounds);
        nextTiledPage->prepare(goingDown, goingLeft, viewportTileBounds);
    }

    float transparency = 1;
    bool doSwap = false;

    // If we fired a request, let's check if it's ready to use
    if (m_glWebViewState->scaleRequestState() == GLWebViewState::kRequestNewScale) {
        TiledPage* nextTiledPage = m_glWebViewState->backPage();
        if (nextTiledPage->ready(viewportTileBounds))
            m_glWebViewState->setScaleRequestState(GLWebViewState::kReceivedNewScale);
    }

    // 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 (m_glWebViewState->scaleRequestState() == GLWebViewState::kReceivedNewScale) {
        TiledPage* nextTiledPage = m_glWebViewState->backPage();
        double transitionTime = m_glWebViewState->transitionTime(currentTime);
        transparency = m_glWebViewState->transparency(currentTime);

        float newTilesTransparency = 1;
        if (scale < m_glWebViewState->currentScale())
            newTilesTransparency = 1 - transparency;

        nextTiledPage->draw(newTilesTransparency, viewport, viewportTileBounds);

        // The transition between the two pages is finished, swap them
        if (currentTime > transitionTime) {
            m_glWebViewState->setCurrentScale(scale);
            m_glWebViewState->resetTransitionTime();
            doSwap = true;
        }
    }

    // Display the current page
    TiledPage* tiledPage = m_glWebViewState->frontPage();
    tiledPage->setScale(m_glWebViewState->currentScale());
    const SkIRect& preZoomBounds = m_glWebViewState->preZoomBounds();
    tiledPage->prepare(goingDown, goingLeft, preZoomBounds);
    tiledPage->draw(transparency, viewport, preZoomBounds);

    bool ret = false;
    if (m_glWebViewState->scaleRequestState() != GLWebViewState::kNoScaleRequest
        || !tiledPage->ready(preZoomBounds))
      ret = true;

    if (doSwap)
        m_glWebViewState->swapPages();

    return ret;
}
#endif // USE(ACCELERATED_COMPOSITING)

bool BaseLayerAndroid::drawGL(IntRect& viewRect, SkRect& visibleRect,
                              float scale, SkColor color)
{
    bool ret = false;
#if USE(ACCELERATED_COMPOSITING)
    int left = viewRect.x();
    int top = viewRect.y();
    int width = viewRect.width();
    int height = viewRect.height();
    XLOG("drawBasePicture drawGL() viewRect: %d, %d, %d, %d",
         left, top, width, height);

    glEnable(GL_SCISSOR_TEST);

    glScissor(left, top, width, height);
    if (!m_glWebViewState || !m_glWebViewState->hasContent()) {
        glClearColor(1, 1, 1, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        return true;
    }
    glClearColor((float)m_color.red() / 255.0,
                 (float)m_color.green() / 255.0,
                 (float)m_color.blue() / 255.0, 1);
    glClear(GL_COLOR_BUFFER_BIT);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glViewport(left, top, width, height);
    ShaderProgram* shader = TilesManager::instance()->shader();
    glUseProgram(shader->program());
    glUniform1i(shader->textureSampler(), 0);
    shader->setViewRect(viewRect);

    ret = drawBasePictureInGL(visibleRect, scale);

    if (countChildren() >= 1) {
        LayerAndroid* compositedRoot = static_cast<LayerAndroid*>(getChild(0));
        TransformationMatrix ident;
        compositedRoot->updateFixedLayersPositions(visibleRect);
        FloatRect clip(0, 0, viewRect.width(), viewRect.height());
        compositedRoot->updateGLPositions(ident, clip, 1);
        SkMatrix matrix;
        matrix.setTranslate(left, top);

        // At this point, the previous LayerAndroid* root has been destroyed,
        // which will have removed the layers as owners of the textures.
        // Let's now do a pass to reserve the textures for the current tree;
        // it will only reserve existing textures, not create them on demand.
#ifdef DEBUG
        TilesManager::instance()->printLayersTextures("reserve");
#endif
        compositedRoot->reserveGLTextures();
        // Now that we marked the textures being used, we delete the unnecessary
        // ones to make space...
        TilesManager::instance()->cleanupLayersTextures();
        // Finally do another pass to create new textures if needed
        compositedRoot->createGLTextures();

        if (compositedRoot->drawGL(matrix))
            ret = true;
    }
    glDisable(GL_SCISSOR_TEST);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    m_previousVisible = visibleRect;

#ifdef DEBUG_COUNT
    XLOG("GLWebViewState(%d) DoubleBufferedTexture(%d) BaseTile(%d) TileSet(%d) TiledPage(%d)",
         GLWebViewState::count(), DoubleBufferedTexture::count(),
         BaseTile::count(), TileSet::count(), TiledPage::count());
#endif // DEBUG_COUNT

#endif // USE(ACCELERATED_COMPOSITING)
    return ret;
}

} // namespace WebCore