e23d28ab22ae5114d9bb67da72d617cf09d39663
[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) 2018 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 #include <cstdint> // uint16_t, uint32_t
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/images/image-operations.h> // Dali::ImageDimensions
26 #include <dali/public-api/rendering/sampler.h>
27 #include <dali/public-api/rendering/texture.h>
28 #include <dali/internal/event/rendering/texture-impl.h>
29 #include <dali/internal/render/gl-resources/context.h>
30 #include <dali/internal/render/renderers/render-sampler.h>
31 #include <dali/integration-api/gl-defines.h>
32
33 namespace Dali
34 {
35 namespace Internal
36 {
37 namespace Render
38 {
39 struct Sampler;
40
41 class Texture
42 {
43 public:
44
45   typedef Dali::TextureType::Type Type;
46
47   /**
48    * Constructor
49    * @param[in] type The type of the texture
50    * @param[in] format The format of the pixel data
51    * @param[in] size The size of the texture
52    */
53   Texture( Type type, Pixel::Format format, ImageDimensions size );
54
55   /**
56    * Constructor from native image
57    * @param[in] nativeImageInterface The native image
58    */
59   Texture( NativeImageInterfacePtr nativeImageInterface );
60
61   /**
62    * Destructor
63    */
64   ~Texture();
65
66   /**
67    * Creates the texture in the GPU.
68    * Creates the texture and reserves memory for the first mipmap level
69    * @param[in] context The GL context
70    */
71   void Initialize( Context& context );
72
73   /**
74    * Deletes the texture from the GPU
75    * @param[in] context The GL context
76    */
77   void Destroy( Context& context );
78
79   /**
80    * Called by RenderManager to inform the texture that the context has been destroyed
81    */
82   void GlContextDestroyed();
83
84   /**
85    * Uploads data to the texture.
86    * @param[in] context The GL context
87    * @param[in] pixelData A pixel data object
88    * @param[in] params Upload parameters. See UploadParams
89    */
90   void Upload( Context& context, PixelDataPtr pixelData, const Internal::Texture::UploadParams& params );
91
92   /**
93    * Bind the texture to the given texture unit and applies the given sampler
94    * @param[in] context The GL context
95    * @param[in] textureUnit the texture unit
96    * @param[in] sampler The sampler to be used with the texture
97    * @return true if the bind succeeded, false otherwise
98    */
99   bool Bind( Context& context, uint32_t textureUnit, Render::Sampler* sampler );
100
101   /**
102    * Auto generates mipmaps for the texture
103    * @param[in] context The GL context
104    */
105   void GenerateMipmaps( Context& context );
106
107   /**
108    * Retrieve wheter the texture has an alpha channel
109    * @return True if the texture has alpha channel, false otherwise
110    */
111   bool HasAlphaChannel() const;
112
113   /**
114    * Get the id of the texture
115    * @return Id of the texture
116    */
117   GLuint GetId() const
118   {
119     return mId;
120   }
121
122   /**
123    * Get the target to which the texture is bound
124    * @return target to which the texture is bound
125    */
126   GLuint GetTarget() const
127   {
128     return mTarget;
129   }
130
131   /**
132    * Get the type of the texture
133    * @return Type of the texture
134    */
135   Type GetType() const
136   {
137     return mType;
138   }
139
140   /**
141    * Check if the texture is a native image
142    * @return if the texture is a native image
143    */
144   bool IsNativeImage() const
145   {
146     return mNativeImage;
147   }
148
149 private:
150
151   /**
152    * Helper method to apply a sampler to the texture
153    * @param[in] context The GL context
154    * @param[in] sampler The sampler
155    */
156   void ApplySampler( Context& context, Render::Sampler* sampler );
157
158   NativeImageInterfacePtr mNativeImage; ///< Pointer to native image
159   Render::Sampler mSampler;             ///< The current sampler state
160   GLuint mId;                           ///< Id of the texture
161   GLuint mTarget;                       ///< Specifies the target to which the texture is bound.
162   GLint mGlInternalFormat;              ///< The gl internal format of the pixel data
163   GLenum mGlFormat;                     ///< The gl format of the pixel data
164   GLenum mPixelDataType;                ///< The data type of the pixel data
165   uint16_t mWidth;                      ///< Width of the texture
166   uint16_t mHeight;                     ///< Height of the texture
167   uint16_t mMaxMipMapLevel;             ///< Maximum mipmap level
168   Type mType:3;                         ///< Type of the texture
169   bool mHasAlpha : 1;                   ///< Whether the format has an alpha channel
170   bool mIsCompressed : 1;               ///< Whether the format is compressed
171
172 };
173
174
175 } // namespace Render
176
177 } // namespace Internal
178
179 } // namespace Dali
180
181
182 #endif //  DALI_INTERNAL_RENDER_TEXTURE_H