File: GraphicsSurfaceWin.cpp

package info (click to toggle)
qtwebkit-opensource-src 5.3.2%2Bdfsg-2~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 291,472 kB
  • sloc: cpp: 1,358,084; python: 70,286; ansic: 42,964; perl: 35,474; ruby: 12,229; objc: 9,465; xml: 8,396; asm: 3,866; yacc: 2,397; sh: 1,647; makefile: 644; lex: 644; java: 110
file content (501 lines) | stat: -rw-r--r-- 16,835 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*

 Copyright (C) 2012 Zeno Albisser <zeno@webkit.org>

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library 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
 Library General Public License for more details.

 You should have received a copy of the GNU Library General Public License
 along with this library; see the file COPYING.LIB.  If not, write to
 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "GraphicsSurface.h"

#if USE(GRAPHICS_SURFACE)
#include "TextureMapperGL.h"

#define EGL_EGLEXT_PROTOTYPES // This must be defined before including egl.h and eglext.h.
#include <EGL/egl.h>
#include <EGL/eglext.h>

#if PLATFORM(QT)
#include <QGuiApplication>
#include <QOpenGLContext>
#include <qpa/qplatformnativeinterface.h>
#endif

namespace WebCore {

#define STRINGIFY(...) #__VA_ARGS__

static GLuint loadShader(GLenum type, const GLchar *shaderSrc)
{
    GLuint shader;
    GLint compiled;

    shader = glCreateShader(type);
    if (!shader)
        return 0;

    glShaderSource(shader, 1, &shaderSrc, 0);
    glCompileShader(shader);
    glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
    if (!compiled) {
        glDeleteShader(shader);
        return 0;
    }
    return shader;
}

struct GraphicsSurfacePrivate {
public:
    GraphicsSurfacePrivate(const IntSize& size, GraphicsSurfaceToken token)
        : m_token(token)
        , m_detachedContext(0)
        , m_detachedReadSurface(0)
        , m_detachedDrawSurface(0)
        , m_size(size)
        , m_eglDisplay(0)
        , m_eglContext(0)
        , m_eglConfig(0)
        , m_eglFrontBufferSurface(0)
        , m_eglBackBufferSurface(0)
        , m_initialFrontBufferShareHandle(token.frontBufferHandle)
        , m_frontBufferShareHandle(token.frontBufferHandle)
        , m_backBufferShareHandle(token.backBufferHandle)
        , m_frontBufferTexture(0)
        , m_shaderProgram(0)
    {
        initializeShaderProgram();
    }


    GraphicsSurfacePrivate(const PlatformGraphicsContext3D shareContext, const IntSize& size, GraphicsSurface::Flags flags)
        : m_detachedContext(0)
        , m_detachedReadSurface(0)
        , m_detachedDrawSurface(0)
        , m_size(size)
        , m_eglDisplay(0)
        , m_eglContext(0)
        , m_eglConfig(0)
        , m_eglFrontBufferSurface(0)
        , m_eglBackBufferSurface(0)
        , m_initialFrontBufferShareHandle(0)
        , m_frontBufferShareHandle(0)
        , m_backBufferShareHandle(0)
        , m_frontBufferTexture(0)
        , m_shaderProgram(0)
    {
        initializeShaderProgram();

        static PFNEGLQUERYSURFACEPOINTERANGLEPROC eglQuerySurfacePointerANGLE = 0;
        if (!eglQuerySurfacePointerANGLE) {
            eglQuerySurfacePointerANGLE = reinterpret_cast<PFNEGLQUERYSURFACEPOINTERANGLEPROC>(eglGetProcAddress("eglQuerySurfacePointerANGLE"));
            if (!eglQuerySurfacePointerANGLE)
                return;
        }

        if (!m_eglDisplay || !m_eglContext || !m_eglConfig) {
            m_eglDisplay = eglGetCurrentDisplay();

#if PLATFORM(QT)
            QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
            m_eglConfig = static_cast<EGLConfig>(nativeInterface->nativeResourceForContext(QByteArrayLiteral("eglConfig"), shareContext));
            EGLContext eglShareContext = static_cast<EGLContext>(nativeInterface->nativeResourceForContext(QByteArrayLiteral("eglContext"), shareContext));
#endif
            EGLint contextAttributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
            m_eglContext = eglCreateContext(m_eglDisplay, m_eglConfig, eglShareContext, contextAttributes);
        }

        EGLint attributes[] = {
            EGL_WIDTH, size.width(),
            EGL_HEIGHT, size.height(),
            EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGB,
            EGL_TEXTURE_TARGET, EGL_TEXTURE_2D,
            EGL_NONE
        };

        m_eglFrontBufferSurface = eglCreatePbufferSurface(m_eglDisplay, m_eglConfig, attributes);
        m_eglBackBufferSurface = eglCreatePbufferSurface(m_eglDisplay, m_eglConfig, attributes);
        if (m_eglFrontBufferSurface == EGL_NO_SURFACE || m_eglBackBufferSurface == EGL_NO_SURFACE)
            return;

        eglQuerySurfacePointerANGLE(m_eglDisplay, m_eglFrontBufferSurface, EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE, &m_frontBufferShareHandle);
        eglQuerySurfacePointerANGLE(m_eglDisplay, m_eglBackBufferSurface, EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE, &m_backBufferShareHandle);

        m_initialFrontBufferShareHandle = m_frontBufferShareHandle;

        m_token = GraphicsSurfaceToken(m_frontBufferShareHandle, m_backBufferShareHandle);
    }

