1 #ifndef DALI_INTERNAL_RENDER_TEXTURE_H
2 #define DALI_INTERNAL_RENDER_TEXTURE_H
5 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <cstdint> // uint16_t, uint32_t
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>
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-upload-helper.h>
32 #include <dali/graphics-api/graphics-texture.h>
33 #include <dali/graphics-api/graphics-types.h>
34 #include <dali/internal/event/rendering/texture-impl.h>
35 #include <dali/internal/render/renderers/render-sampler.h>
44 } // namespace SceneGraph
53 using Type = Dali::TextureType::Type;
56 * Factory method to return a new texture accessed by key.
58 static TextureKey NewKey(Type type, Pixel::Format format, ImageDimensions size);
61 * Factory method to return a new texture accessed by key.
63 static TextureKey NewKey(NativeImageInterfacePtr nativeImageInterface);
66 * Factory method to return a new texture accessed by key.
68 static TextureKey NewKey(Type type, uint32_t resourceId);
72 * @param[in] type The type of the texture
73 * @param[in] format The format of the pixel data
74 * @param[in] size The size of the texture
76 Texture(Type type, Pixel::Format format, ImageDimensions size);
79 * Constructor from native image
80 * @param[in] nativeImageInterface The native image
82 explicit Texture(NativeImageInterfacePtr nativeImageInterface);
85 * Constructor from resource id
86 * @param[in] type The type of the texture
87 * @param[in] resourceId The resouce id for texture upload manager using
89 explicit Texture(Type type, uint32_t resourceId);
97 * Deletes the texture from it's global memory pool
99 void operator delete(void* ptr);
101 static Texture* Get(TextureKey::KeyType);
104 * Get the key of the given renderer in the associated memory pool.
105 * @param[in] renderer the given renderer
106 * @return The key in the associated memory pool.
108 static TextureKey GetKey(const Render::Texture& renderer);
111 * Get the key of the given renderer in the associated memory pool.
112 * @param[in] renderer the given renderer
113 * @return The key in the associated memory pool, or -1 if not
116 static TextureKey GetKey(Render::Texture* renderer);
119 * Stores the graphics controller for use when required.
121 * @param[in] graphicsController The graphics controller to use
122 * @param[in] renderManager The render manager to be used.
124 void Initialize(Graphics::Controller& graphicsController, SceneGraph::RenderManager& renderManager);
127 * Create the texture without a buffer
128 * @param[in] usage How texture will be used
130 void Create(Graphics::TextureUsageFlags usage);
133 * Deletes the texture from the GPU
138 * Uploads data to the texture.
139 * @param[in] pixelData A pixel data object
140 * @param[in] params Upload parameters. See UploadParams
142 void Upload(PixelDataPtr pixelData, const Graphics::UploadParams& params);
145 * Auto generates mipmaps for the texture
147 void GenerateMipmaps();
150 * Retrieve whether the texture has an alpha channel
151 * @return True if the texture has alpha channel, false otherwise
153 [[nodiscard]] bool HasAlphaChannel() const;
156 * Get the graphics object associated with this texture
158 [[nodiscard]] Graphics::Texture* GetGraphicsObject();
161 * Get the type of the texture
162 * @return Type of the texture
164 [[nodiscard]] Type GetType() const
170 * Check if the texture is a native image
171 * @return if the texture is a native image
173 [[nodiscard]] bool IsNativeImage() const
175 return static_cast<bool>(mNativeImage);
179 * Return the pixel format of the texture
180 * @return The pixel format of the texture data.
182 Pixel::Format GetPixelFormat() const
188 * Return the width of the texture
189 * @return The width of the texture
191 uint16_t GetWidth() const
197 * Return the height of the texture
198 * @return The height of the texture
200 uint16_t GetHeight() const
206 * Set the pixel format of the texture
207 * @param[in] pixelFormat The pixel format of the texture data.
209 void SetPixelFormat(Pixel::Format pixelFormat)
211 mPixelFormat = pixelFormat;
215 * Set the width of the texture
216 * @param[in] width The width of the texture
218 void SetWidth(uint16_t width)
224 * Set the height of the texture
225 * @param[in] height The height of the texture
227 void SetHeight(uint16_t height)
233 * Called from RenderManager to notify the texture that current rendering pass has finished.
235 void OnRenderFinished();
238 * Set the updated flag.
239 * @param[in] updated The updated flag
241 void SetUpdated(bool updated)
247 * Check if the texture is updated
248 * @return True if the texture is updated
250 [[nodiscard]] bool Updated()
252 if(mUpdated || (mNativeImage && mNativeImage->SourceChanged()) || (mResourceId != 0u && IsGraphicsObjectChanged()))
260 * Get the updated area
261 * @return The updated area if the texture is updated, otherwise an empty area
262 * @note The origin of the area is the top-left corner of the texture.
264 Rect<uint16_t> GetUpdatedArea();
268 * Helper method to apply a sampler to the texture
269 * @param[in] sampler The sampler
271 void ApplySampler(Render::Sampler* sampler);
274 * Create the texture without a buffer
275 * @param[in] usage How texture will be used
276 * @param[in] allocationPolicy Policy of texture allocation
278 void Create(Graphics::TextureUsageFlags usage, Graphics::TextureAllocationPolicy allocationPolicy);
281 * Create a texture with a buffer if non-null
282 * @param[in] usage How texture will be used
283 * @param[in] allocationPolicy Policy of texture allocation
284 * @param[in] buffer Buffer to copy
285 * @param[in] bufferSize Size of buffer to copy
287 void CreateWithData(Graphics::TextureUsageFlags usage, Graphics::TextureAllocationPolicy allocationPolicy, uint8_t* buffer, uint32_t bufferSize);
290 * @brief Check whether graphics object pointer were changed what we know before.
292 * @return True if graphics object created newly. False otherwised.
294 bool IsGraphicsObjectChanged();
297 * @brief Notify to RenderManager that this texture has been updated.
299 void NotifyTextureUpdated();
302 Graphics::Controller* mGraphicsController;
303 SceneGraph::RenderManager* mRenderManager;
304 Graphics::UniquePtr<Graphics::Texture> mGraphicsTexture;
306 NativeImageInterfacePtr mNativeImage; ///< Pointer to native image
307 Render::Sampler mSampler; ///< The current sampler state
309 Rect<uint16_t> mUpdatedArea{}; ///< Updated area of the texture
311 Pixel::Format mPixelFormat; ///< Pixel format of the texture
312 uint16_t mWidth; ///< Width of the texture
313 uint16_t mHeight; ///< Height of the texture
315 uint32_t mResourceId;
316 Graphics::Texture* mLatestUsedGraphicsTexture{nullptr};
318 Type mType : 3; ///< Type of the texture
319 bool mHasAlpha : 1; ///< Whether the format has an alpha channel
320 bool mUpdated : 1; ///< Whether the texture is updated
322 bool mUseUploadedParameter : 1; ///< Whether ths texture size and format depend on uploaded image or not.
325 } // namespace Render
327 } // namespace Internal
331 #endif // DALI_INTERNAL_RENDER_TEXTURE_H