blob: 154b6433d46718c49126703a9ce9fd3a274c3126 [file] [log] [blame]
Ethan Nicholasa70693b2019-03-04 13:07:36 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColorFilter.h"
11#include "include/core/SkData.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkPaint.h"
14#include "include/core/SkRefCnt.h"
15#include "include/core/SkSize.h"
16#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkColorFilterPriv.h"
18#include "src/core/SkReadBuffer.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040019#include "src/core/SkWriteBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "tools/Resources.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021
22#include <stddef.h>
23#include <utility>
24
25class GrContext;
26class GrRenderTargetContext;
Ethan Nicholasa70693b2019-03-04 13:07:36 -050027
28const char* SKSL_TEST_SRC = R"(
Ethan Nicholasc1c686b2019-04-02 17:30:23 -040029 layout(ctype=float) in uniform half b;
Ethan Nicholasa70693b2019-03-04 13:07:36 -050030
31 void main(inout half4 color) {
Ethan Nicholas7e603db2019-05-03 12:57:47 -040032 color.a = color.r*0.3 + color.g*0.6 + color.b*0.1;
33 color.r = 0;
34 color.g = 0;
35 color.b = 0;
Ethan Nicholasa70693b2019-03-04 13:07:36 -050036 }
37)";
38
39static void runtimeCpuFunc(float color[4], const void* context) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -040040 color[3] = color[0]*0.3 + color[1]*0.6 + color[2]*0.1;
41 color[0] = 0;
42 color[1] = 0;
43 color[2] = 0;
Ethan Nicholasa70693b2019-03-04 13:07:36 -050044}
45
46DEF_SIMPLE_GPU_GM(runtimecolorfilter, context, rtc, canvas, 768, 256) {
47 auto img = GetResourceAsImage("images/mandrill_256.png");
48 canvas->drawImage(img, 0, 0, nullptr);
49
50 float b = 0.75;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040051 sk_sp<SkData> data = SkData::MakeWithCopy(&b, sizeof(b));
Ethan Nicholas0d997662019-04-08 09:46:01 -040052 static SkRuntimeColorFilterFactory fact = SkRuntimeColorFilterFactory(SkString(SKSL_TEST_SRC),
53 runtimeCpuFunc);
54 auto cf1 = fact.make(data);
Ethan Nicholasa70693b2019-03-04 13:07:36 -050055 SkPaint p;
56 p.setColorFilter(cf1);
57 canvas->drawImage(img, 256, 0, &p);
58
59 static constexpr size_t kBufferSize = 512;
60 char buffer[kBufferSize];
61 SkBinaryWriteBuffer wb(buffer, kBufferSize);
62 wb.writeFlattenable(cf1.get());
63 SkReadBuffer rb(buffer, kBufferSize);
64 auto cf2 = rb.readColorFilter();
Ethan Nicholasa1855622019-03-21 13:51:02 -040065 if (cf2) {
66 p.setColorFilter(cf2);
67 canvas->drawImage(img, 512, 0, &p);
68 }
Ethan Nicholasa70693b2019-03-04 13:07:36 -050069}
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040070
71DEF_SIMPLE_GM(runtimecolorfilter_interpreted, canvas, 768, 256) {
72 auto img = GetResourceAsImage("images/mandrill_256.png");
73 canvas->drawImage(img, 0, 0, nullptr);
74
75 float b = 0.75;
76 sk_sp<SkData> data = SkData::MakeWithCopy(&b, sizeof(b));
Ethan Nicholas0d997662019-04-08 09:46:01 -040077 static SkRuntimeColorFilterFactory fact = SkRuntimeColorFilterFactory(SkString(SKSL_TEST_SRC),
78 nullptr);
79 auto cf1 = fact.make(data);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040080 SkPaint p;
81 p.setColorFilter(cf1);
82 canvas->drawImage(img, 256, 0, &p);
83
84 static constexpr size_t kBufferSize = 512;
85 char buffer[kBufferSize];
86 SkBinaryWriteBuffer wb(buffer, kBufferSize);
87 wb.writeFlattenable(cf1.get());
88 SkReadBuffer rb(buffer, kBufferSize);
89 auto cf2 = rb.readColorFilter();
Ethan Nicholasa1855622019-03-21 13:51:02 -040090 if (cf2) {
91 p.setColorFilter(cf2);
92 canvas->drawImage(img, 512, 0, &p);
93 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040094}
Mike Reed33db04c2019-04-24 13:30:38 -040095
96// These need to be static for some dm caching tests in DM...
97static SkRuntimeColorFilterFactory gInterp =
98 SkRuntimeColorFilterFactory(SkString(SKSL_TEST_SRC), nullptr);
99static SkRuntimeColorFilterFactory gCpuProc =
100 SkRuntimeColorFilterFactory(SkString(SKSL_TEST_SRC), runtimeCpuFunc);
101
102class RuntimeCF : public skiagm::GM {
103public:
104 RuntimeCF(bool useCpuProc) : fFact(useCpuProc ? gCpuProc : gInterp) {
105 fName.printf("runtime_cf_interp_%d", !useCpuProc);
106 }
107
108protected:
109 bool runAsBench() const override { return true; }
110
111 SkString onShortName() override {
112 return fName;
113 }
114
115 SkISize onISize() override {
116 return SkISize::Make(512, 256);
117 }
118
119 void onOnceBeforeDraw() override {
120 fImg = GetResourceAsImage("images/mandrill_256.png")->makeRasterImage();
121 }
122
123 void onDraw(SkCanvas* canvas) override {
124 canvas->drawImage(fImg, 0, 0, nullptr);
125
126 float b = 0.75;
127 sk_sp<SkData> data = SkData::MakeWithCopy(&b, sizeof(b));
128 auto cf1 = fFact.make(data);
129 SkPaint p;
130 p.setColorFilter(cf1);
131 canvas->drawImage(fImg, 256, 0, &p);
132 }
133private:
134 sk_sp<SkImage> fImg;
135 SkRuntimeColorFilterFactory fFact;
136 SkString fName;
137
138 typedef skiagm::GM INHERITED;
139};
140DEF_GM(return new RuntimeCF(false);)
141//DEF_GM(return new RuntimeCF(true);)