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;
191 configAttribs[i++] = 8;
193 configAttribs[i++] = 0;
194 configAttribs[i++] = EGL_CONFIG_CAVEAT;
195 configAttribs[i++] = EGL_NON_CONFORMANT_CONFIG;
199 configAttribs[i++] = EGL_MATCH_FORMAT_KHR;
200 // FIXME : This strcmp will be replaced.to the feature macro.
201 if (!strcmp(eglQueryString (m_display, EGL_VENDOR), "ARM"))
202 configAttribs[i++] = EGL_FORMAT_RGBA_8888_EXACT_KHR;
203 else if (!strcmp(eglQueryString (m_display, EGL_VENDOR), "Imagination Technologies"))
204 configAttribs[i++] = EGL_FORMAT_RGBA_8888_EXACT_KHR;
206 configAttribs[i++] = EGL_FORMAT_RGBA_8888_KHR;
207 configAttribs[i++] = EGL_SURFACE_TYPE;
208 configAttribs[i++] = EGL_PIXMAP_BIT | EGL_LOCK_SURFACE_BIT_KHR;
209 configAttribs[i++] = EGL_DEPTH_SIZE;
210 configAttribs[i++] = 0;
211 configAttribs[i++] = EGL_STENCIL_SIZE;
212 configAttribs[i++] = 0;
214 configAttribs[i++] = EGL_SURFACE_TYPE;
215 configAttribs[i++] = EGL_PIXMAP_BIT;
216 configAttribs[i++] = EGL_DEPTH_SIZE;
218 configAttribs[i++] = 16;
220 configAttribs[i++] = 0;
221 configAttribs[i++] = EGL_STENCIL_SIZE;
223 configAttribs[i++] = 8;
225 configAttribs[i++] = 0;
227 configAttribs[i++] = EGL_RENDERABLE_TYPE;
228 configAttribs[i++] = EGL_OPENGL_ES2_BIT;
229 configAttribs[i++] = EGL_NONE;
231 if (eglChooseConfig(m_display, configAttribs, &m_surfaceConfig, 1, &configCount) != EGL_TRUE) {
232 HandleEGLError("eglChooseConfig");
235 m_context = eglCreateContext(m_display, m_surfaceConfig, EGL_NO_CONTEXT, contextAttribs);
236 if( m_context == EGL_NO_CONTEXT) {
237 HandleEGLError("eglCreateContext");
243 bool PixmapContextTizen::makeCurrent(EGLSurface surface)
245 if (eglGetCurrentSurface(EGL_READ) == surface && eglGetCurrentSurface(EGL_DRAW) == surface && eglGetCurrentContext() == m_context)
248 if (eglMakeCurrent(m_display, surface, surface, m_context) != EGL_TRUE) {
249 HandleEGLError("eglMakeCurrent");
255 void* PixmapContextTizen::createSurface(int pixmapID)
257 EGLSurface surface = eglCreatePixmapSurface(m_display, m_surfaceConfig, pixmapID, NULL);
258 if (surface == EGL_NO_SURFACE)
259 HandleEGLError("eglCreatePixmapSurface");
264 void PixmapContextTizen::destroySurface(EGLSurface surface)
267 if (eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) != EGL_TRUE)
268 HandleEGLError("eglMakeCurrent");
269 eglDestroySurface(m_display, surface);
273 bool PixmapContextTizen::lockSurface(EGLSurface surface)
275 EGLint lockAttrib[] = { EGL_LOCK_USAGE_HINT_KHR, EGL_WRITE_SURFACE_BIT_KHR, EGL_NONE };
276 if (!eglLockSurfaceKHR)
277 eglLockSurfaceKHR = (PFNEGLLOCKSURFACEKHRPROC)eglGetProcAddress("eglLockSurfaceKHR");
279 if (strcmp(eglQueryString (m_display, EGL_VENDOR), "ARM") != 0)
280 eglMakeCurrent(m_display, surface, surface, m_context);
282 if (eglLockSurfaceKHR(m_display, surface, lockAttrib) != EGL_TRUE) {
283 HandleEGLError("eglLockSurfaceKHR");
289 bool PixmapContextTizen::unlockSurface(EGLSurface surface)
291 if (!eglUnlockSurfaceKHR)
292 eglUnlockSurfaceKHR = (PFNEGLUNLOCKSURFACEKHRPROC)eglGetProcAddress("eglUnlockSurfaceKHR");
293 if (eglUnlockSurfaceKHR(m_display, surface) != EGL_TRUE) {
294 HandleEGLError("eglUnlockSurfaceKHR");
300 bool PixmapContextTizen::querySurface(EGLSurface surface, EGLint* value)
302 if (eglQuerySurface(m_display, surface, EGL_BITMAP_POINTER_KHR, value) != EGL_TRUE) {
303 HandleEGLError("eglQuerySurface");
309 int PixmapContextTizen::createPixmap(int width, int height)
311 // FIXME : Create 16 bit pixmap when alpha is off. Graphics driver needs to be fixed first.
312 int pix = XCreatePixmap(g_nativeDisplay, g_nativeWindow, width, height, 32);
313 XFlush(g_nativeDisplay);
318 void PixmapContextTizen::freePixmap(int pixmap)
321 XFreePixmap(g_nativeDisplay, pixmap);
324 PassOwnPtr<SharedPlatformSurfaceTizen> SharedPlatformSurfaceTizen::create(const IntSize& size, bool lockable, bool hasAlpha, bool hasDepth, bool hasStencil, PixmapContextTizen* pixmapContext)
326 OwnPtr<SharedPlatformSurfaceTizen> pixmap = adoptPtr(new SharedPlatformSurfaceTizen(size, lockable, hasAlpha, hasDepth, hasStencil, pixmapContext));
327 if (!pixmap->initialize())
329 return pixmap.release();
332 SharedPlatformSurfaceTizen::SharedPlatformSurfaceTizen(const IntSize& size, bool lockable, bool hasAlpha, bool hasDepth, bool hasStencil, PixmapContextTizen* pixmapContext)
336 , m_isLockable(lockable)
337 , m_hasAlpha(hasAlpha)
338 , m_hasDepth(hasDepth)
339 , m_hasStencil(hasStencil)
342 m_pixmapContext = pixmapContext;
345 SharedPlatformSurfaceTizen::~SharedPlatformSurfaceTizen()
347 if (m_surface != EGL_NO_SURFACE) {
348 m_pixmapContext->destroySurface(m_surface);
349 m_surface = EGL_NO_SURFACE;
352 m_pixmapContext->freePixmap(m_pixmap);
357 bool SharedPlatformSurfaceTizen::initialize()
359 if (m_size.isEmpty())
362 if(!m_pixmapContext) {
363 PixmapContextPool& pixmapContextPool = PixmapContextPool::getInstance();
364 m_pixmapContext = pixmapContextPool.getContext(m_isLockable, m_hasAlpha, m_hasDepth, m_hasStencil);
370 m_pixmap = m_pixmapContext->createPixmap(m_size.width(), m_size.height());
373 m_surface = (void*)(m_pixmapContext->createSurface(m_pixmap));
374 if (m_surface == EGL_NO_SURFACE)
379 void* SharedPlatformSurfaceTizen::context()
381 return m_pixmapContext->context();
384 bool SharedPlatformSurfaceTizen::makeContextCurrent()
386 return m_pixmapContext->makeCurrent(m_surface);
389 bool SharedPlatformSurfaceTizen::lockSurface()
391 return m_pixmapContext->lockSurface(m_surface);
394 bool SharedPlatformSurfaceTizen::unlockSurface()
396 return m_pixmapContext->unlockSurface(m_surface);
399 bool SharedPlatformSurfaceTizen::querySurface(int* value)
404 ret = m_pixmapContext->querySurface(m_surface, (EGLint*)&value_local);
405 *value = (int)value_local;
410 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING)