blob: fb27ba1822c260bb13811cdade91c785cb2ccdd5 [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
Robert Phillipsbb606772019-02-04 17:50:57 -050022GrContextThreadSafeProxy::GrContextThreadSafeProxy(GrBackendApi backend,
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050023 const GrContextOptions& options,
Robert Phillipsbb606772019-02-04 17:50:57 -050024 uint32_t contextID)
25 : INHERITED(backend, options, contextID) {
26}
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050027
28GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default;
29
Brian Osman7b1678a2019-12-16 09:17:25 -050030bool GrContextThreadSafeProxy::init(sk_sp<const GrCaps> caps) {
31 return INHERITED::init(std::move(caps));
Robert Phillipsbb606772019-02-04 17:50:57 -050032}
33
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050034SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
35 size_t cacheMaxResourceBytes,
36 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
37 int sampleCnt, GrSurfaceOrigin origin,
38 const SkSurfaceProps& surfaceProps,
Robert Phillips3cd54322019-07-10 09:28:59 -040039 bool isMipMapped, bool willUseGLFBO0, bool isTextureable,
40 GrProtected isProtected) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050041 if (!backendFormat.isValid()) {
42 return SkSurfaceCharacterization(); // return an invalid characterization
43 }
44
Robert Phillipsc046ff02019-07-01 10:34:03 -040045 SkASSERT(isTextureable || !isMipMapped);
46
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050047 if (GrBackendApi::kOpenGL != backendFormat.backend() && willUseGLFBO0) {
48 // The willUseGLFBO0 flags can only be used for a GL backend.
49 return SkSurfaceCharacterization(); // return an invalid characterization
50 }
51
Robert Phillipsbb606772019-02-04 17:50:57 -050052 if (!this->caps()->mipMapSupport()) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050053 isMipMapped = false;
54 }
55
Greg Daniel2f2caea2019-07-08 14:24:47 -040056 GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
Robert Phillipsb2adbef2019-07-02 16:33:05 -040057
Greg Daniel627d0532019-07-08 16:48:14 -040058 if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, backendFormat)) {
Robert Phillipsb2adbef2019-07-02 16:33:05 -040059 return SkSurfaceCharacterization(); // return an invalid characterization
60 }
61
Greg Daniel6fa62e22019-08-07 15:52:37 -040062 if (!this->caps()->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050063 return SkSurfaceCharacterization(); // return an invalid characterization
64 }
65
Greg Daniel6fa62e22019-08-07 15:52:37 -040066 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendFormat);
67 SkASSERT(sampleCnt);
68
Robert Phillipsb45f47d2019-02-03 17:17:54 -050069 if (willUseGLFBO0 && isTextureable) {
70 return SkSurfaceCharacterization(); // return an invalid characterization
71 }
72
Greg Daniel7bfc9132019-08-14 14:23:53 -040073 if (isTextureable && !this->caps()->isFormatTexturable(backendFormat)) {
Robert Phillipsb45f47d2019-02-03 17:17:54 -050074 // Skia doesn't agree that this is textureable.
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050075 return SkSurfaceCharacterization(); // return an invalid characterization
76 }
77
Robert Phillips3cd54322019-07-10 09:28:59 -040078 if (GrBackendApi::kVulkan == backendFormat.backend()) {
79 if (GrBackendApi::kVulkan != this->backend()) {
80 return SkSurfaceCharacterization(); // return an invalid characterization
81 }
82
83#ifdef SK_VULKAN
84 const GrVkCaps* vkCaps = (const GrVkCaps*) this->caps();
85
86 // The protection status of the characterization and the context need to match
87 if (isProtected != GrProtected(vkCaps->supportsProtectedMemory())) {
88 return SkSurfaceCharacterization(); // return an invalid characterization
89 }
90#endif
91 }
92
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050093 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
Robert Phillipsb2adbef2019-07-02 16:33:05 -040094 cacheMaxResourceBytes, ii, backendFormat,
95 origin, sampleCnt,
Robert Phillipsb45f47d2019-02-03 17:17:54 -050096 SkSurfaceCharacterization::Textureable(isTextureable),
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050097 SkSurfaceCharacterization::MipMapped(isMipMapped),
98 SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
99 SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false),
Robert Phillips3cd54322019-07-10 09:28:59 -0400100 isProtected,
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500101 surfaceProps);
102}
103
104////////////////////////////////////////////////////////////////////////////////
Robert Phillipsbb606772019-02-04 17:50:57 -0500105sk_sp<GrContextThreadSafeProxy> GrContextThreadSafeProxyPriv::Make(
106 GrBackendApi backend,
107 const GrContextOptions& options,
108 uint32_t contextID,
Brian Osman7b1678a2019-12-16 09:17:25 -0500109 sk_sp<const GrCaps> caps) {
Robert Phillipsbb606772019-02-04 17:50:57 -0500110 sk_sp<GrContextThreadSafeProxy> proxy(new GrContextThreadSafeProxy(backend, options,
111 contextID));
112
Brian Osman7b1678a2019-12-16 09:17:25 -0500113 if (!proxy->init(std::move(caps))) {
Robert Phillipsbb606772019-02-04 17:50:57 -0500114 return nullptr;
115 }
116 return proxy;
117}
118