summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/graphics/android/LayerGroup.cpp
blob: f360919332b62f10a7f938e8d2497f4511df1ded (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
/*
 * Copyright 2012, 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 "LayerGroup"
#define LOG_NDEBUG 1

#include "config.h"
#include "LayerGroup.h"

#include "AndroidLog.h"
#include "ClassTracker.h"
#include "LayerAndroid.h"
#include "TiledTexture.h"
#include "TilesManager.h"

// LayerGroups with an area larger than 2048*2048 should never be unclipped
#define MAX_UNCLIPPED_AREA 4194304

namespace WebCore {

LayerGroup::LayerGroup()
    : m_dualTiledTexture(0)
    , m_needsTexture(false)
    , m_hasText(false)
{
#ifdef DEBUG_COUNT
    ClassTracker::instance()->increment("LayerGroup");
#endif
}

LayerGroup::~LayerGroup()
{
    for (unsigned int i = 0; i < m_layers.size(); i++)
        SkSafeUnref(m_layers[i]);
    if (m_dualTiledTexture)
        SkSafeUnref(m_dualTiledTexture);
#ifdef DEBUG_COUNT
    ClassTracker::instance()->decrement("LayerGroup");
#endif
}

bool LayerGroup::tryUpdateLayerGroup(LayerGroup* oldLayerGroup)
{
    if (!needsTexture() || !oldLayerGroup->needsTexture())
        return false;

    // merge layer group based on first layer ID
    if (getFirstLayer()->uniqueId() != oldLayerGroup->getFirstLayer()->uniqueId())
        return false;

    m_dualTiledTexture = oldLayerGroup->m_dualTiledTexture;
    SkSafeRef(m_dualTiledTexture);

    ALOGV("%p taking old DTT %p from group %p, nt %d",
          this, m_dualTiledTexture, oldLayerGroup, oldLayerGroup->needsTexture());

    if (!m_dualTiledTexture) {
        // no DTT to inval, so don't worry about it.
        return true;
    }

    if (singleLayer() && oldLayerGroup->singleLayer()) {
        // both are single matching layers, simply apply inval
        SkRegion* layerInval = getFirstLayer()->getInvalRegion();
        m_dualTiledTexture->markAsDirty(*layerInval);
    } else {
        SkRegion invalRegion;
        bool fullInval = m_layers.size() != oldLayerGroup->m_layers.size();
        if (!fullInval) {
            for (unsigned int i = 0; i < m_layers.size(); i++) {
                if (m_layers[i]->uniqueId() != oldLayerGroup->m_layers[i]->uniqueId()) {
                    // layer list has changed, fully invalidate
                    // TODO: partially invalidate based on layer size/position
                    fullInval = true;
                    break;
                } else if (!m_layers[i]->getInvalRegion()->isEmpty()) {
                    // merge layer inval - translate the layer's inval region into group coordinates
                    SkPoint pos = m_layers[i]->getPosition();
                    m_layers[i]->getInvalRegion()->translate(pos.fX, pos.fY);
                    invalRegion.op(*(m_layers[i]->getInvalRegion()), SkRegion::kUnion_Op);
                    break;
                }
            }
        }

        if (fullInval)
            invalRegion.setRect(-1e8, -1e8, 2e8, 2e8);

        m_dualTiledTexture->markAsDirty(invalRegion);
    }
    return true;
}

void LayerGroup::addLayer(LayerAndroid* layer, const TransformationMatrix& transform)
{
    m_layers.append(layer);
    SkSafeRef(layer);

    m_needsTexture |= layer->needsTexture();
    m_hasText |= layer->hasText();

    // calculate area size for comparison later
    IntRect rect = layer->unclippedArea();
    SkPoint pos = layer->getPosition();
    rect.setLocation(IntPoint(pos.fX, pos.fY));

    if (layer->needsTexture()) {
        if (m_unclippedArea.isEmpty()) {
            m_drawTransform = transform;
            m_drawTransform.translate3d(-pos.fX, -pos.fY, 0);
            m_unclippedArea = rect;
        } else
            m_unclippedArea.unite(rect);
        ALOGV("LG %p adding LA %p, size  %d, %d  %dx%d, now LG size %d,%d  %dx%d",
              this, layer, rect.x(), rect.y(), rect.width(), rect.height(),
              m_unclippedArea.x(), m_unclippedArea.y(),
              m_unclippedArea.width(), m_unclippedArea.height());
    }
}

IntRect LayerGroup::visibleArea()
{
    if (singleLayer())
        return getFirstLayer()->visibleArea();

    IntRect rect = m_unclippedArea;

    // clip with the viewport in documents coordinate
    IntRect documentViewport(TilesManager::instance()->shader()->documentViewport());
    rect.intersect(documentViewport);

    // TODO: handle recursive layer clip

    return rect;
}

void LayerGroup::prepareGL(bool layerTilesDisabled)
{
    if (!m_dualTiledTexture) {
        ALOGV("prepareGL on LG %p, no DTT, needsTexture? %d",
              this, m_dualTiledTexture, needsTexture());

        if (needsTexture())
            m_dualTiledTexture = new DualTiledTexture();
        else
            return;
    }

    if (layerTilesDisabled) {
        m_dualTiledTexture->discardTextures();
    } else {
        bool allowZoom = hasText(); // only allow for scale > 1 if painting vectors
        IntRect prepareArea = computePrepareArea();

        ALOGV("prepareGL on LG %p with DTT %p, %d layers",
              this, m_dualTiledTexture, m_layers.size());
        m_dualTiledTexture->prepareGL(getFirstLayer()->state(), allowZoom,
                                      prepareArea, this);
    }
}

bool LayerGroup::drawGL(bool layerTilesDisabled)
{
    if (!getFirstLayer()->visible())
        return false;

    FloatRect drawClip = getFirstLayer()->drawClip();
    FloatRect clippingRect = TilesManager::instance()->shader()->rectInScreenCoord(drawClip);
    TilesManager::instance()->shader()->clip(clippingRect);

    bool askRedraw = false;
    if (m_dualTiledTexture && !layerTilesDisabled) {
        ALOGV("drawGL on LG %p with DTT %p", this, m_dualTiledTexture);

        IntRect drawArea = visibleArea();
        askRedraw |= m_dualTiledTexture->drawGL(drawArea, opacity(), drawTransform());
    }

    // draw member layers (draws image textures, glextras)
    for (unsigned int i = 0; i < m_layers.size(); i++)
        askRedraw |= m_layers[i]->drawGL(layerTilesDisabled);

    return askRedraw;
}

void LayerGroup::swapTiles()
{
    if (!m_dualTiledTexture)
        return;

    m_dualTiledTexture->swapTiles();
}

bool LayerGroup::isReady()
{
    if (!m_dualTiledTexture)
        return true;

    return m_dualTiledTexture->isReady();
}

IntRect LayerGroup::computePrepareArea() {
    IntRect area;

    if (!getFirstLayer()->contentIsScrollable()
        && getFirstLayer()->state()->layersRenderingMode() == GLWebViewState::kAllTextures) {

        area = singleLayer() ? getFirstLayer()->unclippedArea() : m_unclippedArea;

        double total = ((double) area.width()) * ((double) area.height());
        if (total > MAX_UNCLIPPED_AREA)
            area = visibleArea();
    } else {
        area = visibleArea();
    }

    return area;
}

void LayerGroup::computeTexturesAmount(TexturesResult* result)
{
    if (!m_dualTiledTexture)
        return;

    m_dualTiledTexture->computeTexturesAmount(result, getFirstLayer());
}

bool LayerGroup::paint(BaseTile* tile, SkCanvas* canvas)
{
    if (singleLayer()) {
        getFirstLayer()->contentDraw(canvas, Layer::UnmergedLayers);
    } else {
        SkAutoCanvasRestore acr(canvas, true);
        SkMatrix matrix;
        GLUtils::toSkMatrix(matrix, m_drawTransform);

        SkMatrix inverse;
        inverse.reset();
        matrix.invert(&inverse);

        SkMatrix canvasMatrix = canvas->getTotalMatrix();
        inverse.postConcat(canvasMatrix);
        canvas->setMatrix(inverse);

        for (unsigned int i=0; i<m_layers.size(); i++)
            m_layers[i]->drawCanvas(canvas, false, Layer::MergedLayers);
    }
    return true;
}

float LayerGroup::opacity()
{
    if (singleLayer())
        return getFirstLayer()->getOpacity();
    return 1.0;
}

const TransformationMatrix* LayerGroup::drawTransform()
{
    // single layer groups query the layer's draw transform, while multi-layer
    // groups copy the draw transform once, during initialization
    // TODO: support fixed multi-layer groups by querying the changing drawTransform
    if (singleLayer())
        return getFirstLayer()->drawTransform();

    return &m_drawTransform;
}

} // namespace WebCore