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 SCENEPLUGIN_INTF_ASSETLOADER_H
17 #define SCENEPLUGIN_INTF_ASSETLOADER_H
18 
19 #include <scene_plugin/interface/intf_entity_collection.h>
20 
21 #include <base/containers/string.h>
22 
SCENE_BEGIN_NAMESPACE()23 SCENE_BEGIN_NAMESPACE()
24 
25 /** Asset loader interface */
26 class IAssetLoader {
27 public:
28     /** Listener for asset loading events. */
29     class IListener {
30     public:
31         /** On load finished */
32         virtual void OnLoadFinished(const IAssetLoader& loader) = 0;
33 
34     protected:
35         virtual ~IListener() = default;
36     };
37 
38     /** Describes result of the loading operation. */
39     struct Result {
40         /** Indicates, whether the loading operation is successful. */
41         bool success { true };
42 
43         /** In case of a loading error, contains the description of the error. */
44         BASE_NS::string error;
45     };
46 
47     /** Returns the entity collection used by this loader. */
48     virtual IEntityCollection& GetEntityCollection() const = 0;
49 
50     /** Returns the src for this loader that is used to resolve the final loading uri. */
51     virtual BASE_NS::string GetSrc() const = 0;
52 
53     /** Returns the context uri for this loader. The src value is resolved using this context to get the actual uri. */
54     virtual BASE_NS::string GetContextUri() const = 0;
55 
56     /** Returns the resolved uri that is used for loading. */
57     virtual BASE_NS::string GetUri() const = 0;
58 
59     /** Add a listener that gets callbacks about loading events. */
60     virtual void AddListener(IListener& listener) = 0;
61 
62     /** Add a listener that gets callbacks about loading events. */
63     virtual void RemoveListener(IListener& listener) = 0;
64 
65     /** Load asset data synchronously. The previous loaded data will be discarded. */
66     virtual void LoadAsset() = 0;
67 
68     /** Load asset data asynchronously, user is required to call Execute() from main thread until it returns true.
69      * The previous loaded data will be discarded. */
70     virtual void LoadAssetAsync() = 0;
71 
72     /** Advances the loading process, needs to be called from the main thread when performing asynchronous loading.
73      *  @param timeBudget Time budget for resource loading in microseconds, if 0 all available work will be executed
74      *  during this frame.
75      *  @return true if the task has completed.
76      */
77     virtual bool Execute(uint32_t timeBudget) = 0;
78 
79     /** Cancel the loading operation */
80     virtual void Cancel() = 0;
81 
82     /** Returns true if the loading process has been completed. */
83     virtual bool IsCompleted() const = 0;
84 
85     /** Returns the status of the loading operation. */
86     virtual Result GetResult() const = 0;
87 
88     struct Deleter {
89         constexpr Deleter() noexcept = default;
90         void operator()(IAssetLoader* ptr) const
91         {
92             ptr->Destroy();
93         }
94     };
95     using Ptr = BASE_NS::unique_ptr<IAssetLoader, Deleter>;
96 
97 protected:
98     IAssetLoader() = default;
99     virtual ~IAssetLoader() = default;
100     virtual void Destroy() = 0;
101 };
102 
103 SCENE_END_NAMESPACE()
104 
105 #endif // SCENEPLUGIN_INTF_IASSETLOADER_H
106