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 "GrContextThreadSafeProxy.h"
9 #include "GrContextThreadSafeProxyPriv.h"
10 
11 #include "GrBaseContextPriv.h"
12 #include "GrCaps.h"
13 #include "GrContext.h"
14 #include "GrSkSLFPFactoryCache.h"
15 #include "SkSurface_Gpu.h"
16 #include "SkSurfaceCharacterization.h"
17 
18 GrContextThreadSafeProxy::GrContextThreadSafeProxy(sk_sp<const GrCaps> caps, uint32_t contextID,
19                                                    GrBackendApi backend,
20                                                    const GrContextOptions& options,
21                                                    sk_sp<GrSkSLFPFactoryCache> cache)
22         : fCaps(std::move(caps))
23         , fContextID(contextID)
24         , fBackend(backend)
25         , fOptions(options)
26         , fFPFactoryCache(std::move(cache)) {}
27 
28 GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default;
29 
30 bool GrContextThreadSafeProxy::matches(GrContext_Base* context) const {
31     return context->priv().contextID() == fContextID;
32 }
33 
34 SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
35                                      size_t cacheMaxResourceBytes,
36                                      const SkImageInfo& ii, const GrBackendFormat& backendFormat,
37                                      int sampleCnt, GrSurfaceOrigin origin,
38                                      const SkSurfaceProps& surfaceProps,
39                                      bool isMipMapped, bool willUseGLFBO0, bool isTextureable) {
40     if (!backendFormat.isValid()) {
41         return SkSurfaceCharacterization(); // return an invalid characterization
42     }
43 
44     if (GrBackendApi::kOpenGL != backendFormat.backend() && willUseGLFBO0) {
45         // The willUseGLFBO0 flags can only be used for a GL backend.
46         return SkSurfaceCharacterization(); // return an invalid characterization
47     }
48 
49     if (!fCaps->mipMapSupport()) {
50         isMipMapped = false;
51     }
52 
53     GrPixelConfig config = fCaps->getConfigFromBackendFormat(backendFormat, ii.colorType());
54     if (config == kUnknown_GrPixelConfig) {
55         return SkSurfaceCharacterization(); // return an invalid characterization
56     }
57 
58     if (!SkSurface_Gpu::Valid(fCaps.get(), config, ii.colorSpace())) {
59         return SkSurfaceCharacterization(); // return an invalid characterization
60     }
61 
62     sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, config);
63     if (!sampleCnt) {
64         return SkSurfaceCharacterization(); // return an invalid characterization
65     }
66 
67     GrFSAAType FSAAType = GrFSAAType::kNone;
68     if (sampleCnt > 1) {
69         FSAAType = fCaps->usesMixedSamples() ? GrFSAAType::kMixedSamples : GrFSAAType::kUnifiedMSAA;
70     }
71 
72     if (willUseGLFBO0 && isTextureable) {
73         return SkSurfaceCharacterization(); // return an invalid characterization
74     }
75 
76     if (isTextureable && !fCaps->isConfigTexturable(config)) {
77         // Skia doesn't agree that this is textureable.
78         return SkSurfaceCharacterization(); // return an invalid characterization
79     }
80 
81     return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
82                                      cacheMaxResourceBytes, ii,
83                                      origin, config, FSAAType, sampleCnt,
84                                      SkSurfaceCharacterization::Textureable(isTextureable),
85                                      SkSurfaceCharacterization::MipMapped(isMipMapped),
86                                      SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
87                                      SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false),
88                                      surfaceProps);
89 }
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 sk_sp<GrSkSLFPFactoryCache> GrContextThreadSafeProxyPriv::fpFactoryCache() const {
93     return fProxy->fFPFactoryCache;
94 }
95