Tizen 2.1 base
[framework/osp/uifw.git] / src / graphics / opengl / FGrp_CanvasTexture.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /*
19  * @file        FGrp_CanvasTexture.cpp
20  * @brief       This is the implementation file for internal CanvasTexture class.
21  *
22  */
23
24 #include <new>
25 #include <memory>
26
27 #include <Ecore_X.h>
28
29 #include <FBaseString.h>
30
31 #include <FBaseSysLog.h>
32 #include <FGrpBitmap.h>
33 #include <FGrpBufferInfo.h>
34 #include <FGrpCanvas.h>
35
36 #include "FGrp_CanvasTexture.h"
37
38 using namespace Tizen::Base;
39 using namespace Tizen::Graphics;
40
41 namespace Tizen { namespace Graphics { namespace Opengl
42 {
43
44 #ifdef __cplusplus
45 extern "C"
46 {
47 #endif
48
49 EGLNativePixmapType _CreateNativePixmapEx(Bitmap* pBitmap, BufferInfo& bufferInfo);
50
51 #ifdef __cplusplus
52 }
53 #endif
54
55 _CanvasTexture::_CanvasTexture(void)
56         : __nativePixmap((EGLNativePixmapType)0)
57         , __eglImage((EGLImageKHR)0)
58         , __pBitmap(null)
59 {
60 }
61
62 _CanvasTexture::~_CanvasTexture(void)
63 {
64         __Release();
65 }
66
67 result
68 _CanvasTexture::Construct(const int textureId, const int width, const int height)
69 {
70 #if !defined(_OSP_EMUL_)
71         EGLDisplay display = eglGetCurrentDisplay();
72         GLint maxTextureSize = 0;
73
74         SysTryReturnResult(NID_GRP, IsSupported(), E_SYSTEM, "CanvasTexture not supported on this device.");
75
76         PFNEGLCREATEIMAGEKHRPROC funcEglCreateImageKhr =
77                         (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");
78         PFNGLEGLIMAGETARGETTEXTURE2DOESPROC funcGlEglImageTargetTexture2dOes =
79                         (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES");
80         SysTryReturnResult(NID_GRP, (funcEglCreateImageKhr != null && funcGlEglImageTargetTexture2dOes != null),
81                         E_UNSUPPORTED_OPERATION, "CanvasTexture not supported on this device");
82
83         glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
84
85         SysTryReturnResult(NID_GRP, (width > 0 && height > 0 && width <= maxTextureSize && height <= maxTextureSize),
86                         E_INVALID_ARG, "width(%d) or height(%d) not supported.", width, height);
87
88
89         std::auto_ptr <Bitmap> bitmap(new (std::nothrow) Bitmap);
90         SysTryReturnResult(NID_GRP, bitmap.get() != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
91
92         BufferInfo bufferInfo;
93         bufferInfo.width = width;
94         bufferInfo.height = height;
95         bufferInfo.bitsPerPixel = ecore_x_default_depth_get(ecore_x_display_get(), ecore_x_default_screen_get());
96
97         this->__nativePixmap = _CreateNativePixmapEx(bitmap.get(), bufferInfo);
98         SysTryReturnResult(NID_GRP, this->__nativePixmap != (EGLNativePixmapType)0, E_OUT_OF_MEMORY, "Creating native pixmap failed.");
99
100         this->__eglImage = funcEglCreateImageKhr(display, EGL_NO_CONTEXT, EGL_NATIVE_PIXMAP_KHR,
101                         (EGLClientBuffer)this->__nativePixmap, null);
102         SysTryReturnResult(NID_GRP, this->__eglImage != (EGLImageKHR)0, E_OUT_OF_MEMORY, "Creating ImageKHR failed.");
103
104         glBindTexture(GL_TEXTURE_2D, textureId);
105         funcGlEglImageTargetTexture2dOes(GL_TEXTURE_2D, this->__eglImage);
106
107         this->__pBitmap = bitmap.release();
108         return E_SUCCESS;
109 #else
110         return E_UNSUPPORTED_OPERATION;
111 #endif
112 }
113
114 Canvas*
115 _CanvasTexture::GetCanvasN(void) const
116 {
117         SysTryReturn(NID_GRP, this->__IsValid(), null, E_OPERATION_FAILED, "This instance is not constructed yet.\n");
118
119         BufferInfo bufferInfo;
120         this->__pBitmap->Lock(bufferInfo);
121         this->__pBitmap->Unlock();
122
123         std::auto_ptr <Canvas> canvas(new (std::nothrow) Canvas);
124         SysTryReturn(NID_GRP, canvas.get() != null, null, E_OUT_OF_MEMORY, "The memory is insufficient.");
125
126         result r = canvas.get()->Construct(bufferInfo);
127         SysTryReturn(NID_GRP, r == E_SUCCESS, null, E_OPERATION_FAILED, "[E_SYSTEM] Canvas construction failed.");
128
129         return canvas.release();
130 }
131
132 bool
133 _CanvasTexture::IsSupported(void)
134 {
135         const char* pString = (const char*)glGetString(GL_EXTENSIONS);
136         String extensions(pString);
137
138         return extensions.Contains(Tizen::Base::String("GL_OES_EGL_image"));
139 }
140
141 result
142 _CanvasTexture::__Release(void)
143 {
144         if (this->__eglImage != (EGLImageKHR)0)
145         {
146                 PFNEGLDESTROYIMAGEKHRPROC funcEglDestroyImageKhr =
147                                 (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");
148                 SysTryReturnResult(NID_GRP, funcEglDestroyImageKhr != null, E_SYSTEM, "Destroy ImageKHR failed.");
149
150                 EGLDisplay display = eglGetCurrentDisplay();
151                 funcEglDestroyImageKhr(display, this->__eglImage);
152                 this->__eglImage = (EGLImageKHR)0;
153         }
154
155         delete this->__pBitmap;
156         this->__pBitmap = null;
157         this->__nativePixmap = (EGLNativePixmapType)0;
158
159         return E_SUCCESS;
160 }
161
162 bool
163 _CanvasTexture::__IsValid(void) const
164 {
165         return (this->__nativePixmap != (EGLNativePixmapType)0 &&
166                         this->__eglImage != (EGLImageKHR)0 &&
167                         this->__pBitmap != null);
168 }
169
170 }}} // Tizen::Graphics::Opengl