blob: d257695a813ae5a951286df39e69968b24bd4dc5 [file] [log] [blame]
Matt Sarett030cbd52016-11-22 15:48:50 -05001/*
2 * Copyright 2016 Google Inc.
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"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkColor.h"
12#include "include/core/SkColorFilter.h"
13#include "include/core/SkImageInfo.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRefCnt.h"
16#include "include/core/SkSize.h"
17#include "include/core/SkString.h"
18#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/effects/SkOverdrawColorFilter.h"
Mike Kleind7d14622020-04-09 11:27:09 -050020#include "include/effects/SkRuntimeEffect.h"
21
22static const char* code = R"(
23uniform half4 color0;
24uniform half4 color1;
25uniform half4 color2;
26uniform half4 color3;
27uniform half4 color4;
28uniform half4 color5;
29
30void main(inout half4 color) {
31 half alpha = 255.0 * color.a;
32 color = alpha < 0.5 ? color0
33 : alpha < 1.5 ? color1
34 : alpha < 2.5 ? color2
35 : alpha < 3.5 ? color3
36 : alpha < 4.5 ? color4
37 : color5;
38}
39)";
Matt Sarett030cbd52016-11-22 15:48:50 -050040
41static inline void set_bitmap(SkBitmap* bitmap, uint8_t alpha) {
42 for (int y = 0; y < bitmap->height(); y++) {
43 for (int x = 0; x < bitmap->width(); x++) {
44 uint8_t* addr = bitmap->getAddr8(x, y);
45 *addr = alpha;
46 }
47 }
48
49 bitmap->notifyPixelsChanged();
50}
51
Mike Kleind7d14622020-04-09 11:27:09 -050052struct OverdrawColorFilter : public skiagm::GM {
53 bool fRuntime;
54
55 OverdrawColorFilter(bool runtime) : fRuntime(runtime) {}
56
57 SkString onShortName() override {
58 return SkString{fRuntime ? "overdrawcolorfilter_runtime"
59 : "overdrawcolorfilter"};
60 }
Matt Sarett030cbd52016-11-22 15:48:50 -050061
Hal Canarybd865e22019-07-18 11:51:19 -040062 SkISize onISize() override { return {200, 400}; }
Matt Sarett030cbd52016-11-22 15:48:50 -050063
64 void onDraw(SkCanvas* canvas) override {
Mike Reeddc2782c2020-02-11 05:35:04 -050065 static const SkColor colors[SkOverdrawColorFilter::kNumColors] = {
66 0x80FF0000, 0x8000FF00, 0x800000FF, 0x80FFFF00, 0x8000FFFF, 0x80FF00FF,
Matt Sarett030cbd52016-11-22 15:48:50 -050067 };
68
69 SkPaint paint;
Mike Kleind7d14622020-04-09 11:27:09 -050070 if (fRuntime) {
71 auto [effect, err] = SkRuntimeEffect::Make(SkString{code});
72 if (effect) {
73
74 SkRGBA4f<kPremul_SkAlphaType> uniforms[SK_ARRAY_COUNT(colors)];
75 for (int i = 0; i < (int)SK_ARRAY_COUNT(colors); i++) {
76 uniforms[i] = SkColor4f::FromColor(colors[i]).premul();
77 }
78
79 paint.setColorFilter(
80 effect->makeColorFilter(SkData::MakeWithCopy(uniforms, sizeof(uniforms))));
81 } else {
82 SkDebugf("SkRuntimeEffect error: %s\n", err.c_str());
83 }
84 } else {
85 paint.setColorFilter(SkOverdrawColorFilter::MakeWithSkColors(colors));
86 }
87
88 SkASSERT(paint.getColorFilter());
Matt Sarett030cbd52016-11-22 15:48:50 -050089
90 SkImageInfo info = SkImageInfo::MakeA8(100, 100);
91 SkBitmap bitmap;
92 bitmap.allocPixels(info);
93 set_bitmap(&bitmap, 0);
94 canvas->drawBitmap(bitmap, 0, 0, &paint);
95 set_bitmap(&bitmap, 1);
96 canvas->drawBitmap(bitmap, 0, 100, &paint);
97 set_bitmap(&bitmap, 2);
98 canvas->drawBitmap(bitmap, 0, 200, &paint);
99 set_bitmap(&bitmap, 3);
100 canvas->drawBitmap(bitmap, 0, 300, &paint);
101 set_bitmap(&bitmap, 4);
102 canvas->drawBitmap(bitmap, 100, 0, &paint);
103 set_bitmap(&bitmap, 5);
104 canvas->drawBitmap(bitmap, 100, 100, &paint);
105 set_bitmap(&bitmap, 6);
106 canvas->drawBitmap(bitmap, 100, 200, &paint);
107 }
Matt Sarett030cbd52016-11-22 15:48:50 -0500108};
109
Mike Kleind7d14622020-04-09 11:27:09 -0500110DEF_GM(return new OverdrawColorFilter(false);)
111DEF_GM(return new OverdrawColorFilter( true);)