1 /* 2 * Copyright 2016 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 "Benchmark.h" 9 10 #include "GrContext.h" 11 #include "GrContextPriv.h" 12 #include "GrGeometryProcessor.h" 13 #include "GrMemoryPool.h" 14 #include "GrRenderTargetContext.h" 15 #include "GrRenderTargetContextPriv.h" 16 #include "SkColorSpacePriv.h" 17 #include "SkGr.h" 18 #include "SkHalf.h" 19 #include "SkString.h" 20 #include "glsl/GrGLSLColorSpaceXformHelper.h" 21 #include "glsl/GrGLSLFragmentShaderBuilder.h" 22 #include "glsl/GrGLSLGeometryProcessor.h" 23 #include "glsl/GrGLSLVarying.h" 24 #include "glsl/GrGLSLVertexGeoBuilder.h" 25 #include "ops/GrMeshDrawOp.h" 26 27 namespace { 28 29 enum Mode { 30 kBaseline_Mode, // Do the wrong thing, but quickly. 31 kFloat_Mode, // Transform colors on CPU, use float4 attributes. 32 kHalf_Mode, // Transform colors on CPU, use half4 attributes. 33 kShader_Mode, // Use ubyte4 attributes, transform colors on GPU (vertex shader). 34 }; 35 36 class GP : public GrGeometryProcessor { 37 public: GP(Mode mode,sk_sp<GrColorSpaceXform> colorSpaceXform)38 GP(Mode mode, sk_sp<GrColorSpaceXform> colorSpaceXform) 39 : INHERITED(kVertexColorSpaceBenchGP_ClassID) 40 , fMode(mode) 41 , fColorSpaceXform(std::move(colorSpaceXform)) { 42 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; 43 switch (fMode) { 44 case kBaseline_Mode: 45 case kShader_Mode: 46 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType}; 47 break; 48 case kFloat_Mode: 49 fInColor = {"inColor", kFloat4_GrVertexAttribType, kHalf4_GrSLType}; 50 break; 51 case kHalf_Mode: 52 fInColor = {"inColor", kHalf4_GrVertexAttribType, kHalf4_GrSLType}; 53 break; 54 } 55 this->setVertexAttributes(&fInPosition, 2); 56 } name() const57 const char* name() const override { return "VertexColorXformGP"; } 58 createGLSLInstance(const GrShaderCaps &) const59 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { 60 class GLSLGP : public GrGLSLGeometryProcessor { 61 public: 62 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { 63 const GP& gp = args.fGP.cast<GP>(); 64 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; 65 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; 66 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; 67 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; 68 69 varyingHandler->emitAttributes(gp); 70 71 // Setup color 72 GrGLSLVarying varying(kHalf4_GrSLType); 73 varyingHandler->addVarying("color", &varying); 74 vertBuilder->codeAppendf("half4 color = %s;", gp.fInColor.name()); 75 76 if (kShader_Mode == gp.fMode) { 77 fColorSpaceHelper.emitCode(uniformHandler, gp.fColorSpaceXform.get(), 78 kVertex_GrShaderFlag); 79 SkString xformedColor; 80 vertBuilder->appendColorGamutXform(&xformedColor, "color", &fColorSpaceHelper); 81 vertBuilder->codeAppendf("color = %s;", xformedColor.c_str()); 82 vertBuilder->codeAppend("color = half4(color.rgb * color.a, color.a);"); 83 } 84 85 vertBuilder->codeAppendf("%s = color;", varying.vsOut()); 86 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, varying.fsIn()); 87 88 // Position 89 this->writeOutputPosition(args.fVertBuilder, gpArgs, gp.fInPosition.name()); 90 91 // Coverage 92 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage); 93 } 94 void setData(const GrGLSLProgramDataManager& pdman, 95 const GrPrimitiveProcessor& primProc, 96 FPCoordTransformIter&&) override { 97 const GP& gp = primProc.cast<GP>(); 98 fColorSpaceHelper.setData(pdman, gp.fColorSpaceXform.get()); 99 } 100 101 GrGLSLColorSpaceXformHelper fColorSpaceHelper; 102 }; 103 return new GLSLGP(); 104 } 105 getGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder * b) const106 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { 107 b->add32(fMode); 108 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get())); 109 } 110 111 private: 112 Mode fMode; 113 sk_sp<GrColorSpaceXform> fColorSpaceXform; 114 115 Attribute fInPosition; 116 Attribute fInColor; 117 118 typedef GrGeometryProcessor INHERITED; 119 }; 120 121 class Op : public GrMeshDrawOp { 122 public: 123 DEFINE_OP_CLASS_ID 124 name() const125 const char* name() const override { return "VertColorXformOp"; } 126 Op(GrColor color)127 Op(GrColor color) 128 : INHERITED(ClassID()) 129 , fMode(kBaseline_Mode) 130 , fColor(color) { 131 this->setBounds(SkRect::MakeWH(100.f, 100.f), HasAABloat::kNo, IsZeroArea::kNo); 132 } 133 Op(const SkColor4f & color4f,Mode mode)134 Op(const SkColor4f& color4f, Mode mode) 135 : INHERITED(ClassID()) 136 , fMode(mode) 137 , fColor4f(color4f) { 138 SkASSERT(kFloat_Mode == fMode || kHalf_Mode == mode); 139 this->setBounds(SkRect::MakeWH(100.f, 100.f), HasAABloat::kNo, IsZeroArea::kNo); 140 } 141 Op(GrColor color,sk_sp<GrColorSpaceXform> colorSpaceXform)142 Op(GrColor color, sk_sp<GrColorSpaceXform> colorSpaceXform) 143 : INHERITED(ClassID()) 144 , fMode(kShader_Mode) 145 , fColor(color) 146 , fColorSpaceXform(std::move(colorSpaceXform)) { 147 this->setBounds(SkRect::MakeWH(100.f, 100.f), HasAABloat::kNo, IsZeroArea::kNo); 148 } 149 fixedFunctionFlags() const150 FixedFunctionFlags fixedFunctionFlags() const override { 151 return FixedFunctionFlags::kNone; 152 } 153 finalize(const GrCaps &,const GrAppliedClip *)154 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*) override { 155 return GrProcessorSet::EmptySetAnalysis(); 156 } 157 158 private: 159 friend class ::GrOpMemoryPool; 160 onPrepareDraws(Target * target)161 void onPrepareDraws(Target* target) override { 162 sk_sp<GrGeometryProcessor> gp(new GP(fMode, fColorSpaceXform)); 163 164 size_t vertexStride = gp->vertexStride(); 165 const int kVertexCount = 1024; 166 sk_sp<const GrBuffer> vertexBuffer; 167 int firstVertex = 0; 168 void* verts = target->makeVertexSpace(vertexStride, kVertexCount, &vertexBuffer, 169 &firstVertex); 170 if (!verts) { 171 return; 172 } 173 174 const float dx = 100.0f / kVertexCount; 175 if (kFloat_Mode == fMode) { 176 struct V { 177 SkPoint fPos; 178 SkColor4f fColor; 179 }; 180 SkASSERT(sizeof(V) == vertexStride); 181 V* v = (V*)verts; 182 for (int i = 0; i < kVertexCount; i += 2) { 183 v[i + 0].fPos.set(dx * i, 0.0f); 184 v[i + 0].fColor = fColor4f; 185 v[i + 1].fPos.set(dx * i, 100.0f); 186 v[i + 1].fColor = fColor4f; 187 } 188 } else if (kHalf_Mode == fMode) { 189 struct V { 190 SkPoint fPos; 191 uint64_t fColor; 192 }; 193 SkASSERT(sizeof(V) == vertexStride); 194 uint64_t color; 195 Sk4h halfColor = SkFloatToHalf_finite_ftz(Sk4f::Load(&fColor4f)); 196 color = (uint64_t)halfColor[0] << 48 | 197 (uint64_t)halfColor[1] << 32 | 198 (uint64_t)halfColor[2] << 16 | 199 (uint64_t)halfColor[3] << 0; 200 V* v = (V*)verts; 201 for (int i = 0; i < kVertexCount; i += 2) { 202 v[i + 0].fPos.set(dx * i, 0.0f); 203 v[i + 0].fColor = color; 204 v[i + 1].fPos.set(dx * i, 100.0f); 205 v[i + 1].fColor = color; 206 } 207 } else { 208 struct V { 209 SkPoint fPos; 210 GrColor fColor; 211 }; 212 SkASSERT(sizeof(V) == vertexStride); 213 V* v = (V*)verts; 214 for (int i = 0; i < kVertexCount; i += 2) { 215 v[i + 0].fPos.set(dx * i, 0.0f); 216 v[i + 0].fColor = fColor; 217 v[i + 1].fPos.set(dx * i, 100.0f); 218 v[i + 1].fColor = fColor; 219 } 220 } 221 222 GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangleStrip); 223 mesh->setNonIndexedNonInstanced(kVertexCount); 224 mesh->setVertexData(std::move(vertexBuffer), firstVertex); 225 auto pipe = target->makePipeline(0, GrProcessorSet::MakeEmptySet(), 226 target->detachAppliedClip()); 227 target->draw(gp, pipe.fPipeline, pipe.fFixedDynamicState, mesh); 228 } 229 230 Mode fMode; 231 GrColor fColor; 232 SkColor4f fColor4f; 233 sk_sp<GrColorSpaceXform> fColorSpaceXform; 234 235 typedef GrMeshDrawOp INHERITED; 236 }; 237 } 238 239 class VertexColorSpaceBench : public Benchmark { 240 public: VertexColorSpaceBench(Mode mode,const char * name)241 VertexColorSpaceBench(Mode mode, const char* name) : fMode(mode) { 242 fName = "vertexcolorspace"; 243 fName.appendf("_%s", name); 244 } 245 isSuitableFor(Backend backend)246 bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; } onGetName()247 const char* onGetName() override { return fName.c_str(); } 248 onDraw(int loops,SkCanvas * canvas)249 void onDraw(int loops, SkCanvas* canvas) override { 250 GrContext* context = canvas->getGrContext(); 251 SkASSERT(context); 252 253 if (kHalf_Mode == fMode && 254 !context->contextPriv().caps()->halfFloatVertexAttributeSupport()) { 255 return; 256 } 257 258 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool(); 259 260 auto p3 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, 261 SkNamedGamut::kDCIP3); 262 auto xform = GrColorSpaceXform::Make(sk_srgb_singleton(), kUnpremul_SkAlphaType, 263 p3.get(), kUnpremul_SkAlphaType); 264 265 SkRandom r; 266 const int kDrawsPerLoop = 32; 267 268 const GrBackendFormat format = 269 context->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType); 270 for (int i = 0; i < loops; ++i) { 271 sk_sp<GrRenderTargetContext> rtc( 272 context->contextPriv().makeDeferredRenderTargetContext( 273 format, SkBackingFit::kApprox, 100, 100, kRGBA_8888_GrPixelConfig, p3)); 274 SkASSERT(rtc); 275 276 for (int j = 0; j < kDrawsPerLoop; ++j) { 277 SkColor c = r.nextU(); 278 std::unique_ptr<GrDrawOp> op = nullptr; 279 280 switch (fMode) { 281 case kBaseline_Mode: 282 op = pool->allocate<Op>(SkColorToPremulGrColor(c)); 283 break; 284 case kShader_Mode: 285 op = pool->allocate<Op>(SkColorToUnpremulGrColor(c), xform); 286 break; 287 case kHalf_Mode: 288 case kFloat_Mode: { 289 SkColor4f c4f = SkColor4f::FromColor(c); 290 c4f = xform->apply(c4f); 291 op = pool->allocate<Op>(c4f, fMode); 292 } 293 } 294 rtc->priv().testingOnly_addDrawOp(std::move(op)); 295 } 296 297 context->flush(); 298 } 299 } 300 301 private: 302 SkString fName; 303 Mode fMode; 304 305 typedef Benchmark INHERITED; 306 }; 307 308 DEF_BENCH(return new VertexColorSpaceBench(kBaseline_Mode, "baseline")); 309 DEF_BENCH(return new VertexColorSpaceBench(kFloat_Mode, "float")); 310 DEF_BENCH(return new VertexColorSpaceBench(kHalf_Mode, "half")); 311 DEF_BENCH(return new VertexColorSpaceBench(kShader_Mode, "shader")); 312