clockwise.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include "gm/gm.h"
  8. #include "include/core/SkBlendMode.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColorSpace.h"
  11. #include "include/core/SkMatrix.h"
  12. #include "include/core/SkPoint.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkRefCnt.h"
  15. #include "include/core/SkSize.h"
  16. #include "include/core/SkString.h"
  17. #include "include/core/SkTypes.h"
  18. #include "include/gpu/GrContext.h"
  19. #include "include/gpu/GrSamplerState.h"
  20. #include "include/gpu/GrTypes.h"
  21. #include "include/private/GrRecordingContext.h"
  22. #include "include/private/GrTypesPriv.h"
  23. #include "include/private/SkColorData.h"
  24. #include "src/gpu/GrBuffer.h"
  25. #include "src/gpu/GrCaps.h"
  26. #include "src/gpu/GrClip.h"
  27. #include "src/gpu/GrColorSpaceXform.h"
  28. #include "src/gpu/GrContextPriv.h"
  29. #include "src/gpu/GrGeometryProcessor.h"
  30. #include "src/gpu/GrGpuBuffer.h"
  31. #include "src/gpu/GrGpuCommandBuffer.h"
  32. #include "src/gpu/GrMemoryPool.h"
  33. #include "src/gpu/GrMesh.h"
  34. #include "src/gpu/GrOpFlushState.h"
  35. #include "src/gpu/GrPipeline.h"
  36. #include "src/gpu/GrPrimitiveProcessor.h"
  37. #include "src/gpu/GrProcessor.h"
  38. #include "src/gpu/GrProcessorSet.h"
  39. #include "src/gpu/GrRecordingContextPriv.h"
  40. #include "src/gpu/GrRenderTargetContext.h"
  41. #include "src/gpu/GrRenderTargetContextPriv.h"
  42. #include "src/gpu/GrResourceProvider.h"
  43. #include "src/gpu/GrShaderCaps.h"
  44. #include "src/gpu/GrShaderVar.h"
  45. #include "src/gpu/GrSurfaceProxy.h"
  46. #include "src/gpu/GrTextureProxy.h"
  47. #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
  48. #include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
  49. #include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
  50. #include "src/gpu/glsl/GrGLSLVarying.h"
  51. #include "src/gpu/ops/GrDrawOp.h"
  52. #include "src/gpu/ops/GrOp.h"
  53. #include <memory>
  54. #include <utility>
  55. class GrAppliedClip;
  56. class GrGLSLProgramDataManager;
  57. namespace {
  58. static constexpr GrGeometryProcessor::Attribute gVertex =
  59. {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
  60. /**
  61. * This is a GPU-backend specific test. It ensures that SkSL properly identifies clockwise-winding
  62. * triangles (sk_Clockwise), in terms of to Skia device space, in all backends and with all render
  63. * target origins. We draw clockwise triangles green and counter-clockwise red.
  64. */
  65. class ClockwiseGM : public skiagm::GpuGM {
  66. SkString onShortName() override { return SkString("clockwise"); }
  67. SkISize onISize() override { return {300, 200}; }
  68. void onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*) override;
  69. };
  70. ////////////////////////////////////////////////////////////////////////////////////////////////////
  71. // SkSL code.
  72. class ClockwiseTestProcessor : public GrGeometryProcessor {
  73. public:
  74. ClockwiseTestProcessor(bool readSkFragCoord)
  75. : GrGeometryProcessor(kClockwiseTestProcessor_ClassID)
  76. , fReadSkFragCoord(readSkFragCoord) {
  77. this->setVertexAttributes(&gVertex, 1);
  78. }
  79. const char* name() const override { return "ClockwiseTestProcessor"; }
  80. void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
  81. b->add32(fReadSkFragCoord);
  82. }
  83. GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
  84. private:
  85. const bool fReadSkFragCoord;
  86. friend class GLSLClockwiseTestProcessor;
  87. };
  88. class GLSLClockwiseTestProcessor : public GrGLSLGeometryProcessor {
  89. void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
  90. FPCoordTransformIter&& transformIter) override {}
  91. void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
  92. const ClockwiseTestProcessor& proc = args.fGP.cast<ClockwiseTestProcessor>();
  93. args.fVaryingHandler->emitAttributes(proc);
  94. gpArgs->fPositionVar.set(kFloat2_GrSLType, "position");
  95. args.fFragBuilder->codeAppendf(
  96. "%s = sk_Clockwise ? half4(0,1,0,1) : half4(1,0,0,1);", args.fOutputColor);
  97. if (!proc.fReadSkFragCoord) {
  98. args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
  99. } else {
  100. // Verify layout(origin_upper_left) on gl_FragCoord does not affect gl_FrontFacing.
  101. args.fFragBuilder->codeAppendf("%s = half4(min(half(sk_FragCoord.y), 1));",
  102. args.fOutputCoverage);
  103. }
  104. }
  105. };
  106. GrGLSLPrimitiveProcessor* ClockwiseTestProcessor::createGLSLInstance(
  107. const GrShaderCaps&) const {
  108. return new GLSLClockwiseTestProcessor;
  109. }
  110. ////////////////////////////////////////////////////////////////////////////////////////////////////
  111. // Draw Op.
  112. class ClockwiseTestOp : public GrDrawOp {
  113. public:
  114. DEFINE_OP_CLASS_ID
  115. static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
  116. bool readSkFragCoord, int y = 0) {
  117. GrOpMemoryPool* pool = context->priv().opMemoryPool();
  118. return pool->allocate<ClockwiseTestOp>(readSkFragCoord, y);
  119. }
  120. private:
  121. ClockwiseTestOp(bool readSkFragCoord, float y)
  122. : GrDrawOp(ClassID()), fReadSkFragCoord(readSkFragCoord), fY(y) {
  123. this->setBounds(SkRect::MakeIWH(300, 100), HasAABloat::kNo, IsZeroArea::kNo);
  124. }
  125. const char* name() const override { return "ClockwiseTestOp"; }
  126. FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
  127. GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
  128. bool hasMixedSampledCoverage, GrClampType) override {
  129. return GrProcessorSet::EmptySetAnalysis();
  130. }
  131. void onPrepare(GrOpFlushState*) override {}
  132. void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
  133. SkPoint vertices[4] = {
  134. {100, fY},
  135. {0, fY+100},
  136. {0, fY},
  137. {100, fY+100},
  138. };
  139. sk_sp<const GrBuffer> vertexBuffer(flushState->resourceProvider()->createBuffer(
  140. sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices));
  141. if (!vertexBuffer) {
  142. return;
  143. }
  144. GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kPlus,
  145. flushState->drawOpArgs().fOutputSwizzle);
  146. GrMesh mesh(GrPrimitiveType::kTriangleStrip);
  147. mesh.setNonIndexedNonInstanced(4);
  148. mesh.setVertexData(std::move(vertexBuffer));
  149. flushState->rtCommandBuffer()->draw(ClockwiseTestProcessor(fReadSkFragCoord), pipeline,
  150. nullptr, nullptr, &mesh, 1, SkRect::MakeIWH(100, 100));
  151. }
  152. const bool fReadSkFragCoord;
  153. const float fY;
  154. friend class ::GrOpMemoryPool; // for ctor
  155. };
  156. ////////////////////////////////////////////////////////////////////////////////////////////////////
  157. // Test.
  158. void ClockwiseGM::onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas) {
  159. rtc->clear(nullptr, { 0, 0, 0, 1 }, GrRenderTargetContext::CanClearFullscreen::kYes);
  160. // Draw the test directly to the frame buffer.
  161. rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
  162. rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
  163. // Draw the test to an off-screen, top-down render target.
  164. if (auto topLeftRTC = ctx->priv().makeDeferredRenderTargetContext(
  165. SkBackingFit::kExact, 100, 200, rtc->colorSpaceInfo().colorType(), nullptr, 1,
  166. GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes)) {
  167. topLeftRTC->clear(nullptr, SK_PMColor4fTRANSPARENT,
  168. GrRenderTargetContext::CanClearFullscreen::kYes);
  169. topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
  170. topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
  171. rtc->drawTexture(GrNoClip(), sk_ref_sp(topLeftRTC->asTextureProxy()),
  172. GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver,
  173. SK_PMColor4fWHITE, {0, 0, 100, 200},
  174. {100, 0, 200, 200}, GrAA::kNo, GrQuadAAFlags::kNone,
  175. SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(),
  176. nullptr);
  177. }
  178. // Draw the test to an off-screen, bottom-up render target.
  179. if (auto topLeftRTC = ctx->priv().makeDeferredRenderTargetContext(
  180. SkBackingFit::kExact, 100, 200, rtc->colorSpaceInfo().colorType(), nullptr, 1,
  181. GrMipMapped::kNo, kBottomLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes)) {
  182. topLeftRTC->clear(nullptr, SK_PMColor4fTRANSPARENT,
  183. GrRenderTargetContext::CanClearFullscreen::kYes);
  184. topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
  185. topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
  186. rtc->drawTexture(GrNoClip(), sk_ref_sp(topLeftRTC->asTextureProxy()),
  187. GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver,
  188. SK_PMColor4fWHITE, {0, 0, 100, 200},
  189. {200, 0, 300, 200}, GrAA::kNo, GrQuadAAFlags::kNone,
  190. SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(),
  191. nullptr);
  192. }
  193. }
  194. ////////////////////////////////////////////////////////////////////////////////////////////////////
  195. DEF_GM( return new ClockwiseGM(); )
  196. }