cc58c2f563923199dd962506b3c707d1ce757487
[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/integration-api/resource-declarations.h>
27
28 #include <dali/internal/render/gl-resources/context.h>
29 #include <dali/integration-api/gl-defines.h>
30 #include <dali/internal/render/renderers/render-sampler.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    * Constructor
113    */
114   NewTexture( Type type, Pixel::Format format, unsigned int width, unsigned int height );
115
116   /**
117    * Destructor
118    */
119   ~NewTexture();
120
121   /**
122    * Creates the texture in the GPU.
123    * Creates the texture and reserves memory for the first mipmap level
124    * @param[in] context The GL context
125    */
126   void Initialize(Context& context);
127
128   /**
129    * Deletes the texture from the GPU
130    * @param[in] context The GL context
131    */
132   void Destroy( Context& context );
133
134   /**
135    * Uploads data to the texture.
136    * @param[in] context The GL context
137    * @param[in] buffer A vector with the data to be uploaded
138    * @param[in] params Upload parameters. See TextureUploadParams
139    */
140   void Upload( Context& context, Vector<unsigned char>& buffer, const Dali::TextureUploadParams& params );
141
142   /**
143    * Bind the texture to the given texture unit and applies the given sampler
144    * @param[in] context The GL context
145    * @param[in] textureUnit the texture unit
146    * @param[in] sampler The sampler to be used with the texture
147    */
148   void Bind( Context& context, unsigned int textureUnit, Render::Sampler* sampler );
149
150   /**
151    * Auto generates mipmaps for the texture
152    */
153   void GenerateMipmaps( Context& context );
154
155   /**
156    * Retrieve wheter the texture has an alpha channel
157    * @return True if the texture has alpha channel, false otherwise
158    */
159   bool HasAlphaChannel();
160
161   GLuint GetId()
162   {
163     return mId;
164   }
165
166   unsigned int GetWidth() const
167   {
168     return mWidth;
169   }
170
171   unsigned int GetHeight() const
172   {
173     return mHeight;
174   }
175
176   Type GetType() const
177   {
178     return mType;
179   }
180
181 private:
182
183   /**
184    * Helper method to apply a sampler to the texture
185    * @param[in] context The GL context
186    * @param[in] sampler The sampler
187    */
188   void ApplySampler( Context& context, Render::Sampler* sampler );
189
190
191   GLuint mId;                         ///<Id of the texture
192   Type mType;                         ///<Type of the texture
193   GLenum mInternalFormat;             ///<The format of the pixel data
194   GLenum mPixelDataType;              ///<The data type of the pixel data
195   unsigned int mWidth;                ///<Widht of the texture
196   unsigned int mHeight;               ///<Height of the texture
197   Render::Sampler mSampler;           ///<The current sampler state
198   bool mHasAlpha : 1;                 ///<Whether the format has an alpha channel
199 };
200
201
202 } // namespace Render
203
204 } // namespace Internal
205
206 } // namespace Dali
207
208
209 #endif //  DALI_INTERNAL_RENDER_TEXTURE_H