398735e70abb4943843e117219e24b6cf31cea57
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-texture.h
1 #ifndef DALI_GRAPHICS_GLES_TEXTURE_H
2 #define DALI_GRAPHICS_GLES_TEXTURE_H
3
4 /*
5  * Copyright (c) 2022 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
21 // EXTERNAL INCLUDES
22 #include <dali/graphics-api/graphics-command-buffer.h>
23 #include <dali/graphics-api/graphics-texture-create-info.h>
24 #include <dali/graphics-api/graphics-texture.h>
25 #include <dali/integration-api/gl-abstraction.h>
26
27 // INTERNAL INCLUDES
28 #include "gles-graphics-resource.h"
29 #include "gles-graphics-types.h"
30
31 namespace Dali::Graphics::GLES
32 {
33 using TextureResource = Resource<Graphics::Texture, Graphics::TextureCreateInfo>;
34 class Sampler;
35
36 /**
37  * The Texture class represents a GPU texture object. It's slightly
38  * higher level than the Vulkan VkImage (more like combined image sampler).
39  */
40 class Texture : public TextureResource
41 {
42 public:
43   /**
44    * @brief Constructor
45    * @param[in] createInfo valid TextureCreateInfo structure
46    * @param[in] controller Reference to the Controller
47    */
48   Texture(const Graphics::TextureCreateInfo& createInfo, Graphics::EglGraphicsController& controller);
49
50   /**
51    * @brief Destructor
52    */
53   ~Texture() override = default;
54
55   /**
56    * @brief Called when GL resources are destroyed
57    */
58   void DestroyResource() override;
59
60   /**
61    * @brief Returns the Gl texture
62    * @return GL texture id
63    */
64   [[nodiscard]] uint32_t GetGLTexture() const
65   {
66     return mTextureId;
67   }
68
69   /**
70    * @brief Returns the type of the bound Gl texture
71    * @return The type of the bound Gl texture
72    */
73   [[nodiscard]] BoundTextureType GetTextureTypeId() const
74   {
75     return mCreateInfo.nativeImagePtr ? BoundTextureType::TEXTURE_EXTERNAL_OES : static_cast<BoundTextureType>(mCreateInfo.textureType);
76   }
77
78   /**
79    * @brief Called when initializing the resource
80    *
81    * @return True on success
82    */
83   bool InitializeResource() override;
84
85   /**
86    * @brief Called when UniquePtr<> on client-side dies
87    */
88   void DiscardResource() override;
89
90   void Bind(const TextureBinding& binding) const;
91
92   /**
93    * @brief used to prepare native texture before drawing.
94    *
95    * Checks if native texture has changed size (e.g. if rotated)
96    * and updates as appropriate.
97    *
98    * Gives the callback a chance to draw to the backing texture.
99    */
100   void Prepare();
101
102   /**
103    * @brief Returns the GL Target
104    * @return the Gl target
105    */
106   [[nodiscard]] GLenum GetGlTarget() const
107   {
108     return mGlTarget;
109   }
110
111   /**
112    * @brief Sets the maximum mipmap level
113    * @param[in] maxMipMapLevel The maximum mipmap level
114    */
115   void SetMaxMipMapLevel(const uint32_t maxMipMapLevel)
116   {
117     mMaxMipMapLevel = maxMipMapLevel;
118   }
119
120   /**
121    * @brief Returns the maximum mipmap level
122    * @return The maximum mipmap level
123    */
124   [[nodiscard]] uint32_t GetMaxMipMapLevel() const
125   {
126     return mMaxMipMapLevel;
127   }
128
129   /**
130    * @param pData  Input data
131    * @param sizeInBytes Size of the input data in bytes
132    * @param inStride Stride of the input data.
133    * @param width Width of the input/output buffer
134    * @param height Height of the intput/output buffer
135    * @param outputBuffer The buffer to write to
136    * @note output Buffer will be packed without stride.
137    * @return true if converted, or false otherwise
138    */
139   bool TryConvertPixelData(const void* pData, Graphics::Format srcFormat, Graphics::Format destFormat, uint32_t sizeInBytes, uint32_t inStride, uint32_t width, uint32_t height, std::vector<uint8_t>& outputBuffer);
140
141   bool InitializeNativeImage();
142
143   bool InitializeTexture();
144
145   Format ValidateFormat(Format sourceFormat);
146
147   bool IsCompressed()
148   {
149     return mIsCompressed;
150   }
151
152   void SetSamplerParameter(uint32_t param, uint32_t& cacheValue, uint32_t value) const;
153
154   uint32_t GetDependencyIndex() const
155   {
156     return mDependencyIndex;
157   }
158   void SetDependencyIndex(uint32_t dependencyIndex)
159   {
160     mDependencyIndex = dependencyIndex;
161   }
162
163 private:
164   mutable struct SamplerStateCache
165   {
166     uint32_t minFilter{0};
167     uint32_t magFilter{0};
168     uint32_t wrapS{0};
169     uint32_t wrapT{0};
170     uint32_t wrapR{0};
171     uint32_t maxLevel{0};
172   } mDefaultSamplerState;
173
174   std::vector<char> mStagingBuffer{};
175   uint32_t          mTextureId{0u};
176   GLenum            mGlTarget{0u};
177   uint32_t          mMaxMipMapLevel{0u};
178   uint32_t          mDependencyIndex{0xFFFFFFFF};
179   void*             mGLOwnerContext{nullptr};
180   bool              mIsCompressed{false};
181 };
182
183 } // namespace Dali::Graphics::GLES
184
185 #endif