Bugfix: Gfx program discarded while DALi ProgramCache still holds to the GL resource
[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) 2021 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 <cstdint> // uint16_t, uint32_t
22 #include <string>
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
29 #include <dali/graphics-api/graphics-controller.h>
30 #include <dali/graphics-api/graphics-texture-create-info.h>
31 #include <dali/graphics-api/graphics-texture.h>
32 #include <dali/graphics-api/graphics-types.h>
33 #include <dali/integration-api/gl-defines.h>
34 #include <dali/internal/event/rendering/texture-impl.h>
35 #include <dali/internal/render/gl-resources/context.h>
36 #include <dali/internal/render/renderers/render-sampler.h>
37
38 namespace Dali
39 {
40 namespace Internal
41 {
42 namespace Render
43 {
44 struct Sampler;
45
46 class Texture
47 {
48 public:
49   using Type = Dali::TextureType::Type;
50
51   /**
52    * Constructor
53    * @param[in] type The type of the texture
54    * @param[in] format The format of the pixel data
55    * @param[in] size The size of the texture
56    */
57   Texture(Type type, Pixel::Format format, ImageDimensions size);
58
59   /**
60    * Constructor from native image
61    * @param[in] nativeImageInterface The native image
62    */
63   Texture(NativeImageInterfacePtr nativeImageInterface);
64
65   /**
66    * Destructor
67    */
68   ~Texture();
69
70   /**
71    * Stores the graphics controller for use when required.
72    *
73    * @param[in] graphicsController The graphics controller to use
74    */
75   void Initialize(Graphics::Controller& graphicsController);
76
77   /**
78    * Create the texture without a buffer
79    * @param[in] usage How texture will be used
80    */
81   void Create(Graphics::TextureUsageFlags usage);
82
83   /**
84    * Create a texture with a buffer if non-null
85    * @param[in] usage How texture will be used
86    * @param[in] buffer Buffer to copy
87    */
88   void CreateWithData(Graphics::TextureUsageFlags usage, uint8_t* buffer, uint32_t bufferSize);
89
90   /**
91    * Deletes the texture from the GPU
92    */
93   void Destroy();
94
95   /**
96    * Uploads data to the texture.
97    * @param[in] pixelData A pixel data object
98    * @param[in] params Upload parameters. See UploadParams
99    */
100   void Upload(PixelDataPtr pixelData, const Internal::Texture::UploadParams& params);
101
102   /**
103    * Called when the texture is about to be used for drawing.
104    * Allows native textures to be set up appropriately.
105    */
106   void Prepare();
107
108   /**
109    * Auto generates mipmaps for the texture
110    */
111   void GenerateMipmaps();
112
113   /**
114    * Retrieve whether the texture has an alpha channel
115    * @return True if the texture has alpha channel, false otherwise
116    */
117   bool HasAlphaChannel() const;
118
119   /**
120    * Get the graphics object associated with this texture
121    */
122   Graphics::Texture* GetGraphicsObject() const;
123
124   /**
125    * Get the type of the texture
126    * @return Type of the texture
127    */
128   Type GetType() const
129   {
130     return mType;
131   }
132
133   /**
134    * Check if the texture is a native image
135    * @return if the texture is a native image
136    */
137   bool IsNativeImage() const
138   {
139     return mNativeImage;
140   }
141
142 private:
143   /**
144    * Helper method to apply a sampler to the texture
145    * @param[in] context The GL context
146    * @param[in] sampler The sampler
147    */
148   void ApplySampler(Render::Sampler* sampler);
149
150 private:
151   Graphics::Controller*                  mGraphicsController;
152   Graphics::UniquePtr<Graphics::Texture> mGraphicsTexture;
153
154   NativeImageInterfacePtr mNativeImage; ///< Pointer to native image
155   Render::Sampler         mSampler;     ///< The current sampler state
156
157   Pixel::Format mPixelFormat;    ///< Pixel format of the texture
158   uint16_t      mWidth;          ///< Width of the texture
159   uint16_t      mHeight;         ///< Height of the texture
160   uint16_t      mMaxMipMapLevel; ///< Maximum mipmap level
161   Type          mType : 3;       ///< Type of the texture
162   bool          mHasAlpha : 1;   ///< Whether the format has an alpha channel
163 };
164
165 } // namespace Render
166
167 } // namespace Internal
168
169 } // namespace Dali
170
171 #endif //  DALI_INTERNAL_RENDER_TEXTURE_H