blob: 1642c9259ae66b4331b754ccdef9cd7e2fbda767 [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()) {
53 return SkSurfaceCharacterization(); // return an invalid characterization
54 }
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.
60 return SkSurfaceCharacterization(); // return an invalid characterization
61 }
62
Brian Salomon69100f02020-07-21 10:49:25 -040063 if (!fCaps->mipmapSupport()) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050064 isMipMapped = false;
65 }
66
Greg Daniel2f2caea2019-07-08 14:24:47 -040067 GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
Robert Phillipsb2adbef2019-07-02 16:33:05 -040068
Adlai Hollere219d1c2020-06-02 11:23:16 -040069 if (!fCaps->areColorTypeAndFormatCompatible(grColorType, backendFormat)) {
Robert Phillipsb2adbef2019-07-02 16:33:05 -040070 return SkSurfaceCharacterization(); // return an invalid characterization
71 }
72
Adlai Hollere219d1c2020-06-02 11:23:16 -040073 if (!fCaps->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050074 return SkSurfaceCharacterization(); // return an invalid characterization
75 }
76
Adlai Hollere219d1c2020-06-02 11:23:16 -040077 sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, backendFormat);
Greg Daniel6fa62e22019-08-07 15:52:37 -040078 SkASSERT(sampleCnt);
79
Robert Phillipsb45f47d2019-02-03 17:17:54 -050080 if (willUseGLFBO0 && isTextureable) {
81 return SkSurfaceCharacterization(); // return an invalid characterization
82 }
83
Adlai Hollere219d1c2020-06-02 11:23:16 -040084 if (isTextureable && !fCaps->isFormatTexturable(backendFormat)) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -050085 // Skia doesn't agree that this is textureable.
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050086 return SkSurfaceCharacterization(); // return an invalid characterization
87 }
88
Robert Phillips3cd54322019-07-10 09:28:59 -040089 if (GrBackendApi::kVulkan == backendFormat.backend()) {
Adlai Hollere219d1c2020-06-02 11:23:16 -040090 if (GrBackendApi::kVulkan != fBackend) {
Robert Phillips3cd54322019-07-10 09:28:59 -040091 return SkSurfaceCharacterization(); // return an invalid characterization
92 }
93
94#ifdef SK_VULKAN
Adlai Hollere219d1c2020-06-02 11:23:16 -040095 const GrVkCaps* vkCaps = (const GrVkCaps*) fCaps.get();
Robert Phillips3cd54322019-07-10 09:28:59 -040096
97 // The protection status of the characterization and the context need to match
98 if (isProtected != GrProtected(vkCaps->supportsProtectedMemory())) {
99 return SkSurfaceCharacterization(); // return an invalid characterization
100 }
101#endif
102 }
103
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500104 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
Robert Phillipsb2adbef2019-07-02 16:33:05 -0400105 cacheMaxResourceBytes, ii, backendFormat,
106 origin, sampleCnt,
Robert Phillipsb45f47d2019-02-03 17:17:54 -0500107 SkSurfaceCharacterization::Textureable(isTextureable),
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500108 SkSurfaceCharacterization::MipMapped(isMipMapped),
109 SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
110 SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false),
Robert Phillips3cd54322019-07-10 09:28:59 -0400111 isProtected,
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500112 surfaceProps);
113}
114
Adlai Hollere219d1c2020-06-02 11:23:16 -0400115GrBackendFormat GrContextThreadSafeProxy::defaultBackendFormat(SkColorType skColorType,
116 GrRenderable renderable) const {
117 SkASSERT(fCaps);
118 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
119
120 GrBackendFormat format = fCaps->getDefaultBackendFormat(grColorType, renderable);
121 if (!format.isValid()) {
122 return GrBackendFormat();
123 }
124
125 SkASSERT(renderable == GrRenderable::kNo ||
126 fCaps->isFormatAsColorTypeRenderable(grColorType, format));
127
128 return format;
129}
130
Adlai Holler43b15792020-06-01 10:11:49 -0400131void GrContextThreadSafeProxy::abandonContext() {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400132 if (!fAbandoned.exchange(true)) {
133 fTextBlobCache->freeAll();
134 }
Adlai Holler43b15792020-06-01 10:11:49 -0400135}
136
137bool GrContextThreadSafeProxy::abandoned() const {
Herb Derbybc4d13a2020-06-25 15:35:23 -0400138 return fAbandoned;
Adlai Holler43b15792020-06-01 10:11:49 -0400139}
140
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500141////////////////////////////////////////////////////////////////////////////////
Robert Phillipsbb606772019-02-04 17:50:57 -0500142sk_sp<GrContextThreadSafeProxy> GrContextThreadSafeProxyPriv::Make(
143 GrBackendApi backend,
Adlai Hollere219d1c2020-06-02 11:23:16 -0400144 const GrContextOptions& options) {
145 return sk_sp<GrContextThreadSafeProxy>(new GrContextThreadSafeProxy(backend, options));
Robert Phillipsbb606772019-02-04 17:50:57 -0500146}
147