blob: 2c3a221b0e20235b9659950192cf6f44baf1328e [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 Phillipsd464feb2020-10-08 11:00:02 -040016#include "src/gpu/GrThreadSafeCache.h"
Robert Phillipsae67c522021-03-03 11:03:38 -050017#include "src/gpu/GrThreadSafePipelineBuilder.h"
Robert Phillips7f11fb52019-12-03 13:35:19 -050018#include "src/gpu/effects/GrSkSLFP.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/image/SkSurface_Gpu.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050020
Robert Phillips3cd54322019-07-10 09:28:59 -040021#ifdef SK_VULKAN
22#include "src/gpu/vk/GrVkCaps.h"
23#endif
24
Adlai Hollere219d1c2020-06-02 11:23:16 -040025static int32_t next_id() {
26 static std::atomic<int32_t> nextID{1};
27 int32_t id;
28 do {
Adlai Holler4888cda2020-11-06 16:37:37 -050029 id = nextID.fetch_add(1, std::memory_order_relaxed);
Adlai Hollere219d1c2020-06-02 11:23:16 -040030 } while (id == SK_InvalidGenID);
31 return id;
32}
33
Robert Phillipsbb606772019-02-04 17:50:57 -050034GrContextThreadSafeProxy::GrContextThreadSafeProxy(GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -040035 const GrContextOptions& options)
36 : fBackend(backend), fOptions(options), fContextID(next_id()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050037}
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050038
39GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default;
40
Robert Phillipsae67c522021-03-03 11:03:38 -050041void GrContextThreadSafeProxy::init(sk_sp<const GrCaps> caps,
42 sk_sp<GrThreadSafePipelineBuilder> pipelineBuilder) {
Adlai Hollere219d1c2020-06-02 11:23:16 -040043 fCaps = std::move(caps);
John Stilesfbd050b2020-08-03 13:21:46 -040044 fTextBlobCache = std::make_unique<GrTextBlobCache>(fContextID);
Robert Phillipsd464feb2020-10-08 11:00:02 -040045 fThreadSafeCache = std::make_unique<GrThreadSafeCache>();
Robert Phillipsae67c522021-03-03 11:03:38 -050046 fPipelineBuilder = std::move(pipelineBuilder);
Robert Phillipsbb606772019-02-04 17:50:57 -050047}
48
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050049SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
50 size_t cacheMaxResourceBytes,
51 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
52 int sampleCnt, GrSurfaceOrigin origin,
53 const SkSurfaceProps& surfaceProps,
Robert Phillips3cd54322019-07-10 09:28:59 -040054 bool isMipMapped, bool willUseGLFBO0, bool isTextureable,
Greg Daniel007d97d2020-11-04 10:50:39 -050055 GrProtected isProtected, bool vkRTSupportsInputAttachment,
56 bool forVulkanSecondaryCommandBuffer) {
Adlai Hollere219d1c2020-06-02 11:23:16 -040057 SkASSERT(fCaps);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050058 if (!backendFormat.isValid()) {
Robert Phillipsdc369702020-08-13 13:34:27 -040059 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050060 }
61
Robert Phillipsc046ff02019-07-01 10:34:03 -040062 SkASSERT(isTextureable || !isMipMapped);
63
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050064 if (GrBackendApi::kOpenGL != backendFormat.backend() && willUseGLFBO0) {
65 // The willUseGLFBO0 flags can only be used for a GL backend.
Robert Phillipsdc369702020-08-13 13:34:27 -040066 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050067 }
68
Greg Daniel007d97d2020-11-04 10:50:39 -050069 if (GrBackendApi::kVulkan != backendFormat.backend() &&
70 (vkRTSupportsInputAttachment || forVulkanSecondaryCommandBuffer)) {
71 // The vkRTSupportsInputAttachment and forVulkanSecondaryCommandBuffer flags can only be
72 // used for a Vulkan backend.
Greg Daniel638b2e82020-08-27 14:29:00 -040073 return {};
74 }
75
Brian Salomon69100f02020-07-21 10:49:25 -040076 if (!fCaps->mipmapSupport()) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050077 isMipMapped = false;
78 }
79
Robert Phillipsdc369702020-08-13 13:34:27 -040080 if (ii.width() < 1 || ii.width() > fCaps->maxRenderTargetSize() ||
81 ii.height() < 1 || ii.height() > fCaps->maxRenderTargetSize()) {
82 return {};
83 }
84
Greg Daniel2f2caea2019-07-08 14:24:47 -040085 GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
Robert Phillipsb2adbef2019-07-02 16:33:05 -040086
Adlai Hollere219d1c2020-06-02 11:23:16 -040087 if (!fCaps->areColorTypeAndFormatCompatible(grColorType, backendFormat)) {
Robert Phillipsdc369702020-08-13 13:34:27 -040088 return {};
Robert Phillipsb2adbef2019-07-02 16:33:05 -040089 }
90
Adlai Hollere219d1c2020-06-02 11:23:16 -040091 if (!fCaps->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) {
Robert Phillipsdc369702020-08-13 13:34:27 -040092 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050093 }
94
Adlai Hollere219d1c2020-06-02 11:23:16 -040095 sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, backendFormat);
Greg Daniel6fa62e22019-08-07 15:52:37 -040096 SkASSERT(sampleCnt);
97
Robert Phillipsb45f47d2019-02-03 17:17:54 -050098 if (willUseGLFBO0 && isTextureable) {
Robert Phillipsdc369702020-08-13 13:34:27 -040099 return {};
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500100 }
101
Adlai Hollere219d1c2020-06-02 11:23:16 -0400102 if (isTextureable && !fCaps->isFormatTexturable(backendFormat)) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500103 // Skia doesn't agree that this is textureable.
Robert Phillipsdc369702020-08-13 13:34:27 -0400104 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500105 }
106
Greg Daniel007d97d2020-11-04 10:50:39 -0500107 if (forVulkanSecondaryCommandBuffer &&
108 (isTextureable || isMipMapped || willUseGLFBO0 || vkRTSupportsInputAttachment)) {
109 return {};
110 }
111
Robert Phillips3cd54322019-07-10 09:28:59 -0400112 if (GrBackendApi::kVulkan == backendFormat.backend()) {
Adlai Hollere219d1c2020-06-02 11:23:16 -0400113 if (GrBackendApi::kVulkan != fBackend) {
Robert Phillipsdc369702020-08-13 13:34:27 -0400114 return {};
Robert Phillips3cd54322019-07-10 09:28:59 -0400115 }
116
117#ifdef SK_VULKAN
Adlai Hollere219d1c2020-06-02 11:23:16 -0400118 const GrVkCaps* vkCaps = (const GrVkCaps*) fCaps.get();
Robert Phillips3cd54322019-07-10 09:28:59 -0400119
120 // The protection status of the characterization and the context need to match
121 if (isProtected != GrProtected(vkCaps->supportsProtectedMemory())) {
Robert Phillipsdc369702020-08-13 13:34:27 -0400122 return {};
Robert Phillips3cd54322019-07-10 09:28:59 -0400123 }
124#endif
125 }
126
Greg Daniel638b2e82020-08-27 14:29:00 -0400127 return SkSurfaceCharacterization(
128 sk_ref_sp<GrContextThreadSafeProxy>(this),
129 cacheMaxResourceBytes, ii, backendFormat,
130 origin, sampleCnt,
131 SkSurfaceCharacterization::Textureable(isTextureable),
132 SkSurfaceCharacterization::MipMapped(isMipMapped),
133 SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
134 SkSurfaceCharacterization::VkRTSupportsInputAttachment(vkRTSupportsInputAttachment),
Greg Daniel007d97d2020-11-04 10:50:39 -0500135 SkSurfaceCharacterization::VulkanSecondaryCBCompatible(forVulkanSecondaryCommandBuffer),
Greg Daniel638b2e82020-08-27 14:29:00 -0400136 isProtected,
137 surfaceProps);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500138}
139
Adlai Hollere219d1c2020-06-02 11:23:16 -0400140GrBackendFormat GrContextThreadSafeProxy::defaultBackendFormat(SkColorType skColorType,
141 GrRenderable renderable) const {
142 SkASSERT(fCaps);
143 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
144
145 GrBackendFormat format = fCaps->getDefaultBackendFormat(grColorType, renderable);
146 if (!format.isValid()) {
147 return GrBackendFormat();
148 }
149
150 SkASSERT(renderable == GrRenderable::kNo ||
151 fCaps->isFormatAsColorTypeRenderable(grColorType, format));
152
153 return format;
154}
155
Adlai Holler43b15792020-06-01 10:11:49 -0400156void GrContextThreadSafeProxy::abandonContext() {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400157 if (!fAbandoned.exchange(true)) {
158 fTextBlobCache->freeAll();
159 }
Adlai Holler43b15792020-06-01 10:11:49 -0400160}
161
162bool GrContextThreadSafeProxy::abandoned() const {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400163 return fAbandoned;
Adlai Holler43b15792020-06-01 10:11:49 -0400164}
165
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500166////////////////////////////////////////////////////////////////////////////////
Robert Phillipsbb606772019-02-04 17:50:57 -0500167sk_sp<GrContextThreadSafeProxy> GrContextThreadSafeProxyPriv::Make(
168 GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -0400169 const GrContextOptions& options) {
170 return sk_sp<GrContextThreadSafeProxy>(new GrContextThreadSafeProxy(backend, options));
Robert Phillipsbb606772019-02-04 17:50:57 -0500171}
172
Robert Phillipsae67c522021-03-03 11:03:38 -0500173void GrContextThreadSafeProxyPriv::init(sk_sp<const GrCaps> caps,
174 sk_sp<GrThreadSafePipelineBuilder> builder) const {
175 fProxy->init(std::move(caps), std::move(builder));
176}
177