Merge branch 'devel/master' into devel/graphics
[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) 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
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
30 namespace Dali::Graphics::GLES
31 {
32 using TextureResource = Resource<Graphics::Texture, Graphics::TextureCreateInfo>;
33
34 /**
35  * The Texture class represents a GPU texture object. It's slightly
36  * higher level than the Vulkan VkImage (more like combined image sampler).
37  */
38 class Texture : public TextureResource
39 {
40 public:
41   /**
42    * @brief Constructor
43    * @param[in] createInfo valid TextureCreateInfo structure
44    * @param[in] controller Reference to the Controller
45    */
46   Texture(const Graphics::TextureCreateInfo& createInfo, Graphics::EglGraphicsController& controller);
47
48   /**
49    * @brief Destructor
50    */
51   ~Texture() override = default;
52
53   /**
54    * @brief Called when GL resources are destroyed
55    */
56   void DestroyResource() override;
57
58   /**
59    * @brief Returns the Gl texture
60    * @return GL texture id
61    */
62   [[nodiscard]] uint32_t GetGLTexture() const
63   {
64     return mTextureId;
65   }
66
67   /**
68    * @brief Called when initializing the resource
69    *
70    * @return True on success
71    */
72   bool InitializeResource() override;
73
74   /**
75    * @brief Called when UniquePtr<> on client-side dies
76    */
77   void DiscardResource() override;
78
79   void Bind(const TextureBinding& binding) const;
80
81   /**
82    * @brief used to prepare native texture before drawing.
83    *
84    * Checks if native texture has changed size (e.g. if rotated)
85    * and updates as appropriate.
86    *
87    * Gives the callback a chance to draw to the backing texture.
88    */
89   void Prepare();
90
91   /**
92    * @brief Returns the GL Target
93    * @return the Gl target
94    */
95   [[nodiscard]] GLenum GetGlTarget() const
96   {
97     return mGlTarget;
98   }
99
100   /**
101    * @brief Sets the maximum mipmap level
102    * @param[in] maxMipMapLevel The maximum mipmap level
103    */
104   void SetMaxMipMapLevel(const uint32_t maxMipMapLevel)
105   {
106     mMaxMipMapLevel = maxMipMapLevel;
107   }
108
109   /**
110    * @brief Returns the maximum mipmap level
111    * @return The maximum mipmap level
112    */
113   [[nodiscard]] uint32_t GetMaxMipMapLevel() const
114   {
115     return mMaxMipMapLevel;
116   }
117
118   /**
119    * @param pData  Input data
120    * @param sizeInBytes Size of the input data in bytes
121    * @param width  Width of the output buffer
122    * @param height height of the output buffer
123    * @param outputBuffer The buffer to write to
124    * @return true if converted, or false otherwise
125    */
126   bool TryConvertPixelData(const void* pData, Graphics::Format srcFormat, Graphics::Format destFormat, uint32_t sizeInBytes, uint32_t width, uint32_t height, std::vector<uint8_t>& outputBuffer);
127
128   bool InitializeNativeImage();
129
130   bool InitializeTexture();
131
132   Format ValidateFormat(Format sourceFormat);
133
134   bool IsCompressed()
135   {
136     return mIsCompressed;
137   }
138
139 private:
140   std::vector<char> mStagingBuffer;
141   uint32_t          mTextureId{0u};
142   GLenum            mGlTarget{0u};
143   uint32_t          mMaxMipMapLevel{0u};
144   void*             mGLOwnerContext{nullptr};
145   bool              mIsCompressed{false};
146 };
147
148 } // namespace Dali::Graphics::GLES
149
150 #endif