blob: 665a0fd09630a0952724f519c2a5c54b76a64a93 [file] [log] [blame]
senorblanco5878dbd2016-05-19 14:50:29 -07001/*
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/SkBlendMode.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkColorFilter.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkImage.h"
14#include "include/core/SkImageFilter.h"
15#include "include/core/SkImageInfo.h"
16#include "include/core/SkPaint.h"
17#include "include/core/SkPoint.h"
18#include "include/core/SkRect.h"
19#include "include/core/SkRefCnt.h"
20#include "include/core/SkScalar.h"
21#include "include/core/SkSize.h"
22#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/effects/SkBlurImageFilter.h"
26#include "include/effects/SkColorFilterImageFilter.h"
27#include "include/effects/SkDropShadowImageFilter.h"
28#include "tools/ToolUtils.h"
senorblanco5878dbd2016-05-19 14:50:29 -070029
Ben Wagner7fde8e12019-05-01 17:28:53 -040030#include <utility>
31
senorblanco5878dbd2016-05-19 14:50:29 -070032///////////////////////////////////////////////////////////////////////////////
33
34static void show_bounds(SkCanvas* canvas, const SkIRect& subset, const SkIRect& clip) {
35 SkIRect rects[] { subset, clip };
36 SkColor colors[] { SK_ColorRED, SK_ColorBLUE };
37
38 SkPaint paint;
39 paint.setStyle(SkPaint::kStroke_Style);
40
41 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
42 paint.setColor(colors[i]);
43 canvas->drawRect(SkRect::Make(rects[i]), paint);
44 }
45}
46
47// In this GM, we're going to feed the inner portion of a 100x100 checkboard
48// (i.e., strip off a 25-wide border) through the makeWithFilter method.
49// We'll then draw the appropriate subset of the result to the screen at the
50// given offset.
51class ImageMakeWithFilterGM : public skiagm::GM {
52public:
53 ImageMakeWithFilterGM () {}
54
55protected:
56 SkString onShortName() override {
57 return SkString("imagemakewithfilter");
58 }
59
senorblanco56f7dfe2016-05-20 07:59:09 -070060 SkISize onISize() override { return SkISize::Make(440, 530); }
senorblanco5878dbd2016-05-19 14:50:29 -070061
62 void onDraw(SkCanvas* canvas) override {
Mike Reedb286bc22019-04-08 16:23:20 -040063 auto cf = SkColorFilters::Blend(SK_ColorGREEN, SkBlendMode::kSrc);
senorblanco5878dbd2016-05-19 14:50:29 -070064 sk_sp<SkImageFilter> filters[] = {
65 SkColorFilterImageFilter::Make(std::move(cf), nullptr),
66 SkBlurImageFilter::Make(2.0f, 2.0f, nullptr),
67 SkDropShadowImageFilter::Make(
68 10.0f, 5.0f, 3.0f, 3.0f, SK_ColorBLUE,
69 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
70 nullptr),
71 };
72
73 SkIRect clipBounds[] {
74 { -20, -20, 100, 100 },
75 { 0, 0, 75, 75 },
76 { 20, 20, 100, 100 },
77 { -20, -20, 50, 50 },
78 { 20, 20, 50, 50 },
79 };
80
81 SkImageInfo info = SkImageInfo::MakeN32(100, 100, kPremul_SkAlphaType);
82 SkScalar MARGIN = SkIntToScalar(40);
83 SkScalar DX = info.width() + MARGIN;
84 SkScalar DY = info.height() + MARGIN;
85
86 canvas->translate(MARGIN, MARGIN);
87
Mike Kleinea3f0142019-03-20 11:12:10 -050088 sk_sp<SkSurface> surface = ToolUtils::makeSurface(canvas, info);
89 ToolUtils::draw_checkerboard(surface->getCanvas());
senorblanco5878dbd2016-05-19 14:50:29 -070090 sk_sp<SkImage> source = surface->makeImageSnapshot();
91
92 for (auto clipBound : clipBounds) {
93 canvas->save();
94 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
95 SkIRect subset = SkIRect::MakeXYWH(25, 25, 50, 50);
96 SkIRect outSubset;
97 SkIPoint offset;
98 sk_sp<SkImage> result = source->makeWithFilter(filters[i].get(), subset, clipBound,
99 &outSubset, &offset);
100 SkASSERT(result);
101 SkASSERT(source->isTextureBacked() == result->isTextureBacked());
102 result = result->makeSubset(outSubset);
103 canvas->drawImage(result.get(), SkIntToScalar(offset.fX), SkIntToScalar(offset.fY));
104 show_bounds(canvas, SkIRect::MakeXYWH(offset.x(), offset.y(), outSubset.width(),
105 outSubset.height()), clipBound);
106 canvas->translate(DX, 0);
107 }
108 canvas->restore();
109 canvas->translate(0, DY);
110 }
111 }
112
113private:
114 typedef GM INHERITED;
115};
116DEF_GM( return new ImageMakeWithFilterGM; )