constcolorprocessor.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // This test only works with the GPU backend.
  8. #include "gm/gm.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkFont.h"
  12. #include "include/core/SkFontTypes.h"
  13. #include "include/core/SkMatrix.h"
  14. #include "include/core/SkPaint.h"
  15. #include "include/core/SkPoint.h"
  16. #include "include/core/SkRect.h"
  17. #include "include/core/SkRefCnt.h"
  18. #include "include/core/SkScalar.h"
  19. #include "include/core/SkShader.h"
  20. #include "include/core/SkSize.h"
  21. #include "include/core/SkString.h"
  22. #include "include/core/SkTileMode.h"
  23. #include "include/core/SkTypeface.h"
  24. #include "include/core/SkTypes.h"
  25. #include "include/effects/SkGradientShader.h"
  26. #include "include/gpu/GrConfig.h"
  27. #include "include/gpu/GrContext.h"
  28. #include "include/private/GrTypesPriv.h"
  29. #include "include/private/SkColorData.h"
  30. #include "src/gpu/GrColor.h"
  31. #include "src/gpu/GrFragmentProcessor.h"
  32. #include "src/gpu/GrPaint.h"
  33. #include "src/gpu/GrRenderTargetContext.h"
  34. #include "src/gpu/GrRenderTargetContextPriv.h"
  35. #include "src/gpu/SkGr.h"
  36. #include "src/gpu/effects/generated/GrConstColorProcessor.h"
  37. #include "src/gpu/ops/GrDrawOp.h"
  38. #include "src/gpu/ops/GrFillRectOp.h"
  39. #include "tools/ToolUtils.h"
  40. #include <utility>
  41. namespace skiagm {
  42. /**
  43. * This GM directly exercises GrConstColorProcessor.
  44. */
  45. class ConstColorProcessor : public GpuGM {
  46. public:
  47. ConstColorProcessor() {
  48. this->setBGColor(0xFFDDDDDD);
  49. }
  50. protected:
  51. SkString onShortName() override {
  52. return SkString("const_color_processor");
  53. }
  54. SkISize onISize() override {
  55. return SkISize::Make(kWidth, kHeight);
  56. }
  57. void onOnceBeforeDraw() override {
  58. SkColor colors[] = { 0xFFFF0000, 0x2000FF00, 0xFF0000FF};
  59. SkPoint pts[] = { SkPoint::Make(0, 0), SkPoint::Make(kRectSize, kRectSize) };
  60. fShader = SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
  61. SkTileMode::kClamp);
  62. }
  63. void onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext,
  64. SkCanvas* canvas) override {
  65. constexpr GrColor kColors[] = {
  66. 0xFFFFFFFF,
  67. 0xFFFF00FF,
  68. 0x80000000,
  69. 0x00000000,
  70. };
  71. constexpr SkColor kPaintColors[] = {
  72. 0xFFFFFFFF,
  73. 0xFFFF0000,
  74. 0x80FF0000,
  75. 0x00000000,
  76. };
  77. const char* kModeStrs[] {
  78. "kIgnore",
  79. "kModulateRGBA",
  80. "kModulateA",
  81. };
  82. GR_STATIC_ASSERT(SK_ARRAY_COUNT(kModeStrs) == GrConstColorProcessor::kInputModeCnt);
  83. SkScalar y = kPad;
  84. SkScalar x = kPad;
  85. SkScalar maxW = 0;
  86. for (size_t paintType = 0; paintType < SK_ARRAY_COUNT(kPaintColors) + 1; ++paintType) {
  87. for (size_t procColor = 0; procColor < SK_ARRAY_COUNT(kColors); ++procColor) {
  88. for (int m = 0; m < GrConstColorProcessor::kInputModeCnt; ++m) {
  89. // translate by x,y for the canvas draws and the test target draws.
  90. canvas->save();
  91. canvas->translate(x, y);
  92. const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
  93. // rect to draw
  94. SkRect renderRect = SkRect::MakeXYWH(0, 0, kRectSize, kRectSize);
  95. GrPaint grPaint;
  96. SkPaint skPaint;
  97. if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
  98. skPaint.setShader(fShader);
  99. } else {
  100. skPaint.setColor(kPaintColors[paintType]);
  101. }
  102. SkAssertResult(SkPaintToGrPaint(context, renderTargetContext->colorSpaceInfo(),
  103. skPaint, viewMatrix, &grPaint));
  104. GrConstColorProcessor::InputMode mode = (GrConstColorProcessor::InputMode) m;
  105. SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(kColors[procColor]);
  106. auto fp = GrConstColorProcessor::Make(color, mode);
  107. grPaint.addColorFragmentProcessor(std::move(fp));
  108. renderTargetContext->priv().testingOnly_addDrawOp(
  109. GrFillRectOp::MakeNonAARect(context, std::move(grPaint),
  110. viewMatrix, renderRect));
  111. // Draw labels for the input to the processor and the processor to the right of
  112. // the test rect. The input label appears above the processor label.
  113. SkFont labelFont;
  114. labelFont.setTypeface(ToolUtils::create_portable_typeface());
  115. labelFont.setEdging(SkFont::Edging::kAntiAlias);
  116. labelFont.setSize(10.f);
  117. SkPaint labelPaint;
  118. labelPaint.setAntiAlias(true);
  119. SkString inputLabel;
  120. inputLabel.set("Input: ");
  121. if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
  122. inputLabel.append("gradient");
  123. } else {
  124. inputLabel.appendf("0x%08x", kPaintColors[paintType]);
  125. }
  126. SkString procLabel;
  127. procLabel.printf("Proc: [0x%08x, %s]", kColors[procColor], kModeStrs[m]);
  128. SkRect inputLabelBounds;
  129. // get the bounds of the text in order to position it
  130. labelFont.measureText(inputLabel.c_str(), inputLabel.size(),
  131. SkTextEncoding::kUTF8, &inputLabelBounds);
  132. canvas->drawString(inputLabel, renderRect.fRight + kPad, -inputLabelBounds.fTop,
  133. labelFont, labelPaint);
  134. // update the bounds to reflect the offset we used to draw it.
  135. inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop);
  136. SkRect procLabelBounds;
  137. labelFont.measureText(procLabel.c_str(), procLabel.size(),
  138. SkTextEncoding::kUTF8, &procLabelBounds);
  139. canvas->drawString(procLabel, renderRect.fRight + kPad,
  140. inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
  141. labelFont, labelPaint);
  142. procLabelBounds.offset(renderRect.fRight + kPad,
  143. inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop);
  144. labelPaint.setStrokeWidth(0);
  145. labelPaint.setStyle(SkPaint::kStroke_Style);
  146. canvas->drawRect(renderRect, labelPaint);
  147. canvas->restore();
  148. // update x and y for the next test case.
  149. SkScalar height = renderRect.height();
  150. SkScalar width = SkTMax(inputLabelBounds.fRight, procLabelBounds.fRight);
  151. maxW = SkTMax(maxW, width);
  152. y += height + kPad;
  153. if (y + height > kHeight) {
  154. y = kPad;
  155. x += maxW + kPad;
  156. maxW = 0;
  157. }
  158. }
  159. }
  160. }
  161. }
  162. private:
  163. // Use this as a way of generating and input FP
  164. sk_sp<SkShader> fShader;
  165. static constexpr SkScalar kPad = 10.f;
  166. static constexpr SkScalar kRectSize = 20.f;
  167. static constexpr int kWidth = 820;
  168. static constexpr int kHeight = 500;
  169. typedef GM INHERITED;
  170. };
  171. DEF_GM(return new ConstColorProcessor;)
  172. }