• 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 #include "skia_gpu_context.h"
17 
18 #include "include/gpu/gl/GrGLInterface.h"
19 
20 #include "skia_data.h"
21 #include "utils/data.h"
22 #include "utils/log.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 namespace Drawing {
SkiaPersistentCache(GPUContextOptions::PersistentCache * cache)27 SkiaPersistentCache::SkiaPersistentCache(GPUContextOptions::PersistentCache* cache) : cache_(cache) {}
28 
load(const SkData & key)29 sk_sp<SkData> SkiaPersistentCache::load(const SkData& key)
30 {
31     Data keyData;
32     if (!cache_) {
33         LOGE("SkiaPersistentCache::load, failed! cache or key invalid");
34         return nullptr;
35     }
36     auto skiaKeyDataImpl = keyData.GetImpl<SkiaData>();
37     skiaKeyDataImpl->SetSkData(sk_ref_sp(&key));
38 
39     auto retData = cache_->Load(keyData);
40     if (retData == nullptr) {
41         LOGE("SkiaPersistentCache::load, failed! load data invalid");
42         return nullptr;
43     }
44 
45     return retData->GetImpl<SkiaData>()->GetSkData();
46 }
47 
store(const SkData & key,const SkData & data)48 void SkiaPersistentCache::store(const SkData& key, const SkData& data)
49 {
50     Data keyData;
51     Data storeData;
52     if (!cache_) {
53         LOGE("SkiaPersistentCache::store, failed! cache or {key,data} invalid");
54         return;
55     }
56 
57     keyData.GetImpl<SkiaData>()->SetSkData(sk_ref_sp(&key));
58     storeData.GetImpl<SkiaData>()->SetSkData(sk_ref_sp(&data));
59     cache_->Store(keyData, storeData);
60 }
61 
SkiaGPUContext()62 SkiaGPUContext::SkiaGPUContext() : grContext_(nullptr), skiaPersistentCache_(nullptr) {}
63 
BuildFromGL(const GPUContextOptions & options)64 bool SkiaGPUContext::BuildFromGL(const GPUContextOptions& options)
65 {
66     sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
67     if (options.GetPersistentCache() != nullptr) {
68         skiaPersistentCache_ = std::make_shared<SkiaPersistentCache>(options.GetPersistentCache());
69     }
70 
71     GrContextOptions grOptions;
72     grOptions.fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
73     grOptions.fPreferExternalImagesOverES3 = true;
74     grOptions.fDisableDistanceFieldPaths = true;
75     grOptions.fAllowPathMaskCaching = true;
76     grOptions.fPersistentCache = skiaPersistentCache_.get();
77 #ifdef NEW_SKIA
78     grContext_ = GrDirectContext::MakeGL(std::move(glInterface), grOptions);
79 #else
80     grContext_ = GrContext::MakeGL(std::move(glInterface), grOptions);
81 #endif
82     return grContext_ != nullptr ? true : false;
83 }
84 
Flush()85 void SkiaGPUContext::Flush()
86 {
87     if (!grContext_) {
88         LOGE("SkiaGPUContext::Flush, grContext_ is nullptr");
89         return;
90     }
91     grContext_->flush();
92 }
93 
PerformDeferredCleanup(std::chrono::milliseconds msNotUsed)94 void SkiaGPUContext::PerformDeferredCleanup(std::chrono::milliseconds msNotUsed)
95 {
96     if (!grContext_) {
97         LOGE("SkiaGPUContext::PerformDeferredCleanup, grContext_ is nullptr");
98         return;
99     }
100     grContext_->performDeferredCleanup(msNotUsed);
101 }
102 
GetResourceCacheLimits(int & maxResource,size_t & maxResourceBytes) const103 void SkiaGPUContext::GetResourceCacheLimits(int& maxResource, size_t& maxResourceBytes) const
104 {
105     if (!grContext_) {
106         LOGE("SkiaGPUContext::GetResourceCacheLimits, grContext_ is nullptr");
107         return;
108     }
109     grContext_->getResourceCacheLimits(&maxResource, &maxResourceBytes);
110 }
111 
SetResourceCacheLimits(int maxResource,size_t maxResourceBytes)112 void SkiaGPUContext::SetResourceCacheLimits(int maxResource, size_t maxResourceBytes)
113 {
114     if (!grContext_) {
115         LOGE("SkiaGPUContext::SetResourceCacheLimits, grContext_ is nullptr");
116         return;
117     }
118     grContext_->setResourceCacheLimits(maxResource, maxResourceBytes);
119 }
120 #ifdef NEW_SKIA
GetGrContext() const121 sk_sp<GrDirectContext> SkiaGPUContext::GetGrContext() const
122 #else
123 sk_sp<GrContext> SkiaGPUContext::GetGrContext() const
124 #endif
125 {
126     return grContext_;
127 }
128 #ifdef NEW_SKIA
SetGrContext(const sk_sp<GrDirectContext> & grContext)129 void SkiaGPUContext::SetGrContext(const sk_sp<GrDirectContext>& grContext)
130 #else
131 void SkiaGPUContext::SetGrContext(const sk_sp<GrContext>& grContext)
132 #endif
133 {
134     grContext_ = grContext;
135 }
136 } // namespace Drawing
137 } // namespace Rosen
138 } // namespace OHOS
139