1/*
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
8#include "src/gpu/GrContextPriv.h"
9
10#include "include/gpu/GrContextThreadSafeProxy.h"
11#include "src/gpu/GrAuditTrail.h"
12#include "src/gpu/GrContextThreadSafeProxyPriv.h"
13#include "src/gpu/GrDrawingManager.h"
14#include "src/gpu/GrGpu.h"
15#include "src/gpu/GrMemoryPool.h"
16#include "src/gpu/GrRenderTargetContext.h"
17#include "src/gpu/GrSurfaceContext.h"
18#include "src/gpu/GrSurfaceContextPriv.h"
19#include "src/gpu/GrSurfacePriv.h"
20#include "src/gpu/GrTexture.h"
21#include "src/gpu/SkGr.h"
22#include "src/gpu/effects/GrSkSLFP.h"
23#include "src/gpu/effects/generated/GrConfigConversionEffect.h"
24#include "src/gpu/text/GrAtlasManager.h"
25#include "src/gpu/text/GrTextBlobCache.h"
26#include "src/image/SkImage_Base.h"
27#include "src/image/SkImage_Gpu.h"
28
29#define ASSERT_OWNED_PROXY(P) \
30 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext)
31#define ASSERT_SINGLE_OWNER \
32 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->singleOwner());)
33#define RETURN_VALUE_IF_ABANDONED(value) if (fContext->abandoned()) { return (value); }
34#define RETURN_IF_ABANDONED RETURN_VALUE_IF_ABANDONED(void)
35
36sk_sp<const GrCaps> GrContextPriv::refCaps() const {
37 return fContext->refCaps();
38}
39
40void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
41 fContext->addOnFlushCallbackObject(onFlushCBObject);
42}
43
44GrSemaphoresSubmitted GrContextPriv::flushSurfaces(GrSurfaceProxy* proxies[], int numProxies,
45 const GrFlushInfo& info) {
46 ASSERT_SINGLE_OWNER
47 RETURN_VALUE_IF_ABANDONED(GrSemaphoresSubmitted::kNo)
48 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "flushSurfaces", fContext);
49 SkASSERT(numProxies >= 0);
50 SkASSERT(!numProxies || proxies);
51 for (int i = 0; i < numProxies; ++i) {
52 SkASSERT(proxies[i]);
53 ASSERT_OWNED_PROXY(proxies[i]);
54 }
55 return fContext->drawingManager()->flushSurfaces(
56 proxies, numProxies, SkSurface::BackendSurfaceAccess::kNoAccess, info);
57}
58
59void GrContextPriv::flushSurface(GrSurfaceProxy* proxy) {
60 this->flushSurfaces(proxy ? &proxy : nullptr, proxy ? 1 : 0, {});
61}
62
63void GrContextPriv::moveRenderTasksToDDL(SkDeferredDisplayList* ddl) {
64 fContext->drawingManager()->moveRenderTasksToDDL(ddl);
65}
66
67void GrContextPriv::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
68 GrRenderTargetProxy* newDest) {
69 fContext->drawingManager()->copyRenderTasksFromDDL(ddl, newDest);
70}
71
72void GrContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
73 GrGpu* gpu = this->getGpu();
74 if (!gpu) {
75 return;
76 }
77
78 gpu->compile(desc, info);
79}
80
81
82//////////////////////////////////////////////////////////////////////////////
83
84#if GR_TEST_UTILS
85void GrContextPriv::resetGpuStats() const {
86#if GR_GPU_STATS
87 fContext->fGpu->stats()->reset();
88#endif
89}
90
91void GrContextPriv::dumpCacheStats(SkString* out) const {
92#if GR_CACHE_STATS
93 fContext->fResourceCache->dumpStats(out);
94#endif
95}
96
97void GrContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
98 SkTArray<double>* values) const {
99#if GR_CACHE_STATS
100 fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values);
101#endif
102}
103
104void GrContextPriv::printCacheStats() const {
105 SkString out;
106 this->dumpCacheStats(&out);
107 SkDebugf("%s", out.c_str());
108}
109
110void GrContextPriv::dumpGpuStats(SkString* out) const {
111#if GR_GPU_STATS
112 return fContext->fGpu->stats()->dump(out);
113#endif
114}
115
116void GrContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
117 SkTArray<double>* values) const {
118#if GR_GPU_STATS
119 return fContext->fGpu->stats()->dumpKeyValuePairs(keys, values);
120#endif
121}
122
123void GrContextPriv::printGpuStats() const {
124 SkString out;
125 this->dumpGpuStats(&out);
126 SkDebugf("%s", out.c_str());
127}
128
129void GrContextPriv::testingOnly_setTextBlobCacheLimit(size_t bytes) {
130 fContext->priv().getTextBlobCache()->setBudget(bytes);
131}
132
133sk_sp<SkImage> GrContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index) {
134 auto atlasManager = this->getAtlasManager();
135 if (!atlasManager) {
136 return nullptr;
137 }
138
139 unsigned int numActiveProxies;
140 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
141 if (index >= numActiveProxies || !views || !views[index].proxy()) {
142 return nullptr;
143 }
144
145 SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
146 SkASSERT(views[index].proxy()->priv().isExact());
147 sk_sp<SkImage> image(new SkImage_Gpu(sk_ref_sp(fContext), kNeedNewImageUniqueID,
148 views[index], colorType, kPremul_SkAlphaType, nullptr));
149 return image;
150}
151
152void GrContextPriv::testingOnly_purgeAllUnlockedResources() {
153 fContext->fResourceCache->purgeAllUnlocked();
154}
155
156void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
157 fContext->flush();
158 fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
159}
160#endif
161
162bool GrContextPriv::validPMUPMConversionExists() {
163 ASSERT_SINGLE_OWNER
164 if (!fContext->fDidTestPMConversions) {
165 fContext->fPMUPMConversionsRoundTrip =
166 GrConfigConversionEffect::TestForPreservingPMConversions(fContext);
167 fContext->fDidTestPMConversions = true;
168 }
169
170 // The PM<->UPM tests fail or succeed together so we only need to check one.
171 return fContext->fPMUPMConversionsRoundTrip;
172}
173
174std::unique_ptr<GrFragmentProcessor> GrContextPriv::createPMToUPMEffect(
175 std::unique_ptr<GrFragmentProcessor> fp) {
176 ASSERT_SINGLE_OWNER
177 // We should have already called this->priv().validPMUPMConversionExists() in this case
178 SkASSERT(fContext->fDidTestPMConversions);
179 // ...and it should have succeeded
180 SkASSERT(this->validPMUPMConversionExists());
181
182 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
183}
184
185std::unique_ptr<GrFragmentProcessor> GrContextPriv::createUPMToPMEffect(
186 std::unique_ptr<GrFragmentProcessor> fp) {
187 ASSERT_SINGLE_OWNER
188 // We should have already called this->priv().validPMUPMConversionExists() in this case
189 SkASSERT(fContext->fDidTestPMConversions);
190 // ...and it should have succeeded
191 SkASSERT(this->validPMUPMConversionExists());
192
193 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
194}
195