2 * Copyright (C) 2012 Samsung Electronics. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
29 #include "SharedPlatformSurfaceTizen.h"
30 #include "PixmapContextTizen.h"
32 #include "NotImplemented.h"
35 #include <EGL/eglext.h>
36 #include <wtf/HashMap.h>
37 #include <wtf/OwnPtr.h>
38 #include <wtf/RefCounted.h>
39 #include <wtf/StdLibExtras.h>
41 #include <X11/Xutil.h>
45 static PFNEGLLOCKSURFACEKHRPROC eglLockSurfaceKHR = 0;
46 static PFNEGLUNLOCKSURFACEKHRPROC eglUnlockSurfaceKHR = 0;
50 Display* g_nativeDisplay;
52 class PixmapContextPool {
54 PixmapContextPool(void);
55 virtual ~PixmapContextPool();
57 static inline PixmapContextPool& getInstance()
59 static PixmapContextPool pixmapContextPool;
60 return pixmapContextPool;
63 PixmapContextTizen* getContext(bool isLockable, bool hasAlpha, bool hasDepth, bool hasStencil);
66 typedef HashMap<int, RefPtr<PixmapContextTizen> > PixmapContextMap;
67 PixmapContextMap m_pixmapContexts;
70 PixmapContextPool::PixmapContextPool()
73 g_nativeDisplay = XOpenDisplay(0);
75 g_nativeWindow = XCreateSimpleWindow(g_nativeDisplay, XDefaultRootWindow(g_nativeDisplay),
77 BlackPixel(g_nativeDisplay, 0), WhitePixel(g_nativeDisplay, 0));
78 XFlush(g_nativeDisplay);
81 PixmapContextPool::~PixmapContextPool()
83 PixmapContextMap::iterator end = m_pixmapContexts.end();
84 for (PixmapContextMap::iterator iter = m_pixmapContexts.begin(); iter != end; ++iter) {
85 RefPtr<PixmapContextTizen> context = iter->second;
88 m_pixmapContexts.clear();
91 XDestroyWindow(g_nativeDisplay, g_nativeWindow);
96 PixmapContextTizen* PixmapContextPool::getContext(bool isLockable, bool hasAlpha, bool hasDepth, bool hasStencil)
98 int contextId = ((isLockable) | (hasAlpha << 1) | (hasDepth << 2) | (hasStencil << 3));
99 RefPtr<PixmapContextTizen> pixmapContext = m_pixmapContexts.get(contextId);
100 if (!pixmapContext) {
101 pixmapContext = PixmapContextTizen::create(isLockable, hasAlpha, hasDepth, hasStencil);
103 m_pixmapContexts.add(contextId, pixmapContext);
107 return pixmapContext.get();
110 PassRefPtr<PixmapContextTizen> PixmapContextTizen::create(bool isLockable, bool hasAlpha, bool hasDepth, bool hasStencil)
112 RefPtr<PixmapContextTizen> context = adoptRef(new PixmapContextTizen(isLockable, hasAlpha, hasDepth, hasStencil));
113 if (!context->initialize())
115 return context.release();
118 void PixmapContextTizen::HandleEGLError(const char* name)
120 static const char* const egErrorStrings[] =
123 "EGL_NOT_INITIALIZED",
129 "EGL_BAD_CURRENT_SURFACE",
132 "EGL_BAD_NATIVE_PIXMAP",
133 "EGL_BAD_NATIVE_WINDOW",
138 EGLint errorCode = eglGetError();
140 if (errorCode != EGL_SUCCESS)
141 TIZEN_LOGE("'%s' returned egl error '%s' (0x%x)", name, egErrorStrings[errorCode - EGL_SUCCESS], errorCode);
144 PixmapContextTizen::PixmapContextTizen(bool isLockable, bool hasAlpha, bool hasDepth, bool hasStencil)
145 : m_initialized(false)
146 , m_display(EGL_NO_DISPLAY)
147 , m_context(EGL_NO_CONTEXT)
148 , m_isLockable(isLockable)
149 , m_hasAlpha(hasAlpha)
150 , m_hasDepth(hasDepth)
151 , m_hasStencil(hasStencil)
155 PixmapContextTizen::~PixmapContextTizen()
158 eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
159 eglDestroyContext(m_display, m_context);
160 m_context = EGL_NO_CONTEXT;
165 bool PixmapContextTizen::initialize()
168 EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
170 m_display = eglGetDisplay(g_nativeDisplay);
171 if (m_display == EGL_NO_DISPLAY)
172 HandleEGLError("eglGetDisplay");
174 if (eglInitialize(m_display, &major, &minor) != EGL_TRUE)
175 HandleEGLError("eglInitialize");
177 EGLint configCount = 0;
179 EGLint configAttribs[32];
180 configAttribs[i++] = EGL_LEVEL;
181 configAttribs[i++] = 0;
182 // FIXME : Create pixmap surface with RGB_565 when alpha is off. Graphics driver needs to be fixed first.
183 configAttribs[i++] = EGL_RED_SIZE;
184 configAttribs[i++] = 8;
185 configAttribs[i++] = EGL_GREEN_SIZE;
186 configAttribs[i++] = 8;
187 configAttribs[i++] = EGL_BLUE_SIZE;
188 configAttribs[i++] = 8;
189 configAttribs[i++] = EGL_ALPHA_SIZE;
190 configAttribs[i++] = 8;
193 configAttribs[i++] = EGL_MATCH_FORMAT_KHR;
194 // FIXME : This strcmp will be replaced.to the feature macro.
195 if (!strcmp(eglQueryString (m_display, EGL_VENDOR), "ARM"))
196 configAttribs[i++] = EGL_FORMAT_RGBA_8888_EXACT_KHR;
197 else if (!strcmp(eglQueryString (m_display, EGL_VENDOR), "Imagination Technologies"))
198 configAttribs[i++] = EGL_FORMAT_RGBA_8888_EXACT_KHR;
200 configAttribs[i++] = EGL_FORMAT_RGBA_8888_KHR;
201 configAttribs[i++] = EGL_SURFACE_TYPE;
202 configAttribs[i++] = EGL_PIXMAP_BIT | EGL_LOCK_SURFACE_BIT_KHR;
203 configAttribs[i++] = EGL_DEPTH_SIZE;
204 configAttribs[i++] = 0;
205 configAttribs[i++] = EGL_STENCIL_SIZE;
206 configAttribs[i++] = 0;
208 configAttribs[i++] = EGL_SURFACE_TYPE;
209 configAttribs[i++] = EGL_PIXMAP_BIT;
210 configAttribs[i++] = EGL_DEPTH_SIZE;
212 configAttribs[i++] = 16;
214 configAttribs[i++] = 0;
215 configAttribs[i++] = EGL_STENCIL_SIZE;
217 configAttribs[i++] = 8;
219 configAttribs[i++] = 0;
221 configAttribs[i++] = EGL_RENDERABLE_TYPE;
222 configAttribs[i++] = EGL_OPENGL_ES2_BIT;
223 configAttribs[i++] = EGL_NONE;
225 if (eglChooseConfig(m_display, configAttribs, &m_surfaceConfig, 1, &configCount) != EGL_TRUE) {
226 HandleEGLError("eglChooseConfig");
229 m_context = eglCreateContext(m_display, m_surfaceConfig, EGL_NO_CONTEXT, contextAttribs);
230 if( m_context == EGL_NO_CONTEXT) {
231 HandleEGLError("eglCreateContext");
237 bool PixmapContextTizen::makeCurrent(EGLSurface surface)
239 if (eglGetCurrentSurface(EGL_READ) == surface && eglGetCurrentSurface(EGL_DRAW) == surface && eglGetCurrentContext() == m_context)
242 if (eglMakeCurrent(m_display, surface, surface, m_context) != EGL_TRUE) {
243 HandleEGLError("eglMakeCurrent");
249 void* PixmapContextTizen::createSurface(int pixmapID)
251 EGLSurface surface = eglCreatePixmapSurface(m_display, m_surfaceConfig, pixmapID, NULL);
252 if (surface == EGL_NO_SURFACE)
253 HandleEGLError("eglCreatePixmapSurface");
258 void PixmapContextTizen::destroySurface(EGLSurface surface)
261 if (eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) != EGL_TRUE)
262 HandleEGLError("eglMakeCurrent");
263 eglDestroySurface(m_display, surface);
267 bool PixmapContextTizen::lockSurface(EGLSurface surface)
269 EGLint lockAttrib[] = { EGL_LOCK_USAGE_HINT_KHR, EGL_WRITE_SURFACE_BIT_KHR, EGL_NONE };
270 if (!eglLockSurfaceKHR)
271 eglLockSurfaceKHR = (PFNEGLLOCKSURFACEKHRPROC)eglGetProcAddress("eglLockSurfaceKHR");
273 if (strcmp(eglQueryString (m_display, EGL_VENDOR), "ARM") != 0)
274 eglMakeCurrent(m_display, surface, surface, m_context);
276 if (eglLockSurfaceKHR(m_display, surface, lockAttrib) != EGL_TRUE) {
277 HandleEGLError("eglLockSurfaceKHR");
283 bool PixmapContextTizen::unlockSurface(EGLSurface surface)
285 if (!eglUnlockSurfaceKHR)
286 eglUnlockSurfaceKHR = (PFNEGLUNLOCKSURFACEKHRPROC)eglGetProcAddress("eglUnlockSurfaceKHR");
287 if (eglUnlockSurfaceKHR(m_display, surface) != EGL_TRUE) {
288 HandleEGLError("eglUnlockSurfaceKHR");
294 bool PixmapContextTizen::querySurface(EGLSurface surface, EGLint* value)
296 if (eglQuerySurface(m_display, surface, EGL_BITMAP_POINTER_KHR, value) != EGL_TRUE) {
297 HandleEGLError("eglQuerySurface");
303 int PixmapContextTizen::createPixmap(int width, int height)
305 // FIXME : Create 16 bit pixmap when alpha is off. Graphics driver needs to be fixed first.
306 int pix = XCreatePixmap(g_nativeDisplay, g_nativeWindow, width, height, 32);
307 XFlush(g_nativeDisplay);
312 void PixmapContextTizen::freePixmap(int pixmap)
315 XFreePixmap(g_nativeDisplay, pixmap);
318 PassOwnPtr<SharedPlatformSurfaceTizen> SharedPlatformSurfaceTizen::create(const IntSize& size, bool lockable, bool hasAlpha, bool hasDepth, bool hasStencil, PixmapContextTizen* pixmapContext)
320 OwnPtr<SharedPlatformSurfaceTizen> pixmap = adoptPtr(new SharedPlatformSurfaceTizen(size, lockable, hasAlpha, hasDepth, hasStencil, pixmapContext));
321 if (!pixmap->initialize())
323 return pixmap.release();
326 SharedPlatformSurfaceTizen::SharedPlatformSurfaceTizen(const IntSize& size, bool lockable, bool hasAlpha, bool hasDepth, bool hasStencil, PixmapContextTizen* pixmapContext)
330 , m_isLockable(lockable)
331 , m_hasAlpha(hasAlpha)
332 , m_hasDepth(hasDepth)
333 , m_hasStencil(hasStencil)
336 m_pixmapContext = pixmapContext;
339 SharedPlatformSurfaceTizen::~SharedPlatformSurfaceTizen()
341 if (m_surface != EGL_NO_SURFACE) {
342 m_pixmapContext->destroySurface(m_surface);
343 m_surface = EGL_NO_SURFACE;
346 m_pixmapContext->freePixmap(m_pixmap);
351 bool SharedPlatformSurfaceTizen::initialize()
353 if (m_size.isEmpty())
356 if(!m_pixmapContext) {
357 PixmapContextPool& pixmapContextPool = PixmapContextPool::getInstance();
358 m_pixmapContext = pixmapContextPool.getContext(m_isLockable, m_hasAlpha, m_hasDepth, m_hasStencil);
364 m_pixmap = m_pixmapContext->createPixmap(m_size.width(), m_size.height());
367 m_surface = (void*)(m_pixmapContext->createSurface(m_pixmap));
368 if (m_surface == EGL_NO_SURFACE)
373 void* SharedPlatformSurfaceTizen::context()
375 return m_pixmapContext->context();
378 bool SharedPlatformSurfaceTizen::makeContextCurrent()
380 return m_pixmapContext->makeCurrent(m_surface);
383 bool SharedPlatformSurfaceTizen::lockSurface()
385 return m_pixmapContext->lockSurface(m_surface);
388 bool SharedPlatformSurfaceTizen::unlockSurface()
390 return m_pixmapContext->unlockSurface(m_surface);
393 bool SharedPlatformSurfaceTizen::querySurface(int* value)
398 ret = m_pixmapContext->querySurface(m_surface, (EGLint*)&value_local);
399 *value = (int)value_local;
404 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING)