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