1 #ifndef DALI_TOOLKIT_IMAGE_ATLAS_IMPL_H
2 #define DALI_TOOLKIT_IMAGE_ATLAS_IMPL_H
5 * Copyright (c) 2022 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 <dali/devel-api/common/owner-container.h>
22 #include <dali/public-api/adaptor-framework/encoded-image-buffer.h>
23 #include <dali/public-api/common/intrusive-ptr.h>
24 #include <dali/public-api/object/base-object.h>
25 #include <dali/public-api/signals/connection-tracker.h>
28 #include <dali-toolkit/devel-api/image-loader/image-atlas.h>
29 #include <dali-toolkit/internal/image-loader/atlas-packer.h>
30 #include <dali-toolkit/internal/visuals/visual-url.h>
31 #include <dali-toolkit/public-api/image-loader/async-image-loader.h>
35 class EventThreadCallback;
41 class ImageAtlas : public BaseObject, public ConnectionTracker
44 typedef Toolkit::ImageAtlas::SizeType SizeType;
47 * @copydoc ImageAtlas::PackToAtlas( const std::vector<PixelData>&, Dali::Vector<Vector4>& )
49 static Texture PackToAtlas(const std::vector<PixelData>& pixelData, Dali::Vector<Vector4>& textureRects);
53 * @param [in] width The atlas width in pixels.
54 * @param [in] height The atlas height in pixels.
55 * @param [in] pixelFormat The pixel format.
57 ImageAtlas(SizeType width, SizeType height, Pixel::Format pixelFormat);
60 * @copydoc Toolkit::ImageAtlas::New
62 static IntrusivePtr<ImageAtlas> New(SizeType width, SizeType height, Pixel::Format pixelFormat);
65 * @copydoc Toolkit::ImageAtlas::GetAtlas
70 * @copydoc Toolkit::ImageAtlas::GetOccupancyRate
72 float GetOccupancyRate() const;
75 * @copydoc Toolkit::ImageAtlas::SetBrokenImage
77 void SetBrokenImage(const std::string& brokenImageUrl);
80 * @copydoc Toolkit::ImageAtlas::Upload( Vector4&, const std::string&, ImageDimensions,FittingMode::Type, bool )
82 bool Upload(Vector4& textureRect,
85 FittingMode::Type fittingMode,
86 bool orientationCorrection,
87 AtlasUploadObserver* atlasUploadObserver);
90 * @brief Upload a resource image to the atlas by encoded buffer.
92 * @note To make the atlasing efficient, a valid size should be provided.
93 * If size is not provided, then SegFault occured.
94 * Do not set a size that is bigger than the actual image size, as the up-scaling is not available,
95 * the content of the area not covered by actual image is undefined, it will not be cleared.
97 * SamplingMode::BOX_THEN_LINEAR is used to sampling pixels from the input image while fitting it to desired size.
99 * @param [out] textureRect The texture area of the resource image in the atlas.
100 * @param [in] encodedImageBuffer The encoded raw buffer of the resource image file to use.
101 * @param [in] size The width and height to fit the loaded image to.
102 * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
103 * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
104 * @param [in] atlasUploadObserver The observer to observe the upload state inside the ImageAtlas.
105 * @return True if there is enough space to fit this image in,false otherwise.
106 * @note The valid callback function here is required to have the signature of void( void ).
108 bool Upload(Vector4& textureRect,
109 const EncodedImageBuffer& encodedImageBuffer,
110 ImageDimensions size,
111 FittingMode::Type fittingMode,
112 bool orientationCorrection,
113 AtlasUploadObserver* atlasUploadObserver);
116 * @copydoc Toolkit::ImageAtlas::Upload( Vector4&, PixelData )
118 bool Upload(Vector4& textureRect, PixelData pixelData);
121 * @copydoc Toolkit::ImageAtlas::Remove
123 void Remove(const Vector4& textureRect);
126 * Resets the destroying observer pointer so that we know not to call methods of this object any more.
128 void ObserverDestroyed(AtlasUploadObserver* observer);
138 * @copydoc PixelDataRequester::ProcessPixels
140 void UploadToAtlas(uint32_t id, PixelData pixelData);
143 * Upload broken image
145 * @param[in] area The pixel area for uploading.
147 void UploadBrokenImage(const Rect<uint32_t>& area);
150 ImageAtlas(const ImageAtlas& imageAtlas);
153 ImageAtlas& operator=(const ImageAtlas& imageAtlas);
157 * Each loading task( identified with an ID ) is associated with a rect region for packing the loaded pixel data into the atlas,
158 * and an AtlasUploadObserver whose UploadCompleted method should get executed once the sub texture is ready.
160 struct LoadingTaskInfo
162 LoadingTaskInfo(uint32_t loadTaskId,
163 uint32_t packPositionX,
164 uint32_t packPositionY,
167 AtlasUploadObserver* observer)
168 : loadTaskId(loadTaskId),
169 packRect(packPositionX, packPositionY, width, height),
175 Rect<uint32_t> packRect;
176 AtlasUploadObserver* observer;
179 OwnerContainer<LoadingTaskInfo*> mLoadingTaskInfoContainer;
183 Toolkit::AsyncImageLoader mAsyncLoader;
184 std::string mBrokenImageUrl;
185 ImageDimensions mBrokenImageSize;
188 Pixel::Format mPixelFormat;
191 } // namespace Internal
193 inline const Internal::ImageAtlas& GetImplementation(const Toolkit::ImageAtlas& imageAtlas)
195 DALI_ASSERT_ALWAYS(imageAtlas && "ImageAtlas handle is empty");
197 const BaseObject& handle = imageAtlas.GetBaseObject();
199 return static_cast<const Internal::ImageAtlas&>(handle);
202 inline Internal::ImageAtlas& GetImplementation(Toolkit::ImageAtlas& imageAtlas)
204 DALI_ASSERT_ALWAYS(imageAtlas && "ImageAtlas handle is empty");
206 BaseObject& handle = imageAtlas.GetBaseObject();
208 return static_cast<Internal::ImageAtlas&>(handle);
211 } // namespace Toolkit
215 #endif // DALI_TOOLKIT_IMAGE_ATLAS_IMPL_H