Ocean
ResourceManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_PLATFORM_RESOURCE_MANAGER_H
9 #define META_OCEAN_PLATFORM_RESOURCE_MANAGER_H
10 
12 
13 #include "ocean/base/Frame.h"
14 
15 namespace Ocean
16 {
17 
18 namespace Platform
19 {
20 
21 /**
22  * This class implements the platform independent base class for all resource managers which can be specialized for specific applications.
23  * Application specific resource manager should be implemented as singletons and should be derived from this base class.
24  * @ingroup platform
25  */
26 class OCEAN_PLATFORM_EXPORT ResourceManager
27 {
28  public:
29 
30  /**
31  * Definition of a vector holding 8 bit values.
32  */
33  typedef std::vector<uint8_t> Buffer;
34 
35  /**
36  * Definition of an object reference holding a buffer.
37  */
39 
40  protected:
41 
42  /**
43  * Definition of a map mapping resource ids to frames with individual resolutions.
44  */
45  typedef std::unordered_map<unsigned int, FrameRefs> FrameMap;
46 
47  /**
48  * Definition of a map mapping resource ids to buffer objects.
49  */
50  typedef std::unordered_map<unsigned int, BufferRef> BufferMap;
51 
52  public:
53 
54  /**
55  * Adds a new frame resolution of/for a specified resource frame.
56  * Beware: The first resolution of a unique resource frame is expected to be the native resolution for screens without any explicit DPI scaling.
57  * @param id The application-wide unique id of the resource frame for which a frame with specific resolution will be registered
58  * @param frame The frame representing one specific resolution of the specified resource frame
59  */
60  void addFrameResolution(const unsigned int id, const FrameRef& frame);
61 
62  /**
63  * Adds a new data/buffer resource.
64  * @param id The application-wide unique id of the resource
65  * @param buffer The buffer of the resource
66  */
67  void addData(const unsigned int id, const BufferRef& buffer);
68 
69  /**
70  * Adds a new data/buffer resource (moves the buffer into this manager).
71  * @param id The application-wide unique id of the resource
72  * @param buffer The buffer of the resource to be moved
73  */
74  void addData(const unsigned int id, Buffer&& buffer);
75 
76  /**
77  * Returns a specific resource frame.
78  * This function allows to specified the dimension of the resulting frame.
79  * @param id The application-wide unique id of the requested resource frame
80  * @param width Optional width of the resulting frame in pixel, with range [1u, infinity), 0 to return the frame with native resolution
81  * @param height Optional height of the resulting frame in pixel, with range [1u, infinity), 0 to return the frame with native resolution
82  * @param downscaleIfNecessary True, to return a frame exactly with the specified frame dimension, otherwise the next larger frame will be returned
83  * @return The specified frame, if any
84  */
85  FrameRef frame(const unsigned int id, const unsigned int width = 0u, const unsigned int height = 0u, const bool downscaleIfNecessary = true);
86 
87  /**
88  * Returns a specific resource frame for a specified screen scaling factor.
89  * The provided factor is the relation between the current screen scaling and a native scaling.<br>
90  * I.e., The resulting frame for a scaling of 2 has twice of the dimension as the frame for a scaling of 1.
91  * @param id The application-wide unique id of the requested resource frame
92  * @param scaleFactor The screen scaling factor for which the frame with matching resolution is determined, with range (0, infinity)
93  * @return The requested frame, if any
94  */
95  FrameRef frame(const unsigned int id, const double scaleFactor);
96 
97  /**
98  * Returns a specified resource buffer (any kind of arbitrary resource data which is not a frame/image).
99  * @param id The application-wide unique id of the requested resource data
100  * @return The requested data, if any
101  */
102  BufferRef data(const unsigned int id);
103 
104  /**
105  * Releases the entire resources of this manager.
106  */
107  void release();
108 
109  protected:
110 
111  /**
112  * The protected default constructor.
113  */
115 
116  /**
117  * Destructs the resource manager.
118  */
119  virtual ~ResourceManager();
120 
121  /**
122  * Returns the interpolated frame of a specified resource frame.
123  * If the specified frame dimension is larger than the dimension of the largest native frame, the largest native frame will be returned.
124  * @param id The id of the resource frame for which the interpolated frame will be returned
125  * @param width The width of the resulting interpolated frame in pixel, with range [1, infinity)
126  * @param height The height of the resulting interpolated frame in pixel, with range [1, infinity)
127  * @return The resulting interpolated frame, or the largest available native frame if the largest native frame is smaller than the requested one
128  */
129  FrameRef interpolatedFrame(const unsigned int id, const unsigned int width, const unsigned int height);
130 
131  /**
132  * Returns the largest frame from a set of given frames.
133  * @param frames The set of given frames, at least one
134  * @return The largest frame from the specified set
135  */
136  static FrameRef largestFrame(const FrameRefs& frames);
137 
138  protected:
139 
140  /// The frame map of the native resource frames.
142 
143  /// The frame map of the interpolated frames.
145 
146  /// The map of resource buffers.
148 
149  /// The manager's lock.
151 };
152 
153 }
154 
155 }
156 
157 #endif // META_OCEAN_PLATFORM_RESOURCE_MANAGER_H
This class implements a recursive lock object.
Definition: Lock.h:31
This template class implements a object reference with an internal reference counter.
Definition: base/ObjectRef.h:58
This class implements the platform independent base class for all resource managers which can be spec...
Definition: ResourceManager.h:27
BufferMap bufferMap_
The map of resource buffers.
Definition: ResourceManager.h:147
FrameRef frame(const unsigned int id, const double scaleFactor)
Returns a specific resource frame for a specified screen scaling factor.
BufferRef data(const unsigned int id)
Returns a specified resource buffer (any kind of arbitrary resource data which is not a frame/image).
FrameRef interpolatedFrame(const unsigned int id, const unsigned int width, const unsigned int height)
Returns the interpolated frame of a specified resource frame.
ResourceManager()
The protected default constructor.
void release()
Releases the entire resources of this manager.
void addData(const unsigned int id, const BufferRef &buffer)
Adds a new data/buffer resource.
Lock lock_
The manager's lock.
Definition: ResourceManager.h:150
void addFrameResolution(const unsigned int id, const FrameRef &frame)
Adds a new frame resolution of/for a specified resource frame.
void addData(const unsigned int id, Buffer &&buffer)
Adds a new data/buffer resource (moves the buffer into this manager).
std::unordered_map< unsigned int, BufferRef > BufferMap
Definition of a map mapping resource ids to buffer objects.
Definition: ResourceManager.h:50
FrameRef frame(const unsigned int id, const unsigned int width=0u, const unsigned int height=0u, const bool downscaleIfNecessary=true)
Returns a specific resource frame.
FrameMap nativeFrames_
The frame map of the native resource frames.
Definition: ResourceManager.h:141
static FrameRef largestFrame(const FrameRefs &frames)
Returns the largest frame from a set of given frames.
ObjectRef< Buffer > BufferRef
Definition of an object reference holding a buffer.
Definition: ResourceManager.h:38
std::unordered_map< unsigned int, FrameRefs > FrameMap
Definition of a map mapping resource ids to frames with individual resolutions.
Definition: ResourceManager.h:45
FrameMap interpolatedFrames_
The frame map of the interpolated frames.
Definition: ResourceManager.h:144
virtual ~ResourceManager()
Destructs the resource manager.
std::vector< uint8_t > Buffer
Definition of a vector holding 8 bit values.
Definition: ResourceManager.h:33
std::vector< FrameRef > FrameRefs
Definition of a vector holding frame references.
Definition: Frame.h:1767
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15