blob: 82e1491ffd14a577bf8ee3019f004f078d0928e3 [file] [log] [blame]
Robert Phillipsdbaf3172019-02-06 15:12:53 -05001/*
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
Adlai Hollera0693042020-10-14 11:23:11 -04008#include "src/gpu/GrDirectContextPriv.h"
Robert Phillipsdbaf3172019-02-06 15:12:53 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrContextThreadSafeProxy.h"
Robert Phillips4e105e22020-07-16 09:18:50 -040011#include "include/gpu/GrDirectContext.h"
Brian Osman4c886ee2021-07-07 13:34:50 -040012#include "src/core/SkRuntimeEffectPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrContextThreadSafeProxyPriv.h"
14#include "src/gpu/GrDrawingManager.h"
15#include "src/gpu/GrGpu.h"
16#include "src/gpu/GrMemoryPool.h"
Robert Phillips516405c2021-06-04 16:37:30 -040017#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000018#include "src/gpu/GrTexture.h"
Robert Phillipsae67c522021-03-03 11:03:38 -050019#include "src/gpu/GrThreadSafePipelineBuilder.h"
Robert Phillips06273bc2021-08-11 15:43:50 -040020#include "src/gpu/GrTracing.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/SkGr.h"
Robert Phillips53eaa642021-08-10 13:49:51 -040022#include "src/gpu/SurfaceContext.h"
Robert Phillipsf3868622021-08-04 13:27:43 -040023#include "src/gpu/SurfaceFillContext.h"
Robert Phillips7f11fb52019-12-03 13:35:19 -050024#include "src/gpu/effects/GrSkSLFP.h"
Brian Osman4c886ee2021-07-07 13:34:50 -040025#include "src/gpu/effects/GrTextureEffect.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040026#include "src/gpu/text/GrAtlasManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/text/GrTextBlobCache.h"
28#include "src/image/SkImage_Base.h"
29#include "src/image/SkImage_Gpu.h"
Robert Phillipsdbaf3172019-02-06 15:12:53 -050030
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040031#define ASSERT_OWNED_PROXY(P) \
Robert Phillipsea3489a2021-08-02 13:10:57 -040032 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); }
Robert Phillipsc5058a62019-02-15 12:52:59 -050035
Robert Phillips80bfda82020-11-12 09:23:36 -050036GrSemaphoresSubmitted GrDirectContextPriv::flushSurfaces(
37 SkSpan<GrSurfaceProxy*> proxies,
38 SkSurface::BackendSurfaceAccess access,
39 const GrFlushInfo& info,
40 const GrBackendSurfaceMutableState* newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040041 ASSERT_SINGLE_OWNER
Robert Phillipsea3489a2021-08-02 13:10:57 -040042 GR_CREATE_TRACE_MARKER_CONTEXT("GrDirectContextPriv", "flushSurfaces", this->context());
Robert Phillips80bfda82020-11-12 09:23:36 -050043
Robert Phillipsea3489a2021-08-02 13:10:57 -040044 if (this->context()->abandoned()) {
Robert Phillips80bfda82020-11-12 09:23:36 -050045 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
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050054#ifdef SK_DEBUG
55 for (GrSurfaceProxy* proxy : proxies) {
56 SkASSERT(proxy);
57 ASSERT_OWNED_PROXY(proxy);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040058 }
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050059#endif
Robert Phillipsea3489a2021-08-02 13:10:57 -040060 return this->context()->drawingManager()->flushSurfaces(proxies, access, info, newState);
Brian Salomon693bc2b2019-05-09 13:48:00 +000061}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050062
Robert Phillips07f675d2020-11-16 13:44:01 -050063void GrDirectContextPriv::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
Robert Phillipseb54bb52021-01-08 17:20:18 -050064 sk_sp<GrRenderTargetProxy> newDest,
Robert Phillips88b29612020-11-16 15:15:08 -050065 SkIPoint offset) {
Robert Phillipsea3489a2021-08-02 13:10:57 -040066 this->context()->drawingManager()->createDDLTask(std::move(ddl), std::move(newDest), offset);
Adlai Holler7580ad42020-06-24 13:45:25 -040067}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050068
Adlai Hollera0693042020-10-14 11:23:11 -040069bool GrDirectContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
Robert Phillips7b0ed552020-02-20 12:45:19 -050070 GrGpu* gpu = this->getGpu();
71 if (!gpu) {
Robert Phillips43e7e4f2020-05-06 13:34:45 -040072 return false;
Robert Phillips7b0ed552020-02-20 12:45:19 -050073 }
74
Robert Phillips43e7e4f2020-05-06 13:34:45 -040075 return gpu->compile(desc, info);
Robert Phillips7b0ed552020-02-20 12:45:19 -050076}
77
78
Robert Phillipsdbaf3172019-02-06 15:12:53 -050079//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -050080#if GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -050081
Adlai Hollera0693042020-10-14 11:23:11 -040082void GrDirectContextPriv::dumpCacheStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050083#if GR_CACHE_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -040084 this->context()->fResourceCache->dumpStats(out);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050085#endif
86}
87
Adlai Hollera0693042020-10-14 11:23:11 -040088void GrDirectContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
89 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050090#if GR_CACHE_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -040091 this->context()->fResourceCache->dumpStatsKeyValuePairs(keys, values);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050092#endif
93}
94
Adlai Hollera0693042020-10-14 11:23:11 -040095void GrDirectContextPriv::printCacheStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050096 SkString out;
97 this->dumpCacheStats(&out);
98 SkDebugf("%s", out.c_str());
99}
100
Robert Phillips273f1072020-05-05 13:03:07 -0400101/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400102void GrDirectContextPriv::resetGpuStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400103#if GR_GPU_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -0400104 this->context()->fGpu->stats()->reset();
Robert Phillips273f1072020-05-05 13:03:07 -0400105#endif
106}
107
Adlai Hollera0693042020-10-14 11:23:11 -0400108void GrDirectContextPriv::dumpGpuStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500109#if GR_GPU_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -0400110 this->context()->fGpu->stats()->dump(out);
111 if (auto builder = this->context()->fGpu->pipelineBuilder()) {
Robert Phillipsae67c522021-03-03 11:03:38 -0500112 builder->stats()->dump(out);
113 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500114#endif
115}
116
Adlai Hollera0693042020-10-14 11:23:11 -0400117void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
118 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500119#if GR_GPU_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -0400120 this->context()->fGpu->stats()->dumpKeyValuePairs(keys, values);
121 if (auto builder = this->context()->fGpu->pipelineBuilder()) {
Robert Phillipsae67c522021-03-03 11:03:38 -0500122 builder->stats()->dumpKeyValuePairs(keys, values);
123 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500124#endif
125}
126
Adlai Hollera0693042020-10-14 11:23:11 -0400127void GrDirectContextPriv::printGpuStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500128 SkString out;
129 this->dumpGpuStats(&out);
130 SkDebugf("%s", out.c_str());
131}
132
Robert Phillips273f1072020-05-05 13:03:07 -0400133/////////////////////////////////////////////////
Robert Phillipsea3489a2021-08-02 13:10:57 -0400134void GrDirectContextPriv::resetContextStats() {
Robert Phillips273f1072020-05-05 13:03:07 -0400135#if GR_GPU_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -0400136 this->context()->stats()->reset();
Robert Phillips273f1072020-05-05 13:03:07 -0400137#endif
138}
139
Adlai Hollera0693042020-10-14 11:23:11 -0400140void GrDirectContextPriv::dumpContextStats(SkString* out) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400141#if GR_GPU_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -0400142 this->context()->stats()->dump(out);
Robert Phillips273f1072020-05-05 13:03:07 -0400143#endif
144}
145
Adlai Hollera0693042020-10-14 11:23:11 -0400146void GrDirectContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys,
147 SkTArray<double>* values) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400148#if GR_GPU_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -0400149 this->context()->stats()->dumpKeyValuePairs(keys, values);
Robert Phillips273f1072020-05-05 13:03:07 -0400150#endif
151}
152
Adlai Hollera0693042020-10-14 11:23:11 -0400153void GrDirectContextPriv::printContextStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400154 SkString out;
155 this->dumpContextStats(&out);
156 SkDebugf("%s", out.c_str());
157}
158
159/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400160sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format,
161 unsigned int index) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500162 auto atlasManager = this->getAtlasManager();
163 if (!atlasManager) {
164 return nullptr;
165 }
166
167 unsigned int numActiveProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500168 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
169 if (index >= numActiveProxies || !views || !views[index].proxy()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500170 return nullptr;
171 }
172
Greg Daniel7c165a42020-01-22 12:22:36 -0500173 SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
Greg Daniel9715b6c2019-12-10 15:03:10 -0500174 SkASSERT(views[index].proxy()->priv().isExact());
Robert Phillipsea3489a2021-08-02 13:10:57 -0400175 return sk_make_sp<SkImage_Gpu>(sk_ref_sp(this->context()),
Brian Salomon9a56eb72021-04-20 16:52:11 -0400176 kNeedNewImageUniqueID,
177 views[index],
178 SkColorInfo(colorType, kPremul_SkAlphaType, nullptr));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500179}
180
Adlai Hollera0693042020-10-14 11:23:11 -0400181void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(
182 GrOnFlushCallbackObject* cb) {
Robert Phillipsea3489a2021-08-02 13:10:57 -0400183 this->context()->flushAndSubmit();
184 this->context()->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500185}
186#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400187
Brian Osman4c886ee2021-07-07 13:34:50 -0400188// 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.
190static 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 color) {
198 color = floor(color * 255 + 0.5) / 255;
199 color.rgb = floor(color.rgb * color.a * 255 + 0.5) / 255;
200 return color;
201 }
202 )");
203
204 fp = GrSkSLFP::Make(effect, "ToPremul", std::move(fp), GrSkSLFP::OptFlags::kNone);
205 return GrFragmentProcessor::HighPrecision(std::move(fp));
206}
207
208static std::unique_ptr<GrFragmentProcessor> make_unpremul_effect(
209 std::unique_ptr<GrFragmentProcessor> fp) {
210 if (!fp) {
211 return nullptr;
212 }
213
214 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"(
215 half4 main(half4 color) {
216 color = floor(color * 255 + 0.5) / 255;
217 color.rgb = color.a <= 0 ? half3(0) : floor(color.rgb / color.a * 255 + 0.5) / 255;
218 return color;
219 }
220 )");
221
222 fp = GrSkSLFP::Make(effect, "ToUnpremul", std::move(fp), GrSkSLFP::OptFlags::kNone);
223 return GrFragmentProcessor::HighPrecision(std::move(fp));
224}
225
226static bool test_for_preserving_PM_conversions(GrDirectContext* dContext) {
227 static constexpr int kSize = 256;
228 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
229 uint32_t* srcData = data.get();
230
231 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
232 // values in row y. We set r, g, and b to the same value since they are handled identically.
233 for (int y = 0; y < kSize; ++y) {
234 for (int x = 0; x < kSize; ++x) {
235 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize*y + x]);
236 color[3] = y;
237 color[2] = std::min(x, y);
238 color[1] = std::min(x, y);
239 color[0] = std::min(x, y);
240 }
241 }
242
243 const SkImageInfo pmII =
244 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
245 const SkImageInfo upmII = pmII.makeAlphaType(kUnpremul_SkAlphaType);
246
Robert Phillips33bf2b52021-08-02 11:14:38 -0400247 auto readSFC = dContext->priv().makeSFC(upmII, SkBackingFit::kExact);
248 auto tempSFC = dContext->priv().makeSFC(pmII, SkBackingFit::kExact);
Brian Osman4c886ee2021-07-07 13:34:50 -0400249 if (!readSFC || !tempSFC) {
250 return false;
251 }
252
253 // This function is only ever called if we are in a GrDirectContext since we are calling read
254 // pixels here. Thus the pixel data will be uploaded immediately and we don't need to keep the
255 // pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
256 SkBitmap bitmap;
257 bitmap.installPixels(pmII, srcData, 4 * kSize);
258 bitmap.setImmutable();
259
260 auto dataView = std::get<0>(GrMakeUncachedBitmapProxyView(dContext, bitmap));
261 if (!dataView) {
262 return false;
263 }
264
265 uint32_t* firstRead = data.get() + kSize*kSize;
266 uint32_t* secondRead = data.get() + 2*kSize*kSize;
267 std::fill_n( firstRead, kSize*kSize, 0);
268 std::fill_n(secondRead, kSize*kSize, 0);
269
270 GrPixmap firstReadPM( upmII, firstRead, kSize*sizeof(uint32_t));
271 GrPixmap secondReadPM(upmII, secondRead, kSize*sizeof(uint32_t));
272
273 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
274 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
275 // We then verify that two reads produced the same values.
276
277 auto fp1 = make_unpremul_effect(GrTextureEffect::Make(std::move(dataView), bitmap.alphaType()));
278 readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp1));
279 if (!readSFC->readPixels(dContext, firstReadPM, {0, 0})) {
280 return false;
281 }
282
283 auto fp2 = make_premul_effect(
284 GrTextureEffect::Make(readSFC->readSurfaceView(), readSFC->colorInfo().alphaType()));
285 tempSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp2));
286
287 auto fp3 = make_unpremul_effect(
288 GrTextureEffect::Make(tempSFC->readSurfaceView(), tempSFC->colorInfo().alphaType()));
289 readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp3));
290
291 if (!readSFC->readPixels(dContext, secondReadPM, {0, 0})) {
292 return false;
293 }
294
295 for (int y = 0; y < kSize; ++y) {
296 for (int x = 0; x <= y; ++x) {
297 if (firstRead[kSize*y + x] != secondRead[kSize*y + x]) {
298 return false;
299 }
300 }
301 }
302
303 return true;
304}
305
Adlai Hollera0693042020-10-14 11:23:11 -0400306bool GrDirectContextPriv::validPMUPMConversionExists() {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400307 ASSERT_SINGLE_OWNER
Robert Phillips07531a02020-07-15 15:11:09 -0400308
Robert Phillipsea3489a2021-08-02 13:10:57 -0400309 auto dContext = this->context();
310
311 if (!dContext->fDidTestPMConversions) {
312 dContext->fPMUPMConversionsRoundTrip = test_for_preserving_PM_conversions(dContext);
313 dContext->fDidTestPMConversions = true;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400314 }
315
316 // The PM<->UPM tests fail or succeed together so we only need to check one.
Robert Phillipsea3489a2021-08-02 13:10:57 -0400317 return dContext->fPMUPMConversionsRoundTrip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400318}
319
Adlai Hollera0693042020-10-14 11:23:11 -0400320std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400321 std::unique_ptr<GrFragmentProcessor> fp) {
322 ASSERT_SINGLE_OWNER
323 // We should have already called this->priv().validPMUPMConversionExists() in this case
Robert Phillipsea3489a2021-08-02 13:10:57 -0400324 SkASSERT(this->context()->fDidTestPMConversions);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400325 // ...and it should have succeeded
326 SkASSERT(this->validPMUPMConversionExists());
327
Brian Osman4c886ee2021-07-07 13:34:50 -0400328 return make_unpremul_effect(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400329}
330
Adlai Hollera0693042020-10-14 11:23:11 -0400331std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400332 std::unique_ptr<GrFragmentProcessor> fp) {
333 ASSERT_SINGLE_OWNER
334 // We should have already called this->priv().validPMUPMConversionExists() in this case
Robert Phillipsea3489a2021-08-02 13:10:57 -0400335 SkASSERT(this->context()->fDidTestPMConversions);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400336 // ...and it should have succeeded
337 SkASSERT(this->validPMUPMConversionExists());
338
Brian Osman4c886ee2021-07-07 13:34:50 -0400339 return make_premul_effect(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400340}