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.
29 #include <FBaseString.h>
31 #include <FBaseSysLog.h>
32 #include <FGrpBitmap.h>
33 #include <FGrpBufferInfo.h>
34 #include <FGrpCanvas.h>
36 #include "FGrp_CanvasTexture.h"
38 using namespace Tizen::Base;
39 using namespace Tizen::Graphics;
41 namespace Tizen { namespace Graphics { namespace Opengl
49 EGLNativePixmapType _CreateNativePixmapEx(Bitmap* pBitmap, BufferInfo& bufferInfo);
55 _CanvasTexture::_CanvasTexture(void)
56 : __nativePixmap((EGLNativePixmapType)0)
57 , __eglImage((EGLImageKHR)0)
62 _CanvasTexture::~_CanvasTexture(void)
68 _CanvasTexture::Construct(const int textureId, const int width, const int height)
70 #if !defined(_OSP_EMUL_)
71 EGLDisplay display = eglGetCurrentDisplay();
72 GLint maxTextureSize = 0;
74 SysTryReturnResult(NID_GRP, IsSupported(), E_SYSTEM, "CanvasTexture not supported on this device.");
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");
83 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
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);
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.");
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());
97 this->__nativePixmap = _CreateNativePixmapEx(bitmap.get(), bufferInfo);
98 SysTryReturnResult(NID_GRP, this->__nativePixmap != (EGLNativePixmapType)0, E_OUT_OF_MEMORY, "Creating native pixmap failed.");
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.");
104 glBindTexture(GL_TEXTURE_2D, textureId);
105 funcGlEglImageTargetTexture2dOes(GL_TEXTURE_2D, this->__eglImage);
107 this->__pBitmap = bitmap.release();
110 return E_UNSUPPORTED_OPERATION;
115 _CanvasTexture::GetCanvasN(void) const
117 SysTryReturn(NID_GRP, this->__IsValid(), null, E_OPERATION_FAILED, "This instance is not constructed yet.\n");
119 BufferInfo bufferInfo;
120 this->__pBitmap->Lock(bufferInfo);
121 this->__pBitmap->Unlock();
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.");
126 result r = canvas.get()->Construct(bufferInfo);
127 SysTryReturn(NID_GRP, r == E_SUCCESS, null, E_OPERATION_FAILED, "[E_SYSTEM] Canvas construction failed.");
129 return canvas.release();
133 _CanvasTexture::IsSupported(void)
135 const char* pString = (const char*)glGetString(GL_EXTENSIONS);
136 String extensions(pString);
138 return extensions.Contains(Tizen::Base::String("GL_OES_EGL_image"));
142 _CanvasTexture::__Release(void)
144 if (this->__eglImage != (EGLImageKHR)0)
146 PFNEGLDESTROYIMAGEKHRPROC funcEglDestroyImageKhr =
147 (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");
148 SysTryReturnResult(NID_GRP, funcEglDestroyImageKhr != null, E_SYSTEM, "Destroy ImageKHR failed.");
150 EGLDisplay display = eglGetCurrentDisplay();
151 funcEglDestroyImageKhr(display, this->__eglImage);
152 this->__eglImage = (EGLImageKHR)0;
155 delete this->__pBitmap;
156 this->__pBitmap = null;
157 this->__nativePixmap = (EGLNativePixmapType)0;
163 _CanvasTexture::__IsValid(void) const
165 return (this->__nativePixmap != (EGLNativePixmapType)0 &&
166 this->__eglImage != (EGLImageKHR)0 &&
167 this->__pBitmap != null);
170 }}} // Tizen::Graphics::Opengl