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