blob: 6fd86bd1a9f849b3916e655e1b5bae8637b4f01a [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);
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
Adlai Hollere219d1c2020-06-02 11:23:16 -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() {
130 fAbandoned.store(true, std::memory_order_relaxed);
131}
132
133bool GrContextThreadSafeProxy::abandoned() const {
134 return fAbandoned.load(std::memory_order_relaxed);
135}
136
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500137////////////////////////////////////////////////////////////////////////////////
Robert Phillipsbb606772019-02-04 17:50:57 -0500138sk_sp<GrContextThreadSafeProxy> GrContextThreadSafeProxyPriv::Make(
139 GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -0400140 const GrContextOptions& options) {
141 return sk_sp<GrContextThreadSafeProxy>(new GrContextThreadSafeProxy(backend, options));
Robert Phillipsbb606772019-02-04 17:50:57 -0500142}
143