ContextObserver removal, part II: Its gone
[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 }
56
57 Texture::~Texture()
58 {
59   // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
60   // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
61 }
62
63 void Texture::SetTextureId(GLuint id)
64 {
65   mId=id;
66 }
67
68 void Texture::Update(Integration::Bitmap* bitmap)
69 {
70   DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
71 }
72
73 void Texture::UpdateArea( const RectArea& area )
74 {
75   DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
76 }
77
78 bool Texture::UpdateOnCreate()
79 {
80   return false;
81 }
82
83 bool Texture::Bind(GLenum target, GLenum textureunit )
84 {
85   // This is the only supported type at the moment
86   DALI_ASSERT_DEBUG( target == GL_TEXTURE_2D );
87   bool created = false;
88
89   if( mId == 0 )
90   {
91     if( CreateGlTexture() )
92     {
93       created = true;
94     }
95   }
96
97   // Bind the texture id
98   mContext.ActiveTexture(textureunit);
99   mContext.Bind2dTexture(mId);
100
101   return created;
102 }
103
104 void Texture::GlContextDestroyed()
105 {
106   // texture is gone
107   mId = 0;
108 }
109
110 void Texture::GlCleanup()
111 {
112   // delete the gl texture
113   if (mId != 0)
114   {
115     mContext.DeleteTextures(1,&mId);
116     mId = 0;
117   }
118 }
119
120 void Texture::MapUV(unsigned int numVerts,Vertex2D *verts, const PixelArea* pixelArea)
121 {
122   MapUV(numVerts, (float*)(&verts->mU), sizeof(Vertex2D)/sizeof(float), pixelArea);
123 }
124
125 void Texture::MapUV(unsigned int numVerts,Vertex3D *verts, const PixelArea* pixelArea)
126 {
127   MapUV(numVerts, (float*)(&verts->mU), sizeof(Vertex3D)/sizeof(float), pixelArea);
128 }
129
130 void Texture::MapUV(unsigned int numVerts, float* verts, unsigned int stride, const PixelArea* pixelArea)
131 {
132   UvRect uv;
133
134   GetTextureCoordinates(uv, pixelArea);
135
136   float uScale = fabsf(uv.u2 - uv.u0);
137   float vScale = fabsf(uv.v2 - uv.v0);
138
139   for (unsigned int i = 0; i < numVerts; ++i)
140   {
141     verts[0] = uv.u0 + verts[0] * uScale;
142     verts[1] = uv.v0 + verts[1] * vScale;
143     verts += stride;
144   }
145 }
146
147 unsigned int Texture::GetWidth() const
148 {
149   return mWidth;
150 }
151
152 unsigned int Texture::GetHeight() const
153 {
154   return mHeight;
155 }
156
157 Pixel::Format Texture::GetPixelFormat() const
158 {
159   return mPixelFormat;
160 }
161
162 void Texture::GetTextureCoordinates(UvRect& uv, const PixelArea* pixelArea)
163 {
164   if( pixelArea == NULL )
165   {
166      GetDefaultTextureCoordinates(uv);
167      return;
168   }
169
170   // pre-calulate the normalized values
171
172   const float uScale = 1.0f / float(mWidth);
173   const float vScale = 1.0f / float(mHeight);
174   const float x = uScale * float(pixelArea->x);
175   const float y = vScale * float(pixelArea->y);
176   const float width  = uScale * float(pixelArea->width);
177   const float height = vScale * float(pixelArea->height);
178
179
180   // bottom left
181   uv.u0 = x;
182   uv.v0 = y;
183
184   // top right
185   uv.u2 = x + width;
186   uv.v2 = y + height;
187
188 };
189
190 void Texture::GetDefaultTextureCoordinates(UvRect& uv) const
191 {
192   if ((mWidth == mImageWidth) && (mHeight == mImageHeight))
193   {
194     // set the uv's to display 0,0 to 1,1
195     uv.Reset();
196     return;
197   }
198
199   // the texture co-ordinates go from 0 to 1. But the image is smaller than the
200   // texture, so we need to adjust the uv values.
201   float uScale = float(mImageWidth)  / float(mWidth);
202   float vScale = float(mImageHeight) / float(mHeight);
203
204   // bottom left
205   uv.u0 = 0.0f;
206   uv.v0 = 0.0f;
207
208   // top right
209   uv.u2 = uScale;
210   uv.v2 = vScale;
211
212 }
213
214 } // namespace Internal
215
216 } // namespace Dali