blob: fa944cb41a42c8f8750a679a44cba832964726bc [file] [log] [blame]
Mike Reed3fd3cc92019-06-20 12:40:30 -04001/*
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
24extern sk_sp<SkShader> SkRuntimeShaderMaker(SkString sksl, sk_sp<SkData> inputs,
25 const SkMatrix* localMatrix, bool isOpaque);
26
27const char* gProg = R"(
28 layout(ctype=float) in uniform half4 gColor;
29
30 half4 main(in float x, in float y) {
31 return half4(half(x)*(1.0/255), half(y)*(1.0/255), gColor.b, 1);
32 }
33)";
34
35class RuntimeShader : public skiagm::GM {
36public:
37 RuntimeShader() {
38 fName.printf("runtime_shader");
39 }
40
41protected:
42 bool runAsBench() const override { return true; }
43
44 SkString onShortName() override {
45 return fName;
46 }
47
48 SkISize onISize() override {
49 return SkISize::Make(512, 256);
50 }
51
52 void onOnceBeforeDraw() override {
53 SkMatrix localM;
54 localM.setRotate(90, 128, 128);
55
56 fData = SkData::MakeUninitialized(sizeof(SkColor4f));
57 SkColor4f* c = (SkColor4f*)fData->writable_data();
58 *c = {1, 1, 0, 1};
59 fShader = SkRuntimeShaderMaker(SkString(gProg), fData, &localM, true);
60 }
61
62 void onDraw(SkCanvas* canvas) override {
63 SkPaint p;
64 p.setShader(fShader);
65 canvas->drawRect({0, 0, 256, 256}, p);
66 }
67private:
68 SkString fName;
69 sk_sp<SkData> fData;
70 sk_sp<SkShader> fShader;
71
72 typedef skiagm::GM INHERITED;
73};
74DEF_GM(return new RuntimeShader;)