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