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