Line data Source code
1 : /**
2 : Copyright (c) 2021 Roman Katuntsev <sbkarr@stappler.org>
3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
4 :
5 : Permission is hereby granted, free of charge, to any person obtaining a copy
6 : of this software and associated documentation files (the "Software"), to deal
7 : in the Software without restriction, including without limitation the rights
8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : copies of the Software, and to permit persons to whom the Software is
10 : furnished to do so, subject to the following conditions:
11 :
12 : The above copyright notice and this permission notice shall be included in
13 : all copies or substantial portions of the Software.
14 :
15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : THE SOFTWARE.
22 : **/
23 :
24 : #ifndef XENOLITH_APPLICATION_XLRESOURCECACHE_H_
25 : #define XENOLITH_APPLICATION_XLRESOURCECACHE_H_
26 :
27 : #include "XLApplicationInfo.h"
28 : #include "XLCoreResource.h"
29 : #include "XLCoreLoop.h"
30 :
31 : namespace STAPPLER_VERSIONIZED stappler::xenolith {
32 :
33 : class TemporaryResource;
34 : class Texture;
35 : class MeshIndex;
36 :
37 : enum class TemporaryResourceFlags {
38 : None = 0,
39 : Loaded = 1 << 0, // Resource is a wrapper around already loaded data (usually, when data created by GPU)
40 : RemoveOnClear = 1 << 1, // Remove whole resource from cache, when no one uses it
41 : CompileWhenAdded = 1 << 2, // Run resource compilation immediately after addTemporaryResource
42 : };
43 :
44 : SP_DEFINE_ENUM_AS_MASK(TemporaryResourceFlags)
45 :
46 : class ResourceCache : public Ref {
47 : public:
48 : virtual ~ResourceCache();
49 :
50 : bool init(Application *);
51 :
52 : void initialize(const core::Loop &);
53 : void invalidate();
54 :
55 : void update(const UpdateTime &);
56 :
57 : void addImage(StringView, const Rc<core::ImageObject> &);
58 : void addResource(const Rc<core::Resource> &);
59 : void removeResource(StringView);
60 :
61 30 : Application *getApplication() const { return _application; }
62 :
63 : Rc<Texture> acquireTexture(StringView) const;
64 : Rc<MeshIndex> acquireMeshIndex(StringView) const;
65 :
66 : const core::ImageData *getEmptyImage() const;
67 : const core::ImageData *getSolidImage() const;
68 :
69 : /*const gl::BufferData * addExternalBufferByRef(StringView key, gl::BufferInfo &&, BytesView data);
70 : const gl::BufferData * addExternalBuffer(StringView key, gl::BufferInfo &&, FilePath data);
71 : const gl::BufferData * addExternalBuffer(StringView key, gl::BufferInfo &&, BytesView data);
72 : const gl::BufferData * addExternalBuffer(StringView key, gl::BufferInfo &&, size_t,
73 : const memory::function<void(const gl::BufferData::DataCallback &)> &cb);*/
74 :
75 : Rc<Texture> addExternalImageByRef(StringView key, core::ImageInfo &&, BytesView data,
76 : TimeInterval = TimeInterval(), TemporaryResourceFlags flags = TemporaryResourceFlags::None);
77 : Rc<Texture> addExternalImage(StringView key, core::ImageInfo &&, FilePath data,
78 : TimeInterval = TimeInterval(), TemporaryResourceFlags flags = TemporaryResourceFlags::None);
79 : Rc<Texture> addExternalImage(StringView key, core::ImageInfo &&, BytesView data,
80 : TimeInterval = TimeInterval(), TemporaryResourceFlags flags = TemporaryResourceFlags::None);
81 : Rc<Texture> addExternalImage(StringView key, core::ImageInfo &&,
82 : const memory::function<void(uint8_t *, uint64_t, const core::ImageData::DataCallback &)> &cb,
83 : TimeInterval = TimeInterval(), TemporaryResourceFlags flags = TemporaryResourceFlags::None);
84 :
85 : Rc<TemporaryResource> addTemporaryResource(Rc<core::Resource> &&, TimeInterval = TimeInterval(),
86 : TemporaryResourceFlags flags = TemporaryResourceFlags::None);
87 :
88 : Rc<TemporaryResource> getTemporaryResource(StringView str) const;
89 : bool hasTemporaryResource(StringView) const;
90 : void removeTemporaryResource(StringView);
91 :
92 : protected:
93 : void compileResource(TemporaryResource *);
94 : bool clearResource(TemporaryResource *);
95 :
96 : Application *_application = nullptr;
97 : const core::Loop *_loop = nullptr;
98 : Map<StringView, core::ImageData> _images;
99 : Map<StringView, Rc<core::Resource>> _resources;
100 : Map<StringView, Rc<TemporaryResource>> _temporaries;
101 : };
102 :
103 : }
104 :
105 : #endif /* XENOLITH_APPLICATION_XLRESOURCECACHE_H_ */
|