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