File: EGLImageLayerWebKitThread.cpp

package info (click to toggle)
qtwebkit-opensource-src 5.7.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 291,692 kB
  • ctags: 268,122
  • sloc: cpp: 1,360,420; python: 70,286; ansic: 42,986; perl: 35,476; ruby: 12,236; objc: 9,465; xml: 8,396; asm: 3,873; yacc: 2,397; sh: 1,647; makefile: 650; lex: 644; java: 110
file content (224 lines) | stat: -rw-r--r-- 7,349 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2010, 2011, 2012 Research In Motion Limited. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "EGLImageLayerWebKitThread.h"

#if USE(ACCELERATED_COMPOSITING)

#include "LayerCompositingThread.h"
#include "LayerRenderer.h"
#include "SharedGraphicsContext3D.h"
#include <BlackBerryPlatformGLES2ContextState.h>
#include <BlackBerryPlatformGLES2SharedTexture.h>
#include <BlackBerryPlatformGraphics.h>
#include <BlackBerryPlatformLog.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>

using namespace BlackBerry::Platform::Graphics;

namespace WebCore {

EGLImageLayerWebKitThread::EGLImageLayerWebKitThread(LayerType type)
    : LayerWebKitThread(type, 0)
    , m_client(EGLImageLayerCompositingThreadClient::create())
    , m_needsDisplay(false)
    , m_program(0)
    , m_texture(0)
    , m_textureAccessor(0)
{
    layerCompositingThread()->setClient(m_client.get());
    setLayerProgram(LayerProgramRGBA);
}

EGLImageLayerWebKitThread::~EGLImageLayerWebKitThread()
{
    // The subclass is responsible for calling deleteFrontBuffer()
    // before we get this far.
    ASSERT(!m_program);
    ASSERT(!m_texture);
    ASSERT(!m_textureAccessor);
}

void EGLImageLayerWebKitThread::setNeedsDisplay()
{
    m_needsDisplay = true;
    setNeedsCommit();
}

void EGLImageLayerWebKitThread::updateFrontBuffer(const IntSize& size, unsigned backBufferTexture)
{
    if (!m_needsDisplay)
        return;

    if (size.isEmpty()) {
        if (size != m_size) {
            deleteFrontBuffer();
            m_size = size;
        }

        m_needsDisplay = false;
        return;
    }

    GLenum currentActiveTexture = 0;
    glGetIntegerv(GL_ACTIVE_TEXTURE, reinterpret_cast<GLint*>(&currentActiveTexture));
    glActiveTexture(GL_TEXTURE0);

    {
        GLES2ContextState::TextureAndFBOStateSaver textureAndFBOStateSaver;

        if (!createTextureIfNeeded(size))
            return;

        m_needsDisplay = false;

        blitToFrontBuffer(backBufferTexture);
    }

    glActiveTexture(currentActiveTexture);
}

void EGLImageLayerWebKitThread::deleteFrontBuffer()
{
    delete m_texture;
    m_texture = 0;
    glDeleteProgram(m_program);
    m_program = 0;

    // The texture accessor is in our EGLImageLayerCompositingThreadClient's custody
    // at this point, and that object is responsible for deleting it.
    m_textureAccessor = 0;

    // If anyone tries to render us after this, we're certainly going to need display.
    m_needsDisplay = true;
}

void EGLImageLayerWebKitThread::commitPendingTextureUploads()
{
    // This call is serialized with the compositing thread, so it's safe to update the
    // image and destroy any old images.

    m_client->setTextureAccessor(m_textureAccessor);
}

bool EGLImageLayerWebKitThread::createTextureIfNeeded(const IntSize& size)
{
    if (!m_texture || size != m_size) {
        delete m_texture;
        m_texture = new GLES2SharedTexture(size);
        if (!m_texture->isValid()) {
            delete m_texture;
            m_texture = 0;
            return false;
        }

        m_textureAccessor = m_texture->createTextureAccessor();

        m_size = size;
    }

    return true;
}

void EGLImageLayerWebKitThread::createShaderIfNeeded()
{
    // Shaders for drawing the layer contents.
    static char vertexShaderString[] =
        "attribute vec4 a_position;   \n"
        "attribute vec2 a_texCoord;   \n"
        "varying vec2 v_texCoord;     \n"
        "void main()                  \n"
        "{                            \n"
        "  gl_Position = a_position;  \n"
        "  v_texCoord = a_texCoord;   \n"
        "}                            \n";

    static char fragmentShaderStringRGBA[] =
        "varying mediump vec2 v_texCoord;                   \n"
        "uniform lowp sampler2D s_texture;                  \n"
        "void main()                                        \n"
        "{                                                  \n"
        "  gl_FragColor = texture2D(s_texture, v_texCoord); \n"
        "}                                                  \n";

    if (!m_program) {
        m_program = LayerRenderer::loadShaderProgram(vertexShaderString, fragmentShaderStringRGBA);
        if (!m_program)
            return;
        glBindAttribLocation(m_program, GLES2Program::PositionAttributeIndex, "a_position");
        glBindAttribLocation(m_program, GLES2Program::TexCoordAttributeIndex, "a_texCoord");
        glLinkProgram(m_program);
        unsigned samplerLocation = glGetUniformLocation(m_program, "s_texture");
        glUseProgram(m_program);
        glUniform1i(samplerLocation, 0);
    }
}

void EGLImageLayerWebKitThread::blitToFrontBuffer(unsigned backBufferTexture)
{
    if (!backBufferTexture)
        return;

    GLES2ContextState::ProgramStateSaver programStateSaver;

    createShaderIfNeeded();

    GLES2ContextState::BufferBindingSaver bufferBindingSaver;
    GLES2ContextState::ViewportSaver viewportSaver;
    GLES2ContextState::GlobalFlagStateSaver flagStateSaver(GLES2ContextState::GlobalFlagState::DontRestoreBlendFunc);

    glDisable(GL_DEPTH_TEST);
    glDisable(GL_STENCIL_TEST);
    glDisable(GL_SCISSOR_TEST);
    glDisable(GL_CULL_FACE);
    glDisable(GL_BLEND);

    glViewport(0, 0, m_size.width(), m_size.height());
    makeBufferCurrent(m_texture->buffer(), GLES2);
    glUseProgram(m_program);
    glBindTexture(GL_TEXTURE_2D, backBufferTexture);
    glColorMask(true, true, true, true);

    {
        GLES2ContextState::VertexAttributeStateSaver vertexAttribStateSaver;
        GLES2ContextState::VertexArrayOESStateSaver vertexArrayOESStateSaver;

        static float texcoords[4 * 2] = { 0, 0,  0, 1,  1, 1,  1, 0 };
        static float vertices[] = { -1, -1, -1, 1, 1, 1, 1, -1 };
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glEnableVertexAttribArray(GLES2Program::PositionAttributeIndex);
        glEnableVertexAttribArray(GLES2Program::TexCoordAttributeIndex);
        glVertexAttribPointer(GLES2Program::PositionAttributeIndex, 2, GL_FLOAT, GL_FALSE, 0, vertices);
        glVertexAttribPointer(GLES2Program::TexCoordAttributeIndex, 2, GL_FLOAT, GL_FALSE, 0, texcoords);
        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

        // If we don't flush, the EGLImage may never update its appearance
        glFlush();

        glDisableVertexAttribArray(GLES2Program::PositionAttributeIndex);
        glDisableVertexAttribArray(GLES2Program::TexCoordAttributeIndex);
    }
}

}

#endif // USE(ACCELERATED_COMPOSITING) && ENABLE(ACCELERATED_2D_CANVAS)