Removed legacy resource tracking / logging
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / texture.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/render/gl-resources/texture.h>
19
20 // EXTERNAL INCLUDES
21 #include <math.h>
22 #include <memory.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26 #include <dali/internal/render/common/vertex.h>
27 #include <dali/internal/render/gl-resources/context.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 using Dali::Internal::Vertex2D;
36 using Dali::Internal::Vertex3D;
37
38 using namespace Dali::Pixel;
39
40 Texture::Texture(Context&      context,
41                  unsigned int  width,
42                  unsigned int  height,
43                  unsigned int  imageWidth,
44                  unsigned int  imageHeight,
45                  Pixel::Format pixelFormat)
46 : mContext(context),
47   mId(0),
48   mWidth(width),
49   mHeight(height),
50   mImageWidth(imageWidth),
51   mImageHeight(imageHeight),
52   mPixelFormat(pixelFormat),
53   mDiscarded(false)
54 {
55   mContext.AddObserver(*this);
56 }
57
58 Texture::~Texture()
59 {
60   mContext.RemoveObserver(*this);
61   // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
62   // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
63 }
64
65 void Texture::SetTextureId(GLuint id)
66 {
67   mId=id;
68 }
69
70 void Texture::Update(Integration::Bitmap* bitmap)
71 {
72   DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
73 }
74
75 void Texture::UpdateArea( const RectArea& area )
76 {
77   DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
78 }
79
80 bool Texture::UpdateOnCreate()
81 {
82   return false;
83 }
84
85 bool Texture::Bind(GLenum target, GLenum textureunit )
86 {
87   // This is the only supported type at the moment
88   DALI_ASSERT_DEBUG( target == GL_TEXTURE_2D );
89   bool created = false;
90
91   if( mId == 0 )
92   {
93     if( CreateGlTexture() )
94     {
95       created = true;
96     }
97   }
98
99   // Bind the texture id
100   mContext.ActiveTexture(textureunit);
101   mContext.Bind2dTexture(mId);
102
103   return created;
104 }
105
106 void Texture::GlCleanup()
107 {
108   // otherwise, delete the gl texture
109   if (mId != 0)
110   {
111     mContext.DeleteTextures(1,&mId);
112     mId = 0;
113   }
114 }
115
116 void Texture::MapUV(unsigned int numVerts,Vertex2D *verts, const PixelArea* pixelArea)
117 {
118   MapUV(numVerts, (float*)(&verts->mU), sizeof(Vertex2D)/sizeof(float), pixelArea);
119 }
120
121 void Texture::MapUV(unsigned int numVerts,Vertex3D *verts, const PixelArea* pixelArea)
122 {
123   MapUV(numVerts, (float*)(&verts->mU), sizeof(Vertex3D)/sizeof(float), pixelArea);
124 }
125
126 void Texture::MapUV(unsigned int numVerts, float* verts, unsigned int stride, const PixelArea* pixelArea)
127 {
128   UvRect uv;
129
130   GetTextureCoordinates(uv, pixelArea);
131
132   float uScale = fabsf(uv.u2 - uv.u0);
133   float vScale = fabsf(uv.v2 - uv.v0);
134
135   for (unsigned int i = 0; i < numVerts; ++i)
136   {
137     verts[0] = uv.u0 + verts[0] * uScale;
138     verts[1] = uv.v0 + verts[1] * vScale;
139     verts += stride;
140   }
141 }
142
143 unsigned int Texture::GetWidth() const
144 {
145   return mWidth;
146 }
147
148 unsigned int Texture::GetHeight() const
149 {
150   return mHeight;
151 }
152
153 Pixel::Format Texture::GetPixelFormat() const
154 {
155   return mPixelFormat;
156 }
157
158 /*
159  * When an OpenGL context is created and made active we don't
160  * do anything, because we use lazy binding.
161  * This means when a texture is required that's when it's loaded
162  * into OpenGL.
163  */
164 void Texture::GlContextCreated()
165 {
166 }
167
168 /*
169  * From Context::Observer, called just before the OpenGL context is destroyed.
170  */
171 void Texture::GlContextToBeDestroyed()
172 {
173   GlCleanup();
174 }
175
176 void Texture::GetTextureCoordinates(UvRect& uv, const PixelArea* pixelArea)
177 {
178   if( pixelArea == NULL )
179   {
180      GetDefaultTextureCoordinates(uv);
181      return;
182   }
183
184   // pre-calulate the normalized values
185
186   const float uScale = 1.0f / float(mWidth);
187   const float vScale = 1.0f / float(mHeight);
188   const float x = uScale * float(pixelArea->x);
189   const float y = vScale * float(pixelArea->y);
190   const float width  = uScale * float(pixelArea->width);
191   const float height = vScale * float(pixelArea->height);
192
193
194   // bottom left
195   uv.u0 = x;
196   uv.v0 = y;
197
198   // top right
199   uv.u2 = x + width;
200   uv.v2 = y + height;
201
202 };
203
204 void Texture::GetDefaultTextureCoordinates(UvRect& uv) const
205 {
206   if ((mWidth == mImageWidth) && (mHeight == mImageHeight))
207   {
208     // set the uv's to display 0,0 to 1,1
209     uv.Reset();
210     return;
211   }
212
213   // the texture co-ordinates go from 0 to 1. But the image is smaller than the
214   // texture, so we need to adjust the uv values.
215   float uScale = float(mImageWidth)  / float(mWidth);
216   float vScale = float(mImageHeight) / float(mHeight);
217
218   // bottom left
219   uv.u0 = 0.0f;
220   uv.v0 = 0.0f;
221
222   // top right
223   uv.u2 = uScale;
224   uv.v2 = vScale;
225
226 }
227
228 } // namespace Internal
229
230 } // namespace Dali