Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 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 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 8 | #include "src/gpu/GrDirectContextPriv.h" |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrContextThreadSafeProxy.h" |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrDirectContext.h" |
Brian Osman | 4c886ee | 2021-07-07 13:34:50 -0400 | [diff] [blame] | 12 | #include "src/core/SkRuntimeEffectPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrContextThreadSafeProxyPriv.h" |
| 14 | #include "src/gpu/GrDrawingManager.h" |
| 15 | #include "src/gpu/GrGpu.h" |
| 16 | #include "src/gpu/GrMemoryPool.h" |
Robert Phillips | 516405c | 2021-06-04 16:37:30 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrRecordingContextPriv.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 18 | #include "src/gpu/GrTexture.h" |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrThreadSafePipelineBuilder.h" |
Robert Phillips | 06273bc | 2021-08-11 15:43:50 -0400 | [diff] [blame^] | 20 | #include "src/gpu/GrTracing.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/SkGr.h" |
Robert Phillips | 53eaa64 | 2021-08-10 13:49:51 -0400 | [diff] [blame] | 22 | #include "src/gpu/SurfaceContext.h" |
Robert Phillips | f386862 | 2021-08-04 13:27:43 -0400 | [diff] [blame] | 23 | #include "src/gpu/SurfaceFillContext.h" |
Robert Phillips | 7f11fb5 | 2019-12-03 13:35:19 -0500 | [diff] [blame] | 24 | #include "src/gpu/effects/GrSkSLFP.h" |
Brian Osman | 4c886ee | 2021-07-07 13:34:50 -0400 | [diff] [blame] | 25 | #include "src/gpu/effects/GrTextureEffect.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 26 | #include "src/gpu/text/GrAtlasManager.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/gpu/text/GrTextBlobCache.h" |
| 28 | #include "src/image/SkImage_Base.h" |
| 29 | #include "src/image/SkImage_Gpu.h" |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 30 | |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 31 | #define ASSERT_OWNED_PROXY(P) \ |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 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); } |
Robert Phillips | c5058a6 | 2019-02-15 12:52:59 -0500 | [diff] [blame] | 35 | |
Robert Phillips | 80bfda8 | 2020-11-12 09:23:36 -0500 | [diff] [blame] | 36 | GrSemaphoresSubmitted GrDirectContextPriv::flushSurfaces( |
| 37 | SkSpan<GrSurfaceProxy*> proxies, |
| 38 | SkSurface::BackendSurfaceAccess access, |
| 39 | const GrFlushInfo& info, |
| 40 | const GrBackendSurfaceMutableState* newState) { |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 41 | ASSERT_SINGLE_OWNER |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 42 | GR_CREATE_TRACE_MARKER_CONTEXT("GrDirectContextPriv", "flushSurfaces", this->context()); |
Robert Phillips | 80bfda8 | 2020-11-12 09:23:36 -0500 | [diff] [blame] | 43 | |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 44 | if (this->context()->abandoned()) { |
Robert Phillips | 80bfda8 | 2020-11-12 09:23:36 -0500 | [diff] [blame] | 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 | |
Adlai Holler | c2bfcff | 2020-11-06 15:39:36 -0500 | [diff] [blame] | 54 | #ifdef SK_DEBUG |
| 55 | for (GrSurfaceProxy* proxy : proxies) { |
| 56 | SkASSERT(proxy); |
| 57 | ASSERT_OWNED_PROXY(proxy); |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 58 | } |
Adlai Holler | c2bfcff | 2020-11-06 15:39:36 -0500 | [diff] [blame] | 59 | #endif |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 60 | return this->context()->drawingManager()->flushSurfaces(proxies, access, info, newState); |
Brian Salomon | 693bc2b | 2019-05-09 13:48:00 +0000 | [diff] [blame] | 61 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 62 | |
Robert Phillips | 07f675d | 2020-11-16 13:44:01 -0500 | [diff] [blame] | 63 | void GrDirectContextPriv::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl, |
Robert Phillips | eb54bb5 | 2021-01-08 17:20:18 -0500 | [diff] [blame] | 64 | sk_sp<GrRenderTargetProxy> newDest, |
Robert Phillips | 88b2961 | 2020-11-16 15:15:08 -0500 | [diff] [blame] | 65 | SkIPoint offset) { |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 66 | this->context()->drawingManager()->createDDLTask(std::move(ddl), std::move(newDest), offset); |
Adlai Holler | 7580ad4 | 2020-06-24 13:45:25 -0400 | [diff] [blame] | 67 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 68 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 69 | bool GrDirectContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) { |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 70 | GrGpu* gpu = this->getGpu(); |
| 71 | if (!gpu) { |
Robert Phillips | 43e7e4f | 2020-05-06 13:34:45 -0400 | [diff] [blame] | 72 | return false; |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 73 | } |
| 74 | |
Robert Phillips | 43e7e4f | 2020-05-06 13:34:45 -0400 | [diff] [blame] | 75 | return gpu->compile(desc, info); |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 79 | ////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 80 | #if GR_TEST_UTILS |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 81 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 82 | void GrDirectContextPriv::dumpCacheStats(SkString* out) const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 83 | #if GR_CACHE_STATS |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 84 | this->context()->fResourceCache->dumpStats(out); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 85 | #endif |
| 86 | } |
| 87 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 88 | void GrDirectContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, |
| 89 | SkTArray<double>* values) const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 90 | #if GR_CACHE_STATS |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 91 | this->context()->fResourceCache->dumpStatsKeyValuePairs(keys, values); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 92 | #endif |
| 93 | } |
| 94 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 95 | void GrDirectContextPriv::printCacheStats() const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 96 | SkString out; |
| 97 | this->dumpCacheStats(&out); |
| 98 | SkDebugf("%s", out.c_str()); |
| 99 | } |
| 100 | |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 101 | ///////////////////////////////////////////////// |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 102 | void GrDirectContextPriv::resetGpuStats() const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 103 | #if GR_GPU_STATS |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 104 | this->context()->fGpu->stats()->reset(); |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 105 | #endif |
| 106 | } |
| 107 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 108 | void GrDirectContextPriv::dumpGpuStats(SkString* out) const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 109 | #if GR_GPU_STATS |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 110 | this->context()->fGpu->stats()->dump(out); |
| 111 | if (auto builder = this->context()->fGpu->pipelineBuilder()) { |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame] | 112 | builder->stats()->dump(out); |
| 113 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 114 | #endif |
| 115 | } |
| 116 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 117 | void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, |
| 118 | SkTArray<double>* values) const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 119 | #if GR_GPU_STATS |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 120 | this->context()->fGpu->stats()->dumpKeyValuePairs(keys, values); |
| 121 | if (auto builder = this->context()->fGpu->pipelineBuilder()) { |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame] | 122 | builder->stats()->dumpKeyValuePairs(keys, values); |
| 123 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 124 | #endif |
| 125 | } |
| 126 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 127 | void GrDirectContextPriv::printGpuStats() const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 128 | SkString out; |
| 129 | this->dumpGpuStats(&out); |
| 130 | SkDebugf("%s", out.c_str()); |
| 131 | } |
| 132 | |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 133 | ///////////////////////////////////////////////// |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 134 | void GrDirectContextPriv::resetContextStats() { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 135 | #if GR_GPU_STATS |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 136 | this->context()->stats()->reset(); |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 137 | #endif |
| 138 | } |
| 139 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 140 | void GrDirectContextPriv::dumpContextStats(SkString* out) const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 141 | #if GR_GPU_STATS |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 142 | this->context()->stats()->dump(out); |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 143 | #endif |
| 144 | } |
| 145 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 146 | void GrDirectContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys, |
| 147 | SkTArray<double>* values) const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 148 | #if GR_GPU_STATS |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 149 | this->context()->stats()->dumpKeyValuePairs(keys, values); |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 150 | #endif |
| 151 | } |
| 152 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 153 | void GrDirectContextPriv::printContextStats() const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 154 | SkString out; |
| 155 | this->dumpContextStats(&out); |
| 156 | SkDebugf("%s", out.c_str()); |
| 157 | } |
| 158 | |
| 159 | ///////////////////////////////////////////////// |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 160 | sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, |
| 161 | unsigned int index) { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 162 | auto atlasManager = this->getAtlasManager(); |
| 163 | if (!atlasManager) { |
| 164 | return nullptr; |
| 165 | } |
| 166 | |
| 167 | unsigned int numActiveProxies; |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 168 | const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies); |
| 169 | if (index >= numActiveProxies || !views || !views[index].proxy()) { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 170 | return nullptr; |
| 171 | } |
| 172 | |
Greg Daniel | 7c165a4 | 2020-01-22 12:22:36 -0500 | [diff] [blame] | 173 | SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format)); |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 174 | SkASSERT(views[index].proxy()->priv().isExact()); |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 175 | return sk_make_sp<SkImage_Gpu>(sk_ref_sp(this->context()), |
Brian Salomon | 9a56eb7 | 2021-04-20 16:52:11 -0400 | [diff] [blame] | 176 | kNeedNewImageUniqueID, |
| 177 | views[index], |
| 178 | SkColorInfo(colorType, kPremul_SkAlphaType, nullptr)); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 181 | void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject( |
| 182 | GrOnFlushCallbackObject* cb) { |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 183 | this->context()->flushAndSubmit(); |
| 184 | this->context()->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 185 | } |
| 186 | #endif |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 187 | |
Brian Osman | 4c886ee | 2021-07-07 13:34:50 -0400 | [diff] [blame] | 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. |
| 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 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 | |
| 208 | static 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 | |
| 226 | static 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 Phillips | 33bf2b5 | 2021-08-02 11:14:38 -0400 | [diff] [blame] | 247 | auto readSFC = dContext->priv().makeSFC(upmII, SkBackingFit::kExact); |
| 248 | auto tempSFC = dContext->priv().makeSFC(pmII, SkBackingFit::kExact); |
Brian Osman | 4c886ee | 2021-07-07 13:34:50 -0400 | [diff] [blame] | 249 | 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 Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 306 | bool GrDirectContextPriv::validPMUPMConversionExists() { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 307 | ASSERT_SINGLE_OWNER |
Robert Phillips | 07531a0 | 2020-07-15 15:11:09 -0400 | [diff] [blame] | 308 | |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 309 | auto dContext = this->context(); |
| 310 | |
| 311 | if (!dContext->fDidTestPMConversions) { |
| 312 | dContext->fPMUPMConversionsRoundTrip = test_for_preserving_PM_conversions(dContext); |
| 313 | dContext->fDidTestPMConversions = true; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // The PM<->UPM tests fail or succeed together so we only need to check one. |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 317 | return dContext->fPMUPMConversionsRoundTrip; |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 318 | } |
| 319 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 320 | std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect( |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 321 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 322 | ASSERT_SINGLE_OWNER |
| 323 | // We should have already called this->priv().validPMUPMConversionExists() in this case |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 324 | SkASSERT(this->context()->fDidTestPMConversions); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 325 | // ...and it should have succeeded |
| 326 | SkASSERT(this->validPMUPMConversionExists()); |
| 327 | |
Brian Osman | 4c886ee | 2021-07-07 13:34:50 -0400 | [diff] [blame] | 328 | return make_unpremul_effect(std::move(fp)); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 329 | } |
| 330 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 331 | std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect( |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 332 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 333 | ASSERT_SINGLE_OWNER |
| 334 | // We should have already called this->priv().validPMUPMConversionExists() in this case |
Robert Phillips | ea3489a | 2021-08-02 13:10:57 -0400 | [diff] [blame] | 335 | SkASSERT(this->context()->fDidTestPMConversions); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 336 | // ...and it should have succeeded |
| 337 | SkASSERT(this->validPMUPMConversionExists()); |
| 338 | |
Brian Osman | 4c886ee | 2021-07-07 13:34:50 -0400 | [diff] [blame] | 339 | return make_premul_effect(std::move(fp)); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 340 | } |