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 | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrSurfaceContext.h" |
Robert Phillips | 400f52e | 2021-07-26 13:23:10 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrSurfaceFillContext.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 20 | #include "src/gpu/GrTexture.h" |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrThreadSafePipelineBuilder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/SkGr.h" |
Robert Phillips | 7f11fb5 | 2019-12-03 13:35:19 -0500 | [diff] [blame] | 23 | #include "src/gpu/effects/GrSkSLFP.h" |
Brian Osman | 4c886ee | 2021-07-07 13:34:50 -0400 | [diff] [blame] | 24 | #include "src/gpu/effects/GrTextureEffect.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 25 | #include "src/gpu/text/GrAtlasManager.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "src/gpu/text/GrTextBlobCache.h" |
| 27 | #include "src/image/SkImage_Base.h" |
| 28 | #include "src/image/SkImage_Gpu.h" |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 29 | |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 30 | #define ASSERT_OWNED_PROXY(P) \ |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 31 | SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext) |
Adlai Holler | 33dbd65 | 2020-06-01 12:35:42 -0400 | [diff] [blame] | 32 | #define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fContext->singleOwner()) |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 33 | #define RETURN_VALUE_IF_ABANDONED(value) if (fContext->abandoned()) { return (value); } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 34 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 35 | sk_sp<const GrCaps> GrDirectContextPriv::refCaps() const { |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 36 | return fContext->refCaps(); |
| 37 | } |
| 38 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 39 | void GrDirectContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) { |
Robert Phillips | c5058a6 | 2019-02-15 12:52:59 -0500 | [diff] [blame] | 40 | fContext->addOnFlushCallbackObject(onFlushCBObject); |
| 41 | } |
| 42 | |
Robert Phillips | 80bfda8 | 2020-11-12 09:23:36 -0500 | [diff] [blame] | 43 | GrSemaphoresSubmitted GrDirectContextPriv::flushSurfaces( |
| 44 | SkSpan<GrSurfaceProxy*> proxies, |
| 45 | SkSurface::BackendSurfaceAccess access, |
| 46 | const GrFlushInfo& info, |
| 47 | const GrBackendSurfaceMutableState* newState) { |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 48 | ASSERT_SINGLE_OWNER |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 49 | GR_CREATE_TRACE_MARKER_CONTEXT("GrDirectContextPriv", "flushSurfaces", fContext); |
Robert Phillips | 80bfda8 | 2020-11-12 09:23:36 -0500 | [diff] [blame] | 50 | |
| 51 | if (fContext->abandoned()) { |
| 52 | if (info.fSubmittedProc) { |
| 53 | info.fSubmittedProc(info.fSubmittedContext, false); |
| 54 | } |
| 55 | if (info.fFinishedProc) { |
| 56 | info.fFinishedProc(info.fFinishedContext); |
| 57 | } |
| 58 | return GrSemaphoresSubmitted::kNo; |
| 59 | } |
| 60 | |
Adlai Holler | c2bfcff | 2020-11-06 15:39:36 -0500 | [diff] [blame] | 61 | #ifdef SK_DEBUG |
| 62 | for (GrSurfaceProxy* proxy : proxies) { |
| 63 | SkASSERT(proxy); |
| 64 | ASSERT_OWNED_PROXY(proxy); |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 65 | } |
Adlai Holler | c2bfcff | 2020-11-06 15:39:36 -0500 | [diff] [blame] | 66 | #endif |
Robert Phillips | 80bfda8 | 2020-11-12 09:23:36 -0500 | [diff] [blame] | 67 | return fContext->drawingManager()->flushSurfaces(proxies, access, info, newState); |
Brian Salomon | 693bc2b | 2019-05-09 13:48:00 +0000 | [diff] [blame] | 68 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 69 | |
Robert Phillips | 07f675d | 2020-11-16 13:44:01 -0500 | [diff] [blame] | 70 | void GrDirectContextPriv::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl, |
Robert Phillips | eb54bb5 | 2021-01-08 17:20:18 -0500 | [diff] [blame] | 71 | sk_sp<GrRenderTargetProxy> newDest, |
Robert Phillips | 88b2961 | 2020-11-16 15:15:08 -0500 | [diff] [blame] | 72 | SkIPoint offset) { |
Robert Phillips | eb54bb5 | 2021-01-08 17:20:18 -0500 | [diff] [blame] | 73 | fContext->drawingManager()->createDDLTask(std::move(ddl), std::move(newDest), offset); |
Adlai Holler | 7580ad4 | 2020-06-24 13:45:25 -0400 | [diff] [blame] | 74 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 75 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 76 | bool GrDirectContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) { |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 77 | GrGpu* gpu = this->getGpu(); |
| 78 | if (!gpu) { |
Robert Phillips | 43e7e4f | 2020-05-06 13:34:45 -0400 | [diff] [blame] | 79 | return false; |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 80 | } |
| 81 | |
Robert Phillips | 43e7e4f | 2020-05-06 13:34:45 -0400 | [diff] [blame] | 82 | return gpu->compile(desc, info); |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 86 | ////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 87 | #if GR_TEST_UTILS |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 88 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 89 | void GrDirectContextPriv::dumpCacheStats(SkString* out) const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 90 | #if GR_CACHE_STATS |
| 91 | fContext->fResourceCache->dumpStats(out); |
| 92 | #endif |
| 93 | } |
| 94 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 95 | void GrDirectContextPriv::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, |
| 96 | SkTArray<double>* values) const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 97 | #if GR_CACHE_STATS |
| 98 | fContext->fResourceCache->dumpStatsKeyValuePairs(keys, values); |
| 99 | #endif |
| 100 | } |
| 101 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 102 | void GrDirectContextPriv::printCacheStats() const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 103 | SkString out; |
| 104 | this->dumpCacheStats(&out); |
| 105 | SkDebugf("%s", out.c_str()); |
| 106 | } |
| 107 | |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 108 | ///////////////////////////////////////////////// |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 109 | void GrDirectContextPriv::resetGpuStats() const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 110 | #if GR_GPU_STATS |
| 111 | fContext->fGpu->stats()->reset(); |
| 112 | #endif |
| 113 | } |
| 114 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 115 | void GrDirectContextPriv::dumpGpuStats(SkString* out) const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 116 | #if GR_GPU_STATS |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame] | 117 | fContext->fGpu->stats()->dump(out); |
| 118 | if (auto builder = fContext->fGpu->pipelineBuilder()) { |
| 119 | builder->stats()->dump(out); |
| 120 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 121 | #endif |
| 122 | } |
| 123 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 124 | void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, |
| 125 | SkTArray<double>* values) const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 126 | #if GR_GPU_STATS |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame] | 127 | fContext->fGpu->stats()->dumpKeyValuePairs(keys, values); |
| 128 | if (auto builder = fContext->fGpu->pipelineBuilder()) { |
| 129 | builder->stats()->dumpKeyValuePairs(keys, values); |
| 130 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 131 | #endif |
| 132 | } |
| 133 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 134 | void GrDirectContextPriv::printGpuStats() const { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 135 | SkString out; |
| 136 | this->dumpGpuStats(&out); |
| 137 | SkDebugf("%s", out.c_str()); |
| 138 | } |
| 139 | |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 140 | ///////////////////////////////////////////////// |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 141 | void GrDirectContextPriv::resetContextStats() const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 142 | #if GR_GPU_STATS |
| 143 | fContext->stats()->reset(); |
| 144 | #endif |
| 145 | } |
| 146 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 147 | void GrDirectContextPriv::dumpContextStats(SkString* out) const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 148 | #if GR_GPU_STATS |
| 149 | return fContext->stats()->dump(out); |
| 150 | #endif |
| 151 | } |
| 152 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 153 | void GrDirectContextPriv::dumpContextStatsKeyValuePairs(SkTArray<SkString>* keys, |
| 154 | SkTArray<double>* values) const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 155 | #if GR_GPU_STATS |
| 156 | return fContext->stats()->dumpKeyValuePairs(keys, values); |
| 157 | #endif |
| 158 | } |
| 159 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 160 | void GrDirectContextPriv::printContextStats() const { |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 161 | SkString out; |
| 162 | this->dumpContextStats(&out); |
| 163 | SkDebugf("%s", out.c_str()); |
| 164 | } |
| 165 | |
| 166 | ///////////////////////////////////////////////// |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 167 | sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(GrMaskFormat format, |
| 168 | unsigned int index) { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 169 | auto atlasManager = this->getAtlasManager(); |
| 170 | if (!atlasManager) { |
| 171 | return nullptr; |
| 172 | } |
| 173 | |
| 174 | unsigned int numActiveProxies; |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 175 | const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies); |
| 176 | if (index >= numActiveProxies || !views || !views[index].proxy()) { |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 177 | return nullptr; |
| 178 | } |
| 179 | |
Greg Daniel | 7c165a4 | 2020-01-22 12:22:36 -0500 | [diff] [blame] | 180 | SkColorType colorType = GrColorTypeToSkColorType(GrMaskFormatToColorType(format)); |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 181 | SkASSERT(views[index].proxy()->priv().isExact()); |
Brian Salomon | 9a56eb7 | 2021-04-20 16:52:11 -0400 | [diff] [blame] | 182 | return sk_make_sp<SkImage_Gpu>(sk_ref_sp(fContext), |
| 183 | kNeedNewImageUniqueID, |
| 184 | views[index], |
| 185 | SkColorInfo(colorType, kPremul_SkAlphaType, nullptr)); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 188 | void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject( |
| 189 | GrOnFlushCallbackObject* cb) { |
| 190 | fContext->flushAndSubmit(); |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 191 | fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 192 | } |
| 193 | #endif |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 194 | |
Brian Osman | 4c886ee | 2021-07-07 13:34:50 -0400 | [diff] [blame] | 195 | // Both of these effects aggressively round to the nearest exact (N / 255) floating point values. |
| 196 | // This lets us find a round-trip preserving pair on some GPUs that do odd byte to float conversion. |
| 197 | static std::unique_ptr<GrFragmentProcessor> make_premul_effect( |
| 198 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 199 | if (!fp) { |
| 200 | return nullptr; |
| 201 | } |
| 202 | |
| 203 | static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"( |
| 204 | half4 main(half4 color) { |
| 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 | |
| 215 | static std::unique_ptr<GrFragmentProcessor> make_unpremul_effect( |
| 216 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 217 | if (!fp) { |
| 218 | return nullptr; |
| 219 | } |
| 220 | |
| 221 | static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"( |
| 222 | half4 main(half4 color) { |
| 223 | color = floor(color * 255 + 0.5) / 255; |
| 224 | color.rgb = color.a <= 0 ? half3(0) : floor(color.rgb / color.a * 255 + 0.5) / 255; |
| 225 | return color; |
| 226 | } |
| 227 | )"); |
| 228 | |
| 229 | fp = GrSkSLFP::Make(effect, "ToUnpremul", std::move(fp), GrSkSLFP::OptFlags::kNone); |
| 230 | return GrFragmentProcessor::HighPrecision(std::move(fp)); |
| 231 | } |
| 232 | |
| 233 | static bool test_for_preserving_PM_conversions(GrDirectContext* dContext) { |
| 234 | static constexpr int kSize = 256; |
| 235 | SkAutoTMalloc<uint32_t> data(kSize * kSize * 3); |
| 236 | uint32_t* srcData = data.get(); |
| 237 | |
| 238 | // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate |
| 239 | // values in row y. We set r, g, and b to the same value since they are handled identically. |
| 240 | for (int y = 0; y < kSize; ++y) { |
| 241 | for (int x = 0; x < kSize; ++x) { |
| 242 | uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize*y + x]); |
| 243 | color[3] = y; |
| 244 | color[2] = std::min(x, y); |
| 245 | color[1] = std::min(x, y); |
| 246 | color[0] = std::min(x, y); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | const SkImageInfo pmII = |
| 251 | SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 252 | const SkImageInfo upmII = pmII.makeAlphaType(kUnpremul_SkAlphaType); |
| 253 | |
Robert Phillips | 33bf2b5 | 2021-08-02 11:14:38 -0400 | [diff] [blame^] | 254 | auto readSFC = dContext->priv().makeSFC(upmII, SkBackingFit::kExact); |
| 255 | auto tempSFC = dContext->priv().makeSFC(pmII, SkBackingFit::kExact); |
Brian Osman | 4c886ee | 2021-07-07 13:34:50 -0400 | [diff] [blame] | 256 | if (!readSFC || !tempSFC) { |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | // This function is only ever called if we are in a GrDirectContext since we are calling read |
| 261 | // pixels here. Thus the pixel data will be uploaded immediately and we don't need to keep the |
| 262 | // pixel data alive in the proxy. Therefore the ReleaseProc is nullptr. |
| 263 | SkBitmap bitmap; |
| 264 | bitmap.installPixels(pmII, srcData, 4 * kSize); |
| 265 | bitmap.setImmutable(); |
| 266 | |
| 267 | auto dataView = std::get<0>(GrMakeUncachedBitmapProxyView(dContext, bitmap)); |
| 268 | if (!dataView) { |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | uint32_t* firstRead = data.get() + kSize*kSize; |
| 273 | uint32_t* secondRead = data.get() + 2*kSize*kSize; |
| 274 | std::fill_n( firstRead, kSize*kSize, 0); |
| 275 | std::fill_n(secondRead, kSize*kSize, 0); |
| 276 | |
| 277 | GrPixmap firstReadPM( upmII, firstRead, kSize*sizeof(uint32_t)); |
| 278 | GrPixmap secondReadPM(upmII, secondRead, kSize*sizeof(uint32_t)); |
| 279 | |
| 280 | // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw |
| 281 | // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data. |
| 282 | // We then verify that two reads produced the same values. |
| 283 | |
| 284 | auto fp1 = make_unpremul_effect(GrTextureEffect::Make(std::move(dataView), bitmap.alphaType())); |
| 285 | readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp1)); |
| 286 | if (!readSFC->readPixels(dContext, firstReadPM, {0, 0})) { |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | auto fp2 = make_premul_effect( |
| 291 | GrTextureEffect::Make(readSFC->readSurfaceView(), readSFC->colorInfo().alphaType())); |
| 292 | tempSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp2)); |
| 293 | |
| 294 | auto fp3 = make_unpremul_effect( |
| 295 | GrTextureEffect::Make(tempSFC->readSurfaceView(), tempSFC->colorInfo().alphaType())); |
| 296 | readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp3)); |
| 297 | |
| 298 | if (!readSFC->readPixels(dContext, secondReadPM, {0, 0})) { |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | for (int y = 0; y < kSize; ++y) { |
| 303 | for (int x = 0; x <= y; ++x) { |
| 304 | if (firstRead[kSize*y + x] != secondRead[kSize*y + x]) { |
| 305 | return false; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return true; |
| 311 | } |
| 312 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 313 | bool GrDirectContextPriv::validPMUPMConversionExists() { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 314 | ASSERT_SINGLE_OWNER |
Robert Phillips | 07531a0 | 2020-07-15 15:11:09 -0400 | [diff] [blame] | 315 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 316 | if (!fContext->fDidTestPMConversions) { |
Brian Osman | 4c886ee | 2021-07-07 13:34:50 -0400 | [diff] [blame] | 317 | fContext->fPMUPMConversionsRoundTrip = test_for_preserving_PM_conversions(fContext); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 318 | fContext->fDidTestPMConversions = true; |
| 319 | } |
| 320 | |
| 321 | // The PM<->UPM tests fail or succeed together so we only need to check one. |
| 322 | return fContext->fPMUPMConversionsRoundTrip; |
| 323 | } |
| 324 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 325 | std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect( |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 326 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 327 | ASSERT_SINGLE_OWNER |
| 328 | // We should have already called this->priv().validPMUPMConversionExists() in this case |
| 329 | SkASSERT(fContext->fDidTestPMConversions); |
| 330 | // ...and it should have succeeded |
| 331 | SkASSERT(this->validPMUPMConversionExists()); |
| 332 | |
Brian Osman | 4c886ee | 2021-07-07 13:34:50 -0400 | [diff] [blame] | 333 | return make_unpremul_effect(std::move(fp)); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 334 | } |
| 335 | |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 336 | std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect( |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 337 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 338 | ASSERT_SINGLE_OWNER |
| 339 | // We should have already called this->priv().validPMUPMConversionExists() in this case |
| 340 | SkASSERT(fContext->fDidTestPMConversions); |
| 341 | // ...and it should have succeeded |
| 342 | SkASSERT(this->validPMUPMConversionExists()); |
| 343 | |
Brian Osman | 4c886ee | 2021-07-07 13:34:50 -0400 | [diff] [blame] | 344 | return make_premul_effect(std::move(fp)); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 345 | } |
Robert Phillips | 516405c | 2021-06-04 16:37:30 -0400 | [diff] [blame] | 346 | |
Robert Phillips | cc44feb | 2021-07-06 12:21:37 -0400 | [diff] [blame] | 347 | sk_sp<skgpu::BaseDevice> GrDirectContextPriv::createDevice(GrColorType colorType, |
| 348 | sk_sp<GrSurfaceProxy> proxy, |
| 349 | sk_sp<SkColorSpace> colorSpace, |
| 350 | GrSurfaceOrigin origin, |
| 351 | const SkSurfaceProps& props, |
| 352 | skgpu::BaseDevice::InitContents init) { |
Robert Phillips | 516405c | 2021-06-04 16:37:30 -0400 | [diff] [blame] | 353 | return fContext->GrRecordingContext::priv().createDevice(colorType, std::move(proxy), |
| 354 | std::move(colorSpace), |
| 355 | origin, props, init); |
| 356 | } |
| 357 | |
Robert Phillips | cc44feb | 2021-07-06 12:21:37 -0400 | [diff] [blame] | 358 | sk_sp<skgpu::BaseDevice> GrDirectContextPriv::createDevice(SkBudgeted budgeted, |
| 359 | const SkImageInfo& ii, |
| 360 | SkBackingFit fit, |
| 361 | int sampleCount, |
| 362 | GrMipmapped mipmapped, |
| 363 | GrProtected isProtected, |
| 364 | GrSurfaceOrigin origin, |
| 365 | const SkSurfaceProps& props, |
| 366 | skgpu::BaseDevice::InitContents init) { |
Robert Phillips | 516405c | 2021-06-04 16:37:30 -0400 | [diff] [blame] | 367 | return fContext->GrRecordingContext::priv().createDevice(budgeted, ii, fit, sampleCount, |
| 368 | mipmapped, isProtected, |
| 369 | origin, props, init); |
| 370 | } |
Robert Phillips | 33bf2b5 | 2021-08-02 11:14:38 -0400 | [diff] [blame^] | 371 | |
| 372 | std::unique_ptr<GrSurfaceContext> GrDirectContextPriv::makeSC(GrSurfaceProxyView readView, |
| 373 | const GrColorInfo& info) { |
| 374 | return fContext->GrRecordingContext::priv().makeSC(readView, info); |
| 375 | } |
| 376 | |
| 377 | std::unique_ptr<GrSurfaceFillContext> GrDirectContextPriv::makeSFC(GrImageInfo info, |
| 378 | SkBackingFit fit, |
| 379 | int sampleCount, |
| 380 | GrMipmapped mipmapped, |
| 381 | GrProtected isProtected, |
| 382 | GrSurfaceOrigin origin, |
| 383 | SkBudgeted budgeted) { |
| 384 | return fContext->GrRecordingContext::priv().makeSFC(info, fit, sampleCount, mipmapped, |
| 385 | isProtected, origin, budgeted); |
| 386 | } |
| 387 | |
| 388 | std::unique_ptr<GrSurfaceFillContext> GrDirectContextPriv::makeSFCWithFallback( |
| 389 | GrImageInfo info, |
| 390 | SkBackingFit fit, |
| 391 | int sampleCount, |
| 392 | GrMipmapped mipmapped, |
| 393 | GrProtected isProtected, |
| 394 | GrSurfaceOrigin origin, |
| 395 | SkBudgeted budgeted) { |
| 396 | return fContext->GrRecordingContext::priv().makeSFCWithFallback(info, fit, sampleCount, |
| 397 | mipmapped, isProtected, |
| 398 | origin, budgeted); |
| 399 | } |
| 400 | |
| 401 | std::unique_ptr<GrSurfaceFillContext> GrDirectContextPriv::makeSFCFromBackendTexture( |
| 402 | GrColorInfo info, |
| 403 | const GrBackendTexture& tex, |
| 404 | int sampleCount, |
| 405 | GrSurfaceOrigin origin, |
| 406 | sk_sp<GrRefCntedCallback> releaseHelper) { |
| 407 | return fContext->GrRecordingContext::priv().makeSFCFromBackendTexture(info, tex, sampleCount, |
| 408 | origin, |
| 409 | std::move(releaseHelper)); |
| 410 | |
| 411 | } |