gamma.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright 2015 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. #include "gm/gm.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkBlendMode.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkColor.h"
  12. #include "include/core/SkColorPriv.h"
  13. #include "include/core/SkColorSpace.h"
  14. #include "include/core/SkFilterQuality.h"
  15. #include "include/core/SkFont.h"
  16. #include "include/core/SkFontTypes.h"
  17. #include "include/core/SkImageInfo.h"
  18. #include "include/core/SkMatrix.h"
  19. #include "include/core/SkPaint.h"
  20. #include "include/core/SkPoint.h"
  21. #include "include/core/SkRect.h"
  22. #include "include/core/SkScalar.h"
  23. #include "include/core/SkShader.h"
  24. #include "include/core/SkString.h"
  25. #include "include/core/SkTileMode.h"
  26. #include "include/core/SkTypeface.h"
  27. #include "include/effects/SkGradientShader.h"
  28. #include "tools/ToolUtils.h"
  29. #include <string.h>
  30. DEF_SIMPLE_GM(gamma, canvas, 850, 200) {
  31. SkPaint p;
  32. const SkScalar sz = 50.0f;
  33. const int szInt = SkScalarTruncToInt(sz);
  34. const SkScalar tx = sz + 15.0f;
  35. const SkRect r = SkRect::MakeXYWH(0, 0, sz, sz);
  36. SkTileMode rpt = SkTileMode::kRepeat;
  37. auto srgbColorSpace = SkColorSpace::MakeSRGB();
  38. SkBitmap ditherBmp;
  39. ditherBmp.allocN32Pixels(2, 2);
  40. SkPMColor* pixels = reinterpret_cast<SkPMColor*>(ditherBmp.getPixels());
  41. pixels[0] = pixels[3] = SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF);
  42. pixels[1] = pixels[2] = SkPackARGB32(0xFF, 0, 0, 0);
  43. SkBitmap linearGreyBmp;
  44. SkImageInfo linearGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAlphaType, nullptr);
  45. linearGreyBmp.allocPixels(linearGreyInfo);
  46. linearGreyBmp.eraseARGB(0xFF, 0x7F, 0x7F, 0x7F);
  47. SkBitmap srgbGreyBmp;
  48. SkImageInfo srgbGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAlphaType,
  49. srgbColorSpace);
  50. srgbGreyBmp.allocPixels(srgbGreyInfo);
  51. // 0xBC = 255 * linear_to_srgb(0.5f)
  52. srgbGreyBmp.eraseARGB(0xFF, 0xBC, 0xBC, 0xBC);
  53. SkBitmap mipmapBmp;
  54. SkImageInfo mipmapInfo = SkImageInfo::Make(2, 2, kN32_SkColorType, kOpaque_SkAlphaType,
  55. srgbColorSpace);
  56. mipmapBmp.allocPixels(mipmapInfo);
  57. SkPMColor* mipmapPixels = reinterpret_cast<SkPMColor*>(mipmapBmp.getPixels());
  58. unsigned s25 = 0x89; // 255 * linear_to_srgb(0.25f)
  59. unsigned s75 = 0xE1; // 255 * linear_to_srgb(0.75f)
  60. mipmapPixels[0] = mipmapPixels[3] = SkPackARGB32(0xFF, s25, s25, s25);
  61. mipmapPixels[1] = mipmapPixels[2] = SkPackARGB32(0xFF, s75, s75, s75);
  62. SkFont font(ToolUtils::create_portable_typeface());
  63. SkPaint textPaint;
  64. textPaint.setAntiAlias(true);
  65. textPaint.setColor(SK_ColorWHITE);
  66. // Helpers:
  67. auto advance = [&]() {
  68. canvas->translate(tx, 0);
  69. p.reset();
  70. };
  71. auto drawString = [&](const char str[], SkScalar x, SkScalar y) {
  72. canvas->drawSimpleText(str, strlen(str), SkTextEncoding::kUTF8, x, y, font, textPaint);
  73. };
  74. auto nextRect = [&](const char* label, const char* label2) {
  75. canvas->drawRect(r, p);
  76. drawString(label, 0, sz + font.getSpacing());
  77. if (label2) {
  78. drawString(label2, 0, sz + 2 * font.getSpacing());
  79. }
  80. advance();
  81. };
  82. auto nextBitmap = [&](const SkBitmap& bmp, const char* label) {
  83. canvas->drawBitmap(bmp, 0, 0);
  84. drawString(label, 0, sz + font.getSpacing());
  85. advance();
  86. };
  87. auto nextXferRect = [&](SkColor srcColor, SkBlendMode mode, SkColor dstColor) {
  88. p.setColor(dstColor);
  89. canvas->drawRect(r, p);
  90. p.setColor(srcColor);
  91. p.setBlendMode(mode);
  92. canvas->drawRect(r, p);
  93. SkString srcText = SkStringPrintf("%08X", srcColor);
  94. SkString dstText = SkStringPrintf("%08X", dstColor);
  95. drawString(srcText.c_str(), 0, sz + font.getSpacing());
  96. const char* modeName = SkBlendMode_Name(mode);
  97. drawString(modeName, 0, sz + 2 * font.getSpacing());
  98. drawString(dstText.c_str(), 0, sz + 3 * font.getSpacing());
  99. advance();
  100. };
  101. // Necessary for certain Xfermode tests to work (ie some of them output white @ 50% alpha):
  102. canvas->clear(SK_ColorBLACK);
  103. // *Everything* should be perceptually 50% grey. Only the first rectangle
  104. // is guaranteed to draw that way, though.
  105. canvas->save();
  106. // Black/white dither, pixel perfect. This is ground truth.
  107. p.setShader(ditherBmp.makeShader(rpt, rpt));
  108. p.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality);
  109. nextRect("Dither", "Reference");
  110. // Black/white dither, sampled at half-texel offset. Tests bilerp.
  111. // NOTE: We need to apply a non-identity scale and/or rotation to trick
  112. // the raster pipeline into *not* snapping to nearest.
  113. SkMatrix offsetMatrix = SkMatrix::Concat(
  114. SkMatrix::MakeScale(-1.0f), SkMatrix::MakeTrans(0.5f, 0.0f));
  115. p.setShader(ditherBmp.makeShader(rpt, rpt, &offsetMatrix));
  116. p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
  117. nextRect("Dither", "Bilerp");
  118. // Black/white dither, scaled down by 2x. Tests minification.
  119. SkMatrix scaleMatrix = SkMatrix::MakeScale(0.5f);
  120. p.setShader(ditherBmp.makeShader(rpt, rpt, &scaleMatrix));
  121. p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
  122. nextRect("Dither", "Scale");
  123. // 25%/75% dither, scaled down by 2x. Tests ALL aspects of minification. Specifically, are
  124. // sRGB sources decoded to linear before computing mipmaps?
  125. p.setShader(mipmapBmp.makeShader(rpt, rpt, &scaleMatrix));
  126. p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
  127. nextRect("MipMaps", nullptr);
  128. // 50% grey via paint color. Paint color (SkColor) is specified to be sRGB!
  129. p.setColor(0xffbcbcbc);
  130. nextRect("Color", nullptr);
  131. {
  132. // Black -> White gradient, scaled to sample just the middle.
  133. // Tests gradient interpolation.
  134. SkPoint points[2] = {
  135. SkPoint::Make(0 - (sz * 10), 0),
  136. SkPoint::Make(sz + (sz * 10), 0)
  137. };
  138. SkColor colors[2] = { SK_ColorBLACK, SK_ColorWHITE };
  139. p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 2, SkTileMode::kClamp));
  140. nextRect("Gradient", "Interpolation");
  141. }
  142. {
  143. // Shallow gradient around 50% (perceptual) gray. Endpoints are SkColor, so sRGB.
  144. // Tests gamma-correction of gradient stops before interpolation in two-stop case
  145. SkPoint points[2] = {
  146. SkPoint::Make(0, 0),
  147. SkPoint::Make(sz, 0)
  148. };
  149. SkColor colors[2] = { 0xffbbbbbb, 0xffbdbdbd };
  150. p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 2, SkTileMode::kClamp));
  151. nextRect("Gradient", "Endpoints");
  152. }
  153. {
  154. // Shallow 3-stop gradient around 50% (perceptual) gray. Endpoints are SkColor, so sRGB.
  155. // Tests gamma-correction of gradient stops before interpolation in three-stop case
  156. SkPoint points[2] = {
  157. SkPoint::Make(0, 0),
  158. SkPoint::Make(sz, 0)
  159. };
  160. SkColor colors[3] = { 0xffbbbbbb, 0xffbdbdbd, 0xffbbbbbb };
  161. p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 3, SkTileMode::kClamp));
  162. nextRect("Gradient", "3-Stop");
  163. }
  164. {
  165. // Shallow N-stop gradient around 50% (perceptual) gray. Endpoints are SkColor, so sRGB.
  166. // Tests gamma-correction of gradient stops before interpolation in texture implementation
  167. SkPoint points[2] = {
  168. SkPoint::Make(0, 0),
  169. SkPoint::Make(sz, 0)
  170. };
  171. SkColor colors[5] = { 0xffbbbbbb, 0xffbdbdbd, 0xffbbbbbb, 0xffbdbdbd, 0xffbbbbbb };
  172. p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 5, SkTileMode::kClamp));
  173. nextRect("Gradient", "Texture");
  174. }
  175. // 50% grey from linear bitmap, with drawBitmap
  176. nextBitmap(linearGreyBmp, "Lnr BMP");
  177. // 50% grey from sRGB bitmap, with drawBitmap
  178. nextBitmap(srgbGreyBmp, "sRGB BMP");
  179. // Bitmap wrapped in a shader (linear):
  180. p.setShader(linearGreyBmp.makeShader(rpt, rpt));
  181. p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
  182. nextRect("Lnr BMP", "Shader");
  183. // Bitmap wrapped in a shader (sRGB):
  184. p.setShader(srgbGreyBmp.makeShader(rpt, rpt));
  185. p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
  186. nextRect("sRGB BMP", "Shader");
  187. // Carriage return.
  188. canvas->restore();
  189. canvas->translate(0, 2 * sz);
  190. // Xfermode tests, all done off-screen so certain modes work...
  191. canvas->saveLayer(nullptr, nullptr);
  192. nextXferRect(0x7fffffff, SkBlendMode::kSrcOver, SK_ColorBLACK);
  193. nextXferRect(0x7f000000, SkBlendMode::kSrcOver, SK_ColorWHITE);
  194. nextXferRect(SK_ColorBLACK, SkBlendMode::kDstOver, 0x7fffffff);
  195. nextXferRect(SK_ColorWHITE, SkBlendMode::kSrcIn, 0x7fff00ff);
  196. nextXferRect(0x7fff00ff, SkBlendMode::kDstIn, SK_ColorWHITE);
  197. // 0x89 = 255 * linear_to_srgb(0.25)
  198. nextXferRect(0xff898989, SkBlendMode::kPlus, 0xff898989);
  199. // 0xDB = 255 * linear_to_srgb(sqrt(0.5))
  200. nextXferRect(0xffdbdbdb, SkBlendMode::kModulate, 0xffdbdbdb);
  201. canvas->restore();
  202. }