blob: ebc6a20619984c4f2e7849e70d486434420623c1 [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
Ethan Nicholasa70693b2019-03-04 13:07:36 -05008#include "Resources.h"
Mike Kleinea3f0142019-03-20 11:12:10 -05009#include "SkCanvas.h"
Ethan Nicholasa70693b2019-03-04 13:07:36 -050010#include "SkColorFilterPriv.h"
Mike Kleinea3f0142019-03-20 11:12:10 -050011#include "SkImage.h"
Ethan Nicholasa70693b2019-03-04 13:07:36 -050012#include "SkReadBuffer.h"
Mike Kleinea3f0142019-03-20 11:12:10 -050013#include "ToolUtils.h"
Ethan Nicholasa70693b2019-03-04 13:07:36 -050014#include "effects/GrSkSLFP.h"
Mike Kleinea3f0142019-03-20 11:12:10 -050015#include "gm.h"
Ethan Nicholasa70693b2019-03-04 13:07:36 -050016
17const char* SKSL_TEST_SRC = R"(
18 in uniform float b;
19
20 void main(inout half4 color) {
21 color.rg = color.gr;
22 color.b = half(b);
23 }
24)";
25
26static void runtimeCpuFunc(float color[4], const void* context) {
27 std::swap(color[0], color[1]);
28 color[2] = *(float*) context;
29}
30
31DEF_SIMPLE_GPU_GM(runtimecolorfilter, context, rtc, canvas, 768, 256) {
32 auto img = GetResourceAsImage("images/mandrill_256.png");
33 canvas->drawImage(img, 0, 0, nullptr);
34
35 float b = 0.75;
36 sk_sp<SkData> data = SkData::MakeWithoutCopy(&b, sizeof(b));
37 auto cf1 = SkRuntimeColorFilterFactory(SkString(SKSL_TEST_SRC), runtimeCpuFunc).make(data);
38 SkPaint p;
39 p.setColorFilter(cf1);
40 canvas->drawImage(img, 256, 0, &p);
41
42 static constexpr size_t kBufferSize = 512;
43 char buffer[kBufferSize];
44 SkBinaryWriteBuffer wb(buffer, kBufferSize);
45 wb.writeFlattenable(cf1.get());
46 SkReadBuffer rb(buffer, kBufferSize);
47 auto cf2 = rb.readColorFilter();
48 SkASSERT(cf2);
49 p.setColorFilter(cf2);
50 canvas->drawImage(img, 512, 0, &p);
51}