    ~GraphicsSurfacePrivate()
    {
        releaseFrontBufferTexture();

        if (m_eglBackBufferSurface)
            eglDestroySurface(m_eglDisplay, m_eglBackBufferSurface);

        if (m_shaderProgram)
            glDeleteProgram(m_shaderProgram);
        m_shaderProgram = 0;

        if (m_eglDisplay && m_eglContext)
            eglDestroyContext(m_eglDisplay, m_eglContext);
    }

    void copyFromTexture(uint32_t texture, const IntRect& sourceRect)
    {
        if (!makeCurrent())
            return;
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        drawTexture(texture);
        glFinish();
        doneCurrent();
    }

    bool makeCurrent()
    {
        m_detachedContext = eglGetCurrentContext();
        m_detachedReadSurface = eglGetCurrentSurface(EGL_READ);
        m_detachedDrawSurface = eglGetCurrentSurface(EGL_DRAW);

        return eglMakeCurrent(m_eglDisplay, m_eglBackBufferSurface, m_eglBackBufferSurface, m_eglContext);
    }

    bool doneCurrent()
    {
        if (!m_detachedContext || !m_detachedDrawSurface || !m_detachedReadSurface)
            return false;

        bool success = eglMakeCurrent(m_eglDisplay, m_detachedDrawSurface, m_detachedReadSurface, m_detachedContext);
        m_detachedContext = 0;
        m_detachedReadSurface = 0;
        m_detachedDrawSurface = 0;
        return success;
    }

    PlatformGraphicsSurface swapBuffers()
    {
        if (m_frontBufferTexture)
            releaseFrontBufferTexture();
        std::swap(m_eglFrontBufferSurface, m_eglBackBufferSurface);
        std::swap(m_frontBufferShareHandle, m_backBufferShareHandle);

        return m_frontBufferShareHandle;
    }

    GraphicsSurfaceToken token() const
    {
        return m_token;
    }

    uint32_t frontBufferTextureID()
    {
        if (!m_eglFrontBufferSurface) {
            m_eglFrontBufferSurface = createSurfaceFromShareHandle(m_size, m_frontBufferShareHandle);

            glGenTextures(1, &m_frontBufferTexture);
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, m_frontBufferTexture);
            eglBindTexImage(m_eglDisplay, m_eglFrontBufferSurface, EGL_BACK_BUFFER);

            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        }

        return m_frontBufferTexture;
    }

    PlatformGraphicsSurface initialFrontBufferShareHandle() const
    {
        return m_initialFrontBufferShareHandle;
    }

    PlatformGraphicsSurface frontBufferShareHandle() const
    {
        return m_frontBufferShareHandle;
    }

    PlatformGraphicsSurface backBufferShareHandle() const
    {
        return m_backBufferShareHandle;
    }

    void releaseFrontBufferTexture()
    {
        if (m_frontBufferTexture) {
            eglReleaseTexImage(m_eglDisplay, m_eglFrontBufferSurface, EGL_BACK_BUFFER);
            glDeleteTextures(1, &m_frontBufferTexture);
            m_frontBufferTexture = 0;
        }

        if (m_eglFrontBufferSurface)
            eglDestroySurface(m_eglDisplay, m_eglFrontBufferSurface);
        m_eglFrontBufferSurface = 0;
    }

