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