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