blob: 2f1ac1e053f2e1f9d62ad27c1968f91bc4a3eb2e [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
John Stilesfbd050b2020-08-03 13:21:46 -04008#include <memory>
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrContextThreadSafeProxy.h"
11#include "src/gpu/GrContextThreadSafeProxyPriv.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050012
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkSurfaceCharacterization.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrBaseContextPriv.h"
15#include "src/gpu/GrCaps.h"
Robert Phillips7f11fb52019-12-03 13:35:19 -050016#include "src/gpu/effects/GrSkSLFP.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/image/SkSurface_Gpu.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050018
Robert Phillips3cd54322019-07-10 09:28:59 -040019#ifdef SK_VULKAN
20#include "src/gpu/vk/GrVkCaps.h"
21#endif
22
Adlai Hollere219d1c2020-06-02 11:23:16 -040023static int32_t next_id() {
24 static std::atomic<int32_t> nextID{1};
25 int32_t id;
26 do {
27 id = nextID++;
28 } while (id == SK_InvalidGenID);
29 return id;
30}
31
Robert Phillipsbb606772019-02-04 17:50:57 -050032GrContextThreadSafeProxy::GrContextThreadSafeProxy(GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -040033 const GrContextOptions& options)
34 : fBackend(backend), fOptions(options), fContextID(next_id()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050035}
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050036
37GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default;
38
Adlai Hollere219d1c2020-06-02 11:23:16 -040039void GrContextThreadSafeProxy::init(sk_sp<const GrCaps> caps) {
40 fCaps = std::move(caps);
John Stilesfbd050b2020-08-03 13:21:46 -040041 fTextBlobCache = std::make_unique<GrTextBlobCache>(fContextID);
Robert Phillipsbb606772019-02-04 17:50:57 -050042}
43
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050044SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
45 size_t cacheMaxResourceBytes,
46 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
47 int sampleCnt, GrSurfaceOrigin origin,
48 const SkSurfaceProps& surfaceProps,
Robert Phillips3cd54322019-07-10 09:28:59 -040049 bool isMipMapped, bool willUseGLFBO0, bool isTextureable,
Greg Daniel638b2e82020-08-27 14:29:00 -040050 GrProtected isProtected, bool vkRTSupportsInputAttachment) {
Adlai Hollere219d1c2020-06-02 11:23:16 -040051 SkASSERT(fCaps);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050052 if (!backendFormat.isValid()) {
Robert Phillipsdc369702020-08-13 13:34:27 -040053 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050054 }
55
Robert Phillipsc046ff02019-07-01 10:34:03 -040056 SkASSERT(isTextureable || !isMipMapped);
57
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050058 if (GrBackendApi::kOpenGL != backendFormat.backend() && willUseGLFBO0) {
59 // The willUseGLFBO0 flags can only be used for a GL backend.
Robert Phillipsdc369702020-08-13 13:34:27 -040060 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050061 }
62
Greg Daniel638b2e82020-08-27 14:29:00 -040063 if (GrBackendApi::kVulkan != backendFormat.backend() && vkRTSupportsInputAttachment) {
64 // The vkRTSupportsInputAttachment flags can only be used for a Vulkan backend.
65 return {};
66 }
67
Brian Salomon69100f02020-07-21 10:49:25 -040068 if (!fCaps->mipmapSupport()) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050069 isMipMapped = false;
70 }
71
Robert Phillipsdc369702020-08-13 13:34:27 -040072 if (ii.width() < 1 || ii.width() > fCaps->maxRenderTargetSize() ||
73 ii.height() < 1 || ii.height() > fCaps->maxRenderTargetSize()) {
74 return {};
75 }
76
Greg Daniel2f2caea2019-07-08 14:24:47 -040077 GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
Robert Phillipsb2adbef2019-07-02 16:33:05 -040078
Adlai Hollere219d1c2020-06-02 11:23:16 -040079 if (!fCaps->areColorTypeAndFormatCompatible(grColorType, backendFormat)) {
Robert Phillipsdc369702020-08-13 13:34:27 -040080 return {};
Robert Phillipsb2adbef2019-07-02 16:33:05 -040081 }
82
Adlai Hollere219d1c2020-06-02 11:23:16 -040083 if (!fCaps->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) {
Robert Phillipsdc369702020-08-13 13:34:27 -040084 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050085 }
86
Adlai Hollere219d1c2020-06-02 11:23:16 -040087 sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, backendFormat);
Greg Daniel6fa62e22019-08-07 15:52:37 -040088 SkASSERT(sampleCnt);
89
Robert Phillipsb45f47d2019-02-03 17:17:54 -050090 if (willUseGLFBO0 && isTextureable) {
Robert Phillipsdc369702020-08-13 13:34:27 -040091 return {};
Robert Phillipsb45f47d2019-02-03 17:17:54 -050092 }
93
Adlai Hollere219d1c2020-06-02 11:23:16 -040094 if (isTextureable && !fCaps->isFormatTexturable(backendFormat)) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -050095 // Skia doesn't agree that this is textureable.
Robert Phillipsdc369702020-08-13 13:34:27 -040096 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050097 }
98
Robert Phillips3cd54322019-07-10 09:28:59 -040099 if (GrBackendApi::kVulkan == backendFormat.backend()) {
Adlai Hollere219d1c2020-06-02 11:23:16 -0400100 if (GrBackendApi::kVulkan != fBackend) {
Robert Phillipsdc369702020-08-13 13:34:27 -0400101 return {};
Robert Phillips3cd54322019-07-10 09:28:59 -0400102 }
103
104#ifdef SK_VULKAN
Adlai Hollere219d1c2020-06-02 11:23:16 -0400105 const GrVkCaps* vkCaps = (const GrVkCaps*) fCaps.get();
Robert Phillips3cd54322019-07-10 09:28:59 -0400106
107 // The protection status of the characterization and the context need to match
108 if (isProtected != GrProtected(vkCaps->supportsProtectedMemory())) {
Robert Phillipsdc369702020-08-13 13:34:27 -0400109 return {};
Robert Phillips3cd54322019-07-10 09:28:59 -0400110 }
111#endif
112 }
113
Greg Daniel638b2e82020-08-27 14:29:00 -0400114 return SkSurfaceCharacterization(
115 sk_ref_sp<GrContextThreadSafeProxy>(this),
116 cacheMaxResourceBytes, ii, backendFormat,
117 origin, sampleCnt,
118 SkSurfaceCharacterization::Textureable(isTextureable),
119 SkSurfaceCharacterization::MipMapped(isMipMapped),
120 SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
121 SkSurfaceCharacterization::VkRTSupportsInputAttachment(vkRTSupportsInputAttachment),
122 SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false),
123 isProtected,
124 surfaceProps);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500125}
126
Adlai Hollere219d1c2020-06-02 11:23:16 -0400127GrBackendFormat GrContextThreadSafeProxy::defaultBackendFormat(SkColorType skColorType,
128 GrRenderable renderable) const {
129 SkASSERT(fCaps);
130 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
131
132 GrBackendFormat format = fCaps->getDefaultBackendFormat(grColorType, renderable);
133 if (!format.isValid()) {
134 return GrBackendFormat();
135 }
136
137 SkASSERT(renderable == GrRenderable::kNo ||
138 fCaps->isFormatAsColorTypeRenderable(grColorType, format));
139
140 return format;
141}
142
Adlai Holler43b15792020-06-01 10:11:49 -0400143void GrContextThreadSafeProxy::abandonContext() {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400144 if (!fAbandoned.exchange(true)) {
145 fTextBlobCache->freeAll();
146 }
Adlai Holler43b15792020-06-01 10:11:49 -0400147}
148
149bool GrContextThreadSafeProxy::abandoned() const {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400150 return fAbandoned;
Adlai Holler43b15792020-06-01 10:11:49 -0400151}
152
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500153////////////////////////////////////////////////////////////////////////////////
Robert Phillipsbb606772019-02-04 17:50:57 -0500154sk_sp<GrContextThreadSafeProxy> GrContextThreadSafeProxyPriv::Make(
155 GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -0400156 const GrContextOptions& options) {
157 return sk_sp<GrContextThreadSafeProxy>(new GrContextThreadSafeProxy(backend, options));
Robert Phillipsbb606772019-02-04 17:50:57 -0500158}
159