2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://floralicense.org/license/
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.
19 * @file FGrp_CanvasTexture.cpp
20 * @brief This is the implementation file for internal CanvasTexture class.
27 #include <FSysSystemInfo.h>
28 #include <FBaseString.h>
29 #include <FBaseSysLog.h>
30 #include <FGrpBitmap.h>
31 #include <FGrpBufferInfo.h>
32 #include <FGrpCanvas.h>
34 #include "FGrp_CanvasTool.h"
35 #include "FGrp_CanvasTexture.h"
37 using namespace Tizen::Base;
38 using namespace Tizen::Graphics;
40 namespace Tizen { namespace Graphics { namespace Opengl
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);
65 _CanvasTexture::_CanvasTexture(void)
67 , __eglDisplay(EGL_NO_DISPLAY)
68 , __eglSurface(EGL_NO_SURFACE)
69 , __eglContext(EGL_NO_CONTEXT)
70 , __nativePixmap((EGLNativePixmapType)0)
72 , __textureFormat(GL_RGBA)
74 , __pBitmapColorConvert(null)
78 _CanvasTexture::~_CanvasTexture(void)
80 delete this->__pBitmap;
81 delete this->__pBitmapColorConvert;
85 _CanvasTexture::Construct(const int textureId, const int width, const int height)
87 bool isSupported = false;
88 bool isSupported11 = false;
89 bool isSupported20 = false;
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);
95 SysTryReturnResult(NID_GRP, isSupported || isSupported11 || isSupported20
96 , E_UNSUPPORTED_OPERATION, "Not supported on this device.");
98 this->__eglDisplay = eglGetCurrentDisplay();
99 this->__eglSurface = eglGetCurrentSurface(EGL_DRAW);
100 this->__eglContext = eglGetCurrentContext();
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);
111 GLint maxTextureSize = 0;
112 if (this->__glVersion == 1)
114 _GlGetIntegerv_1(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
118 _GlGetIntegerv_2(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
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);
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.");
126 result r = bitmap.get()->Construct(Rectangle(0, 0, width, height));
127 SysTryReturnResult(NID_GRP, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
129 if (IsSupported(L"GL_EXT_texture_format_BGRA8888"))
131 this->__textureFormat = GL_BGRA;
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.");
138 r = bitmapColorConvert.get()->Construct(Rectangle(0, 0, width, height));
139 SysTryReturnResult(NID_GRP, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
141 this->__pBitmapColorConvert = bitmapColorConvert.release();
144 bitmap.get()->Lock(__bufferInfo);
145 memset(__bufferInfo.pPixels, 0, __bufferInfo.pitch * __bufferInfo.height);
146 bitmap.get()->Unlock();
148 if (this->__glVersion == 1)
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);
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);
159 this->__pBitmap = bitmap.release();
160 this->__textureId = textureId;
166 _CanvasTexture::ShowCallback(void* pParam)
168 _CanvasTexture* pCanvasTexture = dynamic_cast<_CanvasTexture*> ((_CanvasTexture*)pParam);
169 SysTryReturnResult(NID_GRP, pCanvasTexture != null, true, "Invalid parameter.");
171 pCanvasTexture->UpdateTexture();
177 _CanvasTexture::GetCanvasN(void) const
179 std::auto_ptr <Canvas> canvas(new (std::nothrow) Canvas);
180 SysTryReturnResult(NID_GRP, canvas.get() != null, null, "The memory is insufficient.");
182 result r = canvas.get()->Construct(__bufferInfo);
183 SysTryReturnResult(NID_GRP, !IsFailed(r), null, "[%s] Propagating.", GetErrorMessage(r));
185 bool ret = _CanvasTool::SetCallback(*canvas.get(), this->ShowCallback, (void*)this);
186 SysTryReturnResult(NID_GRP, ret, null, "[%s] Propagating.");
188 return canvas.release();
192 _CanvasTexture::IsSupported(String string)
194 char* pString = null;
195 if (this->__glVersion == 1)
197 pString = (char*)_GlGetString_1(GL_EXTENSIONS);
201 pString = (char*)_GlGetString_2(GL_EXTENSIONS);
203 String extensions(pString);
205 return extensions.Contains(string);
209 _CanvasTexture::UpdateTexture(void)
211 bool needMakeCurrent = false;
213 EGLDisplay eglDisplay = eglGetCurrentDisplay();
214 EGLSurface eglSurface = eglGetCurrentSurface(EGL_DRAW);
215 EGLContext eglContext = eglGetCurrentContext();
217 if (this->__eglDisplay != eglDisplay
218 || this->__eglSurface == eglSurface
219 || this->__eglContext == eglContext)
221 needMakeCurrent = true;
222 eglMakeCurrent(this->__eglDisplay, this->__eglSurface, this->__eglSurface, this->__eglContext);
225 if (__glVersion == 1)
227 _GlBindTexture_1(GL_TEXTURE_2D, __textureId);
231 _GlBindTexture_2(GL_TEXTURE_2D, __textureId);
234 void* pPixels = __bufferInfo.pPixels;
236 if (this->__textureFormat == GL_RGBA && this->__pBitmapColorConvert != null)
239 BufferInfo bufferInfoConvert;
240 this->__pBitmapColorConvert->Lock(bufferInfoConvert);
241 this->__pBitmapColorConvert->Unlock();
243 unsigned int* pSource = (unsigned int*)__bufferInfo.pPixels;
244 unsigned int* pDestination = (unsigned int*)bufferInfoConvert.pPixels;
246 for (int y = 0; y < __bufferInfo.height; y++)
248 index = __bufferInfo.width * y;
249 for (int x = 0; x < __bufferInfo.width; x++)
251 pDestination[index + x] = (0x000000ffu & (pSource[index + x]>> 16))
252 | (0xff00ff00u & (pSource[index + x]))
253 | (0x00ff0000u & (pSource[index + x] << 16));
256 pPixels = (unsigned int*)pDestination;
259 if (__glVersion == 1)
261 _GlTexSubImage2D_1(GL_TEXTURE_2D, 0, 0, 0, __bufferInfo.width, __bufferInfo.height
262 , __textureFormat, GL_UNSIGNED_BYTE, pPixels);
266 _GlTexSubImage2D_2(GL_TEXTURE_2D, 0, 0, 0, __bufferInfo.width, __bufferInfo.height
267 , __textureFormat, GL_UNSIGNED_BYTE, pPixels);
271 }}} // Tizen::Graphics::Opengl