blob: 1fbab7699044ef88098475108c2716e2d1b85860 [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"
Brian Osmanee426f22020-01-02 11:55:24 -050017#include "include/effects/SkRuntimeEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "tools/Resources.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040019
20#include <stddef.h>
21#include <utility>
22
Brian Osman5240e182020-08-12 10:22:34 -040023const char* gLumaSrc = R"(
Brian Osman767f4442020-08-13 16:59:48 -040024 in shader input;
25 half4 main() {
26 return dot(sample(input).rgb, half3(0.3, 0.6, 0.1)).000r;
Ethan Nicholasa70693b2019-03-04 13:07:36 -050027 }
28)";
29
Brian Osman5240e182020-08-12 10:22:34 -040030const char* gLumaSrcWithCoords = R"(
Brian Osman767f4442020-08-13 16:59:48 -040031 in shader input;
32 half4 main(float2 p) {
33 return dot(sample(input).rgb, half3(0.3, 0.6, 0.1)).000r;
Brian Osman5240e182020-08-12 10:22:34 -040034 }
35)";
36
37DEF_SIMPLE_GM(runtimecolorfilter, canvas, 256 * 3, 256) {
Ethan Nicholasa70693b2019-03-04 13:07:36 -050038 auto img = GetResourceAsImage("images/mandrill_256.png");
39 canvas->drawImage(img, 0, 0, nullptr);
40
Brian Osman5240e182020-08-12 10:22:34 -040041 for (auto src : { gLumaSrc, gLumaSrcWithCoords }) {
42 sk_sp<SkRuntimeEffect> effect = std::get<0>(SkRuntimeEffect::Make(SkString(src)));
Ethan Nicholas63d7ee32020-08-17 10:57:12 -040043 SkASSERT(effect);
Brian Osman5240e182020-08-12 10:22:34 -040044 SkPaint p;
Brian Osman767f4442020-08-13 16:59:48 -040045 sk_sp<SkColorFilter> input = nullptr;
46 p.setColorFilter(effect->makeColorFilter(nullptr, &input, 1));
Brian Osman5240e182020-08-12 10:22:34 -040047 canvas->translate(256, 0);
48 canvas->drawImage(img, 0, 0, &p);
49 }
Ethan Nicholasa70693b2019-03-04 13:07:36 -050050}