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 if (!SkSurface_Gpu::Valid(this->caps(), backendFormat)) {
58 return SkSurfaceCharacterization(); // return an invalid characterization
59 }
60
61 GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
62
63 if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, backendFormat)) {
64 return SkSurfaceCharacterization(); // return an invalid characterization
65 }
66
67 if (!this->caps()->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) {
68 return SkSurfaceCharacterization(); // return an invalid characterization
69 }
70
71 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendFormat);
72 SkASSERT(sampleCnt);
73
74 if (willUseGLFBO0 && isTextureable) {
75 return SkSurfaceCharacterization(); // return an invalid characterization
76 }
77
78 if (isTextureable && !this->caps()->isFormatTexturable(backendFormat)) {
79 // Skia doesn't agree that this is textureable.
80 return SkSurfaceCharacterization(); // return an invalid characterization
81 }
82
83 if (GrBackendApi::kVulkan == backendFormat.backend()) {
84 if (GrBackendApi::kVulkan != this->backend()) {
85 return SkSurfaceCharacterization(); // return an invalid characterization
86 }
87
88 #ifdef SK_VULKAN
89 const GrVkCaps* vkCaps = (const GrVkCaps*) this->caps();
90
91 // The protection status of the characterization and the context need to match
92 if (isProtected != GrProtected(vkCaps->supportsProtectedMemory())) {
93 return SkSurfaceCharacterization(); // return an invalid characterization
94 }
95 #endif
96 }
97
98 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
99 cacheMaxResourceBytes, ii, backendFormat,
100 origin, sampleCnt,
101 SkSurfaceCharacterization::Textureable(isTextureable),
102 SkSurfaceCharacterization::MipMapped(isMipMapped),
103 SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
104 SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false),
105 isProtected,
106 surfaceProps);
107 }
108
109 ////////////////////////////////////////////////////////////////////////////////
fpFactoryCache()110 sk_sp<GrSkSLFPFactoryCache> GrContextThreadSafeProxyPriv::fpFactoryCache() {
111 return fProxy->fpFactoryCache();
112 }
113
Make(GrBackendApi backend,const GrContextOptions & options,uint32_t contextID,sk_sp<const GrCaps> caps,sk_sp<GrSkSLFPFactoryCache> cache)114 sk_sp<GrContextThreadSafeProxy> GrContextThreadSafeProxyPriv::Make(
115 GrBackendApi backend,
116 const GrContextOptions& options,
117 uint32_t contextID,
118 sk_sp<const GrCaps> caps,
119 sk_sp<GrSkSLFPFactoryCache> cache) {
120 sk_sp<GrContextThreadSafeProxy> proxy(new GrContextThreadSafeProxy(backend, options,
121 contextID));
122
123 if (!proxy->init(std::move(caps), std::move(cache))) {
124 return nullptr;
125 }
126 return proxy;
127 }
128
129