Remove some ancient dead code, Vertex3d
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / texture.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/render/gl-resources/texture.h>
20
21 // EXTERNAL INCLUDES
22 #include <math.h>
23 #include <memory.h>
24
25 // INTERNAL INCLUDES
26 #include <dali/integration-api/debug.h>
27 #include <dali/internal/render/common/vertex.h>
28 #include <dali/internal/render/gl-resources/context.h>
29 #include <dali/internal/common/image-sampler.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace
38 {
39
40 // These match the GL specification
41 const GLint SYSTEM_MINIFY_DEFAULT  = GL_NEAREST_MIPMAP_LINEAR;
42 const GLint SYSTEM_MAGNIFY_DEFAULT = GL_LINEAR;
43 const GLint SYSTEM_WRAP_DEFAULT  = GL_CLAMP_TO_EDGE;
44
45 // These are the Dali defaults
46 const GLint DALI_MINIFY_DEFAULT  = GL_LINEAR;
47 const GLint DALI_MAGNIFY_DEFAULT = GL_LINEAR;
48
49 } // namespace
50
51 /**
52  * @brief Convert a FilterMode to it's corresponding GL type.
53  *
54  * @param[in] filterMode The FilterMode type.
55  * @param[in] defaultfilterMode The filter mode to use if filterMode is DEFAULT.
56  * @param[in] defaultSystemFilterMode The filter mode to use if filterMode is NONE.
57  * @return Return the equivalent GL type.
58  */
59 GLint FilterModeToGL( FilterMode::Type filterMode, GLint defaultfilterMode, GLint defaultSystemFilterMode )
60 {
61   switch( filterMode )
62   {
63     case FilterMode::NEAREST:
64     {
65       return GL_NEAREST;
66     }
67     case FilterMode::LINEAR:
68     {
69       return GL_LINEAR;
70     }
71     case FilterMode::NONE:
72     {
73       return defaultSystemFilterMode;
74     }
75     case FilterMode::DEFAULT:
76     {
77       return defaultfilterMode;
78     }
79   }
80
81   return GL_LINEAR;
82 }
83
84 GLint WrapModeToGL( WrapMode::Type wrapMode, GLint defaultWrapMode )
85 {
86   switch( wrapMode )
87   {
88     case WrapMode::DEFAULT:
89     {
90       return defaultWrapMode;
91     }
92     case WrapMode::CLAMP_TO_EDGE:
93     {
94       return GL_CLAMP_TO_EDGE;
95     }
96     case WrapMode::REPEAT:
97     {
98       return GL_REPEAT;
99     }
100     case WrapMode::MIRRORED_REPEAT:
101     {
102       return GL_MIRRORED_REPEAT;
103     }
104   }
105
106   return defaultWrapMode;
107 }
108
109 using Dali::Internal::Vertex2D;
110
111 using namespace Dali::Pixel;
112
113 Texture::Texture(Context&      context,
114                  unsigned int  width,
115                  unsigned int  height,
116                  unsigned int  imageWidth,
117                  unsigned int  imageHeight)
118 : mContext(context),
119   mId(0),
120   mSamplerBitfield( 0 ),
121   mWidth(width),
122   mHeight(height),
123   mImageWidth(imageWidth),
124   mImageHeight(imageHeight)
125 {
126 }
127
128 Texture::Texture(Context&      context,
129                  unsigned int  width,
130                  unsigned int  height)
131 : mContext(context),
132   mId(0),
133   mSamplerBitfield( 0 ),
134   mWidth(width),
135   mHeight(height),
136   mImageWidth(width),
137   mImageHeight(height)
138 {
139 }
140
141 Texture::~Texture()
142 {
143   // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
144   // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
145 }
146
147 void Texture::SetTextureId(GLuint id)
148 {
149   mId=id;
150 }
151
152 void Texture::Update(Integration::Bitmap* bitmap)
153 {
154   DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
155 }
156
157 void Texture::UpdateArea( const RectArea& area )
158 {
159   DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
160 }
161
162 bool Texture::UpdateOnCreate()
163 {
164   return false;
165 }
166
167 bool Texture::Bind(GLenum target, TextureUnit textureunit )
168 {
169   // This is the only supported type at the moment
170   DALI_ASSERT_DEBUG( target == GL_TEXTURE_2D );
171   bool created = false;
172
173   if( mId == 0 )
174   {
175     if( CreateGlTexture() )
176     {
177       created = true;
178     }
179   }
180
181   // Bind the texture id
182   mContext.BindTextureForUnit(textureunit, mId );
183
184   return created;
185 }
186
187 void Texture::GlContextDestroyed()
188 {
189   // texture is gone
190   mId = 0;
191   // reset sampler state as well
192   mSamplerBitfield = 0;
193 }
194
195 void Texture::GlCleanup()
196 {
197   // delete the gl texture
198   if (mId != 0)
199   {
200     mContext.DeleteTextures(1,&mId);
201     mId = 0;
202   }
203 }
204
205 void Texture::MapUV(unsigned int numVerts,Vertex2D *verts, const PixelArea* pixelArea)
206 {
207   MapUV(numVerts, (float*)(&verts->mU), sizeof(Vertex2D)/sizeof(float), pixelArea);
208 }
209
210 void Texture::MapUV(unsigned int numVerts, float* verts, unsigned int stride, const PixelArea* pixelArea)
211 {
212   UvRect uv;
213
214   GetTextureCoordinates(uv, pixelArea);
215
216   float uScale = fabsf(uv.u2 - uv.u0);
217   float vScale = fabsf(uv.v2 - uv.v0);
218
219   for (unsigned int i = 0; i < numVerts; ++i)
220   {
221     verts[0] = uv.u0 + verts[0] * uScale;
222     verts[1] = uv.v0 + verts[1] * vScale;
223     verts += stride;
224   }
225 }
226
227 unsigned int Texture::GetWidth() const
228 {
229   return mWidth;
230 }
231
232 unsigned int Texture::GetHeight() const
233 {
234   return mHeight;
235 }
236
237 void Texture::GetTextureCoordinates(UvRect& uv, const PixelArea* pixelArea)
238 {
239   if( pixelArea == NULL )
240   {
241      GetDefaultTextureCoordinates(uv);
242      return;
243   }
244
245   // pre-calulate the normalized values
246
247   const float uScale = 1.0f / float(mWidth);
248   const float vScale = 1.0f / float(mHeight);
249   const float x = uScale * float(pixelArea->x);
250   const float y = vScale * float(pixelArea->y);
251   const float width  = uScale * float(pixelArea->width);
252   const float height = vScale * float(pixelArea->height);
253
254
255   // bottom left
256   uv.u0 = x;
257   uv.v0 = y;
258
259   // top right
260   uv.u2 = x + width;
261   uv.v2 = y + height;
262
263 };
264
265 void Texture::GetDefaultTextureCoordinates(UvRect& uv) const
266 {
267   if ((mWidth == mImageWidth) && (mHeight == mImageHeight))
268   {
269     // set the uv's to display 0,0 to 1,1
270     uv.Reset();
271     return;
272   }
273
274   // the texture co-ordinates go from 0 to 1. But the image is smaller than the
275   // texture, so we need to adjust the uv values.
276   float uScale = float(mImageWidth)  / float(mWidth);
277   float vScale = float(mImageHeight) / float(mHeight);
278
279   // bottom left
280   uv.u0 = 0.0f;
281   uv.v0 = 0.0f;
282
283   // top right
284   uv.u2 = uScale;
285   uv.v2 = vScale;
286
287 }
288
289 void Texture::ApplyFilterModeParameter( TextureUnit unit, GLint filterType, FilterMode::Type currentFilterMode, FilterMode::Type newFilterMode, GLint daliDefault, GLint systemDefault )
290 {
291   GLint newFilterModeGL = FilterModeToGL( newFilterMode, daliDefault, systemDefault );
292   GLint currentFilterModeGL = FilterModeToGL( currentFilterMode, daliDefault, systemDefault );
293
294   if( newFilterModeGL != currentFilterModeGL )
295   {
296     mContext.ActiveTexture( unit );
297     mContext.TexParameteri( GL_TEXTURE_2D, filterType, newFilterModeGL );
298   }
299 }
300
301 void Texture::ApplyWrapModeParameter( TextureUnit unit, GLint wrapType, WrapMode::Type currentWrapMode, WrapMode::Type newWrapMode )
302 {
303   GLint newWrapModeGL = WrapModeToGL( newWrapMode, SYSTEM_WRAP_DEFAULT );
304   GLint currentWrapModeGL = WrapModeToGL( currentWrapMode, SYSTEM_WRAP_DEFAULT );
305
306   if( newWrapModeGL != currentWrapModeGL )
307   {
308     mContext.ActiveTexture( unit );
309     mContext.TexParameteri( GL_TEXTURE_2D, wrapType, newWrapModeGL );
310   }
311 }
312
313 void Texture::ApplySampler( TextureUnit unit, unsigned int samplerBitfield )
314 {
315   if( mSamplerBitfield != samplerBitfield && mId != 0 )
316   {
317     ApplyFilterModeParameter( unit,
318                            GL_TEXTURE_MIN_FILTER,
319                            ImageSampler::GetMinifyFilterMode( mSamplerBitfield ),
320                            ImageSampler::GetMinifyFilterMode( samplerBitfield ),
321                            DALI_MINIFY_DEFAULT,
322                            SYSTEM_MINIFY_DEFAULT );
323
324     ApplyFilterModeParameter( unit,
325                               GL_TEXTURE_MAG_FILTER,
326                               ImageSampler::GetMagnifyFilterMode( mSamplerBitfield ),
327                               ImageSampler::GetMagnifyFilterMode( samplerBitfield ),
328                               DALI_MAGNIFY_DEFAULT,
329                               SYSTEM_MAGNIFY_DEFAULT );
330
331     ApplyWrapModeParameter( unit,
332                             GL_TEXTURE_WRAP_S,
333                             ImageSampler::GetUWrapMode( mSamplerBitfield ),
334                             ImageSampler::GetUWrapMode( samplerBitfield ));
335
336     ApplyWrapModeParameter( unit,
337                             GL_TEXTURE_WRAP_T,
338                             ImageSampler::GetVWrapMode( mSamplerBitfield ),
339                             ImageSampler::GetVWrapMode( samplerBitfield ));
340
341
342
343     mSamplerBitfield = samplerBitfield;
344   }
345 }
346
347 } // namespace Internal
348
349 } // namespace Dali