    IntSize size() const
    {
        return m_size;
    }

protected:
    void initializeShaderProgram()
    {
        if (m_shaderProgram)
            return;

        GLchar vShaderStr[] =
            STRINGIFY(
                attribute highp vec2 vertex;
                attribute highp vec2 textureCoordinates;
                varying highp vec2 textureCoords;
                void main(void)
                {
                    gl_Position = vec4(vertex, 0.0, 1.0);
                    textureCoords = textureCoordinates;
                }
            );

        GLchar fShaderStr[] =
            STRINGIFY(
                varying highp vec2 textureCoords;
                uniform sampler2D texture;
                void main(void)
                {
                    highp vec3 color = texture2D(texture, textureCoords).rgb;
                    gl_FragColor = vec4(clamp(color, 0.0, 1.0), 1.0);
                }
            );

        GLuint vertexShader;
        GLuint fragmentShader;
        GLint linked;

        vertexShader = loadShader(GL_VERTEX_SHADER, vShaderStr);
        fragmentShader = loadShader(GL_FRAGMENT_SHADER, fShaderStr);
        if (!vertexShader || !fragmentShader)
            return;

        m_shaderProgram = glCreateProgram();
        if (!m_shaderProgram)
            return;

        glAttachShader(m_shaderProgram, vertexShader);
        glAttachShader(m_shaderProgram, fragmentShader);

        glLinkProgram(m_shaderProgram);
        glGetProgramiv(m_shaderProgram, GL_LINK_STATUS, &linked);
        if (!linked) {
            glDeleteProgram(m_shaderProgram);
            m_shaderProgram = 0;
        }

        m_vertexAttr = glGetAttribLocation(m_shaderProgram, "vertex");
        m_textureCoordinatesAttr = glGetAttribLocation(m_shaderProgram, "textureCoordinates");
        m_textureUniform = glGetAttribLocation(m_shaderProgram, "texture");

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    }

    EGLSurface createSurfaceFromShareHandle(const IntSize& size, HANDLE shareHandle)
    {
        if (!m_eglDisplay  || !m_eglConfig) {
            m_eglDisplay = eglGetCurrentDisplay();

#if PLATFORM(QT)
            QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
            m_eglConfig = static_cast<EGLConfig>(nativeInterface->nativeResourceForContext(QByteArrayLiteral("eglConfig"), QOpenGLContext::currentContext()));
#endif
        }

        EGLint attributes[] = {
            EGL_WIDTH, size.width(),
            EGL_HEIGHT, size.height(),
            EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGB,
            EGL_TEXTURE_TARGET, EGL_TEXTURE_2D,
            EGL_NONE
        };

        EGLSurface surface = eglCreatePbufferFromClientBuffer(m_eglDisplay, EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE, reinterpret_cast<EGLClientBuffer>(shareHandle), m_eglConfig, attributes);
        ASSERT(surface);

        return surface;
    }

    void drawTexture(uint32_t texture)
    {
        glUseProgram(m_shaderProgram);

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindTexture(GL_TEXTURE_2D, texture);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

        GLfloat afVertices[] = {
            -1, -1,
             1, -1,
            -1,  1,
             1,  1
        };
        glVertexAttribPointer(m_vertexAttr, 2, GL_FLOAT, GL_FALSE, 0, afVertices);

        GLfloat aftextureCoordinates[] = {
            0, 1,
            1, 1,
            0, 0,
            1, 0
        };
        glVertexAttribPointer(m_textureCoordinatesAttr, 2, GL_FLOAT, GL_FALSE, 0, aftextureCoordinates);

        glUniform1i(m_textureUniform, 0);

        glEnableVertexAttribArray(m_vertexAttr);
        glEnableVertexAttribArray(m_textureCoordinatesAttr);

        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

        glDisableVertexAttribArray(m_vertexAttr);
        glDisableVertexAttribArray(m_textureCoordinatesAttr);

        glBindTexture(GL_TEXTURE_2D, 0);
    }

private:
    GraphicsSurfaceToken m_token;
    EGLContext m_detachedContext;
    EGLSurface m_detachedReadSurface;
    EGLSurface m_detachedDrawSurface;
    IntSize m_size;
    EGLDisplay m_eglDisplay;
    EGLContext m_eglContext;
    EGLConfig m_eglConfig;
    EGLSurface m_eglFrontBufferSurface;
    EGLSurface m_eglBackBufferSurface;
    PlatformGraphicsSurface m_initialFrontBufferShareHandle;
    PlatformGraphicsSurface m_frontBufferShareHandle;
    PlatformGraphicsSurface m_backBufferShareHandle;
    GLuint m_frontBufferTexture;
    GLint m_shaderProgram;

