blob: 7de217059985618ec6a1ad7fda57e293160956c7 [file] [log] [blame]
Robert Phillipsa0bc39d2019-01-29 13:14:47 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrContextThreadSafeProxy.h"
9#include "src/gpu/GrContextThreadSafeProxyPriv.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkSurfaceCharacterization.h"
12#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrBaseContextPriv.h"
14#include "src/gpu/GrCaps.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrSkSLFPFactoryCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/image/SkSurface_Gpu.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050017
Robert Phillips3cd54322019-07-10 09:28:59 -040018#ifdef SK_VULKAN
19#include "src/gpu/vk/GrVkCaps.h"
20#endif
21
Robert Phillipsbb606772019-02-04 17:50:57 -050022GrContextThreadSafeProxy::GrContextThreadSafeProxy(GrBackendApi backend,
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050023 const GrContextOptions& options,
Robert Phillipsbb606772019-02-04 17:50:57 -050024 uint32_t contextID)
25 : INHERITED(backend, options, contextID) {
26}
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050027
28GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default;
29
Robert Phillipsbb606772019-02-04 17:50:57 -050030bool GrContextThreadSafeProxy::init(sk_sp<const GrCaps> caps,
31 sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) {
32 return INHERITED::init(std::move(caps), std::move(FPFactoryCache));
33}
34
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050035SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
36 size_t cacheMaxResourceBytes,
37 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
38 int sampleCnt, GrSurfaceOrigin origin,
39 const SkSurfaceProps& surfaceProps,
Robert Phillips3cd54322019-07-10 09:28:59 -040040 bool isMipMapped, bool willUseGLFBO0, bool isTextureable,
41 GrProtected isProtected) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050042 if (!backendFormat.isValid()) {
43 return SkSurfaceCharacterization(); // return an invalid characterization
44 }
45
Robert Phillipsc046ff02019-07-01 10:34:03 -040046 SkASSERT(isTextureable || !isMipMapped);
47
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050048 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
Robert Phillipsbb606772019-02-04 17:50:57 -050053 if (!this->caps()->mipMapSupport()) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050054 isMipMapped = false;
55 }
56
Robert Phillipsf209e882019-06-25 15:59:50 -040057 if (!SkSurface_Gpu::Valid(this->caps(), backendFormat)) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050058 return SkSurfaceCharacterization(); // return an invalid characterization
59 }
60
Greg Daniel2f2caea2019-07-08 14:24:47 -040061 GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
Robert Phillipsb2adbef2019-07-02 16:33:05 -040062
Greg Daniel627d0532019-07-08 16:48:14 -040063 if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, backendFormat)) {
Robert Phillipsb2adbef2019-07-02 16:33:05 -040064 return SkSurfaceCharacterization(); // return an invalid characterization
65 }
66
Greg Daniel6fa62e22019-08-07 15:52:37 -040067 if (!this->caps()->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050068 return SkSurfaceCharacterization(); // return an invalid characterization
69 }
70
Greg Daniel6fa62e22019-08-07 15:52:37 -040071 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendFormat);
72 SkASSERT(sampleCnt);
73
Robert Phillipsb45f47d2019-02-03 17:17:54 -050074 if (willUseGLFBO0 && isTextureable) {
75 return SkSurfaceCharacterization(); // return an invalid characterization
76 }
77
Greg Daniel7bfc9132019-08-14 14:23:53 -040078 if (isTextureable && !this->caps()->isFormatTexturable(backendFormat)) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -050079 // Skia doesn't agree that this is textureable.
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050080 return SkSurfaceCharacterization(); // return an invalid characterization
81 }
82
Robert Phillips3cd54322019-07-10 09:28:59 -040083 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
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050098 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
Robert Phillipsb2adbef2019-07-02 16:33:05 -040099 cacheMaxResourceBytes, ii, backendFormat,
100 origin, sampleCnt,
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500101 SkSurfaceCharacterization::Textureable(isTextureable),
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500102 SkSurfaceCharacterization::MipMapped(isMipMapped),
103 SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
104 SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false),
Robert Phillips3cd54322019-07-10 09:28:59 -0400105 isProtected,
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500106 surfaceProps);
107}
108
109////////////////////////////////////////////////////////////////////////////////
Robert Phillipsbb606772019-02-04 17:50:57 -0500110sk_sp<GrSkSLFPFactoryCache> GrContextThreadSafeProxyPriv::fpFactoryCache() {
111 return fProxy->fpFactoryCache();
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500112}
Robert Phillipsbb606772019-02-04 17:50:57 -0500113
114sk_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