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