blob: 4fa6fb719b4583b36ef1ad2a35d10c07114b99e0 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrContextThreadSafeProxy.h"
9#include "src/gpu/GrContextThreadSafeProxyPriv.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkSurfaceCharacterization.h"
12#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrBaseContextPriv.h"
14#include "src/gpu/GrCaps.h"
Robert Phillips7f11fb52019-12-03 13:35:19 -050015#include "src/gpu/effects/GrSkSLFP.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/image/SkSurface_Gpu.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050017
Robert Phillips3cd54322019-07-10 09:28:59 -040018#ifdef SK_VULKAN
19#include "src/gpu/vk/GrVkCaps.h"
20#endif
21
Adlai Hollere219d1c2020-06-02 11:23:16 -040022static int32_t next_id() {
23 static std::atomic<int32_t> nextID{1};
24 int32_t id;
25 do {
26 id = nextID++;
27 } while (id == SK_InvalidGenID);
28 return id;
29}
30
Robert Phillipsbb606772019-02-04 17:50:57 -050031GrContextThreadSafeProxy::GrContextThreadSafeProxy(GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -040032 const GrContextOptions& options)
33 : fBackend(backend), fOptions(options), fContextID(next_id()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050034}
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050035
36GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default;
37
Adlai Hollere219d1c2020-06-02 11:23:16 -040038void GrContextThreadSafeProxy::init(sk_sp<const GrCaps> caps) {
39 fCaps = std::move(caps);
Herb Derbybc4d13a2020-06-25 15:35:23 -040040 fTextBlobCache.reset(new GrTextBlobCache(fContextID));
Robert Phillipsbb606772019-02-04 17:50:57 -050041}
42
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050043SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
44 size_t cacheMaxResourceBytes,
45 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
46 int sampleCnt, GrSurfaceOrigin origin,
47 const SkSurfaceProps& surfaceProps,
Robert Phillips3cd54322019-07-10 09:28:59 -040048 bool isMipMapped, bool willUseGLFBO0, bool isTextureable,
49 GrProtected isProtected) {
Adlai Hollere219d1c2020-06-02 11:23:16 -040050 SkASSERT(fCaps);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050051 if (!backendFormat.isValid()) {
52 return SkSurfaceCharacterization(); // return an invalid characterization
53 }
54
Robert Phillipsc046ff02019-07-01 10:34:03 -040055 SkASSERT(isTextureable || !isMipMapped);
56
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050057 if (GrBackendApi::kOpenGL != backendFormat.backend() && willUseGLFBO0) {
58 // The willUseGLFBO0 flags can only be used for a GL backend.
59 return SkSurfaceCharacterization(); // return an invalid characterization
60 }
61
Adlai Hollere219d1c2020-06-02 11:23:16 -040062 if (!fCaps->mipMapSupport()) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050063 isMipMapped = false;
64 }
65
Greg Daniel2f2caea2019-07-08 14:24:47 -040066 GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
Robert Phillipsb2adbef2019-07-02 16:33:05 -040067
Adlai Hollere219d1c2020-06-02 11:23:16 -040068 if (!fCaps->areColorTypeAndFormatCompatible(grColorType, backendFormat)) {
Robert Phillipsb2adbef2019-07-02 16:33:05 -040069 return SkSurfaceCharacterization(); // return an invalid characterization
70 }
71
Adlai Hollere219d1c2020-06-02 11:23:16 -040072 if (!fCaps->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050073 return SkSurfaceCharacterization(); // return an invalid characterization
74 }
75
Adlai Hollere219d1c2020-06-02 11:23:16 -040076 sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, backendFormat);
Greg Daniel6fa62e22019-08-07 15:52:37 -040077 SkASSERT(sampleCnt);
78
Robert Phillipsb45f47d2019-02-03 17:17:54 -050079 if (willUseGLFBO0 && isTextureable) {
80 return SkSurfaceCharacterization(); // return an invalid characterization
81 }
82
Adlai Hollere219d1c2020-06-02 11:23:16 -040083 if (isTextureable && !fCaps->isFormatTexturable(backendFormat)) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -050084 // Skia doesn't agree that this is textureable.
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050085 return SkSurfaceCharacterization(); // return an invalid characterization
86 }
87
Robert Phillips3cd54322019-07-10 09:28:59 -040088 if (GrBackendApi::kVulkan == backendFormat.backend()) {
Adlai Hollere219d1c2020-06-02 11:23:16 -040089 if (GrBackendApi::kVulkan != fBackend) {
Robert Phillips3cd54322019-07-10 09:28:59 -040090 return SkSurfaceCharacterization(); // return an invalid characterization
91 }
92
93#ifdef SK_VULKAN
Adlai Hollere219d1c2020-06-02 11:23:16 -040094 const GrVkCaps* vkCaps = (const GrVkCaps*) fCaps.get();
Robert Phillips3cd54322019-07-10 09:28:59 -040095
96 // The protection status of the characterization and the context need to match
97 if (isProtected != GrProtected(vkCaps->supportsProtectedMemory())) {
98 return SkSurfaceCharacterization(); // return an invalid characterization
99 }
100#endif
101 }
102
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500103 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
Robert Phillipsb2adbef2019-07-02 16:33:05 -0400104 cacheMaxResourceBytes, ii, backendFormat,
105 origin, sampleCnt,
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500106 SkSurfaceCharacterization::Textureable(isTextureable),
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500107 SkSurfaceCharacterization::MipMapped(isMipMapped),
108 SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
109 SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false),
Robert Phillips3cd54322019-07-10 09:28:59 -0400110 isProtected,
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500111 surfaceProps);
112}
113
Adlai Hollere219d1c2020-06-02 11:23:16 -0400114GrBackendFormat GrContextThreadSafeProxy::defaultBackendFormat(SkColorType skColorType,
115 GrRenderable renderable) const {
116 SkASSERT(fCaps);
117 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
118
119 GrBackendFormat format = fCaps->getDefaultBackendFormat(grColorType, renderable);
120 if (!format.isValid()) {
121 return GrBackendFormat();
122 }
123
124 SkASSERT(renderable == GrRenderable::kNo ||
125 fCaps->isFormatAsColorTypeRenderable(grColorType, format));
126
127 return format;
128}
129
Adlai Holler43b15792020-06-01 10:11:49 -0400130void GrContextThreadSafeProxy::abandonContext() {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400131 if (!fAbandoned.exchange(true)) {
132 fTextBlobCache->freeAll();
133 }
Adlai Holler43b15792020-06-01 10:11:49 -0400134}
135
136bool GrContextThreadSafeProxy::abandoned() const {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400137 return fAbandoned;
Adlai Holler43b15792020-06-01 10:11:49 -0400138}
139
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500140////////////////////////////////////////////////////////////////////////////////
Robert Phillipsbb606772019-02-04 17:50:57 -0500141sk_sp<GrContextThreadSafeProxy> GrContextThreadSafeProxyPriv::Make(
142 GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -0400143 const GrContextOptions& options) {
144 return sk_sp<GrContextThreadSafeProxy>(new GrContextThreadSafeProxy(backend, options));
Robert Phillipsbb606772019-02-04 17:50:57 -0500145}
146