summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/graphics/chromium/ContentLayerChromium.cpp
blob: 4ea9c92e1caad5efec33f2d9912661fbf40159a4 (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
/*
 * Copyright (C) 2010 Google Inc. All rights reserved.
 *
 * 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.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "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"

#if USE(ACCELERATED_COMPOSITING)

#include "ContentLayerChromium.h"

#include "cc/CCLayerImpl.h"
#include "GraphicsContext3D.h"
#include "LayerRendererChromium.h"
#include "LayerTexture.h"
#include "RenderLayerBacking.h"
#include "TextStream.h"

namespace WebCore {

PassRefPtr<ContentLayerChromium> ContentLayerChromium::create(GraphicsLayerChromium* owner)
{
    return adoptRef(new ContentLayerChromium(owner));
}

ContentLayerChromium::ContentLayerChromium(GraphicsLayerChromium* owner)
    : LayerChromium(owner)
    , m_contentsTexture(0)
    , m_skipsDraw(false)
{
}

ContentLayerChromium::~ContentLayerChromium()
{
    cleanupResources();
}

void ContentLayerChromium::cleanupResources()
{
    LayerChromium::cleanupResources();
    m_contentsTexture.clear();
}

bool ContentLayerChromium::requiresClippedUpdateRect()
{
    // To avoid allocating excessively large textures, switch into "large layer mode" if
    // one of the layer's dimensions is larger than 2000 pixels or the size of
    // surface it's rendering into. This is a temporary measure until layer tiling is implemented.
    static const int maxLayerSize = 2000;
    return (bounds().width() > max(maxLayerSize, ccLayerImpl()->targetRenderSurface()->contentRect().width())
            || bounds().height() > max(maxLayerSize, ccLayerImpl()->targetRenderSurface()->contentRect().height())
            || !layerRenderer()->checkTextureSize(bounds()));
}

void ContentLayerChromium::paintContentsIfDirty()
{
    RenderLayerBacking* backing = static_cast<RenderLayerBacking*>(m_owner->client());
    if (!backing || backing->paintingGoesToWindow())
        return;

    ASSERT(drawsContent());

    ASSERT(layerRenderer());

    IntRect dirtyRect;
    IntRect boundsRect(IntPoint(0, 0), bounds());
    IntPoint paintingOffset;

    // FIXME: Remove this test when tiled layers are implemented.
    if (requiresClippedUpdateRect()) {
        // Calculate the region of this layer that is currently visible.
        const IntRect clipRect = ccLayerImpl()->targetRenderSurface()->contentRect();

        TransformationMatrix layerOriginTransform = ccLayerImpl()->drawTransform();
        layerOriginTransform.translate3d(-0.5 * bounds().width(), -0.5 * bounds().height(), 0);

        // We compute the visible portion of the layer by back-mapping the current RenderSurface
        // content area to the layer. To do that, we invert the drawing matrix of the layer
        // and project the content area rectangle to it. If the layer transform is not invertible
        // then we skip rendering the layer.
        if (!layerOriginTransform.isInvertible()) {
            m_skipsDraw = true;
            return;
        }
        TransformationMatrix targetToLayerMatrix = layerOriginTransform.inverse();
        FloatQuad mappedClipToLayer = targetToLayerMatrix.projectQuad(FloatRect(clipRect));
        IntRect visibleRectInLayerCoords = mappedClipToLayer.enclosingBoundingBox();
        visibleRectInLayerCoords.intersect(IntRect(0, 0, bounds().width(), bounds().height()));

        // If this is still too large to render, then skip the layer completely.
        if (!layerRenderer()->checkTextureSize(visibleRectInLayerCoords.size())) {
            m_skipsDraw = true;
            return;
        }

        // If we need to resize the upload buffer we have to repaint everything.
        if (m_canvas.size() != visibleRectInLayerCoords.size()) {
            resizeUploadBuffer(visibleRectInLayerCoords.size());
            m_dirtyRect = boundsRect;
        }
        // If the visible portion of the layer is different from the last upload.
        if (visibleRectInLayerCoords != m_visibleRectInLayerCoords)
            m_dirtyRect = boundsRect;
        m_visibleRectInLayerCoords = visibleRectInLayerCoords;

        // Calculate the portion of the dirty rectangle that is visible.  m_dirtyRect is in layer space.
        IntRect visibleDirtyRectInLayerSpace = enclosingIntRect(m_dirtyRect);
        visibleDirtyRectInLayerSpace.intersect(visibleRectInLayerCoords);

        // What the rectangles mean:
        //   dirtyRect: The region of this layer that will be updated.
        //   m_uploadUpdateRect: The region of the layer's texture that will be uploaded into.
        dirtyRect = visibleDirtyRectInLayerSpace;
        m_uploadUpdateRect = dirtyRect;
        IntSize visibleRectOffsetInLayerCoords(visibleRectInLayerCoords.x(), visibleRectInLayerCoords.y());
        paintingOffset = IntPoint(visibleRectOffsetInLayerCoords);
        m_uploadUpdateRect.move(-visibleRectOffsetInLayerCoords);
    } else {
        dirtyRect = IntRect(m_dirtyRect);
        // If the texture needs to be reallocated then we must redraw the entire
        // contents of the layer.
        if (m_canvas.size() != bounds()) {
            resizeUploadBuffer(bounds());
            dirtyRect = boundsRect;
        } else {
            // Clip the dirtyRect to the size of the layer to avoid drawing
            // outside the bounds of the backing texture.
            dirtyRect.intersect(boundsRect);
        }
        m_uploadUpdateRect = dirtyRect;
    }

    if (dirtyRect.isEmpty())
        return;

    PlatformCanvas::Painter painter(&m_canvas);
    painter.context()->save();
    painter.context()->translate(-paintingOffset.x(), -paintingOffset.y());
    painter.context()->clearRect(dirtyRect);
    painter.context()->clip(dirtyRect);

    m_owner->paintGraphicsLayerContents(*painter.context(), dirtyRect);
    painter.context()->restore();
}

void ContentLayerChromium::resizeUploadBuffer(const IntSize& size)
{
    m_canvas.resize(size);
}

void ContentLayerChromium::updateTextureIfNeeded()
{
    PlatformCanvas::AutoLocker locker(&m_canvas);
    updateTexture(locker.pixels(), m_canvas.size());
}

void ContentLayerChromium::updateTexture(const uint8_t* pixels, const IntSize& size)
{
    if (!pixels)
        return;

    GraphicsContext3D* context = layerRendererContext();
    if (!m_contentsTexture)
        m_contentsTexture = LayerTexture::create(context, layerRenderer()->textureManager());

    // If we have to allocate a new texture we have to upload the full contents.
    if (!m_contentsTexture->isValid(size, GraphicsContext3D::RGBA))
        m_uploadUpdateRect = IntRect(IntPoint(0, 0), size);

    if (!m_contentsTexture->reserve(size, GraphicsContext3D::RGBA)) {
        m_skipsDraw = true;
        return;
    }

    IntRect srcRect = IntRect(IntPoint(0, 0), size);
    if (requiresClippedUpdateRect())
        srcRect = m_visibleRectInLayerCoords;

    const size_t destStride = m_uploadUpdateRect.width() * 4;
    const size_t srcStride = srcRect.width() * 4;

    const uint8_t* uploadPixels = pixels + srcStride * m_uploadUpdateRect.y();
    Vector<uint8_t> uploadBuffer;
    if (srcStride != destStride || m_uploadUpdateRect.x()) {
        uploadBuffer.resize(m_uploadUpdateRect.height() * destStride);
        for (int row = 0; row < m_uploadUpdateRect.height(); ++row) {
            size_t srcOffset = (m_uploadUpdateRect.y() + row) * srcStride + m_uploadUpdateRect.x() * 4;
            ASSERT(srcOffset + destStride <= static_cast<size_t>(size.width() * size.height() * 4));
            size_t destOffset = row * destStride;
            ASSERT(destOffset  + destStride <= uploadBuffer.size());
            memcpy(uploadBuffer.data() + destOffset, pixels + srcOffset, destStride);
        }
        uploadPixels = uploadBuffer.data();
    }

    m_contentsTexture->bindTexture();
    GLC(context, context->texSubImage2D(GraphicsContext3D::TEXTURE_2D, 0,
                                        m_uploadUpdateRect.x(), m_uploadUpdateRect.y(), m_uploadUpdateRect.width(), m_uploadUpdateRect.height(),
                                        GraphicsContext3D::RGBA, GraphicsContext3D::UNSIGNED_BYTE,
                                        uploadPixels));

    m_uploadUpdateRect = IntRect();
    m_dirtyRect.setSize(FloatSize());
    // Large layers always stay dirty, because they need to update when the content rect changes.
    m_contentsDirty = requiresClippedUpdateRect();
}

void ContentLayerChromium::draw()
{
    if (m_skipsDraw)
        return;

    ASSERT(layerRenderer());

    const ContentLayerChromium::Program* program = layerRenderer()->contentLayerProgram();
    ASSERT(program && program->initialized());
    GraphicsContext3D* context = layerRendererContext();
    GLC(context, context->activeTexture(GraphicsContext3D::TEXTURE0));
    bindContentsTexture();
    layerRenderer()->useShader(program->program());
    GLC(context, context->uniform1i(program->fragmentShader().samplerLocation(), 0));
    GLC(context, context->blendFunc(GraphicsContext3D::ONE, GraphicsContext3D::ONE_MINUS_SRC_ALPHA));

    if (requiresClippedUpdateRect()) {
        // Compute the offset between the layer's center point and the center of the visible portion
        // of the layer.
        FloatPoint visibleRectCenterOffset = FloatRect(m_visibleRectInLayerCoords).center();
        visibleRectCenterOffset.move(-0.5 * bounds().width(), -0.5 * bounds().height());

        TransformationMatrix transform = ccLayerImpl()->drawTransform();
        transform.translate(visibleRectCenterOffset.x(), visibleRectCenterOffset.y());

        drawTexturedQuad(context, layerRenderer()->projectionMatrix(),
                         transform, m_visibleRectInLayerCoords.width(),
                         m_visibleRectInLayerCoords.height(), ccLayerImpl()->drawOpacity(),
                         program->vertexShader().matrixLocation(),
                         program->fragmentShader().alphaLocation());
    } else {
        drawTexturedQuad(context, layerRenderer()->projectionMatrix(),
                         ccLayerImpl()->drawTransform(), bounds().width(), bounds().height(),
                         ccLayerImpl()->drawOpacity(), program->vertexShader().matrixLocation(),
                         program->fragmentShader().alphaLocation());
    }
    unreserveContentsTexture();
}

void ContentLayerChromium::updateCompositorResources()
{
    updateTextureIfNeeded();
}

void ContentLayerChromium::unreserveContentsTexture()
{
    if (!m_skipsDraw && m_contentsTexture)
        m_contentsTexture->unreserve();
}

void ContentLayerChromium::bindContentsTexture()
{
    if (!m_skipsDraw && m_contentsTexture)
        m_contentsTexture->bindTexture();
}

static void writeIndent(TextStream& ts, int indent)
{
    for (int i = 0; i != indent; ++i)
        ts << "  ";
}

void ContentLayerChromium::dumpLayerProperties(TextStream& ts, int indent) const
{
    LayerChromium::dumpLayerProperties(ts, indent);
    writeIndent(ts, indent);
    ts << "skipsDraw: " << m_skipsDraw << "\n";
}

}
#endif // USE(ACCELERATED_COMPOSITING)