    GLuint m_vertexAttr;
    GLuint m_textureCoordinatesAttr;
    GLuint m_textureUniform;
};

GraphicsSurfaceToken GraphicsSurface::platformExport()
{
    return m_private->token();
}

uint32_t GraphicsSurface::platformGetTextureID()
{
    return m_private->frontBufferTextureID();
}

void GraphicsSurface::platformCopyToGLTexture(uint32_t target, uint32_t id, const IntRect& targetRect, const IntPoint& offset)
{
    // Currently not implemented.
}

void GraphicsSurface::platformCopyFromTexture(uint32_t texture, const IntRect& sourceRect)
{
    m_private->copyFromTexture(texture, sourceRect);
}

void GraphicsSurface::platformPaintToTextureMapper(TextureMapper* textureMapper, const FloatRect& targetRect, const TransformationMatrix& transform, float opacity)
{
    IntSize size = m_private->size();
    FloatRect rectOnContents(FloatPoint::zero(), size);
    TransformationMatrix adjustedTransform = transform;
    adjustedTransform.multiply(TransformationMatrix::rectToRect(rectOnContents, targetRect));
    static_cast<TextureMapperGL*>(textureMapper)->drawTexture(platformGetTextureID(), 0, size, rectOnContents, adjustedTransform, opacity);
}

uint32_t GraphicsSurface::platformFrontBuffer() const
{
    if (m_private->initialFrontBufferShareHandle() == m_private->frontBufferShareHandle())
        return 0;
    if (m_private->initialFrontBufferShareHandle() == m_private->backBufferShareHandle())
        return 1;
    return 0xFFFF;
}

uint32_t GraphicsSurface::platformSwapBuffers()
{
    m_private->swapBuffers();
    return platformFrontBuffer();
}

IntSize GraphicsSurface::platformSize() const
{
    return m_private->size();
}

PassRefPtr<GraphicsSurface> GraphicsSurface::platformCreate(const IntSize& size, Flags flags, const PlatformGraphicsContext3D shareContext)
{
    // Single buffered GraphicsSurface is currently not supported.
    if (flags & SupportsCopyToTexture || flags & SupportsSingleBuffered)
        return PassRefPtr<GraphicsSurface>();

    RefPtr<GraphicsSurface> surface = adoptRef(new GraphicsSurface(size, flags));
    surface->m_private = new GraphicsSurfacePrivate(shareContext, size, flags);

    if (!surface->m_private->frontBufferShareHandle() || !surface->m_private->backBufferShareHandle())
        return PassRefPtr<GraphicsSurface>();

    return surface;
}

PassRefPtr<GraphicsSurface> GraphicsSurface::platformImport(const IntSize& size, Flags flags, const GraphicsSurfaceToken& token)
{
    // Single buffered GraphicsSurface is currently not supported.
    if (flags & SupportsCopyToTexture || flags & SupportsSingleBuffered)
        return PassRefPtr<GraphicsSurface>();

    RefPtr<GraphicsSurface> surface = adoptRef(new GraphicsSurface(size, flags));
    surface->m_private = new GraphicsSurfacePrivate(size, token);

    if (!surface->m_private->frontBufferShareHandle() || !surface->m_private->backBufferShareHandle())
        return PassRefPtr<GraphicsSurface>();

    return surface;
}

char* GraphicsSurface::platformLock(const IntRect& rect, int* outputStride, LockOptions lockOptions)
{
    // GraphicsSurface is currently only being used for WebGL, which does not require this locking mechanism.
    return 0;
}

void GraphicsSurface::platformUnlock()
{
    // GraphicsSurface is currently only being used for WebGL, which does not require this locking mechanism.
}

void GraphicsSurface::platformDestroy()
{
    delete m_private;
    m_private = 0;
}

}
#endif