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