blob: 2bfef8a4f4f66135131f591cb3dfbeb63399c47f [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 Daniel46e366a2019-12-16 14:38:36 -050018#include "src/gpu/GrSurfaceContext.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000019#include "src/gpu/GrTexture.h"
Robert Phillipsae67c522021-03-03 11:03:38 -050020#include "src/gpu/GrThreadSafePipelineBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/SkGr.h"
Robert Phillipsf3868622021-08-04 13:27:43 -040022#include "src/gpu/SurfaceFillContext.h"
Robert Phillips7f11fb52019-12-03 13:35:19 -050023#include "src/gpu/effects/GrSkSLFP.h"
Brian Osman4c886ee2021-07-07 13:34:50 -040024#include "src/gpu/effects/GrTextureEffect.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040025#include "src/gpu/text/GrAtlasManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/text/GrTextBlobCache.h"
27#include "src/image/SkImage_Base.h"
28#include "src/image/SkImage_Gpu.h"
Robert Phillipsdbaf3172019-02-06 15:12:53 -050029
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040030#define ASSERT_OWNED_PROXY(P) \
Robert Phillipsea3489a2021-08-02 13:10:57 -040031 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this->context())
32#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->context()->singleOwner())
33#define RETURN_VALUE_IF_ABANDONED(value) if (this->context()->abandoned()) { return (value); }
Robert Phillipsc5058a62019-02-15 12:52:59 -050034
Robert Phillips80bfda82020-11-12 09:23:36 -050035GrSemaphoresSubmitted GrDirectContextPriv::flushSurfaces(
36 SkSpan<GrSurfaceProxy*> proxies,
37 SkSurface::BackendSurfaceAccess access,
38 const GrFlushInfo& info,
39 const GrBackendSurfaceMutableState* newState) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040040 ASSERT_SINGLE_OWNER
Robert Phillipsea3489a2021-08-02 13:10:57 -040041 GR_CREATE_TRACE_MARKER_CONTEXT("GrDirectContextPriv", "flushSurfaces", this->context());
Robert Phillips80bfda82020-11-12 09:23:36 -050042
Robert Phillipsea3489a2021-08-02 13:10:57 -040043 if (this->context()->abandoned()) {
Robert Phillips80bfda82020-11-12 09:23:36 -050044 if (info.fSubmittedProc) {
45 info.fSubmittedProc(info.fSubmittedContext, false);
46 }
47 if (info.fFinishedProc) {
48 info.fFinishedProc(info.fFinishedContext);
49 }
50 return GrSemaphoresSubmitted::kNo;
51 }
52
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050053#ifdef SK_DEBUG
54 for (GrSurfaceProxy* proxy : proxies) {
55 SkASSERT(proxy);
56 ASSERT_OWNED_PROXY(proxy);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040057 }
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050058#endif
Robert Phillipsea3489a2021-08-02 13:10:57 -040059 return this->context()->drawingManager()->flushSurfaces(proxies, access, info, newState);
Brian Salomon693bc2b2019-05-09 13:48:00 +000060}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050061
Robert Phillips07f675d2020-11-16 13:44:01 -050062void GrDirectContextPriv::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
Robert Phillipseb54bb52021-01-08 17:20:18 -050063 sk_sp<GrRenderTargetProxy> newDest,
Robert Phillips88b29612020-11-16 15:15:08 -050064 SkIPoint offset) {
Robert Phillipsea3489a2021-08-02 13:10:57 -040065 this->context()->drawingManager()->createDDLTask(std::move(ddl), std::move(newDest), offset);
Adlai Holler7580ad42020-06-24 13:45:25 -040066}
Robert Phillipsdbaf3172019-02-06 15:12:53 -050067
Adlai Hollera0693042020-10-14 11:23:11 -040068bool GrDirectContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) {
Robert Phillips7b0ed552020-02-20 12:45:19 -050069 GrGpu* gpu = this->getGpu();
70 if (!gpu) {
Robert Phillips43e7e4f2020-05-06 13:34:45 -040071 return false;
Robert Phillips7b0ed552020-02-20 12:45:19 -050072 }
73
Robert Phillips43e7e4f2020-05-06 13:34:45 -040074 return gpu->compile(desc, info);
Robert Phillips7b0ed552020-02-20 12:45:19 -050075}
76
77
Robert Phillipsdbaf3172019-02-06 15:12:53 -050078//////////////////////////////////////////////////////////////////////////////
Robert Phillipsdbaf3172019-02-06 15:12:53 -050079#if GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -050080
Adlai Hollera0693042020-10-14 11:23:11 -040081void GrDirectContextPriv::dumpCacheStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050082#if GR_CACHE_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -040083 this->context()->fResourceCache->dumpStats(out);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050084#endif
85}
86
Adlai Hollera0693042020-10-14 11:23:11 -040087void GrDirectContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
88 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050089#if GR_CACHE_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -040090 this->context()->fResourceCache->dumpStatsKeyValuePairs(keys, values);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050091#endif
92}
93
Adlai Hollera0693042020-10-14 11:23:11 -040094void GrDirectContextPriv::printCacheStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050095 SkString out;
96 this->dumpCacheStats(&out);
97 SkDebugf("%s", out.c_str());
98}
99
Robert Phillips273f1072020-05-05 13:03:07 -0400100/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400101void GrDirectContextPriv::resetGpuStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400102#if GR_GPU_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -0400103 this->context()->fGpu->stats()->reset();
Robert Phillips273f1072020-05-05 13:03:07 -0400104#endif
105}
106
Adlai Hollera0693042020-10-14 11:23:11 -0400107void GrDirectContextPriv::dumpGpuStats(SkString* out) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500108#if GR_GPU_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -0400109 this->context()->fGpu->stats()->dump(out);
110 if (auto builder = this->context()->fGpu->pipelineBuilder()) {
Robert Phillipsae67c522021-03-03 11:03:38 -0500111 builder->stats()->dump(out);
112 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500113#endif
114}
115
Adlai Hollera0693042020-10-14 11:23:11 -0400116void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
117 SkTArray<double>* values) const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500118#if GR_GPU_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -0400119 this->context()->fGpu->stats()->dumpKeyValuePairs(keys, values);
120 if (auto builder = this->context()->fGpu->pipelineBuilder()) {
Robert Phillipsae67c522021-03-03 11:03:38 -0500121 builder->stats()->dumpKeyValuePairs(keys, values);
122 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500123#endif
124}
125
Adlai Hollera0693042020-10-14 11:23:11 -0400126void GrDirectContextPriv::printGpuStats() const {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500127 SkString out;
128 this->dumpGpuStats(&out);
129 SkDebugf("%s", out.c_str());
130}
131
Robert Phillips273f1072020-05-05 13:03:07 -0400132/////////////////////////////////////////////////
Robert Phillipsea3489a2021-08-02 13:10:57 -0400133void GrDirectContextPriv::resetContextStats() {
Robert Phillips273f1072020-05-05 13:03:07 -0400134#if GR_GPU_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -0400135 this->context()->stats()->reset();
Robert Phillips273f1072020-05-05 13:03:07 -0400136#endif
137}
138
Adlai Hollera0693042020-10-14 11:23:11 -0400139void GrDirectContextPriv::dumpContextStats(SkString* out) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400140#if GR_GPU_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -0400141 this->context()->stats()->dump(out);
Robert Phillips273f1072020-05-05 13:03:07 -0400142#endif
143}
144
Adlai Hollera0693042020-10-14 11:23:11 -0400145void GrDirectContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys,
146 SkTArray<double>* values) const {
Robert Phillips273f1072020-05-05 13:03:07 -0400147#if GR_GPU_STATS
Robert Phillipsea3489a2021-08-02 13:10:57 -0400148 this->context()->stats()->dumpKeyValuePairs(keys, values);
Robert Phillips273f1072020-05-05 13:03:07 -0400149#endif
150}
151
Adlai Hollera0693042020-10-14 11:23:11 -0400152void GrDirectContextPriv::printContextStats() const {
Robert Phillips273f1072020-05-05 13:03:07 -0400153 SkString out;
154 this->dumpContextStats(&out);
155 SkDebugf("%s", out.c_str());
156}
157
158/////////////////////////////////////////////////
Adlai Hollera0693042020-10-14 11:23:11 -0400159sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format,
160 unsigned int index) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500161 auto atlasManager = this->getAtlasManager();
162 if (!atlasManager) {
163 return nullptr;
164 }
165
166 unsigned int numActiveProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500167 const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies);
168 if (index >= numActiveProxies || !views || !views[index].proxy()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500169 return nullptr;
170 }
171
Greg Daniel7c165a42020-01-22 12:22:36 -0500172 SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format));
Greg Daniel9715b6c2019-12-10 15:03:10 -0500173 SkASSERT(views[index].proxy()->priv().isExact());
Robert Phillipsea3489a2021-08-02 13:10:57 -0400174 return sk_make_sp<SkImage_Gpu>(sk_ref_sp(this->context()),
Brian Salomon9a56eb72021-04-20 16:52:11 -0400175 kNeedNewImageUniqueID,
176 views[index],
177 SkColorInfo(colorType, kPremul_SkAlphaType, nullptr));
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500178}
179
Adlai Hollera0693042020-10-14 11:23:11 -0400180void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(
181 GrOnFlushCallbackObject* cb) {
Robert Phillipsea3489a2021-08-02 13:10:57 -0400182 this->context()->flushAndSubmit();
183 this->context()->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500184}
185#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -0400186
Brian Osman4c886ee2021-07-07 13:34:50 -0400187// Both of these effects aggressively round to the nearest exact (N / 255) floating point values.
188// This lets us find a round-trip preserving pair on some GPUs that do odd byte to float conversion.
189static std::unique_ptr<GrFragmentProcessor> make_premul_effect(
190 std::unique_ptr<GrFragmentProcessor> fp) {
191 if (!fp) {
192 return nullptr;
193 }
194
195 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"(
196 half4 main(half4 color) {
197 color = floor(color * 255 + 0.5) / 255;
198 color.rgb = floor(color.rgb * color.a * 255 + 0.5) / 255;
199 return color;
200 }
201 )");
202
203 fp = GrSkSLFP::Make(effect, "ToPremul", std::move(fp), GrSkSLFP::OptFlags::kNone);
204 return GrFragmentProcessor::HighPrecision(std::move(fp));
205}
206
207static std::unique_ptr<GrFragmentProcessor> make_unpremul_effect(
208 std::unique_ptr<GrFragmentProcessor> fp) {
209 if (!fp) {
210 return nullptr;
211 }
212
213 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"(
214 half4 main(half4 color) {
215 color = floor(color * 255 + 0.5) / 255;
216 color.rgb = color.a <= 0 ? half3(0) : floor(color.rgb / color.a * 255 + 0.5) / 255;
217 return color;
218 }
219 )");
220
221 fp = GrSkSLFP::Make(effect, "ToUnpremul", std::move(fp), GrSkSLFP::OptFlags::kNone);
222 return GrFragmentProcessor::HighPrecision(std::move(fp));
223}
224
225static bool test_for_preserving_PM_conversions(GrDirectContext* dContext) {
226 static constexpr int kSize = 256;
227 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
228 uint32_t* srcData = data.get();
229
230 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
231 // values in row y. We set r, g, and b to the same value since they are handled identically.
232 for (int y = 0; y < kSize; ++y) {
233 for (int x = 0; x < kSize; ++x) {
234 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize*y + x]);
235 color[3] = y;
236 color[2] = std::min(x, y);
237 color[1] = std::min(x, y);
238 color[0] = std::min(x, y);
239 }
240 }
241
242 const SkImageInfo pmII =
243 SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
244 const SkImageInfo upmII = pmII.makeAlphaType(kUnpremul_SkAlphaType);
245
Robert Phillips33bf2b52021-08-02 11:14:38 -0400246 auto readSFC = dContext->priv().makeSFC(upmII, SkBackingFit::kExact);
247 auto tempSFC = dContext->priv().makeSFC(pmII, SkBackingFit::kExact);
Brian Osman4c886ee2021-07-07 13:34:50 -0400248 if (!readSFC || !tempSFC) {
249 return false;
250 }
251
252 // This function is only ever called if we are in a GrDirectContext since we are calling read
253 // pixels here. Thus the pixel data will be uploaded immediately and we don't need to keep the
254 // pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
255 SkBitmap bitmap;
256 bitmap.installPixels(pmII, srcData, 4 * kSize);
257 bitmap.setImmutable();
258
259 auto dataView = std::get<0>(GrMakeUncachedBitmapProxyView(dContext, bitmap));
260 if (!dataView) {
261 return false;
262 }
263
264 uint32_t* firstRead = data.get() + kSize*kSize;
265 uint32_t* secondRead = data.get() + 2*kSize*kSize;
266 std::fill_n( firstRead, kSize*kSize, 0);
267 std::fill_n(secondRead, kSize*kSize, 0);
268
269 GrPixmap firstReadPM( upmII, firstRead, kSize*sizeof(uint32_t));
270 GrPixmap secondReadPM(upmII, secondRead, kSize*sizeof(uint32_t));
271
272 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
273 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
274 // We then verify that two reads produced the same values.
275
276 auto fp1 = make_unpremul_effect(GrTextureEffect::Make(std::move(dataView), bitmap.alphaType()));
277 readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp1));
278 if (!readSFC->readPixels(dContext, firstReadPM, {0, 0})) {
279 return false;
280 }
281
282 auto fp2 = make_premul_effect(
283 GrTextureEffect::Make(readSFC->readSurfaceView(), readSFC->colorInfo().alphaType()));
284 tempSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp2));
285
286 auto fp3 = make_unpremul_effect(
287 GrTextureEffect::Make(tempSFC->readSurfaceView(), tempSFC->colorInfo().alphaType()));
288 readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp3));
289
290 if (!readSFC->readPixels(dContext, secondReadPM, {0, 0})) {
291 return false;
292 }
293
294 for (int y = 0; y < kSize; ++y) {
295 for (int x = 0; x <= y; ++x) {
296 if (firstRead[kSize*y + x] != secondRead[kSize*y + x]) {
297 return false;
298 }
299 }
300 }
301
302 return true;
303}
304
Adlai Hollera0693042020-10-14 11:23:11 -0400305bool GrDirectContextPriv::validPMUPMConversionExists() {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400306 ASSERT_SINGLE_OWNER
Robert Phillips07531a02020-07-15 15:11:09 -0400307
Robert Phillipsea3489a2021-08-02 13:10:57 -0400308 auto dContext = this->context();
309
310 if (!dContext->fDidTestPMConversions) {
311 dContext->fPMUPMConversionsRoundTrip = test_for_preserving_PM_conversions(dContext);
312 dContext->fDidTestPMConversions = true;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400313 }
314
315 // The PM<->UPM tests fail or succeed together so we only need to check one.
Robert Phillipsea3489a2021-08-02 13:10:57 -0400316 return dContext->fPMUPMConversionsRoundTrip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400317}
318
Adlai Hollera0693042020-10-14 11:23:11 -0400319std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400320 std::unique_ptr<GrFragmentProcessor> fp) {
321 ASSERT_SINGLE_OWNER
322 // We should have already called this->priv().validPMUPMConversionExists() in this case
Robert Phillipsea3489a2021-08-02 13:10:57 -0400323 SkASSERT(this->context()->fDidTestPMConversions);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400324 // ...and it should have succeeded
325 SkASSERT(this->validPMUPMConversionExists());
326
Brian Osman4c886ee2021-07-07 13:34:50 -0400327 return make_unpremul_effect(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400328}
329
Adlai Hollera0693042020-10-14 11:23:11 -0400330std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect(
Greg Daniel6eb8c242019-06-05 10:22:24 -0400331 std::unique_ptr<GrFragmentProcessor> fp) {
332 ASSERT_SINGLE_OWNER
333 // We should have already called this->priv().validPMUPMConversionExists() in this case
Robert Phillipsea3489a2021-08-02 13:10:57 -0400334 SkASSERT(this->context()->fDidTestPMConversions);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400335 // ...and it should have succeeded
336 SkASSERT(this->validPMUPMConversionExists());
337
Brian Osman4c886ee2021-07-07 13:34:50 -0400338 return make_premul_effect(std::move(fp));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400339}