blob: b4fbed664c8f3cce2f7bbe20ba99efa500754a0f [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrBaseContextPriv.h"
13#include "src/gpu/GrCaps.h"
Robert Phillips7f11fb52019-12-03 13:35:19 -050014#include "src/gpu/effects/GrSkSLFP.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/image/SkSurface_Gpu.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050016
Robert Phillips3cd54322019-07-10 09:28:59 -040017#ifdef SK_VULKAN
18#include "src/gpu/vk/GrVkCaps.h"
19#endif
20
Adlai Hollere219d1c2020-06-02 11:23:16 -040021static 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 Phillipsbb606772019-02-04 17:50:57 -050030GrContextThreadSafeProxy::GrContextThreadSafeProxy(GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -040031 const GrContextOptions& options)
32 : fBackend(backend), fOptions(options), fContextID(next_id()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050033}
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050034
35GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default;
36
Adlai Hollere219d1c2020-06-02 11:23:16 -040037void GrContextThreadSafeProxy::init(sk_sp<const GrCaps> caps) {
38 fCaps = std::move(caps);
Herb Derbybc4d13a2020-06-25 15:35:23 -040039 fTextBlobCache.reset(new GrTextBlobCache(fContextID));
Robert Phillipsbb606772019-02-04 17:50:57 -050040}
41
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050042SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
43 size_t cacheMaxResourceBytes,
44 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
45 int sampleCnt, GrSurfaceOrigin origin,
46 const SkSurfaceProps& surfaceProps,
Robert Phillips3cd54322019-07-10 09:28:59 -040047 bool isMipMapped, bool willUseGLFBO0, bool isTextureable,
48 GrProtected isProtected) {
Adlai Hollere219d1c2020-06-02 11:23:16 -040049 SkASSERT(fCaps);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050050 if (!backendFormat.isValid()) {
51 return SkSurfaceCharacterization(); // return an invalid characterization
52 }
53
Robert Phillipsc046ff02019-07-01 10:34:03 -040054 SkASSERT(isTextureable || !isMipMapped);
55
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050056 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 Salomon69100f02020-07-21 10:49:25 -040061 if (!fCaps->mipmapSupport()) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050062 isMipMapped = false;
63 }
64
Greg Daniel2f2caea2019-07-08 14:24:47 -040065 GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
Robert Phillipsb2adbef2019-07-02 16:33:05 -040066
Adlai Hollere219d1c2020-06-02 11:23:16 -040067 if (!fCaps->areColorTypeAndFormatCompatible(grColorType, backendFormat)) {
Robert Phillipsb2adbef2019-07-02 16:33:05 -040068 return SkSurfaceCharacterization(); // return an invalid characterization
69 }
70
Adlai Hollere219d1c2020-06-02 11:23:16 -040071 if (!fCaps->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050072 return SkSurfaceCharacterization(); // return an invalid characterization
73 }
74
Adlai Hollere219d1c2020-06-02 11:23:16 -040075 sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, backendFormat);
Greg Daniel6fa62e22019-08-07 15:52:37 -040076 SkASSERT(sampleCnt);
77
Robert Phillipsb45f47d2019-02-03 17:17:54 -050078 if (willUseGLFBO0 && isTextureable) {
79 return SkSurfaceCharacterization(); // return an invalid characterization
80 }
81
Adlai Hollere219d1c2020-06-02 11:23:16 -040082 if (isTextureable && !fCaps->isFormatTexturable(backendFormat)) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -050083 // Skia doesn't agree that this is textureable.
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050084 return SkSurfaceCharacterization(); // return an invalid characterization
85 }
86
Robert Phillips3cd54322019-07-10 09:28:59 -040087 if (GrBackendApi::kVulkan == backendFormat.backend()) {
Adlai Hollere219d1c2020-06-02 11:23:16 -040088 if (GrBackendApi::kVulkan != fBackend) {
Robert Phillips3cd54322019-07-10 09:28:59 -040089 return SkSurfaceCharacterization(); // return an invalid characterization
90 }
91
92#ifdef SK_VULKAN
Adlai Hollere219d1c2020-06-02 11:23:16 -040093 const GrVkCaps* vkCaps = (const GrVkCaps*) fCaps.get();
Robert Phillips3cd54322019-07-10 09:28:59 -040094
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 Phillipsa0bc39d2019-01-29 13:14:47 -0500102 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
Robert Phillipsb2adbef2019-07-02 16:33:05 -0400103 cacheMaxResourceBytes, ii, backendFormat,
104 origin, sampleCnt,
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500105 SkSurfaceCharacterization::Textureable(isTextureable),
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500106 SkSurfaceCharacterization::MipMapped(isMipMapped),
107 SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
108 SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false),
Robert Phillips3cd54322019-07-10 09:28:59 -0400109 isProtected,
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500110 surfaceProps);
111}
112
Adlai Hollere219d1c2020-06-02 11:23:16 -0400113GrBackendFormat 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 Holler43b15792020-06-01 10:11:49 -0400129void GrContextThreadSafeProxy::abandonContext() {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400130 if (!fAbandoned.exchange(true)) {
131 fTextBlobCache->freeAll();
132 }
Adlai Holler43b15792020-06-01 10:11:49 -0400133}
134
135bool GrContextThreadSafeProxy::abandoned() const {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400136 return fAbandoned;
Adlai Holler43b15792020-06-01 10:11:49 -0400137}
138
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500139////////////////////////////////////////////////////////////////////////////////
Robert Phillipsbb606772019-02-04 17:50:57 -0500140sk_sp<GrContextThreadSafeProxy> GrContextThreadSafeProxyPriv::Make(
141 GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -0400142 const GrContextOptions& options) {
143 return sk_sp<GrContextThreadSafeProxy>(new GrContextThreadSafeProxy(backend, options));
Robert Phillipsbb606772019-02-04 17:50:57 -0500144}
145