bdf9d2fdb389acd3b5ad4b2fda5ab3a07b2568f6
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-texture.h
1 #ifndef DALI_INTERNAL_RENDER_TEXTURE_H
2 #define DALI_INTERNAL_RENDER_TEXTURE_H
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // EXTERNAL INCLUDES
21 #include <string>
22
23 // INTERNAL INCLUDES
24 #include <dali/devel-api/rendering/sampler.h>
25 #include <dali/devel-api/rendering/texture.h>
26 #include <dali/internal/event/rendering/texture-impl.h>
27 #include <dali/integration-api/resource-declarations.h>
28
29 #include <dali/internal/render/gl-resources/context.h>
30 #include <dali/integration-api/gl-defines.h>
31 #include <dali/internal/render/renderers/render-sampler.h>
32
33 namespace Dali
34 {
35 namespace Internal
36 {
37 namespace Render
38 {
39 struct Sampler;
40
41 /**
42  * This class is the mapping between texture id, sampler and sampler uniform name
43  */
44 class Texture
45 {
46 public:
47
48   /**
49    * Enumeration to tell that this sampler does not have a unique index yet
50    */
51   enum
52   {
53     NOT_INITIALIZED = -1
54   };
55
56   /**
57    * Constructor
58    */
59   Texture()
60   : mSampler( 0 ),
61     mTextureId( Integration::InvalidResourceId )
62   {}
63
64   /**
65    * Constructor
66    */
67   Texture( Integration::ResourceId textureId, Render::Sampler* sampler )
68   : mSampler( sampler ),
69     mTextureId( textureId)
70   {}
71
72   /**
73    * Destructor
74    */
75   ~Texture()
76   {}
77
78   /*
79    * Get the Render::Sampler used by the texture
80    * @Return The Render::Sampler being used or 0 if using the default
81    */
82   inline const Render::Sampler* GetSampler() const
83   {
84     return mSampler;
85   }
86
87 public: // called from RenderThread
88
89   /**
90    * @param[in] buffer A vector wit
91    * Get the texture ID
92    * @return the id of the associated texture
93    */
94   inline Integration::ResourceId GetTextureId() const
95   {
96     return mTextureId;
97   }
98
99 private:
100
101   Render::Sampler*        mSampler;
102   Integration::ResourceId mTextureId;
103 };
104
105
106 //TODO : Remove the old Render::Texture class (see above) once it is no longer needed by Image
107 class NewTexture
108 {
109 public:
110
111   typedef Dali::TextureType::Type Type;
112
113   /**
114    * Constructor
115    * @param[in] type The type of the texture
116    * @param[in] format The format of the pixel data
117    * @param[in] width The width of the texture
118    * @param[in] height The height of the texture
119    */
120   NewTexture( Type type, Pixel::Format format, unsigned int width, unsigned int height );
121
122   /**
123    * Constructor from native image
124    * @param[in] nativeImageInterface The native image
125    */
126   NewTexture( NativeImageInterfacePtr nativeImageInterface );
127
128   /**
129    * Destructor
130    */
131   ~NewTexture();
132
133   /**
134    * Creates the texture in the GPU.
135    * Creates the texture and reserves memory for the first mipmap level
136    * @param[in] context The GL context
137    */
138   void Initialize(Context& context);
139
140   /**
141    * Deletes the texture from the GPU
142    * @param[in] context The GL context
143    */
144   void Destroy( Context& context );
145
146   /**
147    * Uploads data to the texture.
148    * @param[in] context The GL context
149    * @param[in] buffer A vector with the data to be uploaded
150    * @param[in] params Upload parameters. See UploadParams
151    */
152   void Upload( Context& context, Vector<unsigned char>& buffer, const Internal::NewTexture::UploadParams& params );
153
154   /**
155    * Bind the texture to the given texture unit and applies the given sampler
156    * @param[in] context The GL context
157    * @param[in] textureUnit the texture unit
158    * @param[in] sampler The sampler to be used with the texture
159    * @return true if the bind succeeded, false otherwise
160    */
161   bool Bind( Context& context, unsigned int textureUnit, Render::Sampler* sampler );
162
163   /**
164    * Auto generates mipmaps for the texture
165    * @param[in] context The GL context
166    */
167   void GenerateMipmaps( Context& context );
168
169   /**
170    * Retrieve wheter the texture has an alpha channel
171    * @return True if the texture has alpha channel, false otherwise
172    */
173   bool HasAlphaChannel();
174
175   /**
176    * Get the id of the texture
177    * @return Id of the texture
178    */
179   GLuint GetId()
180   {
181     return mId;
182   }
183
184   /**
185    * Get the width of the texture
186    * @return Width of the texture
187    */
188   unsigned int GetWidth() const
189   {
190     return mWidth;
191   }
192
193   /**
194    * Get the height of the texture
195    * @return Height of the texture
196    */
197   unsigned int GetHeight() const
198   {
199     return mHeight;
200   }
201
202   /**
203    * Get the type of the texture
204    * @return Type of the texture
205    */
206   Type GetType() const
207   {
208     return mType;
209   }
210
211 private:
212
213   /**
214    * Helper method to apply a sampler to the texture
215    * @param[in] context The GL context
216    * @param[in] sampler The sampler
217    */
218   void ApplySampler( Context& context, Render::Sampler* sampler );
219
220   GLuint mId;                         ///<Id of the texture
221   Type mType;                         ///<Type of the texture
222   Render::Sampler mSampler;           ///<The current sampler state
223   NativeImageInterfacePtr mNativeImage; ///<Pointer to native image
224   GLenum mInternalFormat;             ///<The format of the pixel data
225   GLenum mPixelDataType;              ///<The data type of the pixel data
226   unsigned int mWidth;                ///<Widht of the texture
227   unsigned int mHeight;               ///<Height of the texture
228   bool mHasAlpha : 1;                 ///<Whether the format has an alpha channel
229 };
230
231
232 } // namespace Render
233
234 } // namespace Internal
235
236 } // namespace Dali
237
238
239 #endif //  DALI_INTERNAL_RENDER_TEXTURE_H