Revert " modify license, permission and remove ^M char"
[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 <FSysSystemInfo.h>
28 #include <FBaseString.h>
29 #include <FBaseSysLog.h>
30 #include <FGrpBitmap.h>
31 #include <FGrpBufferInfo.h>
32 #include <FGrpCanvas.h>
33
34 #include "FGrp_CanvasTool.h"
35 #include "FGrp_CanvasTexture.h"
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Graphics;
39
40 namespace Tizen { namespace Graphics { namespace Opengl
41 {
42
43 #ifdef __cplusplus
44 extern "C"
45 {
46 #endif
47
48 EGLNativePixmapType _CreateNativePixmapEx(Bitmap* pBitmap, BufferInfo& bufferInfo);
49 void _GlBindTexture_1(GLenum target, GLuint texture);
50 void _GlGetIntegerv_1(GLenum pname, GLint* params);
51 const GLubyte* _GlGetString_1(GLenum name);
52 void _GlTexImage2D_1(GLenum target, GLint level, GLint internalformat
53                 , GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* pPixels);
54 void _GlTexSubImage2D_1(GLenum target, GLint level, GLint xoffset, GLint yoffset,
55                 GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid* pPixels);
56 void _GlTexImage2D_2(GLenum target, GLint level, GLenum internalformat
57                 , GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* pPixels);
58 void _GlTexSubImage2D_2(GLenum target, GLint level, GLint xoffset, GLint yoffset,
59                 GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid* pPixels);
60
61 #ifdef __cplusplus
62 }
63 #endif
64
65 _CanvasTexture::_CanvasTexture(void)
66         : __glVersion(0)
67         , __eglDisplay(EGL_NO_DISPLAY)
68         , __eglSurface(EGL_NO_SURFACE)
69         , __eglContext(EGL_NO_CONTEXT)
70         , __nativePixmap((EGLNativePixmapType)0)
71         , __textureId(0)
72         , __textureFormat(GL_RGBA)
73         , __pBitmap(null)
74         , __pBitmapColorConvert(null)
75 {
76 }
77
78 _CanvasTexture::~_CanvasTexture(void)
79 {
80         delete this->__pBitmap;
81         delete this->__pBitmapColorConvert;
82 }
83
84 result
85 _CanvasTexture::Construct(const int textureId, const int width, const int height)
86 {
87         bool isSupported = false;
88         bool isSupported11 = false;
89         bool isSupported20 = false;
90
91         Tizen::System::SystemInfo::GetValue(L"http://tizen.org/feature/opengles", isSupported);
92         Tizen::System::SystemInfo::GetValue(L"http://tizen.org/feature/opengles.version.1_1", isSupported11);
93         Tizen::System::SystemInfo::GetValue(L"http://tizen.org/feature/opengles.version.2_0", isSupported20);
94
95         SysTryReturnResult(NID_GRP, isSupported || isSupported11 || isSupported20
96                         , E_UNSUPPORTED_OPERATION, "Not supported on this device.");
97
98         this->__eglDisplay = eglGetCurrentDisplay();
99         this->__eglSurface = eglGetCurrentSurface(EGL_DRAW);
100         this->__eglContext = eglGetCurrentContext();
101
102         eglQueryContext(this->__eglDisplay, this->__eglContext,  EGL_CONTEXT_CLIENT_VERSION, &this->__glVersion);
103         SysTryReturnResult(NID_GRP
104                         , this->__eglDisplay != EGL_NO_DISPLAY
105                         && this->__eglSurface != EGL_NO_SURFACE
106                         && this->__eglContext != EGL_NO_CONTEXT
107                         && (this->__glVersion == 1 || this->__glVersion == 2), E_INVALID_STATE
108                         , "Getting egl informations failed! eglDisplay:%#x eglContext:%#x glVersion:%d"
109                         , (unsigned int)this->__eglDisplay, (unsigned int)this->__eglContext, this->__glVersion);
110
111         GLint maxTextureSize = 0;
112         if (this->__glVersion == 1)
113         {
114                 _GlGetIntegerv_1(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
115         }
116         else
117         {
118                 _GlGetIntegerv_2(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
119         }
120         SysTryReturnResult(NID_GRP, (width > 0 && height > 0 && width <= maxTextureSize && height <= maxTextureSize)
121                         , E_INVALID_ARG, "width(%d) or height(%d) not supported.", width, height);
122
123         std::auto_ptr <Bitmap> bitmap(new (std::nothrow) Bitmap);
124         SysTryReturnResult(NID_GRP, bitmap.get() != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
125
126         result r = bitmap.get()->Construct(Rectangle(0, 0, width, height));
127         SysTryReturnResult(NID_GRP, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
128
129         if (IsSupported(L"GL_EXT_texture_format_BGRA8888"))
130         {
131                 this->__textureFormat = GL_BGRA;
132         }
133         else
134         {
135                 std::auto_ptr <Bitmap> bitmapColorConvert(new (std::nothrow) Bitmap);
136                 SysTryReturnResult(NID_GRP, bitmapColorConvert.get() != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
137
138                 r = bitmapColorConvert.get()->Construct(Rectangle(0, 0, width, height));
139                 SysTryReturnResult(NID_GRP, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
140
141                 this->__pBitmapColorConvert = bitmapColorConvert.release();
142         }
143
144         bitmap.get()->Lock(__bufferInfo);
145         memset(__bufferInfo.pPixels, 0, __bufferInfo.pitch * __bufferInfo.height);
146         bitmap.get()->Unlock();
147
148         if (this->__glVersion == 1)
149         {
150                 _GlBindTexture_1(GL_TEXTURE_2D, textureId);
151                 _GlTexImage2D_1(GL_TEXTURE_2D, 0, this->__textureFormat, width, height, 0, this->__textureFormat, GL_UNSIGNED_BYTE, __bufferInfo.pPixels);
152         }
153         else
154         {
155                 _GlBindTexture_2(GL_TEXTURE_2D, textureId);
156                 _GlTexImage2D_2(GL_TEXTURE_2D, 0, this->__textureFormat, width, height, 0, this->__textureFormat, GL_UNSIGNED_BYTE, __bufferInfo.pPixels);
157         }
158
159         this->__pBitmap = bitmap.release();
160         this->__textureId = textureId;
161
162         return r;
163 }
164
165 bool
166 _CanvasTexture::ShowCallback(void* pParam)
167 {
168         _CanvasTexture* pCanvasTexture = dynamic_cast<_CanvasTexture*> ((_CanvasTexture*)pParam);
169         SysTryReturnResult(NID_GRP, pCanvasTexture != null, true, "Invalid parameter.");
170
171         pCanvasTexture->UpdateTexture();
172
173         return true;
174 }
175
176 Canvas*
177 _CanvasTexture::GetCanvasN(void) const
178 {
179         std::auto_ptr <Canvas> canvas(new (std::nothrow) Canvas);
180         SysTryReturnResult(NID_GRP, canvas.get() != null, null, "The memory is insufficient.");
181
182         result r = canvas.get()->Construct(__bufferInfo);
183         SysTryReturnResult(NID_GRP, !IsFailed(r), null, "[%s] Propagating.", GetErrorMessage(r));
184
185         bool ret = _CanvasTool::SetCallback(*canvas.get(), this->ShowCallback, (void*)this);
186         SysTryReturnResult(NID_GRP, ret, null, "[%s] Propagating.");
187
188         return canvas.release();
189 }
190
191 bool
192 _CanvasTexture::IsSupported(String string)
193 {
194         char* pString = null;
195         if (this->__glVersion == 1)
196         {
197                 pString = (char*)_GlGetString_1(GL_EXTENSIONS);
198         }
199         else
200         {
201                 pString = (char*)_GlGetString_2(GL_EXTENSIONS);
202         }
203         String extensions(pString);
204
205         return extensions.Contains(string);
206 }
207
208 void
209 _CanvasTexture::UpdateTexture(void)
210 {
211         bool needMakeCurrent = false;
212
213         EGLDisplay eglDisplay = eglGetCurrentDisplay();
214         EGLSurface eglSurface = eglGetCurrentSurface(EGL_DRAW);
215         EGLContext eglContext = eglGetCurrentContext();
216
217         if (this->__eglDisplay != eglDisplay
218                         || this->__eglSurface == eglSurface
219                         || this->__eglContext == eglContext)
220         {
221                 needMakeCurrent = true;
222                 eglMakeCurrent(this->__eglDisplay, this->__eglSurface, this->__eglSurface, this->__eglContext);
223         }
224
225         if (__glVersion == 1)
226         {
227                 _GlBindTexture_1(GL_TEXTURE_2D, __textureId);
228         }
229         else
230         {
231                 _GlBindTexture_2(GL_TEXTURE_2D, __textureId);
232         }
233
234         void* pPixels = __bufferInfo.pPixels;
235
236         if (this->__textureFormat == GL_RGBA && this->__pBitmapColorConvert != null)
237         {
238                 int index = 0;
239                 BufferInfo bufferInfoConvert;
240                 this->__pBitmapColorConvert->Lock(bufferInfoConvert);
241                 this->__pBitmapColorConvert->Unlock();
242
243                 unsigned int* pSource = (unsigned int*)__bufferInfo.pPixels;
244                 unsigned int* pDestination = (unsigned int*)bufferInfoConvert.pPixels;
245
246                 for (int y = 0; y < __bufferInfo.height; y++)
247                 {
248                         index = __bufferInfo.width * y;
249                         for (int x = 0; x < __bufferInfo.width; x++)
250                         {
251                                 pDestination[index + x] = (0x000000ffu & (pSource[index + x]>> 16))
252                                                                                  | (0xff00ff00u & (pSource[index + x]))
253                                                                                  | (0x00ff0000u & (pSource[index + x] << 16));
254                         }
255                 }
256                 pPixels = (unsigned int*)pDestination;
257         }
258
259         if (__glVersion == 1)
260         {
261                 _GlTexSubImage2D_1(GL_TEXTURE_2D, 0, 0, 0, __bufferInfo.width, __bufferInfo.height
262                                 , __textureFormat, GL_UNSIGNED_BYTE, pPixels);
263         }
264         else
265         {
266                 _GlTexSubImage2D_2(GL_TEXTURE_2D, 0, 0, 0, __bufferInfo.width, __bufferInfo.height
267                                 , __textureFormat, GL_UNSIGNED_BYTE, pPixels);
268         }
269 }
270
271 }}} // Tizen::Graphics::Opengl
272