| 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 |
|
| 8 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 9 |
|
| 10 |
#include "include/core/SkAlphaType.h" |
| 11 |
#include "include/core/SkBitmap.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkColorType.h" |
| 14 |
#include "include/core/SkImage.h" |
| 15 |
#include "include/core/SkImageInfo.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkString.h" |
| 18 |
#include "include/core/SkTypes.h" |
| 19 |
#include "include/effects/SkRuntimeEffect.h" |
| 20 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 21 |
#include "include/gpu/ganesh/GrTypes.h" |
| 22 |
#include "include/private/base/SingleOwner.h" |
| 23 |
#include "include/private/base/SkDebug.h" |
| 24 |
#include "include/private/base/SkTemplates.h" |
| 25 |
#include "include/private/chromium/GrDeferredDisplayList.h" |
| 26 |
#include "src/core/SkRuntimeEffectPriv.h" |
| 27 |
#include "src/core/SkTraceEvent.h" |
| 28 |
#include "src/gpu/AtlasTypes.h" |
| 29 |
#include "src/gpu/SkBackingFit.h" |
| 30 |
#include "src/gpu/ganesh/GrAuditTrail.h" |
| 31 |
#include "src/gpu/ganesh/GrColorInfo.h" |
| 32 |
#include "src/gpu/ganesh/GrDrawingManager.h" |
| 33 |
#include "src/gpu/ganesh/GrFragmentProcessor.h" |
| 34 |
#include "src/gpu/ganesh/GrGpu.h" |
| 35 |
#include "src/gpu/ganesh/GrImageInfo.h" |
| 36 |
#include "src/gpu/ganesh/GrPixmap.h" |
| 37 |
#include "src/gpu/ganesh/GrRenderTargetProxy.h" |
| 38 |
#include "src/gpu/ganesh/GrResourceCache.h" |
| 39 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 40 |
#include "src/gpu/ganesh/GrSurfaceProxyPriv.h" |
| 41 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 42 |
#include "src/gpu/ganesh/GrTexture.h" |
| 43 |
#include "src/gpu/ganesh/GrThreadSafePipelineBuilder.h" |
| 44 |
#include "src/gpu/ganesh/GrTracing.h" |
| 45 |
#include "src/gpu/ganesh/SkGr.h" |
| 46 |
#include "src/gpu/ganesh/SurfaceFillContext.h" |
| 47 |
#include "src/gpu/ganesh/effects/GrSkSLFP.h" |
| 48 |
#include "src/gpu/ganesh/effects/GrTextureEffect.h" |
| 49 |
#include "src/gpu/ganesh/image/GrMippedBitmap.h" |
| 50 |
#include "src/gpu/ganesh/image/SkImage_Ganesh.h" |
| 51 |
#include "src/gpu/ganesh/text/GrAtlasManager.h" |
| 52 |
#include "src/image/SkImage_Base.h" |
| 53 |
|
| 54 |
#include <algorithm> |
| 55 |
#include <cstdint> |
| 56 |
#include <tuple> |
| 57 |
|
| 58 |
class GrProgramDesc; |
| 59 |
class GrProgramInfo; |
| 60 |
|
| 61 |
using namespace skia_private; |
| 62 |
using MaskFormat = skgpu::MaskFormat; |
| 63 |
|
| 64 |
#define ASSERT_OWNED_PROXY(P) \ |
| 65 |
SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this->context()) |
| 66 |
#define ASSERT_SINGLE_OWNER SKGPU_ASSERT_SINGLE_OWNER(this->context()->singleOwner()) |
| 67 |
|
| 68 |
GrSemaphoresSubmitted GrDirectContextPriv::flushSurfaces( |
| 69 |
SkSpan<GrSurfaceProxy*> proxies, |
| 70 |
SkSurfaces::BackendSurfaceAccess access, |
| 71 |
const GrFlushInfo& info, |
| 72 |
const skgpu::MutableTextureState* newState) { |
| 73 |
ASSERT_SINGLE_OWNER |
| 74 |
GR_CREATE_TRACE_MARKER_CONTEXT("GrDirectContextPriv", "flushSurfaces", this->context()); |
| 75 |
|
| 76 |
if (this->context()->abandoned()) { |
| 77 |
if (info.fSubmittedProc) { |
| 78 |
info.fSubmittedProc(info.fSubmittedContext, false); |
| 79 |
} |
| 80 |
if (info.fFinishedProc) { |
| 81 |
info.fFinishedProc(info.fFinishedContext); |
| 82 |
} |
| 83 |
return GrSemaphoresSubmitted::kNo; |
| 84 |
} |
| 85 |
|
| 86 |
#ifdef SK_DEBUG |
| 87 |
for (GrSurfaceProxy* proxy : proxies) { |
| 88 |
SkASSERT(proxy); |
| 89 |
ASSERT_OWNED_PROXY(proxy); |
| 90 |
} |
| 91 |
#endif |
| 92 |
return this->context()->drawingManager()->flushSurfaces(proxies, access, info, newState); |
| 93 |
} |
| 94 |
|
| 95 |
void GrDirectContextPriv::createDDLTask(sk_sp<const GrDeferredDisplayList> ddl, |
| 96 |
sk_sp<GrRenderTargetProxy> newDest) { |
| 97 |
this->context()->drawingManager()->createDDLTask(std::move(ddl), std::move(newDest)); |
| 98 |
} |
| 99 |
|
| 100 |
bool GrDirectContextPriv::compile(const GrProgramDesc& desc, const GrProgramInfo& info) { |
| 101 |
GrGpu* gpu = this->getGpu(); |
| 102 |
if (!gpu) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
return gpu->compile(desc, info); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
////////////////////////////////////////////////////////////////////////////// |
| 111 |
#if defined(GPU_TEST_UTILS) |
| 112 |
|
| 113 |
void GrDirectContextPriv::dumpCacheStats(SkString* out) const { |
| 114 |
#if GR_CACHE_STATS |
| 115 |
this->context()->fResourceCache->dumpStats(out); |
| 116 |
#endif |
| 117 |
} |
| 118 |
|
| 119 |
void GrDirectContextPriv::dumpCacheStatsKeyValuePairs(TArray<SkString>* keys, |
| 120 |
TArray<double>* values) const { |
| 121 |
#if GR_CACHE_STATS |
| 122 |
this->context()->fResourceCache->dumpStatsKeyValuePairs(keys, values); |
| 123 |
#endif |
| 124 |
} |
| 125 |
|
| 126 |
void GrDirectContextPriv::printCacheStats() const { |
| 127 |
SkString out; |
| 128 |
this->dumpCacheStats(&out); |
| 129 |
SkDebugf("%s", out.c_str()); |
| 130 |
} |
| 131 |
|
| 132 |
///////////////////////////////////////////////// |
| 133 |
void GrDirectContextPriv::resetGpuStats() const { |
| 134 |
#if GR_GPU_STATS |
| 135 |
this->context()->fGpu->stats()->reset(); |
| 136 |
#endif |
| 137 |
} |
| 138 |
|
| 139 |
void GrDirectContextPriv::dumpGpuStats(SkString* out) const { |
| 140 |
#if GR_GPU_STATS |
| 141 |
this->context()->fGpu->stats()->dump(out); |
| 142 |
if (auto builder = this->context()->fGpu->pipelineBuilder()) { |
| 143 |
builder->stats()->dump(out); |
| 144 |
} |
| 145 |
#endif |
| 146 |
} |
| 147 |
|
| 148 |
void GrDirectContextPriv::dumpGpuStatsKeyValuePairs(TArray<SkString>* keys, |
| 149 |
TArray<double>* values) const { |
| 150 |
#if GR_GPU_STATS |
| 151 |
this->context()->fGpu->stats()->dumpKeyValuePairs(keys, values); |
| 152 |
if (auto builder = this->context()->fGpu->pipelineBuilder()) { |
| 153 |
builder->stats()->dumpKeyValuePairs(keys, values); |
| 154 |
} |
| 155 |
#endif |
| 156 |
} |
| 157 |
|
| 158 |
void GrDirectContextPriv::printGpuStats() const { |
| 159 |
SkString out; |
| 160 |
this->dumpGpuStats(&out); |
| 161 |
SkDebugf("%s", out.c_str()); |
| 162 |
} |
| 163 |
|
| 164 |
///////////////////////////////////////////////// |
| 165 |
void GrDirectContextPriv::resetContextStats() { |
| 166 |
#if GR_GPU_STATS |
| 167 |
this->context()->stats()->reset(); |
| 168 |
#endif |
| 169 |
} |
| 170 |
|
| 171 |
void GrDirectContextPriv::dumpContextStats(SkString* out) const { |
| 172 |
#if GR_GPU_STATS |
| 173 |
this->context()->stats()->dump(out); |
| 174 |
#endif |
| 175 |
} |
| 176 |
|
| 177 |
void GrDirectContextPriv::dumpContextStatsKeyValuePairs(TArray<SkString>* keys, |
| 178 |
TArray<double>* values) const { |
| 179 |
#if GR_GPU_STATS |
| 180 |
this->context()->stats()->dumpKeyValuePairs(keys, values); |
| 181 |
#endif |
| 182 |
} |
| 183 |
|
| 184 |
void GrDirectContextPriv::printContextStats() const { |
| 185 |
SkString out; |
| 186 |
this->dumpContextStats(&out); |
| 187 |
SkDebugf("%s", out.c_str()); |
| 188 |
} |
| 189 |
|
| 190 |
///////////////////////////////////////////////// |
| 191 |
sk_sp<SkImage> GrDirectContextPriv::testingOnly_getFontAtlasImage(MaskFormat format, |
| 192 |
unsigned int index) { |
| 193 |
auto atlasManager = this->getAtlasManager(); |
| 194 |
if (!atlasManager) { |
| 195 |
return nullptr; |
| 196 |
} |
| 197 |
|
| 198 |
unsigned int numActiveProxies; |
| 199 |
const GrSurfaceProxyView* views = atlasManager->getViews(format, &numActiveProxies); |
| 200 |
if (index >= numActiveProxies || !views || !views[index].proxy()) { |
| 201 |
return nullptr; |
| 202 |
} |
| 203 |
|
| 204 |
SkColorType colorType = skgpu::MaskFormatToColorType(format); |
| 205 |
SkASSERT(views[index].proxy()->priv().isExact()); |
| 206 |
return sk_make_sp<SkImage_Ganesh>(sk_ref_sp(this->context()), |
| 207 |
kNeedNewImageUniqueID, |
| 208 |
views[index], |
| 209 |
SkColorInfo(colorType, kPremul_SkAlphaType, nullptr)); |
| 210 |
} |
| 211 |
|
| 212 |
void GrDirectContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject( |
| 213 |
GrOnFlushCallbackObject* cb) { |
| 214 |
this->context()->flushAndSubmit(); |
| 215 |
this->context()->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb); |
| 216 |
} |
| 217 |
#endif |
| 218 |
|
| 219 |
// Both of these effects aggressively round to the nearest exact (N / 255) floating point values. |
| 220 |
// This lets us find a round-trip preserving pair on some GPUs that do odd byte to float conversion. |
| 221 |
static std::unique_ptr<GrFragmentProcessor> make_premul_effect( |
| 222 |
std::unique_ptr<GrFragmentProcessor> fp) { |
| 223 |
if (!fp) { |
| 224 |
return nullptr; |
| 225 |
} |
| 226 |
|
| 227 |
static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, |
| 228 |
"half4 main(half4 halfColor) {" |
| 229 |
"float4 color = float4(halfColor);" |
| 230 |
"color = floor(color * 255 + 0.5) / 255;" |
| 231 |
"color.rgb = floor(color.rgb * color.a * 255 + 0.5) / 255;" |
| 232 |
"return color;" |
| 233 |
"}" |
| 234 |
); |
| 235 |
|
| 236 |
fp = GrSkSLFP::Make(effect, "ToPremul", std::move(fp), GrSkSLFP::OptFlags::kNone); |
| 237 |
return GrFragmentProcessor::HighPrecision(std::move(fp)); |
| 238 |
} |
| 239 |
|
| 240 |
static std::unique_ptr<GrFragmentProcessor> make_unpremul_effect( |
| 241 |
std::unique_ptr<GrFragmentProcessor> fp) { |
| 242 |
if (!fp) { |
| 243 |
return nullptr; |
| 244 |
} |
| 245 |
|
| 246 |
static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, |
| 247 |
"half4 main(half4 halfColor) {" |
| 248 |
"float4 color = float4(halfColor);" |
| 249 |
"color = floor(color * 255 + 0.5) / 255;" |
| 250 |
"color.rgb = color.a <= 0 ? half3(0) : floor(color.rgb / color.a * 255 + 0.5) / 255;" |
| 251 |
"return color;" |
| 252 |
"}" |
| 253 |
); |
| 254 |
|
| 255 |
fp = GrSkSLFP::Make(effect, "ToUnpremul", std::move(fp), GrSkSLFP::OptFlags::kNone); |
| 256 |
return GrFragmentProcessor::HighPrecision(std::move(fp)); |
| 257 |
} |
| 258 |
|
| 259 |
static bool test_for_preserving_PM_conversions(GrDirectContext* dContext) { |
| 260 |
static constexpr int kSize = 256; |
| 261 |
AutoTMalloc<uint32_t> data(kSize * kSize * 3); |
| 262 |
uint32_t* srcData = data.get(); |
| 263 |
|
| 264 |
// Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate |
| 265 |
// values in row y. We set r, g, and b to the same value since they are handled identically. |
| 266 |
for (int y = 0; y < kSize; ++y) { |
| 267 |
for (int x = 0; x < kSize; ++x) { |
| 268 |
uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize*y + x]); |
| 269 |
color[3] = y; |
| 270 |
color[2] = std::min(x, y); |
| 271 |
color[1] = std::min(x, y); |
| 272 |
color[0] = std::min(x, y); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
const SkImageInfo pmII = |
| 277 |
SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 278 |
const SkImageInfo upmII = pmII.makeAlphaType(kUnpremul_SkAlphaType); |
| 279 |
|
| 280 |
auto readSFC = |
| 281 |
dContext->priv().makeSFC(upmII, "ReadSfcForPMUPMConversion", SkBackingFit::kExact); |
| 282 |
auto tempSFC = |
| 283 |
dContext->priv().makeSFC(pmII, "TempSfcForPMUPMConversion", SkBackingFit::kExact); |
| 284 |
if (!readSFC || !tempSFC) { |
| 285 |
return false; |
| 286 |
} |
| 287 |
|
| 288 |
// This function is only ever called if we are in a GrDirectContext since we are calling read |
| 289 |
// pixels here. Thus the pixel data will be uploaded immediately and we don't need to keep the |
| 290 |
// pixel data alive in the proxy. Therefore the ReleaseProc is nullptr. |
| 291 |
std::optional<GrMippedBitmap> bitmap = GrMippedBitmap::Make(pmII, srcData, 4 * kSize); |
| 292 |
SkASSERT(bitmap); |
| 293 |
SkASSERT(bitmap->alphaType() == kPremul_SkAlphaType); |
| 294 |
|
| 295 |
auto dataView = std::get<0>(GrMakeUncachedBitmapProxyView(dContext, bitmap.value())); |
| 296 |
if (!dataView) { |
| 297 |
return false; |
| 298 |
} |
| 299 |
|
| 300 |
uint32_t* firstRead = data.get() + kSize*kSize; |
| 301 |
uint32_t* secondRead = data.get() + 2*kSize*kSize; |
| 302 |
std::fill_n( firstRead, kSize*kSize, 0); |
| 303 |
std::fill_n(secondRead, kSize*kSize, 0); |
| 304 |
|
| 305 |
GrPixmap firstReadPM( upmII, firstRead, kSize*sizeof(uint32_t)); |
| 306 |
GrPixmap secondReadPM(upmII, secondRead, kSize*sizeof(uint32_t)); |
| 307 |
|
| 308 |
// We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw |
| 309 |
// from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data. |
| 310 |
// We then verify that two reads produced the same values. |
| 311 |
|
| 312 |
auto fp1 = |
| 313 |
make_unpremul_effect(GrTextureEffect::Make(std::move(dataView), kPremul_SkAlphaType)); |
| 314 |
readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp1)); |
| 315 |
if (!readSFC->readPixels(dContext, firstReadPM, {0, 0})) { |
| 316 |
return false; |
| 317 |
} |
| 318 |
|
| 319 |
auto fp2 = make_premul_effect( |
| 320 |
GrTextureEffect::Make(readSFC->readSurfaceView(), readSFC->colorInfo().alphaType())); |
| 321 |
tempSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp2)); |
| 322 |
|
| 323 |
auto fp3 = make_unpremul_effect( |
| 324 |
GrTextureEffect::Make(tempSFC->readSurfaceView(), tempSFC->colorInfo().alphaType())); |
| 325 |
readSFC->fillRectWithFP(SkIRect::MakeWH(kSize, kSize), std::move(fp3)); |
| 326 |
|
| 327 |
if (!readSFC->readPixels(dContext, secondReadPM, {0, 0})) { |
| 328 |
return false; |
| 329 |
} |
| 330 |
|
| 331 |
for (int y = 0; y < kSize; ++y) { |
| 332 |
for (int x = 0; x <= y; ++x) { |
| 333 |
if (firstRead[kSize*y + x] != secondRead[kSize*y + x]) { |
| 334 |
return false; |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
return true; |
| 340 |
} |
| 341 |
|
| 342 |
bool GrDirectContextPriv::validPMUPMConversionExists() { |
| 343 |
ASSERT_SINGLE_OWNER |
| 344 |
|
| 345 |
auto dContext = this->context(); |
| 346 |
|
| 347 |
if (!dContext->fDidTestPMConversions) { |
| 348 |
dContext->fPMUPMConversionsRoundTrip = test_for_preserving_PM_conversions(dContext); |
| 349 |
dContext->fDidTestPMConversions = true; |
| 350 |
} |
| 351 |
|
| 352 |
// The PM<->UPM tests fail or succeed together so we only need to check one. |
| 353 |
return dContext->fPMUPMConversionsRoundTrip; |
| 354 |
} |
| 355 |
|
| 356 |
std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createPMToUPMEffect( |
| 357 |
std::unique_ptr<GrFragmentProcessor> fp) { |
| 358 |
ASSERT_SINGLE_OWNER |
| 359 |
// We should have already called this->priv().validPMUPMConversionExists() in this case |
| 360 |
SkASSERT(this->context()->fDidTestPMConversions); |
| 361 |
// ...and it should have succeeded |
| 362 |
SkASSERT(this->validPMUPMConversionExists()); |
| 363 |
|
| 364 |
return make_unpremul_effect(std::move(fp)); |
| 365 |
} |
| 366 |
|
| 367 |
std::unique_ptr<GrFragmentProcessor> GrDirectContextPriv::createUPMToPMEffect( |
| 368 |
std::unique_ptr<GrFragmentProcessor> fp) { |
| 369 |
ASSERT_SINGLE_OWNER |
| 370 |
// We should have already called this->priv().validPMUPMConversionExists() in this case |
| 371 |
SkASSERT(this->context()->fDidTestPMConversions); |
| 372 |
// ...and it should have succeeded |
| 373 |
SkASSERT(this->validPMUPMConversionExists()); |
| 374 |
|
| 375 |
return make_premul_effect(std::move(fp)); |
| 376 |
} |