GrConfigConversionEffect.fp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2018 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. @header {
  8. #include "include/gpu/GrContext.h"
  9. #include "src/gpu/GrClip.h"
  10. #include "src/gpu/GrContextPriv.h"
  11. #include "src/gpu/GrProxyProvider.h"
  12. #include "src/gpu/GrRenderTargetContext.h"
  13. }
  14. @class {
  15. static bool TestForPreservingPMConversions(GrContext* context) {
  16. static constexpr int kSize = 256;
  17. static constexpr GrColorType kColorType = GrColorType::kRGBA_8888;
  18. SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
  19. uint32_t* srcData = data.get();
  20. uint32_t* firstRead = data.get() + kSize * kSize;
  21. uint32_t* secondRead = data.get() + 2 * kSize * kSize;
  22. // Fill with every possible premultiplied A, color channel value. There will be 256-y
  23. // duplicate values in row y. We set r, g, and b to the same value since they are handled
  24. // identically.
  25. for (int y = 0; y < kSize; ++y) {
  26. for (int x = 0; x < kSize; ++x) {
  27. uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize*y + x]);
  28. color[3] = y;
  29. color[2] = SkTMin(x, y);
  30. color[1] = SkTMin(x, y);
  31. color[0] = SkTMin(x, y);
  32. }
  33. }
  34. memset(firstRead, 0, kSize * kSize * sizeof(uint32_t));
  35. memset(secondRead, 0, kSize * kSize * sizeof(uint32_t));
  36. const SkImageInfo ii = SkImageInfo::Make(kSize, kSize,
  37. kRGBA_8888_SkColorType, kPremul_SkAlphaType);
  38. sk_sp<GrRenderTargetContext> readRTC(
  39. context->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact,
  40. kSize, kSize,
  41. kColorType, nullptr));
  42. sk_sp<GrRenderTargetContext> tempRTC(
  43. context->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact,
  44. kSize, kSize,
  45. kColorType, nullptr));
  46. if (!readRTC || !readRTC->asTextureProxy() || !tempRTC) {
  47. return false;
  48. }
  49. // Adding discard to appease vulkan validation warning about loading uninitialized data on
  50. // draw
  51. readRTC->discard();
  52. GrProxyProvider* proxyProvider = context->priv().proxyProvider();
  53. SkPixmap pixmap(ii, srcData, 4 * kSize);
  54. // This function is only ever called if we are in a GrContext that has a GrGpu since we are
  55. // calling read pixels here. Thus the pixel data will be uploaded immediately and we don't
  56. // need to keep the pixel data alive in the proxy. Therefore the ReleaseProc is nullptr.
  57. sk_sp<SkImage> image = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
  58. sk_sp<GrTextureProxy> dataProxy = proxyProvider->createTextureProxy(std::move(image),
  59. GrRenderable::kNo,
  60. 1,
  61. SkBudgeted::kYes,
  62. SkBackingFit::kExact);
  63. if (!dataProxy) {
  64. return false;
  65. }
  66. static const SkRect kRect = SkRect::MakeIWH(kSize, kSize);
  67. // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
  68. // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
  69. // We then verify that two reads produced the same values.
  70. GrPaint paint1;
  71. GrPaint paint2;
  72. GrPaint paint3;
  73. std::unique_ptr<GrFragmentProcessor> pmToUPM(
  74. new GrConfigConversionEffect(PMConversion::kToUnpremul));
  75. std::unique_ptr<GrFragmentProcessor> upmToPM(
  76. new GrConfigConversionEffect(PMConversion::kToPremul));
  77. paint1.addColorTextureProcessor(dataProxy, SkMatrix::I());
  78. paint1.addColorFragmentProcessor(pmToUPM->clone());
  79. paint1.setPorterDuffXPFactory(SkBlendMode::kSrc);
  80. readRTC->fillRectToRect(GrNoClip(), std::move(paint1), GrAA::kNo, SkMatrix::I(), kRect,
  81. kRect);
  82. if (!readRTC->readPixels(ii, firstRead, 0, {0, 0})) {
  83. return false;
  84. }
  85. // Adding discard to appease vulkan validation warning about loading uninitialized data on
  86. // draw
  87. tempRTC->discard();
  88. paint2.addColorTextureProcessor(readRTC->asTextureProxyRef(), SkMatrix::I());
  89. paint2.addColorFragmentProcessor(std::move(upmToPM));
  90. paint2.setPorterDuffXPFactory(SkBlendMode::kSrc);
  91. tempRTC->fillRectToRect(GrNoClip(), std::move(paint2), GrAA::kNo, SkMatrix::I(), kRect,
  92. kRect);
  93. paint3.addColorTextureProcessor(tempRTC->asTextureProxyRef(), SkMatrix::I());
  94. paint3.addColorFragmentProcessor(std::move(pmToUPM));
  95. paint3.setPorterDuffXPFactory(SkBlendMode::kSrc);
  96. readRTC->fillRectToRect(GrNoClip(), std::move(paint3), GrAA::kNo, SkMatrix::I(), kRect,
  97. kRect);
  98. if (!readRTC->readPixels(ii, secondRead, 0, {0, 0})) {
  99. return false;
  100. }
  101. for (int y = 0; y < kSize; ++y) {
  102. for (int x = 0; x <= y; ++x) {
  103. if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) {
  104. return false;
  105. }
  106. }
  107. }
  108. return true;
  109. }
  110. }
  111. @make {
  112. static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
  113. PMConversion pmConversion) {
  114. if (!fp) {
  115. return nullptr;
  116. }
  117. std::unique_ptr<GrFragmentProcessor> ccFP(new GrConfigConversionEffect(pmConversion));
  118. std::unique_ptr<GrFragmentProcessor> fpPipeline[] = { std::move(fp), std::move(ccFP) };
  119. return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
  120. }
  121. }
  122. layout(key) in PMConversion pmConversion;
  123. @emitCode {
  124. fragBuilder->forceHighPrecision();
  125. }
  126. void main() {
  127. // Aggressively round to the nearest exact (N / 255) floating point value. This lets us find a
  128. // round-trip preserving pair on some GPUs that do odd byte to float conversion.
  129. sk_OutColor = floor(sk_InColor * 255 + 0.5) / 255;
  130. @switch (pmConversion) {
  131. case PMConversion::kToPremul:
  132. sk_OutColor.rgb = floor(sk_OutColor.rgb * sk_OutColor.a * 255 + 0.5) / 255;
  133. break;
  134. case PMConversion::kToUnpremul:
  135. sk_OutColor.rgb = sk_OutColor.a <= 0.0 ?
  136. half3(0) :
  137. floor(sk_OutColor.rgb / sk_OutColor.a * 255 + 0.5) / 255;
  138. break;
  139. }
  140. }
  141. @test(data) {
  142. PMConversion pmConv = static_cast<PMConversion>(data->fRandom->nextULessThan(
  143. (int) PMConversion::kPMConversionCnt));
  144. return std::unique_ptr<GrFragmentProcessor>(new GrConfigConversionEffect(pmConv));
  145. }