blob: 5ebb3062f174cc12908309da7df5ad7632ef6416 [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 Phillips12d06a32020-09-16 12:31:34 -040016#include "src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.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 {
28 id = nextID++;
29 } 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 Phillips12d06a32020-09-16 12:31:34 -040043 fThreadSafeViewCache = std::make_unique<GrThreadSafeUniquelyKeyedProxyViewCache>();
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 Daniel638b2e82020-08-27 14:29:00 -040052 GrProtected isProtected, bool vkRTSupportsInputAttachment) {
Adlai Hollere219d1c2020-06-02 11:23:16 -040053 SkASSERT(fCaps);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050054 if (!backendFormat.isValid()) {
Robert Phillipsdc369702020-08-13 13:34:27 -040055 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050056 }
57
Robert Phillipsc046ff02019-07-01 10:34:03 -040058 SkASSERT(isTextureable || !isMipMapped);
59
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050060 if (GrBackendApi::kOpenGL != backendFormat.backend() && willUseGLFBO0) {
61 // The willUseGLFBO0 flags can only be used for a GL backend.
Robert Phillipsdc369702020-08-13 13:34:27 -040062 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050063 }
64
Greg Daniel638b2e82020-08-27 14:29:00 -040065 if (GrBackendApi::kVulkan != backendFormat.backend() && vkRTSupportsInputAttachment) {
66 // The vkRTSupportsInputAttachment flags can only be used for a Vulkan backend.
67 return {};
68 }
69
Brian Salomon69100f02020-07-21 10:49:25 -040070 if (!fCaps->mipmapSupport()) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050071 isMipMapped = false;
72 }
73
Robert Phillipsdc369702020-08-13 13:34:27 -040074 if (ii.width() < 1 || ii.width() > fCaps->maxRenderTargetSize() ||
75 ii.height() < 1 || ii.height() > fCaps->maxRenderTargetSize()) {
76 return {};
77 }
78
Greg Daniel2f2caea2019-07-08 14:24:47 -040079 GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
Robert Phillipsb2adbef2019-07-02 16:33:05 -040080
Adlai Hollere219d1c2020-06-02 11:23:16 -040081 if (!fCaps->areColorTypeAndFormatCompatible(grColorType, backendFormat)) {
Robert Phillipsdc369702020-08-13 13:34:27 -040082 return {};
Robert Phillipsb2adbef2019-07-02 16:33:05 -040083 }
84
Adlai Hollere219d1c2020-06-02 11:23:16 -040085 if (!fCaps->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) {
Robert Phillipsdc369702020-08-13 13:34:27 -040086 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050087 }
88
Adlai Hollere219d1c2020-06-02 11:23:16 -040089 sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, backendFormat);
Greg Daniel6fa62e22019-08-07 15:52:37 -040090 SkASSERT(sampleCnt);
91
Robert Phillipsb45f47d2019-02-03 17:17:54 -050092 if (willUseGLFBO0 && isTextureable) {
Robert Phillipsdc369702020-08-13 13:34:27 -040093 return {};
Robert Phillipsb45f47d2019-02-03 17:17:54 -050094 }
95
Adlai Hollere219d1c2020-06-02 11:23:16 -040096 if (isTextureable && !fCaps->isFormatTexturable(backendFormat)) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -050097 // Skia doesn't agree that this is textureable.
Robert Phillipsdc369702020-08-13 13:34:27 -040098 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050099 }
100
Robert Phillips3cd54322019-07-10 09:28:59 -0400101 if (GrBackendApi::kVulkan == backendFormat.backend()) {
Adlai Hollere219d1c2020-06-02 11:23:16 -0400102 if (GrBackendApi::kVulkan != fBackend) {
Robert Phillipsdc369702020-08-13 13:34:27 -0400103 return {};
Robert Phillips3cd54322019-07-10 09:28:59 -0400104 }
105
106#ifdef SK_VULKAN
Adlai Hollere219d1c2020-06-02 11:23:16 -0400107 const GrVkCaps* vkCaps = (const GrVkCaps*) fCaps.get();
Robert Phillips3cd54322019-07-10 09:28:59 -0400108
109 // The protection status of the characterization and the context need to match
110 if (isProtected != GrProtected(vkCaps->supportsProtectedMemory())) {
Robert Phillipsdc369702020-08-13 13:34:27 -0400111 return {};
Robert Phillips3cd54322019-07-10 09:28:59 -0400112 }
113#endif
114 }
115
Greg Daniel638b2e82020-08-27 14:29:00 -0400116 return SkSurfaceCharacterization(
117 sk_ref_sp<GrContextThreadSafeProxy>(this),
118 cacheMaxResourceBytes, ii, backendFormat,
119 origin, sampleCnt,
120 SkSurfaceCharacterization::Textureable(isTextureable),
121 SkSurfaceCharacterization::MipMapped(isMipMapped),
122 SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
123 SkSurfaceCharacterization::VkRTSupportsInputAttachment(vkRTSupportsInputAttachment),
124 SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false),
125 isProtected,
126 surfaceProps);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500127}
128
Adlai Hollere219d1c2020-06-02 11:23:16 -0400129GrBackendFormat GrContextThreadSafeProxy::defaultBackendFormat(SkColorType skColorType,
130 GrRenderable renderable) const {
131 SkASSERT(fCaps);
132 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
133
134 GrBackendFormat format = fCaps->getDefaultBackendFormat(grColorType, renderable);
135 if (!format.isValid()) {
136 return GrBackendFormat();
137 }
138
139 SkASSERT(renderable == GrRenderable::kNo ||
140 fCaps->isFormatAsColorTypeRenderable(grColorType, format));
141
142 return format;
143}
144
Adlai Holler43b15792020-06-01 10:11:49 -0400145void GrContextThreadSafeProxy::abandonContext() {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400146 if (!fAbandoned.exchange(true)) {
147 fTextBlobCache->freeAll();
148 }
Adlai Holler43b15792020-06-01 10:11:49 -0400149}
150
151bool GrContextThreadSafeProxy::abandoned() const {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400152 return fAbandoned;
Adlai Holler43b15792020-06-01 10:11:49 -0400153}
154
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500155////////////////////////////////////////////////////////////////////////////////
Robert Phillipsbb606772019-02-04 17:50:57 -0500156sk_sp<GrContextThreadSafeProxy> GrContextThreadSafeProxyPriv::Make(
157 GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -0400158 const GrContextOptions& options) {
159 return sk_sp<GrContextThreadSafeProxy>(new GrContextThreadSafeProxy(backend, options));
Robert Phillipsbb606772019-02-04 17:50:57 -0500160}
161