blob: 8514ff53c7ce973a58a315828b00b7659f7bc5ae [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 Phillips7f11fb52019-12-03 13:35:19 -050016#include "src/gpu/effects/GrSkSLFP.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/image/SkSurface_Gpu.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050018
Robert Phillips3cd54322019-07-10 09:28:59 -040019#ifdef SK_VULKAN
20#include "src/gpu/vk/GrVkCaps.h"
21#endif
22
Adlai Hollere219d1c2020-06-02 11:23:16 -040023static 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 Phillipsbb606772019-02-04 17:50:57 -050032GrContextThreadSafeProxy::GrContextThreadSafeProxy(GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -040033 const GrContextOptions& options)
34 : fBackend(backend), fOptions(options), fContextID(next_id()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050035}
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050036
37GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default;
38
Adlai Hollere219d1c2020-06-02 11:23:16 -040039void GrContextThreadSafeProxy::init(sk_sp<const GrCaps> caps) {
40 fCaps = std::move(caps);
John Stilesfbd050b2020-08-03 13:21:46 -040041 fTextBlobCache = std::make_unique<GrTextBlobCache>(fContextID);
Robert Phillipsbb606772019-02-04 17:50:57 -050042}
43
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050044SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
45 size_t cacheMaxResourceBytes,
46 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
47 int sampleCnt, GrSurfaceOrigin origin,
48 const SkSurfaceProps& surfaceProps,
Robert Phillips3cd54322019-07-10 09:28:59 -040049 bool isMipMapped, bool willUseGLFBO0, bool isTextureable,
50 GrProtected isProtected) {
Adlai Hollere219d1c2020-06-02 11:23:16 -040051 SkASSERT(fCaps);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050052 if (!backendFormat.isValid()) {
Robert Phillipsdc369702020-08-13 13:34:27 -040053 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050054 }
55
Robert Phillipsc046ff02019-07-01 10:34:03 -040056 SkASSERT(isTextureable || !isMipMapped);
57
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050058 if (GrBackendApi::kOpenGL != backendFormat.backend() && willUseGLFBO0) {
59 // The willUseGLFBO0 flags can only be used for a GL backend.
Robert Phillipsdc369702020-08-13 13:34:27 -040060 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050061 }
62
Brian Salomon69100f02020-07-21 10:49:25 -040063 if (!fCaps->mipmapSupport()) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050064 isMipMapped = false;
65 }
66
Robert Phillipsdc369702020-08-13 13:34:27 -040067 if (ii.width() < 1 || ii.width() > fCaps->maxRenderTargetSize() ||
68 ii.height() < 1 || ii.height() > fCaps->maxRenderTargetSize()) {
69 return {};
70 }
71
Greg Daniel2f2caea2019-07-08 14:24:47 -040072 GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
Robert Phillipsb2adbef2019-07-02 16:33:05 -040073
Adlai Hollere219d1c2020-06-02 11:23:16 -040074 if (!fCaps->areColorTypeAndFormatCompatible(grColorType, backendFormat)) {
Robert Phillipsdc369702020-08-13 13:34:27 -040075 return {};
Robert Phillipsb2adbef2019-07-02 16:33:05 -040076 }
77
Adlai Hollere219d1c2020-06-02 11:23:16 -040078 if (!fCaps->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) {
Robert Phillipsdc369702020-08-13 13:34:27 -040079 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050080 }
81
Adlai Hollere219d1c2020-06-02 11:23:16 -040082 sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, backendFormat);
Greg Daniel6fa62e22019-08-07 15:52:37 -040083 SkASSERT(sampleCnt);
84
Robert Phillipsb45f47d2019-02-03 17:17:54 -050085 if (willUseGLFBO0 && isTextureable) {
Robert Phillipsdc369702020-08-13 13:34:27 -040086 return {};
Robert Phillipsb45f47d2019-02-03 17:17:54 -050087 }
88
Adlai Hollere219d1c2020-06-02 11:23:16 -040089 if (isTextureable && !fCaps->isFormatTexturable(backendFormat)) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -050090 // Skia doesn't agree that this is textureable.
Robert Phillipsdc369702020-08-13 13:34:27 -040091 return {};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050092 }
93
Robert Phillips3cd54322019-07-10 09:28:59 -040094 if (GrBackendApi::kVulkan == backendFormat.backend()) {
Adlai Hollere219d1c2020-06-02 11:23:16 -040095 if (GrBackendApi::kVulkan != fBackend) {
Robert Phillipsdc369702020-08-13 13:34:27 -040096 return {};
Robert Phillips3cd54322019-07-10 09:28:59 -040097 }
98
99#ifdef SK_VULKAN
Adlai Hollere219d1c2020-06-02 11:23:16 -0400100 const GrVkCaps* vkCaps = (const GrVkCaps*) fCaps.get();
Robert Phillips3cd54322019-07-10 09:28:59 -0400101
102 // The protection status of the characterization and the context need to match
103 if (isProtected != GrProtected(vkCaps->supportsProtectedMemory())) {
Robert Phillipsdc369702020-08-13 13:34:27 -0400104 return {};
Robert Phillips3cd54322019-07-10 09:28:59 -0400105 }
106#endif
107 }
108
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500109 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
Robert Phillipsb2adbef2019-07-02 16:33:05 -0400110 cacheMaxResourceBytes, ii, backendFormat,
111 origin, sampleCnt,
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500112 SkSurfaceCharacterization::Textureable(isTextureable),
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500113 SkSurfaceCharacterization::MipMapped(isMipMapped),
114 SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
115 SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false),
Robert Phillips3cd54322019-07-10 09:28:59 -0400116 isProtected,
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500117 surfaceProps);
118}
119
Adlai Hollere219d1c2020-06-02 11:23:16 -0400120GrBackendFormat GrContextThreadSafeProxy::defaultBackendFormat(SkColorType skColorType,
121 GrRenderable renderable) const {
122 SkASSERT(fCaps);
123 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
124
125 GrBackendFormat format = fCaps->getDefaultBackendFormat(grColorType, renderable);
126 if (!format.isValid()) {
127 return GrBackendFormat();
128 }
129
130 SkASSERT(renderable == GrRenderable::kNo ||
131 fCaps->isFormatAsColorTypeRenderable(grColorType, format));
132
133 return format;
134}
135
Adlai Holler43b15792020-06-01 10:11:49 -0400136void GrContextThreadSafeProxy::abandonContext() {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400137 if (!fAbandoned.exchange(true)) {
138 fTextBlobCache->freeAll();
139 }
Adlai Holler43b15792020-06-01 10:11:49 -0400140}
141
142bool GrContextThreadSafeProxy::abandoned() const {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400143 return fAbandoned;
Adlai Holler43b15792020-06-01 10:11:49 -0400144}
145
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500146////////////////////////////////////////////////////////////////////////////////
Robert Phillipsbb606772019-02-04 17:50:57 -0500147sk_sp<GrContextThreadSafeProxy> GrContextThreadSafeProxyPriv::Make(
148 GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -0400149 const GrContextOptions& options) {
150 return sk_sp<GrContextThreadSafeProxy>(new GrContextThreadSafeProxy(backend, options));
Robert Phillipsbb606772019-02-04 17:50:57 -0500151}
152