• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 GPU_CONTEXT_H
17 #define GPU_CONTEXT_H
18 
19 #include "impl_interface/gpu_context_impl.h"
20 #include "utils/data.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 namespace Drawing {
25 enum class PathRenderers : uint32_t {
26     NONE              = 0,
27     DASHLINE          = 1 << 0,
28     STENCILANDCOVER   = 1 << 1,
29     COVERAGECOUNTING  = 1 << 2,
30     AAHAIRLINE        = 1 << 3,
31     AACONVEX          = 1 << 4,
32     AALINEARIZING     = 1 << 5,
33     SMALL             = 1 << 6,
34     TESSELLATING      = 1 << 7,
35 
36     ALL               = (TESSELLATING | (TESSELLATING - 1)),
37     DEFAULT           = ALL & ~COVERAGECOUNTING
38 };
39 
40 /*
41  * @brief  Option to create a GPUContext. Currently only supports setting persistent cache,
42            other options may be expanded in the future
43  */
44 class GPUContextOptions {
45 public:
46     /*
47      * @brief  Cache compiled shaders for use between sessions.
48      */
49     class PersistentCache {
50     public:
51         PersistentCache() = default;
52         virtual ~PersistentCache() = default;
53 
54         /*
55          * @brief  Returns the data for the key if it exists in the cache.
56          */
57         virtual std::shared_ptr<Data> Load(const Data& key) = 0;
58 
59         /*
60          * @brief  Stores the data and key.
61          */
62         virtual void Store(const Data& key, const Data& data) = 0;
63     };
64 
65     /*
66      * @brief  Gets persistent cache object.
67      */
68     PersistentCache* GetPersistentCache() const;
69     /*
70      * @brief                  Sets persistent cache object.
71      * @param persistentCache  A pointer to persistent cache object.
72      */
73     void SetPersistentCache(PersistentCache* persistentCache);
74 
75 private:
76     PersistentCache* persistentCache_ = nullptr;
77 };
78 
79 class GPUContext {
80 public:
81     GPUContext();
~GPUContext()82     ~GPUContext() {}
83 
84     /*
85      * @brief           Creates a GL GPUContext for a backend context.
86      * @param options   Option to create a GL GPUContext.
87      */
88     bool BuildFromGL(const GPUContextOptions& options);
89 
90     /*
91      * @brief   Call to ensure all drawing to the context has been flushed to underlying 3D API specific objects.
92      */
93     void Flush();
94 
95     /*
96      * @brief             Purge GPU resources that haven't been used in the past 'msNotUsed' milliseconds
97                           or are otherwise marked for deletion.
98      * @param msNotUsed   Only unlocked resources not used in these last milliseconds will be cleaned up.
99      */
100     void PerformDeferredCleanup(std::chrono::milliseconds msNotUsed);
101 
102     /*
103      * @brief                   Gets the current GPU resource cache limits.
104      * @param maxResource       If non-null, returns maximum number of resources that can be held in the cache.
105      * @param maxResourceBytes  If non-null, returns maximum number of bytes of video memory
106                                 that can be held in the cache.
107      */
108     void GetResourceCacheLimits(int& maxResource, size_t& maxResourceBytes) const;
109 
110     /*
111      * @brief                   Specify the GPU resource cache limits.
112      * @param maxResource       The maximum number of resources that can be held in the cache.
113      * @param maxResourceBytes  The maximum number of bytes of video memory that can be held in the cache.
114      */
115     void SetResourceCacheLimits(int maxResource, size_t maxResourceBytes);
116 
117     /*
118      * @brief   Get the adaptation layer instance, called in the adaptation layer.
119      * @return  Adaptation Layer instance.
120      */
121     template<typename T>
GetImpl()122     const std::shared_ptr<T> GetImpl() const
123     {
124         return impl_->DowncastingTo<T>();
125     }
126 private:
127     std::shared_ptr<GPUContextImpl> impl_;
128 };
129 } // namespace Drawing
130 } // namespace Rosen
131 } // namespace OHOS
132 
133 #endif // !GPU_CONTEXT_H
134