1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef CORE_IMAGE_IMAGE_LOADER_MANAGER_H
17 #define CORE_IMAGE_IMAGE_LOADER_MANAGER_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/string_view.h>
22 #include <base/containers/vector.h>
23 #include <base/namespace.h>
24 #include <core/image/intf_animated_image.h>
25 #include <core/image/intf_image_container.h>
26 #include <core/image/intf_image_loader_manager.h>
27 #include <core/namespace.h>
28 #include <core/plugin/intf_plugin_register.h>
29 
30 BASE_BEGIN_NAMESPACE()
31 template<class T>
32 class array_view;
33 BASE_END_NAMESPACE()
34 
35 CORE_BEGIN_NAMESPACE()
36 class IFile;
37 class IFileManager;
38 
39 class ImageLoaderManager final : public IImageLoaderManager, private IPluginRegister::ITypeInfoListener {
40 public:
41     explicit ImageLoaderManager(IFileManager& fileManager);
42     ~ImageLoaderManager() override;
43 
44     void RegisterImageLoader(IImageLoader::Ptr imageLoader) override;
45 
46     LoadResult LoadImage(const BASE_NS::string_view uri, uint32_t loadFlags) override;
47     LoadResult LoadImage(IFile& file, uint32_t loadFlags) override;
48     LoadResult LoadImage(BASE_NS::array_view<const uint8_t> imageFileBytes, uint32_t loadFlags) override;
49 
50     LoadAnimatedResult LoadAnimatedImage(const BASE_NS::string_view uri, uint32_t loadFlags) override;
51     LoadAnimatedResult LoadAnimatedImage(IFile& file, uint32_t loadFlags) override;
52     LoadAnimatedResult LoadAnimatedImage(
53         BASE_NS::array_view<const uint8_t> imageFileBytes, uint32_t loadFlags) override;
54 
55     BASE_NS::vector<ImageType> GetSupportedTypes() const override;
56 
57     static LoadResult ResultFailure(const BASE_NS::string_view error);
58     static LoadResult ResultSuccess(IImageContainer::Ptr image);
59     static LoadAnimatedResult ResultFailureAnimated(const BASE_NS::string_view error);
60     static LoadAnimatedResult ResultSuccessAnimated(IAnimatedImage::Ptr image);
61 
62 private:
63     void OnTypeInfoEvent(EventType type, BASE_NS::array_view<const ITypeInfo* const> typeInfos) override;
64     IFileManager& fileManager_;
65     struct RegisteredImageLoader {
66         BASE_NS::Uid uid;
67         IImageLoader::Ptr instance;
68     };
69     BASE_NS::vector<RegisteredImageLoader> imageLoaders_;
70 };
71 CORE_END_NAMESPACE()
72 
73 #endif // CORE_IMAGE_IMAGE_LOADER_MANAGER_H
74