Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 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 | |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 8 | #include "Resources.h" |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 9 | #include "SkCanvas.h" |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 10 | #include "SkColorFilterPriv.h" |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 11 | #include "SkImage.h" |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 13 | #include "ToolUtils.h" |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 14 | #include "effects/GrSkSLFP.h" |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 15 | #include "gm.h" |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 16 | |
| 17 | const char* SKSL_TEST_SRC = R"( |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame^] | 18 | layout(ctype=float) in uniform half b; |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 19 | |
| 20 | void main(inout half4 color) { |
| 21 | color.rg = color.gr; |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame^] | 22 | color.b = b; |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 23 | } |
| 24 | )"; |
| 25 | |
| 26 | static void runtimeCpuFunc(float color[4], const void* context) { |
| 27 | std::swap(color[0], color[1]); |
| 28 | color[2] = *(float*) context; |
| 29 | } |
| 30 | |
| 31 | DEF_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; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 36 | sk_sp<SkData> data = SkData::MakeWithCopy(&b, sizeof(b)); |
Mike Reed | a6dd1eb | 2019-03-30 15:14:08 +0000 | [diff] [blame] | 37 | auto cf1 = SkRuntimeColorFilterFactory(SkString(SKSL_TEST_SRC), runtimeCpuFunc).make(data); |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 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(); |
Ethan Nicholas | a185562 | 2019-03-21 13:51:02 -0400 | [diff] [blame] | 48 | if (cf2) { |
| 49 | p.setColorFilter(cf2); |
| 50 | canvas->drawImage(img, 512, 0, &p); |
| 51 | } |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 52 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 53 | |
| 54 | DEF_SIMPLE_GM(runtimecolorfilter_interpreted, canvas, 768, 256) { |
| 55 | auto img = GetResourceAsImage("images/mandrill_256.png"); |
| 56 | canvas->drawImage(img, 0, 0, nullptr); |
| 57 | |
| 58 | float b = 0.75; |
| 59 | sk_sp<SkData> data = SkData::MakeWithCopy(&b, sizeof(b)); |
Mike Reed | a6dd1eb | 2019-03-30 15:14:08 +0000 | [diff] [blame] | 60 | auto cf1 = SkRuntimeColorFilterFactory(SkString(SKSL_TEST_SRC), nullptr).make(data); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 61 | SkPaint p; |
| 62 | p.setColorFilter(cf1); |
| 63 | canvas->drawImage(img, 256, 0, &p); |
| 64 | |
| 65 | static constexpr size_t kBufferSize = 512; |
| 66 | char buffer[kBufferSize]; |
| 67 | SkBinaryWriteBuffer wb(buffer, kBufferSize); |
| 68 | wb.writeFlattenable(cf1.get()); |
| 69 | SkReadBuffer rb(buffer, kBufferSize); |
| 70 | auto cf2 = rb.readColorFilter(); |
Ethan Nicholas | a185562 | 2019-03-21 13:51:02 -0400 | [diff] [blame] | 71 | if (cf2) { |
| 72 | p.setColorFilter(cf2); |
| 73 | canvas->drawImage(img, 512, 0, &p); |
| 74 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 75 | } |