1 /*
2 * Copyright 2012 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
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkImageFilter.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPixelRef.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/effects/SkImageFilters.h"
20 #include "include/effects/SkRuntimeEffect.h"
21 #include "include/utils/SkRandom.h"
22 #include "src/effects/imagefilters/SkRuntimeImageFilter.h"
23 #include "tools/ToolUtils.h"
24
make_filter()25 static sk_sp<SkImageFilter> make_filter() {
26 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(R"(
27 uniform shader child;
28 half4 main(float2 coord) {
29 coord.x += sin(coord.y / 3) * 4;
30 return child.eval(coord);
31 }
32 )")).effect;
33 return SkMakeRuntimeImageFilter(std::move(effect),
34 /*uniforms=*/nullptr,
35 /*input=*/nullptr);
36 }
37
38 DEF_SIMPLE_GM_BG(rtif_distort, canvas, 500, 750, SK_ColorBLACK) {
39 SkRect clip = SkRect::MakeWH(250, 250);
40 SkPaint filterPaint;
41 filterPaint.setImageFilter(make_filter());
42
__anonfeba2aba0102(SkScalar tx, SkScalar ty, SkMatrix m) 43 auto draw_layer = [&](SkScalar tx, SkScalar ty, SkMatrix m) {
44 canvas->save();
45 canvas->translate(tx, ty);
46 canvas->clipRect(clip);
47 canvas->concat(m);
48 canvas->saveLayer(nullptr, &filterPaint);
49 const char* str = "The quick brown fox jumped over the lazy dog.";
50 SkRandom rand;
51 SkFont font(ToolUtils::create_portable_typeface());
52 for (int i = 0; i < 25; ++i) {
53 int x = rand.nextULessThan(500);
54 int y = rand.nextULessThan(500);
55 SkPaint paint;
56 paint.setColor(ToolUtils::color_to_565(rand.nextBits(24) | 0xFF000000));
57 font.setSize(rand.nextRangeScalar(0, 300));
58 canvas->drawString(str, SkIntToScalar(x), SkIntToScalar(y), font, paint);
59 }
60 canvas->restore();
61 canvas->restore();
62 };
63
64 draw_layer( 0, 0, SkMatrix::I());
65 draw_layer(250, 0, SkMatrix::Scale(0.5f, 0.5f));
66 draw_layer( 0, 250, SkMatrix::RotateDeg(45, {125, 125}));
67 draw_layer(250, 250, SkMatrix::Scale(0.5f, 0.5f) * SkMatrix::RotateDeg(45, {125, 125}));
68 draw_layer( 0, 500, SkMatrix::Skew(-0.5f, 0));
69 SkMatrix p = SkMatrix::I();
70 p.setPerspX(0.0015f);
71 p.setPerspY(-0.0015f);
72 draw_layer(250, 500, p);
73 }
74