xref: /aosp_15_r20/external/skia/src/gpu/ganesh/GrDirectContextPriv.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/ganesh/GrDirectContextPriv.h"
9 
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkColorType.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkString.h"
18 #include "include/core/SkTypes.h"
19 #include "include/effects/SkRuntimeEffect.h"
20 #include "include/gpu/ganesh/GrDirectContext.h"
21 #include "include/gpu/ganesh/GrTypes.h"
22 #include "include/private/base/SingleOwner.h"
23 #include "include/private/base/SkDebug.h"
24 #include "include/private/base/SkTemplates.h"
25 #include "include/private/chromium/GrDeferredDisplayList.h"
26 #include "src/core/SkRuntimeEffectPriv.h"
27 #include "src/core/SkTraceEvent.h"
28 #include "src/gpu/AtlasTypes.h"
29 #include "src/gpu/SkBackingFit.h"
30 #include "src/gpu/ganesh/GrAuditTrail.h"
31 #include "src/gpu/ganesh/GrColorInfo.h"
32 #include "src/gpu/ganesh/GrDrawingManager.h"
33 #include "src/gpu/ganesh/GrFragmentProcessor.h"
34 #include "src/gpu/ganesh/GrGpu.h"
35 #include "src/gpu/ganesh/GrImageInfo.h"
36 #include "src/gpu/ganesh/GrPixmap.h"
37 #include "src/gpu/ganesh/GrRenderTargetProxy.h"
38 #include "src/gpu/ganesh/GrResourceCache.h"
39 #include "src/gpu/ganesh/GrSurfaceProxy.h"
40 #include "src/gpu/ganesh/GrSurfaceProxyPriv.h"
41 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
42 #include "src/gpu/ganesh/GrTexture.h"
43 #include "src/gpu/ganesh/GrThreadSafePipelineBuilder.h"
44 #include "src/gpu/ganesh/GrTracing.h"
45 #include "src/gpu/ganesh/SkGr.h"
46 #include "src/gpu/ganesh/SurfaceFillContext.h"
47 #include "src/gpu/ganesh/effects/GrSkSLFP.h"
48 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
49 #include "src/gpu/ganesh/image/SkImage_Ganesh.h"
50 #include "src/gpu/ganesh/text/GrAtlasManager.h"
51 #include "src/image/SkImage_Base.h"
52 
53 #include <algorithm>
54 #include <cstdint>
55 #include <tuple>
56 
57 class GrProgramDesc;
58 class GrProgramInfo;
59 
60 using namespace skia_private;
61 using MaskFormat = skgpu::MaskFormat;
62 
63 #define ASSERT_OWNED_PROXY(P) \
64     SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this->context())
65 #define ASSERT_SINGLE_OWNER SKGPU_ASSERT_SINGLE_OWNER(this->context()->singleOwner())
66 #define RETURN_VALUE_IF_ABANDONED(value) if (this->context()->abandoned()) { return (value); }
67 
flushSurfaces(SkSpan<GrSurfaceProxy * > proxies,SkSurfaces::BackendSurfaceAccess access,const GrFlushInfo & info,const skgpu::MutableTextureState * newState)68 GrSemaphoresSubmitted GrDirectContextPriv::flushSurfaces(
69         SkSpan<GrSurfaceProxy*> proxies,
70         SkSurfaces::BackendSurfaceAccess access,
71         const GrFlushInfo& info,
72         const skgpu::MutableTextureState* newState) {
73     ASSERT_SINGLE_OWNER
74     GR_CREATE_TRACE_MARKER_CONTEXT("GrDirectContextPriv", "flushSurfaces", this->context());
75 
76     if (this->context()->abandoned()) {
77         if (info.fSubmittedProc) {
78             info.fSubmittedProc(info.fSubmittedContext, false);
79         }
80         if (info.fFinishedProc) {
81             info.fFinishedProc(info.fFinishedContext);
82         }
83         return GrSemaphoresSubmitted::kNo;
84     }
85 
86 #ifdef SK_DEBUG
87     for (GrSurfaceProxy* proxy : proxies) {
88         SkASSERT(proxy);
89         ASSERT_OWNED_PROXY(proxy);
90     }
91 #endif
92     return this->context()->drawingManager()->flushSurfaces(proxies, access, info, newState);
93 }
94 
createDDLTask(sk_sp<const GrDeferredDisplayList> ddl,sk_sp<GrRenderTargetProxy> newDest)95 void GrDirectContextPriv::createDDLTask(sk_sp<const GrDeferredDisplayList> ddl,
96                                         sk_sp<GrRenderTargetProxy> newDest) {
97     this->context()->drawingManager()->createDDLTask(std::move(ddl), std::move(newDest));
98 }
99 
compile(const GrProgramDesc & desc,const GrProgramInfo & info)100 bool GrDirectContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
101     GrGpu* gpu = this->getGpu();
102     if (!gpu) {
103         return false;
104     }
105 
106     return gpu->compile(desc, info);
107 }
108 
109 
110 //////////////////////////////////////////////////////////////////////////////
111 #if defined(GPU_TEST_UTILS)
112 
dumpCacheStats(SkString * out) const113 void GrDirectContextPriv::dumpCacheStats(SkString* out) const {
114 #if GR_CACHE_STATS
115     this->context()->fResourceCache->dumpStats(out);
116 #endif
117 }
118 
dumpCacheStatsKeyValuePairs(TArray<SkString> * keys,TArray<double> * values) const119 void GrDirectContextPriv::dumpCacheStatsKeyValuePairs(TArray<SkString>* keys,
120                                                       TArray<double>* values) const {
121 #if GR_CACHE_STATS
122     this->context()->fResourceCache->dumpStatsKeyValuePairs(keys, values);
123 #endif
124 }
125 
printCacheStats() const126 void GrDirectContextPriv::printCacheStats() const {
127     SkString out;
128     this->dumpCacheStats(&out);
129     SkDebugf("%s", out.c_str());
130 }
131 
132 /////////////////////////////////////////////////
resetGpuStats() const133 void GrDirectContextPriv::resetGpuStats() const {
134 #if GR_GPU_STATS
135     this->context()->fGpu->stats()->reset();
136 #endif
137 }
138 
dumpGpuStats(SkString * out) const139 void GrDirectContextPriv::dumpGpuStats(SkString* out) const {
140 #if GR_GPU_STATS
141     this->context()->fGpu->stats()->dump(out);
142     if (auto builder = this->context()->fGpu->pipelineBuilder()) {
143         builder->stats()->dump(out);
144     }
145 #endif
146 }
147 
dumpGpuStatsKeyValuePairs(TArray<SkString> * keys,TArray<double> * values) const148 void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(TArray<SkString>* keys,
149                                                     TArray<double>* values) const {
150 #if GR_GPU_STATS
151     this->context()->fGpu->stats()->dumpKeyValuePairs(keys, values);
152     if (auto builder = this->context()->fGpu->pipelineBuilder()) {
153         builder->stats()->dumpKeyValuePairs(keys, values);
154     }
155 #endif
156 }
157 
printGpuStats() const158 void GrDirectContextPriv::printGpuStats() const {
159     SkString out;
160     this->dumpGpuStats(&out);
161     SkDebugf("%s", out.c_str());
162 }
163 
164 /////////////////////////////////////////////////
resetContextStats()165 void GrDirectContextPriv::resetContextStats() {
166 #if GR_GPU_STATS
167     this->context()->stats()->reset();
168 #endif
169 }
170 
dumpContextStats(SkString * out) const171 void GrDirectContextPriv::dumpContextStats(SkString* out) const {
172 #if GR_GPU_STATS
173     this->context()->stats()->dump(out);
174 #endif
175 }
176 
dumpContextStatsKeyValuePairs(TArray<SkString> * keys,TArray<double> * values) const177 void GrDirectContextPriv::dumpContextStatsKeyValuePairs(TArray<SkString>* keys,
178                                                         TArray<double>* values) const {
179 #if GR_GPU_STATS
180     this->context()->stats()->dumpKeyValuePairs(keys, values);
181 #endif
182 }
183 
printContextStats() const184 void GrDirectContextPriv::printContextStats() const {
185     SkString out;
186     this->dumpContextStats(&out);
187     SkDebugf("%s", out.c_str());
188 }
189 
190 /////////////////////////////////////////////////
testingOnly_getFontAtlasImage(MaskFormat format,unsigned int index)191 sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(MaskFormat format,
192                                                                   unsigned int index) {
193     auto atlasManager = this->getAtlasManager();
194     if (!atlasManager) {
195         return nullptr;
196     }
197 
198     unsigned int numActiveProxies;
199     const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
200     if (index >= numActiveProxies || !views || !views[index].proxy()) {
201         return nullptr;
202     }
203 
204     SkColorType colorType = skgpu::MaskFormatToColorType(format);
205     SkASSERT(views[index].proxy()->priv().isExact());
206     return sk_make_sp<SkImage_Ganesh>(sk_ref_sp(this->context()),
207                                       kNeedNewImageUniqueID,
208                                       views[index],
209                                       SkColorInfo(colorType, kPremul_SkAlphaType, nullptr));
210 }
211 
testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject * cb)212 void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(
213         GrOnFlushCallbackObject* cb) {
214     this->context()->flushAndSubmit();
215     this->context()->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
216 }
217 #endif
218 
219 // Both of these effects aggressively round to the nearest exact (N / 255) floating point values.
220 // This lets us find a round-trip preserving pair on some GPUs that do odd byte to float conversion.
make_premul_effect(std::unique_ptr<GrFragmentProcessor> fp)221 static std::unique_ptr<GrFragmentProcessor> make_premul_effect(
222         std::unique_ptr<GrFragmentProcessor> fp) {
223     if (!fp) {
224         return nullptr;
225     }
226 
227     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter,
228         "half4 main(half4 halfColor) {"
229             "float4 color = float4(halfColor);"
230             "color = floor(color * 255 + 0.5) / 255;"
231             "color.rgb = floor(color.rgb * color.a * 255 + 0.5) / 255;"
232             "return color;"
233         "}"
234     );
235 
236     fp = GrSkSLFP::Make(effect, "ToPremul", std::move(fp), GrSkSLFP::OptFlags::kNone);
237     return GrFragmentProcessor::HighPrecision(std::move(fp));
238 }
239 
make_unpremul_effect(std::unique_ptr<GrFragmentProcessor> fp)240 static std::unique_ptr<GrFragmentProcessor> make_unpremul_effect(
241         std::unique_ptr<GrFragmentProcessor> fp) {
242     if (!fp) {
243         return nullptr;
244     }
245 
246     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter,
247         "half4 main(half4 halfColor) {"
248             "float4 color = float4(halfColor);"
249             "color = floor(color * 255 + 0.5) / 255;"
250             "color.rgb = color.a <= 0 ? half3(0) : floor(color.rgb / color.a * 255 + 0.5) / 255;"
251             "return color;"
252         "}"
253     );
254 
255     fp = GrSkSLFP::Make(effect, "ToUnpremul", std::move(fp), GrSkSLFP::OptFlags::kNone);
256     return GrFragmentProcessor::HighPrecision(std::move(fp));
257 }
258 
test_for_preserving_PM_conversions(GrDirectContext * dContext)259 static bool test_for_preserving_PM_conversions(GrDirectContext* dContext) {
260     static constexpr int kSize = 256;
261     AutoTMalloc<uint32_t> data(kSize * kSize * 3);
262     uint32_t* srcData = data.get();
263 
264     // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
265     // values in row y. We set r, g, and b to the same value since they are handled identically.
266     for (int y = 0; y < kSize; ++y) {
267         for (int x = 0; x < kSize; ++x) {
268             uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize*y + x]);
269             color[3] = y;
270             color[2] = std::min(x, y);
271             color[1] = std::min(x, y);
272             color[0] = std::min(x, y);
273         }
274     }
275 
276     const SkImageInfo pmII =
277             SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
278     const SkImageInfo upmII = pmII.makeAlphaType(kUnpremul_SkAlphaType);
279 
280     auto readSFC =
281             dContext->priv().makeSFC(upmII, "ReadSfcForPMUPMConversion", SkBackingFit::kExact);
282     auto tempSFC =
283             dContext->priv().makeSFC(pmII, "TempSfcForPMUPMConversion", SkBackingFit::kExact);
284     if (!readSFC || !tempSFC) {
285         return false;
286     }
287 
288     // This function is only ever called if we are in a GrDirectContext since we are calling read
289     // pixels here. Thus the pixel data will be uploaded immediately and we don't need to keep the
290     // pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
291     SkBitmap bitmap;
292     bitmap.installPixels(pmII, srcData, 4 * kSize);
293     bitmap.setImmutable();
294 
295     auto dataView = std::get<0>(GrMakeUncachedBitmapProxyView(dContext, bitmap));
296     if (!dataView) {
297         return false;
298     }
299 
300     uint32_t* firstRead  = data.get() +   kSize*kSize;
301     uint32_t* secondRead = data.get() + 2*kSize*kSize;
302     std::fill_n( firstRead, kSize*kSize, 0);
303     std::fill_n(secondRead, kSize*kSize, 0);
304 
305     GrPixmap firstReadPM( upmII,  firstRead, kSize*sizeof(uint32_t));
306     GrPixmap secondReadPM(upmII, secondRead, kSize*sizeof(uint32_t));
307 
308     // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
309     // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
310     // We then verify that two reads produced the same values.
311 
312     auto fp1 = make_unpremul_effect(GrTextureEffect::Make(std::move(dataView), bitmap.alphaType()));
313     readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp1));
314     if (!readSFC->readPixels(dContext, firstReadPM, {0, 0})) {
315         return false;
316     }
317 
318     auto fp2 = make_premul_effect(
319             GrTextureEffect::Make(readSFC->readSurfaceView(), readSFC->colorInfo().alphaType()));
320     tempSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp2));
321 
322     auto fp3 = make_unpremul_effect(
323             GrTextureEffect::Make(tempSFC->readSurfaceView(), tempSFC->colorInfo().alphaType()));
324     readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp3));
325 
326     if (!readSFC->readPixels(dContext, secondReadPM, {0, 0})) {
327         return false;
328     }
329 
330     for (int y = 0; y < kSize; ++y) {
331         for (int x = 0; x <= y; ++x) {
332             if (firstRead[kSize*y + x] != secondRead[kSize*y + x]) {
333                 return false;
334             }
335         }
336     }
337 
338     return true;
339 }
340 
validPMUPMConversionExists()341 bool GrDirectContextPriv::validPMUPMConversionExists() {
342     ASSERT_SINGLE_OWNER
343 
344     auto dContext = this->context();
345 
346     if (!dContext->fDidTestPMConversions) {
347         dContext->fPMUPMConversionsRoundTrip = test_for_preserving_PM_conversions(dContext);
348         dContext->fDidTestPMConversions = true;
349     }
350 
351     // The PM<->UPM tests fail or succeed together so we only need to check one.
352     return dContext->fPMUPMConversionsRoundTrip;
353 }
354 
createPMToUPMEffect(std::unique_ptr<GrFragmentProcessor> fp)355 std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect(
356         std::unique_ptr<GrFragmentProcessor> fp) {
357     ASSERT_SINGLE_OWNER
358     // We should have already called this->priv().validPMUPMConversionExists() in this case
359     SkASSERT(this->context()->fDidTestPMConversions);
360     // ...and it should have succeeded
361     SkASSERT(this->validPMUPMConversionExists());
362 
363     return make_unpremul_effect(std::move(fp));
364 }
365 
createUPMToPMEffect(std::unique_ptr<GrFragmentProcessor> fp)366 std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect(
367         std::unique_ptr<GrFragmentProcessor> fp) {
368     ASSERT_SINGLE_OWNER
369     // We should have already called this->priv().validPMUPMConversionExists() in this case
370     SkASSERT(this->context()->fDidTestPMConversions);
371     // ...and it should have succeeded
372     SkASSERT(this->validPMUPMConversionExists());
373 
374     return make_premul_effect(std::move(fp));
375 }
376