blob: 16b184769e0a1a0202981150a2bc020d8d99c842 [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 Phillips7f11fb52019-12-03 13:35:19 -050017#include "src/gpu/effects/GrSkSLFP.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/image/SkSurface_Gpu.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050019
Robert Phillips3cd54322019-07-10 09:28:59 -040020#ifdef SK_VULKAN
21#include "src/gpu/vk/GrVkCaps.h"
22#endif
23
Adlai Hollere219d1c2020-06-02 11:23:16 -040024static int32_t next_id() {
25 static std::atomic<int32_t> nextID{1};
26 int32_t id;
27 do {
Adlai Holler4888cda2020-11-06 16:37:37 -050028 id = nextID.fetch_add(1, std::memory_order_relaxed);
Adlai Hollere219d1c2020-06-02 11:23:16 -040029 } while (id == SK_InvalidGenID);
30 return id;
31}
32
Robert Phillipsbb606772019-02-04 17:50:57 -050033GrContextThreadSafeProxy::GrContextThreadSafeProxy(GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -040034 const GrContextOptions& options)
35 : fBackend(backend), fOptions(options), fContextID(next_id()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050036}
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050037
38GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default;
39
Adlai Hollere219d1c2020-06-02 11:23:16 -040040void GrContextThreadSafeProxy::init(sk_sp<const GrCaps> caps) {
41 fCaps = std::move(caps);
John Stilesfbd050b2020-08-03 13:21:46 -040042 fTextBlobCache = std::make_unique<GrTextBlobCache>(fContextID);
Robert Phillipsd464feb2020-10-08 11:00:02 -040043 fThreadSafeCache = std::make_unique<GrThreadSafeCache>();
Robert Phillipsbb606772019-02-04 17:50:57 -050044}
45
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050046SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
47 size_t cacheMaxResourceBytes,
48 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
49 int sampleCnt, GrSurfaceOrigin origin,
50 const SkSurfaceProps& surfaceProps,
Robert Phillips3cd54322019-07-10 09:28:59 -040051 bool isMipMapped, bool willUseGLFBO0, bool isTextureable,
Greg Daniel007d97d2020-11-04 10:50:39 -050052 GrProtected isProtected, bool vkRTSupportsInputAttachment,
53 bool forVulkanSecondaryCommandBuffer) {
Adlai Hollere219d1c2020-06-02 11:23:16 -040054 SkASSERT(fCaps);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050055 if (!backendFormat.isValid()) {
Robert Phillipsdc369702020-08-13 13:34:27 -040056 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050057 }
58
Robert Phillipsc046ff02019-07-01 10:34:03 -040059 SkASSERT(isTextureable || !isMipMapped);
60
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050061 if (GrBackendApi::kOpenGL != backendFormat.backend() && willUseGLFBO0) {
62 // The willUseGLFBO0 flags can only be used for a GL backend.
Robert Phillipsdc369702020-08-13 13:34:27 -040063 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050064 }
65
Greg Daniel007d97d2020-11-04 10:50:39 -050066 if (GrBackendApi::kVulkan != backendFormat.backend() &&
67 (vkRTSupportsInputAttachment || forVulkanSecondaryCommandBuffer)) {
68 // The vkRTSupportsInputAttachment and forVulkanSecondaryCommandBuffer flags can only be
69 // used for a Vulkan backend.
Greg Daniel638b2e82020-08-27 14:29:00 -040070 return {};
71 }
72
Brian Salomon69100f02020-07-21 10:49:25 -040073 if (!fCaps->mipmapSupport()) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050074 isMipMapped = false;
75 }
76
Robert Phillipsdc369702020-08-13 13:34:27 -040077 if (ii.width() < 1 || ii.width() > fCaps->maxRenderTargetSize() ||
78 ii.height() < 1 || ii.height() > fCaps->maxRenderTargetSize()) {
79 return {};
80 }
81
Greg Daniel2f2caea2019-07-08 14:24:47 -040082 GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
Robert Phillipsb2adbef2019-07-02 16:33:05 -040083
Adlai Hollere219d1c2020-06-02 11:23:16 -040084 if (!fCaps->areColorTypeAndFormatCompatible(grColorType, backendFormat)) {
Robert Phillipsdc369702020-08-13 13:34:27 -040085 return {};
Robert Phillipsb2adbef2019-07-02 16:33:05 -040086 }
87
Adlai Hollere219d1c2020-06-02 11:23:16 -040088 if (!fCaps->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) {
Robert Phillipsdc369702020-08-13 13:34:27 -040089 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050090 }
91
Adlai Hollere219d1c2020-06-02 11:23:16 -040092 sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, backendFormat);
Greg Daniel6fa62e22019-08-07 15:52:37 -040093 SkASSERT(sampleCnt);
94
Robert Phillipsb45f47d2019-02-03 17:17:54 -050095 if (willUseGLFBO0 && isTextureable) {
Robert Phillipsdc369702020-08-13 13:34:27 -040096 return {};
Robert Phillipsb45f47d2019-02-03 17:17:54 -050097 }
98
Adlai Hollere219d1c2020-06-02 11:23:16 -040099 if (isTextureable && !fCaps->isFormatTexturable(backendFormat)) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500100 // Skia doesn't agree that this is textureable.
Robert Phillipsdc369702020-08-13 13:34:27 -0400101 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500102 }
103
Greg Daniel007d97d2020-11-04 10:50:39 -0500104 if (forVulkanSecondaryCommandBuffer &&
105 (isTextureable || isMipMapped || willUseGLFBO0 || vkRTSupportsInputAttachment)) {
106 return {};
107 }
108
Robert Phillips3cd54322019-07-10 09:28:59 -0400109 if (GrBackendApi::kVulkan == backendFormat.backend()) {
Adlai Hollere219d1c2020-06-02 11:23:16 -0400110 if (GrBackendApi::kVulkan != fBackend) {
Robert Phillipsdc369702020-08-13 13:34:27 -0400111 return {};
Robert Phillips3cd54322019-07-10 09:28:59 -0400112 }
113
114#ifdef SK_VULKAN
Adlai Hollere219d1c2020-06-02 11:23:16 -0400115 const GrVkCaps* vkCaps = (const GrVkCaps*) fCaps.get();
Robert Phillips3cd54322019-07-10 09:28:59 -0400116
117 // The protection status of the characterization and the context need to match
118 if (isProtected != GrProtected(vkCaps->supportsProtectedMemory())) {
Robert Phillipsdc369702020-08-13 13:34:27 -0400119 return {};
Robert Phillips3cd54322019-07-10 09:28:59 -0400120 }
121#endif
122 }
123
Greg Daniel638b2e82020-08-27 14:29:00 -0400124 return SkSurfaceCharacterization(
125 sk_ref_sp<GrContextThreadSafeProxy>(this),
126 cacheMaxResourceBytes, ii, backendFormat,
127 origin, sampleCnt,
128 SkSurfaceCharacterization::Textureable(isTextureable),
129 SkSurfaceCharacterization::MipMapped(isMipMapped),
130 SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
131 SkSurfaceCharacterization::VkRTSupportsInputAttachment(vkRTSupportsInputAttachment),
Greg Daniel007d97d2020-11-04 10:50:39 -0500132 SkSurfaceCharacterization::VulkanSecondaryCBCompatible(forVulkanSecondaryCommandBuffer),
Greg Daniel638b2e82020-08-27 14:29:00 -0400133 isProtected,
134 surfaceProps);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500135}
136
Adlai Hollere219d1c2020-06-02 11:23:16 -0400137GrBackendFormat GrContextThreadSafeProxy::defaultBackendFormat(SkColorType skColorType,
138 GrRenderable renderable) const {
139 SkASSERT(fCaps);
140 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
141
142 GrBackendFormat format = fCaps->getDefaultBackendFormat(grColorType, renderable);
143 if (!format.isValid()) {
144 return GrBackendFormat();
145 }
146
147 SkASSERT(renderable == GrRenderable::kNo ||
148 fCaps->isFormatAsColorTypeRenderable(grColorType, format));
149
150 return format;
151}
152
Adlai Holler43b15792020-06-01 10:11:49 -0400153void GrContextThreadSafeProxy::abandonContext() {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400154 if (!fAbandoned.exchange(true)) {
155 fTextBlobCache->freeAll();
156 }
Adlai Holler43b15792020-06-01 10:11:49 -0400157}
158
159bool GrContextThreadSafeProxy::abandoned() const {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400160 return fAbandoned;
Adlai Holler43b15792020-06-01 10:11:49 -0400161}
162
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500163////////////////////////////////////////////////////////////////////////////////
Robert Phillipsbb606772019-02-04 17:50:57 -0500164sk_sp<GrContextThreadSafeProxy> GrContextThreadSafeProxyPriv::Make(
165 GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -0400166 const GrContextOptions& options) {
167 return sk_sp<GrContextThreadSafeProxy>(new GrContextThreadSafeProxy(backend, options));
Robert Phillipsbb606772019-02-04 17:50:57 -0500168}
169