1 /*
2  * Copyright 2019 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/gpu/GrContextThreadSafeProxy.h"
9 #include "src/gpu/GrContextThreadSafeProxyPriv.h"
10 
11 #include "include/core/SkSurfaceCharacterization.h"
12 #include "include/gpu/GrContext.h"
13 #include "src/gpu/GrBaseContextPriv.h"
14 #include "src/gpu/GrCaps.h"
15 #include "src/gpu/GrSkSLFPFactoryCache.h"
16 #include "src/image/SkSurface_Gpu.h"
17 
18 #ifdef SK_VULKAN
19 #include "src/gpu/vk/GrVkCaps.h"
20 #endif
21 
GrContextThreadSafeProxy(GrBackendApi backend,const GrContextOptions & options,uint32_t contextID)22 GrContextThreadSafeProxy::GrContextThreadSafeProxy(GrBackendApi backend,
23                                                    const GrContextOptions& options,
24                                                    uint32_t contextID)
25         : INHERITED(backend, options, contextID) {
26 }
27 
28 GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default;
29 
init(sk_sp<const GrCaps> caps,sk_sp<GrSkSLFPFactoryCache> FPFactoryCache)30 bool GrContextThreadSafeProxy::init(sk_sp<const GrCaps> caps,
31                                     sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) {
32     return INHERITED::init(std::move(caps), std::move(FPFactoryCache));
33 }
34 
createCharacterization(size_t cacheMaxResourceBytes,const SkImageInfo & ii,const GrBackendFormat & backendFormat,int sampleCnt,GrSurfaceOrigin origin,const SkSurfaceProps & surfaceProps,bool isMipMapped,bool willUseGLFBO0,bool isTextureable,GrProtected isProtected)35 SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
36                                      size_t cacheMaxResourceBytes,
37                                      const SkImageInfo& ii, const GrBackendFormat& backendFormat,
38                                      int sampleCnt, GrSurfaceOrigin origin,
39                                      const SkSurfaceProps& surfaceProps,
40                                      bool isMipMapped, bool willUseGLFBO0, bool isTextureable,
41                                      GrProtected isProtected) {
42     if (!backendFormat.isValid()) {
43         return SkSurfaceCharacterization(); // return an invalid characterization
44     }
45 
46     SkASSERT(isTextureable || !isMipMapped);
47 
48     if (GrBackendApi::kOpenGL != backendFormat.backend() && willUseGLFBO0) {
49         // The willUseGLFBO0 flags can only be used for a GL backend.
50         return SkSurfaceCharacterization(); // return an invalid characterization
51     }
52 
53     if (!this->caps()->mipMapSupport()) {
54         isMipMapped = false;
55     }
56 
57     GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
58 
59     if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, backendFormat)) {
60         return SkSurfaceCharacterization(); // return an invalid characterization
61     }
62 
63     if (!this->caps()->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) {
64         return SkSurfaceCharacterization(); // return an invalid characterization
65     }
66 
67     sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendFormat);
68     SkASSERT(sampleCnt);
69 
70     if (willUseGLFBO0 && isTextureable) {
71         return SkSurfaceCharacterization(); // return an invalid characterization
72     }
73 
74     if (isTextureable && !this->caps()->isFormatTexturable(backendFormat)) {
75         // Skia doesn't agree that this is textureable.
76         return SkSurfaceCharacterization(); // return an invalid characterization
77     }
78 
79     if (GrBackendApi::kVulkan == backendFormat.backend()) {
80         if (GrBackendApi::kVulkan != this->backend()) {
81             return SkSurfaceCharacterization(); // return an invalid characterization
82         }
83 
84 #ifdef SK_VULKAN
85         const GrVkCaps* vkCaps = (const GrVkCaps*) this->caps();
86 
87         // The protection status of the characterization and the context need to match
88         if (isProtected != GrProtected(vkCaps->supportsProtectedMemory())) {
89             return SkSurfaceCharacterization(); // return an invalid characterization
90         }
91 #endif
92     }
93 
94     return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
95                                      cacheMaxResourceBytes, ii, backendFormat,
96                                      origin, sampleCnt,
97                                      SkSurfaceCharacterization::Textureable(isTextureable),
98                                      SkSurfaceCharacterization::MipMapped(isMipMapped),
99                                      SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
100                                      SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false),
101                                      isProtected,
102                                      surfaceProps);
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
fpFactoryCache()106 sk_sp<GrSkSLFPFactoryCache> GrContextThreadSafeProxyPriv::fpFactoryCache() {
107     return fProxy->fpFactoryCache();
108 }
109 
Make(GrBackendApi backend,const GrContextOptions & options,uint32_t contextID,sk_sp<const GrCaps> caps,sk_sp<GrSkSLFPFactoryCache> cache)110 sk_sp<GrContextThreadSafeProxy> GrContextThreadSafeProxyPriv::Make(
111                              GrBackendApi backend,
112                              const GrContextOptions& options,
113                              uint32_t contextID,
114                              sk_sp<const GrCaps> caps,
115                              sk_sp<GrSkSLFPFactoryCache> cache) {
116     sk_sp<GrContextThreadSafeProxy> proxy(new GrContextThreadSafeProxy(backend, options,
117                                                                        contextID));
118 
119     if (!proxy->init(std::move(caps), std::move(cache))) {
120         return nullptr;
121     }
122     return proxy;
123 }
124 
125