Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 1 | /* |
| 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 Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrContextThreadSafeProxy.h" |
| 11 | #include "src/gpu/GrContextThreadSafeProxyPriv.h" |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 12 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkSurfaceCharacterization.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrBaseContextPriv.h" |
| 15 | #include "src/gpu/GrCaps.h" |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrThreadSafeCache.h" |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame^] | 17 | #include "src/gpu/GrThreadSafePipelineBuilder.h" |
Robert Phillips | 7f11fb5 | 2019-12-03 13:35:19 -0500 | [diff] [blame] | 18 | #include "src/gpu/effects/GrSkSLFP.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/image/SkSurface_Gpu.h" |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 20 | |
Robert Phillips | 3cd5432 | 2019-07-10 09:28:59 -0400 | [diff] [blame] | 21 | #ifdef SK_VULKAN |
| 22 | #include "src/gpu/vk/GrVkCaps.h" |
| 23 | #endif |
| 24 | |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 25 | static int32_t next_id() { |
| 26 | static std::atomic<int32_t> nextID{1}; |
| 27 | int32_t id; |
| 28 | do { |
Adlai Holler | 4888cda | 2020-11-06 16:37:37 -0500 | [diff] [blame] | 29 | id = nextID.fetch_add(1, std::memory_order_relaxed); |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 30 | } while (id == SK_InvalidGenID); |
| 31 | return id; |
| 32 | } |
| 33 | |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 34 | GrContextThreadSafeProxy::GrContextThreadSafeProxy(GrBackendApi backend, |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 35 | const GrContextOptions& options) |
| 36 | : fBackend(backend), fOptions(options), fContextID(next_id()) { |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 37 | } |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 38 | |
| 39 | GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default; |
| 40 | |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame^] | 41 | void GrContextThreadSafeProxy::init(sk_sp<const GrCaps> caps, |
| 42 | sk_sp<GrThreadSafePipelineBuilder> pipelineBuilder) { |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 43 | fCaps = std::move(caps); |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 44 | fTextBlobCache = std::make_unique<GrTextBlobCache>(fContextID); |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 45 | fThreadSafeCache = std::make_unique<GrThreadSafeCache>(); |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame^] | 46 | fPipelineBuilder = std::move(pipelineBuilder); |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 47 | } |
| 48 | |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 49 | SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization( |
| 50 | size_t cacheMaxResourceBytes, |
| 51 | const SkImageInfo& ii, const GrBackendFormat& backendFormat, |
| 52 | int sampleCnt, GrSurfaceOrigin origin, |
| 53 | const SkSurfaceProps& surfaceProps, |
Robert Phillips | 3cd5432 | 2019-07-10 09:28:59 -0400 | [diff] [blame] | 54 | bool isMipMapped, bool willUseGLFBO0, bool isTextureable, |
Greg Daniel | 007d97d | 2020-11-04 10:50:39 -0500 | [diff] [blame] | 55 | GrProtected isProtected, bool vkRTSupportsInputAttachment, |
| 56 | bool forVulkanSecondaryCommandBuffer) { |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 57 | SkASSERT(fCaps); |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 58 | if (!backendFormat.isValid()) { |
Robert Phillips | dc36970 | 2020-08-13 13:34:27 -0400 | [diff] [blame] | 59 | return {}; |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Robert Phillips | c046ff0 | 2019-07-01 10:34:03 -0400 | [diff] [blame] | 62 | SkASSERT(isTextureable || !isMipMapped); |
| 63 | |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 64 | if (GrBackendApi::kOpenGL != backendFormat.backend() && willUseGLFBO0) { |
| 65 | // The willUseGLFBO0 flags can only be used for a GL backend. |
Robert Phillips | dc36970 | 2020-08-13 13:34:27 -0400 | [diff] [blame] | 66 | return {}; |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Greg Daniel | 007d97d | 2020-11-04 10:50:39 -0500 | [diff] [blame] | 69 | if (GrBackendApi::kVulkan != backendFormat.backend() && |
| 70 | (vkRTSupportsInputAttachment || forVulkanSecondaryCommandBuffer)) { |
| 71 | // The vkRTSupportsInputAttachment and forVulkanSecondaryCommandBuffer flags can only be |
| 72 | // used for a Vulkan backend. |
Greg Daniel | 638b2e8 | 2020-08-27 14:29:00 -0400 | [diff] [blame] | 73 | return {}; |
| 74 | } |
| 75 | |
Brian Salomon | 69100f0 | 2020-07-21 10:49:25 -0400 | [diff] [blame] | 76 | if (!fCaps->mipmapSupport()) { |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 77 | isMipMapped = false; |
| 78 | } |
| 79 | |
Robert Phillips | dc36970 | 2020-08-13 13:34:27 -0400 | [diff] [blame] | 80 | if (ii.width() < 1 || ii.width() > fCaps->maxRenderTargetSize() || |
| 81 | ii.height() < 1 || ii.height() > fCaps->maxRenderTargetSize()) { |
| 82 | return {}; |
| 83 | } |
| 84 | |
Greg Daniel | 2f2caea | 2019-07-08 14:24:47 -0400 | [diff] [blame] | 85 | GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType()); |
Robert Phillips | b2adbef | 2019-07-02 16:33:05 -0400 | [diff] [blame] | 86 | |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 87 | if (!fCaps->areColorTypeAndFormatCompatible(grColorType, backendFormat)) { |
Robert Phillips | dc36970 | 2020-08-13 13:34:27 -0400 | [diff] [blame] | 88 | return {}; |
Robert Phillips | b2adbef | 2019-07-02 16:33:05 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 91 | if (!fCaps->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) { |
Robert Phillips | dc36970 | 2020-08-13 13:34:27 -0400 | [diff] [blame] | 92 | return {}; |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 95 | sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, backendFormat); |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 96 | SkASSERT(sampleCnt); |
| 97 | |
Robert Phillips | b45f47d | 2019-02-03 17:17:54 -0500 | [diff] [blame] | 98 | if (willUseGLFBO0 && isTextureable) { |
Robert Phillips | dc36970 | 2020-08-13 13:34:27 -0400 | [diff] [blame] | 99 | return {}; |
Robert Phillips | b45f47d | 2019-02-03 17:17:54 -0500 | [diff] [blame] | 100 | } |
| 101 | |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 102 | if (isTextureable && !fCaps->isFormatTexturable(backendFormat)) { |
Robert Phillips | b45f47d | 2019-02-03 17:17:54 -0500 | [diff] [blame] | 103 | // Skia doesn't agree that this is textureable. |
Robert Phillips | dc36970 | 2020-08-13 13:34:27 -0400 | [diff] [blame] | 104 | return {}; |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Greg Daniel | 007d97d | 2020-11-04 10:50:39 -0500 | [diff] [blame] | 107 | if (forVulkanSecondaryCommandBuffer && |
| 108 | (isTextureable || isMipMapped || willUseGLFBO0 || vkRTSupportsInputAttachment)) { |
| 109 | return {}; |
| 110 | } |
| 111 | |
Robert Phillips | 3cd5432 | 2019-07-10 09:28:59 -0400 | [diff] [blame] | 112 | if (GrBackendApi::kVulkan == backendFormat.backend()) { |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 113 | if (GrBackendApi::kVulkan != fBackend) { |
Robert Phillips | dc36970 | 2020-08-13 13:34:27 -0400 | [diff] [blame] | 114 | return {}; |
Robert Phillips | 3cd5432 | 2019-07-10 09:28:59 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | #ifdef SK_VULKAN |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 118 | const GrVkCaps* vkCaps = (const GrVkCaps*) fCaps.get(); |
Robert Phillips | 3cd5432 | 2019-07-10 09:28:59 -0400 | [diff] [blame] | 119 | |
| 120 | // The protection status of the characterization and the context need to match |
| 121 | if (isProtected != GrProtected(vkCaps->supportsProtectedMemory())) { |
Robert Phillips | dc36970 | 2020-08-13 13:34:27 -0400 | [diff] [blame] | 122 | return {}; |
Robert Phillips | 3cd5432 | 2019-07-10 09:28:59 -0400 | [diff] [blame] | 123 | } |
| 124 | #endif |
| 125 | } |
| 126 | |
Greg Daniel | 638b2e8 | 2020-08-27 14:29:00 -0400 | [diff] [blame] | 127 | 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 Daniel | 007d97d | 2020-11-04 10:50:39 -0500 | [diff] [blame] | 135 | SkSurfaceCharacterization::VulkanSecondaryCBCompatible(forVulkanSecondaryCommandBuffer), |
Greg Daniel | 638b2e8 | 2020-08-27 14:29:00 -0400 | [diff] [blame] | 136 | isProtected, |
| 137 | surfaceProps); |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 140 | GrBackendFormat 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 Holler | 43b1579 | 2020-06-01 10:11:49 -0400 | [diff] [blame] | 156 | void GrContextThreadSafeProxy::abandonContext() { |
Herb Derby | bc4d13a | 2020-06-25 15:35:23 -0400 | [diff] [blame] | 157 | if (!fAbandoned.exchange(true)) { |
| 158 | fTextBlobCache->freeAll(); |
| 159 | } |
Adlai Holler | 43b1579 | 2020-06-01 10:11:49 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | bool GrContextThreadSafeProxy::abandoned() const { |
Herb Derby | bc4d13a | 2020-06-25 15:35:23 -0400 | [diff] [blame] | 163 | return fAbandoned; |
Adlai Holler | 43b1579 | 2020-06-01 10:11:49 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 166 | //////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 167 | sk_sp<GrContextThreadSafeProxy> GrContextThreadSafeProxyPriv::Make( |
| 168 | GrBackendApi backend, |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 169 | const GrContextOptions& options) { |
| 170 | return sk_sp<GrContextThreadSafeProxy>(new GrContextThreadSafeProxy(backend, options)); |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 171 | } |
| 172 | |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame^] | 173 | void GrContextThreadSafeProxyPriv::init(sk_sp<const GrCaps> caps, |
| 174 | sk_sp<GrThreadSafePipelineBuilder> builder) const { |
| 175 | fProxy->init(std::move(caps), std::move(builder)); |
| 176 | } |
| 177 | |