1 /* 2 * Copyright 2019 Google LLC 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 "gm/gm.h" 9 #include "include/core/SkCanvas.h" 10 #include "include/core/SkColorFilter.h" 11 #include "include/core/SkData.h" 12 #include "include/core/SkImage.h" 13 #include "include/core/SkPaint.h" 14 #include "include/core/SkRefCnt.h" 15 #include "include/core/SkSize.h" 16 #include "include/core/SkString.h" 17 #include "src/core/SkColorFilterPriv.h" 18 #include "src/core/SkReadBuffer.h" 19 #include "src/core/SkWriteBuffer.h" 20 #include "tools/Resources.h" 21 22 #include <stddef.h> 23 24 extern sk_sp<SkShader> SkRuntimeShaderMaker(SkString sksl, sk_sp<SkData> inputs, 25 const SkMatrix* localMatrix, bool isOpaque); 26 27 const char* gProg = R"( 28 layout(ctype=SkRect) in uniform half4 gColor; 29 30 void main(float x, float y, inout half4 color) { 31 color = half4(half(x)*(1.0/255), half(y)*(1.0/255), gColor.b, 1); 32 } 33 )"; 34 35 static sk_sp<SkShader> gShader; 36 37 class RuntimeShader : public skiagm::GM { 38 sk_sp<SkData> fData; 39 runAsBench() const40 bool runAsBench() const override { return true; } 41 onShortName()42 SkString onShortName() override { return SkString("runtime_shader"); } 43 onISize()44 SkISize onISize() override { return {512, 256}; } 45 onOnceBeforeDraw()46 void onOnceBeforeDraw() override { 47 // use global to pass gl persistent cache test in dm 48 if (!gShader) { 49 SkMatrix localM; 50 localM.setRotate(90, 128, 128); 51 52 fData = SkData::MakeUninitialized(sizeof(SkColor4f)); 53 SkColor4f* c = (SkColor4f*)fData->writable_data(); 54 *c = {1, 0, 0, 1}; 55 gShader = SkRuntimeShaderMaker(SkString(gProg), fData, &localM, true); 56 } 57 } 58 onDraw(SkCanvas * canvas)59 void onDraw(SkCanvas* canvas) override { 60 SkPaint p; 61 p.setShader(gShader); 62 canvas->drawRect({0, 0, 256, 256}, p); 63 } 64 }; 65 DEF_GM(return new RuntimeShader;) 66