summaryrefslogtreecommitdiffstats
path: root/WebCore/platform/graphics/chromium/VideoLayerChromium.cpp
blob: c0da28567b3fe9c1dde69172268cf65d7292427f (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
/*
 * 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 "VideoLayerChromium.h"

#include "LayerRendererChromium.h"
#include "RenderLayerBacking.h"
#include "skia/ext/platform_canvas.h"

#include <GLES2/gl2.h>
#define GL_GLEXT_PROTOTYPES
#include <GLES2/gl2ext.h>

#if PLATFORM(SKIA)
#include "NativeImageSkia.h"
#include "PlatformContextSkia.h"
#endif

namespace WebCore {

PassRefPtr<VideoLayerChromium> VideoLayerChromium::create(GraphicsLayerChromium* owner,
                                                          VideoFrameProvider* provider)
{
    return adoptRef(new VideoLayerChromium(owner, provider));
}

VideoLayerChromium::VideoLayerChromium(GraphicsLayerChromium* owner, VideoFrameProvider* provider)
    : ContentLayerChromium(owner)
#if PLATFORM(SKIA)
    , m_canvas(0)
    , m_skiaContext(0)
#endif
    , m_graphicsContext(0)
    , m_provider(provider)
{
}

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

    ASSERT(drawsContent());

    IntRect dirtyRect(m_dirtyRect);
    IntSize requiredTextureSize;

#if PLATFORM(SKIA)
    requiredTextureSize = m_bounds;
    IntRect boundsRect(IntPoint(0, 0), m_bounds);

    // If the texture needs to be reallocated, then we must redraw the entire
    // contents of the layer.
    if (requiredTextureSize != m_allocatedTextureSize)
        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);
    }

    if (!m_canvas.get()
        || dirtyRect.width() != m_canvas->getDevice()->width()
        || dirtyRect.height() != m_canvas->getDevice()->height()) {
        m_canvas = new skia::PlatformCanvas(dirtyRect.width(), dirtyRect.height(), true);
        m_skiaContext = new PlatformContextSkia(m_canvas.get());

        // This is needed to get text to show up correctly.
        // FIXME: Does this take us down a very slow text rendering path?
        m_skiaContext->setDrawingToImageBuffer(true);
        m_graphicsContext = new GraphicsContext(reinterpret_cast<PlatformGraphicsContext*>(m_skiaContext.get()));
    }

    // Bring the canvas into the coordinate system of the paint rect.
    m_canvas->translate(static_cast<SkScalar>(-dirtyRect.x()), static_cast<SkScalar>(-dirtyRect.y()));

    // FIXME: Remove this test when tiled layers are implemented.
    m_skipsDraw = false;
    if (!layerRenderer()->checkTextureSize(requiredTextureSize)) {
        m_skipsDraw = true;
        return;
    }

    unsigned textureId = m_contentsTexture;
    if (!textureId)
        textureId = layerRenderer()->createLayerTexture();

    // If the texture id or size changed since last time, then we need to tell GL
    // to re-allocate a texture.
    if (m_contentsTexture != textureId || requiredTextureSize != m_allocatedTextureSize)
        createTextureRect(requiredTextureSize, dirtyRect, textureId);
    else
        updateTextureRect(dirtyRect, textureId);
#else
    // FIXME: Implement non-skia path
    notImplemented();
#endif
}

void VideoLayerChromium::createTextureRect(const IntSize& requiredTextureSize, const IntRect& updateRect, unsigned textureId)
{
    // Paint into graphics context and get bitmap.
    m_owner->paintGraphicsLayerContents(*m_graphicsContext, updateRect);
    void* pixels = 0;
    IntSize bitmapSize = IntSize();
#if PLATFORM(SKIA)
    const SkBitmap& bitmap = m_canvas->getDevice()->accessBitmap(false);
    const SkBitmap* skiaBitmap = &bitmap;
    ASSERT(skiaBitmap);

    SkAutoLockPixels lock(*skiaBitmap);
    SkBitmap::Config skiaConfig = skiaBitmap->config();
    // FIXME: Do we need to support more image configurations?
    if (skiaConfig == SkBitmap::kARGB_8888_Config) {
        pixels = skiaBitmap->getPixels();
        bitmapSize = IntSize(skiaBitmap->width(), skiaBitmap->height());
    }
#else
    // FIXME: Implement non-skia path
    notImplemented();
#endif
    if (!pixels)
        return;

    glBindTexture(GL_TEXTURE_2D, textureId);
    ASSERT(bitmapSize == requiredTextureSize);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, requiredTextureSize.width(), requiredTextureSize.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

    m_contentsTexture = textureId;
    m_allocatedTextureSize = requiredTextureSize;

    updateCompleted();
}

void VideoLayerChromium::updateTextureRect(const IntRect& updateRect, unsigned textureId)
{
#if PLATFORM(SKIA)
    const SkBitmap& bitmap = m_canvas->getDevice()->accessBitmap(true);
    SkBitmap* skiaBitmap = const_cast<SkBitmap*>(&bitmap);
    ASSERT(skiaBitmap);

    SkAutoLockPixels lock(*skiaBitmap);
    SkBitmap::Config skiaConfig = skiaBitmap->config();

    if (skiaConfig == SkBitmap::kARGB_8888_Config) {
        glBindTexture(GL_TEXTURE_2D, textureId);
        void* mem = glMapTexSubImage2D(GL_TEXTURE_2D, 0, updateRect.x(), updateRect.y(), updateRect.width(), updateRect.height(), GL_RGBA, GL_UNSIGNED_BYTE, GL_WRITE_ONLY);
        skiaBitmap->setPixels(mem);
        m_owner->paintGraphicsLayerContents(*m_graphicsContext, updateRect);
        glUnmapTexSubImage2D(mem);
    }

    updateCompleted();
#else
    // FIXME: Implement non-skia path
    notImplemented();
#endif
}

void VideoLayerChromium::updateCompleted()
{
    m_dirtyRect.setSize(FloatSize());
    m_contentsDirty = false;
}

}
#endif // USE(ACCELERATED_COMPOSITING)