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 #ifdef SKIA_DFX_FOR_OHOS
143 this->context()->fResourceCache->dumpInfo(out);
144 #endif
145 if (auto builder = this->context()->fGpu->pipelineBuilder()) {
146 builder->stats()->dump(out);
147 }
148 #endif
149 }
150
dumpGpuStatsKeyValuePairs(TArray<SkString> * keys,TArray<double> * values) const151 void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(TArray<SkString>* keys,
152 TArray<double>* values) const {
153 #if GR_GPU_STATS
154 this->context()->fGpu->stats()->dumpKeyValuePairs(keys, values);
155 if (auto builder = this->context()->fGpu->pipelineBuilder()) {
156 builder->stats()->dumpKeyValuePairs(keys, values);
157 }
158 #endif
159 }
160
printGpuStats() const161 void GrDirectContextPriv::printGpuStats() const {
162 SkString out;
163 this->dumpGpuStats(&out);
164 SkDebugf("%s", out.c_str());
165 }
166
167 /////////////////////////////////////////////////
resetContextStats()168 void GrDirectContextPriv::resetContextStats() {
169 #if GR_GPU_STATS
170 this->context()->stats()->reset();
171 #endif
172 }
173
dumpContextStats(SkString * out) const174 void GrDirectContextPriv::dumpContextStats(SkString* out) const {
175 #if GR_GPU_STATS
176 this->context()->stats()->dump(out);
177 #endif
178 }
179
dumpContextStatsKeyValuePairs(TArray<SkString> * keys,TArray<double> * values) const180 void GrDirectContextPriv::dumpContextStatsKeyValuePairs(TArray<SkString>* keys,
181 TArray<double>* values) const {
182 #if GR_GPU_STATS
183 this->context()->stats()->dumpKeyValuePairs(keys, values);
184 #endif
185 }
186
printContextStats() const187 void GrDirectContextPriv::printContextStats() const {
188 SkString out;
189 this->dumpContextStats(&out);
190 SkDebugf("%s", out.c_str());
191 }
192
193 /////////////////////////////////////////////////
testingOnly_getFontAtlasImage(MaskFormat format,unsigned int index)194 sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(MaskFormat format,
195 unsigned int index) {
196 auto atlasManager = this->getAtlasManager();
197 if (!atlasManager) {
198 return nullptr;
199 }
200
201 unsigned int numActiveProxies;
202 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
203 if (index >= numActiveProxies || !views || !views[index].proxy()) {
204 return nullptr;
205 }
206
207 SkColorType colorType = skgpu::MaskFormatToColorType(format);
208 SkASSERT(views[index].proxy()->priv().isExact());
209 return sk_make_sp<SkImage_Ganesh>(sk_ref_sp(this->context()),
210 kNeedNewImageUniqueID,
211 views[index],
212 SkColorInfo(colorType, kPremul_SkAlphaType, nullptr));
213 }
214
testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject * cb)215 void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(
216 GrOnFlushCallbackObject* cb) {
217 this->context()->flushAndSubmit();
218 this->context()->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
219 }
220 #endif
221
222 // Both of these effects aggressively round to the nearest exact (N / 255) floating point values.
223 // 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)224 static std::unique_ptr<GrFragmentProcessor> make_premul_effect(
225 std::unique_ptr<GrFragmentProcessor> fp) {
226 if (!fp) {
227 return nullptr;
228 }
229
230 static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter,
231 "half4 main(half4 halfColor) {"
232 "float4 color = float4(halfColor);"
233 "color = floor(color * 255 + 0.5) / 255;"
234 "color.rgb = floor(color.rgb * color.a * 255 + 0.5) / 255;"
235 "return color;"
236 "}"
237 );
238
239 fp = GrSkSLFP::Make(effect, "ToPremul", std::move(fp), GrSkSLFP::OptFlags::kNone);
240 return GrFragmentProcessor::HighPrecision(std::move(fp));
241 }
242
make_unpremul_effect(std::unique_ptr<GrFragmentProcessor> fp)243 static std::unique_ptr<GrFragmentProcessor> make_unpremul_effect(
244 std::unique_ptr<GrFragmentProcessor> fp) {
245 if (!fp) {
246 return nullptr;
247 }
248
249 static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter,
250 "half4 main(half4 halfColor) {"
251 "float4 color = float4(halfColor);"
252 "color = floor(color * 255 + 0.5) / 255;"
253 "color.rgb = color.a <= 0 ? half3(0) : floor(color.rgb / color.a * 255 + 0.5) / 255;"
254 "return color;"
255 "}"
256 );
257
258 fp = GrSkSLFP::Make(effect, "ToUnpremul", std::move(fp), GrSkSLFP::OptFlags::kNone);
259 return GrFragmentProcessor::HighPrecision(std::move(fp));
260 }
261
test_for_preserving_PM_conversions(GrDirectContext * dContext)262 static bool test_for_preserving_PM_conversions(GrDirectContext* dContext) {
263 static constexpr int kSize = 256;
264 AutoTMalloc<uint32_t> data(kSize * kSize * 3);
265 uint32_t* srcData = data.get();
266
267 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
268 // values in row y. We set r, g, and b to the same value since they are handled identically.
269 for (int y = 0; y < kSize; ++y) {
270 for (int x = 0; x < kSize; ++x) {
271 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize*y + x]);
272 color[3] = y;
273 color[2] = std::min(x, y);
274 color[1] = std::min(x, y);
275 color[0] = std::min(x, y);
276 }
277 }
278
279 const SkImageInfo pmII =
280 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
281 const SkImageInfo upmII = pmII.makeAlphaType(kUnpremul_SkAlphaType);
282
283 auto readSFC =
284 dContext->priv().makeSFC(upmII, "ReadSfcForPMUPMConversion", SkBackingFit::kExact);
285 auto tempSFC =
286 dContext->priv().makeSFC(pmII, "TempSfcForPMUPMConversion", SkBackingFit::kExact);
287 if (!readSFC || !tempSFC) {
288 return false;
289 }
290
291 // This function is only ever called if we are in a GrDirectContext since we are calling read
292 // pixels here. Thus the pixel data will be uploaded immediately and we don't need to keep the
293 // pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
294 SkBitmap bitmap;
295 bitmap.installPixels(pmII, srcData, 4 * kSize);
296 bitmap.setImmutable();
297
298 auto dataView = std::get<0>(GrMakeUncachedBitmapProxyView(dContext, bitmap));
299 if (!dataView) {
300 return false;
301 }
302
303 uint32_t* firstRead = data.get() + kSize*kSize;
304 uint32_t* secondRead = data.get() + 2*kSize*kSize;
305 std::fill_n( firstRead, kSize*kSize, 0);
306 std::fill_n(secondRead, kSize*kSize, 0);
307
308 GrPixmap firstReadPM( upmII, firstRead, kSize*sizeof(uint32_t));
309 GrPixmap secondReadPM(upmII, secondRead, kSize*sizeof(uint32_t));
310
311 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
312 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
313 // We then verify that two reads produced the same values.
314
315 auto fp1 = make_unpremul_effect(GrTextureEffect::Make(std::move(dataView), bitmap.alphaType()));
316 readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp1));
317 if (!readSFC->readPixels(dContext, firstReadPM, {0, 0})) {
318 return false;
319 }
320
321 auto fp2 = make_premul_effect(
322 GrTextureEffect::Make(readSFC->readSurfaceView(), readSFC->colorInfo().alphaType()));
323 tempSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp2));
324
325 auto fp3 = make_unpremul_effect(
326 GrTextureEffect::Make(tempSFC->readSurfaceView(), tempSFC->colorInfo().alphaType()));
327 readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp3));
328
329 if (!readSFC->readPixels(dContext, secondReadPM, {0, 0})) {
330 return false;
331 }
332
333 for (int y = 0; y < kSize; ++y) {
334 for (int x = 0; x <= y; ++x) {
335 if (firstRead[kSize*y + x] != secondRead[kSize*y + x]) {
336 return false;
337 }
338 }
339 }
340
341 return true;
342 }
343
validPMUPMConversionExists()344 bool GrDirectContextPriv::validPMUPMConversionExists() {
345 ASSERT_SINGLE_OWNER
346
347 auto dContext = this->context();
348
349 if (!dContext->fDidTestPMConversions) {
350 dContext->fPMUPMConversionsRoundTrip = test_for_preserving_PM_conversions(dContext);
351 dContext->fDidTestPMConversions = true;
352 }
353
354 // The PM<->UPM tests fail or succeed together so we only need to check one.
355 return dContext->fPMUPMConversionsRoundTrip;
356 }
357
createPMToUPMEffect(std::unique_ptr<GrFragmentProcessor> fp)358 std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect(
359 std::unique_ptr<GrFragmentProcessor> fp) {
360 ASSERT_SINGLE_OWNER
361 // We should have already called this->priv().validPMUPMConversionExists() in this case
362 SkASSERT(this->context()->fDidTestPMConversions);
363 // ...and it should have succeeded
364 SkASSERT(this->validPMUPMConversionExists());
365
366 return make_unpremul_effect(std::move(fp));
367 }
368
createUPMToPMEffect(std::unique_ptr<GrFragmentProcessor> fp)369 std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect(
370 std::unique_ptr<GrFragmentProcessor> fp) {
371 ASSERT_SINGLE_OWNER
372 // We should have already called this->priv().validPMUPMConversionExists() in this case
373 SkASSERT(this->context()->fDidTestPMConversions);
374 // ...and it should have succeeded
375 SkASSERT(this->validPMUPMConversionExists());
376
377 return make_premul_effect(std::move(fp));
378 }
379