Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / renderer / graphics-engine / FUiEffects_RendererGraphicsEngineRenderTextureCache.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                FUiEffects_RendererGraphicsEngineRenderTextureCache.cpp
20  * @brief               Implementation of class for render textures cache
21  *
22  */
23
24 #include <renderer/graphics-engine/FUiEffects_RendererGraphicsEngineOpenGLImplementation.h>
25 #include <renderer/graphics-engine/FUiEffects_RendererGraphicsEngineRenderTextureCache.h>
26
27 namespace Tizen { namespace Ui { namespace Effects { namespace _Renderer { namespace GraphicsEngine
28 {
29
30 RenderTextureCache::RenderTextureCache(Render *aRender):
31         RenderCache(aRender),
32         _textureId(0)
33 {
34 }
35
36 RenderTextureCache::~RenderTextureCache()
37 {
38 }
39
40 void
41 RenderTextureCache::DropCache(void)
42 {
43         _textureId = 0;
44         return;
45 }
46
47 void
48 RenderTextureCache::DestroyCache(void)
49 {
50         if (_textureId)
51         {
52                 glDeleteTextures(1, &_textureId);
53         }
54
55         return;
56 }
57
58 GLenum
59 RenderTextureCache::GetGlInternalFormat(Tizen::Ui::Effects::_Renderer::EngineModel::TextureProperty::InternalFormat::Value internalFormat)
60 {
61         switch (internalFormat)
62         {
63         case EngineModel::TextureProperty::InternalFormat::ONE_COMPONENT:
64                 return 1;
65         case EngineModel::TextureProperty::InternalFormat::TWO_COMPONENTS:
66                 return 2;
67         case EngineModel::TextureProperty::InternalFormat::THREE_COMPONENTS:
68                 return 3;
69         case EngineModel::TextureProperty::InternalFormat::FOUR_COMPONENTS:
70                 return 4;
71         case EngineModel::TextureProperty::InternalFormat::DEPTH_COMPONENT:
72                 return GL_DEPTH_COMPONENT;
73         case EngineModel::TextureProperty::InternalFormat::DEPTH_COMPONENT_16:
74                 return GL_DEPTH_COMPONENT16;
75         case EngineModel::TextureProperty::InternalFormat::DEPTH_COMPONENT_24:
76                 ThrowJmp("GL_DEPTH_COMPONENT24 not allowed under GLES2");
77                 // fall through
78         case EngineModel::TextureProperty::InternalFormat::DEPTH_COMPONENT_32:
79                 ThrowJmp("GL_DEPTH_COMPONENT32 not allowed under GLES2");
80                 // fall through
81         case EngineModel::TextureProperty::InternalFormat::RGB:
82                 return GL_RGB;
83         case EngineModel::TextureProperty::InternalFormat::RGBA:
84                 return GL_RGBA;
85         case EngineModel::TextureProperty::InternalFormat::BGRA:
86                 return GL_BGRA;
87         }
88         return 1;
89 }
90
91 GLenum
92 RenderTextureCache::GetGlDataFormat(Tizen::Ui::Effects::_Renderer::EngineModel::TextureProperty::DataFormat::Value dataFormat)
93 {
94         switch (dataFormat)
95         {
96         case EngineModel::TextureProperty::DataFormat::RGB:
97                 return GL_RGB;
98         case EngineModel::TextureProperty::DataFormat::BGR:
99                 ThrowJmp("GL_BGR not allowed under GLES2");
100                 // fall through
101         case EngineModel::TextureProperty::DataFormat::RGBA:
102                 return GL_RGBA;
103         case EngineModel::TextureProperty::DataFormat::BGRA:
104                 return GL_BGRA_EXT;
105         case EngineModel::TextureProperty::DataFormat::LUMINANCE:
106                 return GL_LUMINANCE;
107         case EngineModel::TextureProperty::DataFormat::LUMINANCE_ALPHA:
108                 return GL_LUMINANCE_ALPHA;
109         }
110         ThrowJmp("RenderTextureCache::getGLDataFormat: unknown data format");
111 }
112
113 GLenum
114 RenderTextureCache::GetGlDataType(Tizen::Ui::Effects::_Renderer::EngineModel::TextureProperty::DataType::Value dataType)
115 {
116         switch (dataType)
117         {
118         case EngineModel::TextureProperty::DataType::UNSIGNED_BYTE:
119                 return GL_UNSIGNED_BYTE;
120         case EngineModel::TextureProperty::DataType::BYTE:
121                 return GL_BYTE;
122         case EngineModel::TextureProperty::DataType::UNSIGNED_SHORT:
123                 return GL_UNSIGNED_SHORT;
124         case EngineModel::TextureProperty::DataType::SHORT:
125                 return GL_SHORT;
126         case EngineModel::TextureProperty::DataType::UNSIGNED_INT:
127                 return GL_UNSIGNED_INT;
128         case EngineModel::TextureProperty::DataType::INT:
129                 return GL_INT;
130         case EngineModel::TextureProperty::DataType::FLOAT:
131                 return GL_FLOAT;
132         case EngineModel::TextureProperty::DataType::UNSIGNED_SHORT_565:
133                 return GL_UNSIGNED_SHORT_5_6_5;
134         }
135         ThrowJmp("RenderTextureCache::getGLDataType: unknown data type");
136 }
137
138 GLenum
139 RenderTextureCache::GetGlFilterType(Tizen::Ui::Effects::_Renderer::EngineModel::TextureProperty::FilterType::Value filterType)
140 {
141         switch (filterType)
142         {
143         case EngineModel::TextureProperty::FilterType::NEAREST:
144                 return GL_NEAREST;
145         case EngineModel::TextureProperty::FilterType::LINEAR:
146                 return GL_LINEAR;
147         case EngineModel::TextureProperty::FilterType::NEAREST_MIPMAP_NEAREST:
148                 return GL_NEAREST_MIPMAP_NEAREST;
149         case EngineModel::TextureProperty::FilterType::LINEAR_MIPMAP_NEAREST:
150                 return GL_LINEAR_MIPMAP_NEAREST;
151         case EngineModel::TextureProperty::FilterType::NEAREST_MIPMAP_LINEAR:
152                 return GL_NEAREST_MIPMAP_LINEAR;
153         case EngineModel::TextureProperty::FilterType::LINEAR_MIPMAP_LINEAR:
154                 return GL_LINEAR_MIPMAP_LINEAR;
155         }
156         ThrowJmp("RenderTextureCache::getGLFilterType: unknown filter type");
157 }
158
159 GLenum
160 RenderTextureCache::GetGlWrapType(Tizen::Ui::Effects::_Renderer::EngineModel::TextureProperty::WrapType::Value wrapType)
161 {
162         switch (wrapType)
163         {
164         case EngineModel::TextureProperty::WrapType::CLAMP:
165                 ThrowJmp("GL_CLAMP not allowed under GLES2");
166                 // fall through
167         case EngineModel::TextureProperty::WrapType::CLAMP_TO_BORDER:
168                 ThrowJmp("GL_CLAMP_TO_BORDER not allowed under GLES2");
169                 // fall through
170         case EngineModel::TextureProperty::WrapType::CLAMP_TO_EDGE:
171                 return GL_CLAMP_TO_EDGE;
172         case EngineModel::TextureProperty::WrapType::MIRRORED_REPEAT:
173                 return GL_MIRRORED_REPEAT;
174         case EngineModel::TextureProperty::WrapType::REPEAT:
175                 return GL_REPEAT;
176         }
177         ThrowJmp("RenderTextureCache::getGLWrapType: unknown wrap type");
178 }
179
180
181 RenderTexture2dCache::RenderTexture2dCache(Render* pRender, EngineModel::Texture2dProperty* pTexture):
182         RenderTextureCache(pRender),
183         __pTexture(pTexture)
184 {
185 }
186
187 RenderTexture2dCache::~RenderTexture2dCache(void)
188 {
189 }
190
191 void
192 RenderTexture2dCache::Die(void)
193 {
194         __pTexture = null;
195         RenderCache::Die();
196         return;
197 }
198
199 void
200 RenderTexture2dCache::DestroyCache(void)
201 {
202         if (__pTexture)
203         {
204                 __pTexture->SetCache(RenderTexture2DCachePtr());
205         }
206
207         __pTexture = null;
208         RenderTextureCache::DestroyCache();
209
210         return;
211 }
212
213 void
214 RenderTexture2dCache::Validate(Render* pRender)
215 {
216         Validate(pRender, 0);
217         return;
218 }
219
220 void
221 RenderTexture2dCache::Validate(Render*, GLuint stage)
222 {
223         if (!_invalid)
224         {
225                 return;
226         }
227         if (!_textureId)
228         {
229                 glGenTextures(1, &_textureId);
230         }
231         glActiveTexture(GL_TEXTURE0 + stage);
232         glBindTexture(GL_TEXTURE_2D, _textureId);
233         const size_t numMipMaps = __pTexture->GetNumMipmaps();
234
235         GLenum internalFormat = GetGlInternalFormat(__pTexture->GetInternalFormat());
236         GLenum dataFormat = GetGlDataFormat(__pTexture->GetDataFormat());
237         GLenum dataType = GetGlDataType(__pTexture->GetDataType());
238
239         for (size_t i = 0; numMipMaps > i; ++i)
240         {
241                 glTexImage2D(GL_TEXTURE_2D, static_cast<GLint>(i), internalFormat,
242                         static_cast<GLsizei>(__pTexture->GetWidth(i)),
243                         static_cast<GLsizei>(__pTexture->GetHeight(i)),
244                         0, dataFormat, dataType,
245                         __pTexture->GetData(i));
246         }
247
248         if (__pTexture->GenerateMipmaps())
249         {
250                 glGenerateMipmap(GL_TEXTURE_2D);
251         }
252
253         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GetGlFilterType(__pTexture->GetMinFilter()));
254         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GetGlFilterType(__pTexture->GetMagFilter()));
255
256         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GetGlWrapType(__pTexture->GetWrapS()));
257         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GetGlWrapType(__pTexture->GetWrapT()));
258
259         _invalid = false;
260         return;
261 }
262
263 void
264 RenderTexture2dCache::Bind(Render* pRender, GLuint stage)
265 {
266         if (_invalid)
267         {
268                 Validate(pRender, stage);
269         }
270         else
271         {
272                 glActiveTexture(GL_TEXTURE0 + stage);
273                 glBindTexture(GL_TEXTURE_2D, _textureId);
274         }
275         return;
276 }
277
278 }}}}} // Tizen::Ui::Effects::_Renderer::GraphicsEngine