1/*
2 * Copyright (C) 2012 Intel Corporation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "EGLSurface.h"
28
29#if USE(EGL) && USE(GRAPHICS_SURFACE)
30
31#include "EGLConfigSelector.h"
32#include "EGLHelper.h"
33#include "GLPlatformContext.h"
34
35#if PLATFORM(X11)
36#include "EGLXSurface.h"
37#endif
38
39namespace WebCore {
40
41PassOwnPtr<GLTransportSurface> EGLTransportSurface::createTransportSurface(const IntSize& size, SurfaceAttributes attributes)
42{
43    OwnPtr<GLTransportSurface> surface;
44#if PLATFORM(X11)
45    surface = adoptPtr(new EGLWindowTransportSurface(size, attributes));
46#else
47    UNUSED_PARAM(size);
48    UNUSED_PARAM(attributes);
49#endif
50
51    if (surface)
52        return surface.release();
53
54    return nullptr;
55}
56
57PassOwnPtr<GLTransportSurfaceClient> EGLTransportSurface::createTransportSurfaceClient(const PlatformBufferHandle handle, const IntSize& size, bool hasAlpha)
58{
59    EGLHelper::resolveEGLBindings();
60    OwnPtr<GLTransportSurfaceClient> client;
61#if PLATFORM(X11)
62    client = adoptPtr(new EGLXTransportSurfaceClient(handle, size, hasAlpha));
63#else
64    UNUSED_PARAM(handle);
65    UNUSED_PARAM(size);
66    UNUSED_PARAM(hasAlpha);
67#endif
68
69    if (client)
70        return client.release();
71
72    return nullptr;
73}
74
75EGLTransportSurface::EGLTransportSurface(const IntSize& size, SurfaceAttributes attributes)
76    : GLTransportSurface(size, attributes)
77{
78    m_sharedDisplay = EGLHelper::eglDisplay();
79
80    if (m_sharedDisplay == EGL_NO_DISPLAY)
81        return;
82
83    m_configSelector = adoptPtr(new EGLConfigSelector(attributes));
84}
85
86GLPlatformSurface::SurfaceAttributes EGLTransportSurface::attributes() const
87{
88    return m_configSelector->attributes();
89}
90
91EGLTransportSurface::~EGLTransportSurface()
92{
93}
94
95void EGLTransportSurface::destroy()
96{
97    if (m_drawable == EGL_NO_SURFACE || m_sharedDisplay == EGL_NO_DISPLAY)
98        return;
99
100    GLTransportSurface::destroy();
101
102    if (m_drawable) {
103        eglDestroySurface(m_sharedDisplay, m_drawable);
104        m_drawable = EGL_NO_SURFACE;
105    }
106
107    m_configSelector = nullptr;
108}
109
110PlatformSurfaceConfig EGLTransportSurface::configuration()
111{
112    return m_configSelector->surfaceContextConfig();
113}
114
115PassOwnPtr<GLPlatformSurface> EGLOffScreenSurface::createOffScreenSurface(SurfaceAttributes attributes)
116{
117    OwnPtr<GLPlatformSurface> surface;
118#if PLATFORM(X11)
119    surface = adoptPtr(new EGLPixmapSurface(attributes));
120#else
121    UNUSED_PARAM(attributes);
122#endif
123
124    if (surface)
125        return surface.release();
126
127    return nullptr;
128}
129
130EGLOffScreenSurface::EGLOffScreenSurface(SurfaceAttributes surfaceAttributes)
131    : GLPlatformSurface(surfaceAttributes)
132{
133    m_sharedDisplay = EGLHelper::eglDisplay();
134
135    if (m_sharedDisplay == EGL_NO_DISPLAY)
136        return;
137
138    m_configSelector = adoptPtr(new EGLConfigSelector(surfaceAttributes));
139}
140
141EGLOffScreenSurface::~EGLOffScreenSurface()
142{
143}
144
145GLPlatformSurface::SurfaceAttributes EGLOffScreenSurface::attributes() const
146{
147    return m_configSelector->attributes();
148}
149
150PlatformSurfaceConfig EGLOffScreenSurface::configuration()
151{
152    return m_configSelector->pixmapContextConfig();
153}
154
155void EGLOffScreenSurface::destroy()
156{
157    if (m_sharedDisplay == EGL_NO_DISPLAY || m_drawable == EGL_NO_SURFACE)
158        return;
159
160    if (m_drawable) {
161        eglDestroySurface(m_sharedDisplay, m_drawable);
162        m_drawable = EGL_NO_SURFACE;
163    }
164
165    m_configSelector = nullptr;
166}
167
168}
169
170#endif
171