Flutter Engine
The Flutter Engine
GrDirectContextPriv.cpp
Go to the documentation of this file.
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
9
25#include "src/gpu/ganesh/SkGr.h"
34
35using namespace skia_private;
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
46 const GrFlushInfo& info,
47 const skgpu::MutableTextureState* newState) {
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 }
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
72 this->context()->drawingManager()->createDDLTask(std::move(ddl), std::move(newDest));
73}
74
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
88void GrDirectContextPriv::dumpCacheStats(SkString* out) const {
89#if GR_CACHE_STATS
90 this->context()->fResourceCache->dumpStats(out);
91#endif
92}
93
94void 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
101void GrDirectContextPriv::printCacheStats() const {
103 this->dumpCacheStats(&out);
104 SkDebugf("%s", out.c_str());
105}
106
107/////////////////////////////////////////////////
108void GrDirectContextPriv::resetGpuStats() const {
109#if GR_GPU_STATS
110 this->context()->fGpu->stats()->reset();
111#endif
112}
113
114void 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
123void 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
133void GrDirectContextPriv::printGpuStats() const {
135 this->dumpGpuStats(&out);
136 SkDebugf("%s", out.c_str());
137}
138
139/////////////////////////////////////////////////
140void GrDirectContextPriv::resetContextStats() {
141#if GR_GPU_STATS
142 this->context()->stats()->reset();
143#endif
144}
145
146void GrDirectContextPriv::dumpContextStats(SkString* out) const {
147#if GR_GPU_STATS
148 this->context()->stats()->dump(out);
149#endif
150}
151
152void 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
159void GrDirectContextPriv::printContextStats() const {
161 this->dumpContextStats(&out);
162 SkDebugf("%s", out.c_str());
163}
164
165/////////////////////////////////////////////////
166sk_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
180 SkASSERT(views[index].proxy()->priv().isExact());
181 return sk_make_sp<SkImage_Ganesh>(sk_ref_sp(this->context()),
183 views[index],
185}
186
187void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(
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.
196static std::unique_ptr<GrFragmentProcessor> make_premul_effect(
197 std::unique_ptr<GrFragmentProcessor> fp) {
198 if (!fp) {
199 return nullptr;
200 }
201
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
215static std::unique_ptr<GrFragmentProcessor> make_unpremul_effect(
216 std::unique_ptr<GrFragmentProcessor> fp) {
217 if (!fp) {
218 return nullptr;
219 }
220
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
235 static constexpr int kSize = 256;
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 =
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.
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
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
330std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect(
331 std::unique_ptr<GrFragmentProcessor> fp) {
333 // We should have already called this->priv().validPMUPMConversionExists() in this case
334 SkASSERT(this->context()->fDidTestPMConversions);
335 // ...and it should have succeeded
337
338 return make_unpremul_effect(std::move(fp));
339}
340
341std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect(
342 std::unique_ptr<GrFragmentProcessor> fp) {
344 // We should have already called this->priv().validPMUPMConversionExists() in this case
345 SkASSERT(this->context()->fDidTestPMConversions);
346 // ...and it should have succeeded
348
349 return make_premul_effect(std::move(fp));
350}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
static bool test_for_preserving_PM_conversions(GrDirectContext *dContext)
static std::unique_ptr< GrFragmentProcessor > make_unpremul_effect(std::unique_ptr< GrFragmentProcessor > fp)
#define ASSERT_OWNED_PROXY(P)
static std::unique_ptr< GrFragmentProcessor > make_premul_effect(std::unique_ptr< GrFragmentProcessor > fp)
#define ASSERT_SINGLE_OWNER
#define GR_CREATE_TRACE_MARKER_CONTEXT(classname, op, context)
Definition: GrTracing.h:18
GrSemaphoresSubmitted
Definition: GrTypes.h:229
kUnpremul_SkAlphaType
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkColorType
Definition: SkColorType.h:19
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
std::tuple< GrSurfaceProxyView, GrColorType > GrMakeUncachedBitmapProxyView(GrRecordingContext *rContext, const SkBitmap &bitmap, skgpu::Mipmapped mipmapped, SkBackingFit fit, skgpu::Budgeted budgeted)
Definition: SkGr.cpp:253
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
@ kNeedNewImageUniqueID
Definition: SkImage_Base.h:33
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
SkRuntimeEffect * SkMakeRuntimeEffect(SkRuntimeEffect::Result(*make)(SkString, const SkRuntimeEffect::Options &), const char *sksl, SkRuntimeEffect::Options options=SkRuntimeEffect::Options{})
void createDDLTask(sk_sp< const GrDeferredDisplayList >, sk_sp< GrRenderTargetProxy > newDest)
std::unique_ptr< GrFragmentProcessor > createPMToUPMEffect(std::unique_ptr< GrFragmentProcessor >)
std::unique_ptr< GrFragmentProcessor > createUPMToPMEffect(std::unique_ptr< GrFragmentProcessor >)
GrAtlasManager * getAtlasManager()
bool compile(const GrProgramDesc &, const GrProgramInfo &)
GrSemaphoresSubmitted flushSurfaces(SkSpan< GrSurfaceProxy * >, SkSurfaces::BackendSurfaceAccess=SkSurfaces::BackendSurfaceAccess::kNoAccess, const GrFlushInfo &={}, const skgpu::MutableTextureState *newState=nullptr)
GrDirectContext * context()
void flushAndSubmit(GrSyncCpu sync=GrSyncCpu::kNo)
GrDirectContextPriv priv()
void createDDLTask(sk_sp< const GrDeferredDisplayList >, sk_sp< GrRenderTargetProxy > newDest)
GrSemaphoresSubmitted flushSurfaces(SkSpan< GrSurfaceProxy * >, SkSurfaces::BackendSurfaceAccess, const GrFlushInfo &, const skgpu::MutableTextureState *newState)
static std::unique_ptr< GrFragmentProcessor > HighPrecision(std::unique_ptr< GrFragmentProcessor >)
Definition: GrGpu.h:62
virtual bool compile(const GrProgramDesc &, const GrProgramInfo &)=0
std::unique_ptr< skgpu::ganesh::SurfaceFillContext > makeSFC(GrImageInfo, std::string_view label, SkBackingFit=SkBackingFit::kExact, int sampleCount=1, skgpu::Mipmapped=skgpu::Mipmapped::kNo, skgpu::Protected=skgpu::Protected::kNo, GrSurfaceOrigin=kTopLeft_GrSurfaceOrigin, skgpu::Budgeted=skgpu::Budgeted::kYes)
GrDrawingManager * drawingManager()
static std::unique_ptr< GrSkSLFP > Make(const SkRuntimeEffect *effect, const char *name, std::unique_ptr< GrFragmentProcessor > inputFP, OptFlags optFlags, Args &&... args)
Definition: GrSkSLFP.h:159
static std::unique_ptr< GrFragmentProcessor > Make(GrSurfaceProxyView, SkAlphaType, const SkMatrix &=SkMatrix::I(), GrSamplerState::Filter=GrSamplerState::Filter::kNearest, GrSamplerState::MipmapMode mipmapMode=GrSamplerState::MipmapMode::kNone)
static Result MakeForColorFilter(SkString sksl, const Options &)
DlColor color
FlPixelBufferTexturePrivate * priv
uint32_t uint32_t * format
static float min(float r, float g, float b)
Definition: hsl.cpp:48
constexpr int kSize
double y
double x
BackendSurfaceAccess
Definition: SkSurface.h:44
Definition: bitmap.py:1
const uint32_t fp
const myers::Point & get< 0 >(const myers::Segment &s)
Definition: Myers.h:80
static constexpr SkColorType MaskFormatToColorType(MaskFormat format)
Definition: AtlasTypes.h:122
MaskFormat
Definition: AtlasTypes.h:98
GrGpuSubmittedProc fSubmittedProc
Definition: GrTypes.h:221
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const
Definition: SkImageInfo.h:466
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63