Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: gm/imagemakewithfilter.cpp

Issue 1964043002: Image filters: implement SkImage::makeWithFilter(). (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fixes per Rob's comments Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | include/core/SkImage.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
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
8 #include "gm.h"
9 #include "SkBlurImageFilter.h"
10 #include "SkCanvas.h"
11 #include "SkColorFilter.h"
12 #include "SkColorFilterImageFilter.h"
13 #include "SkDropShadowImageFilter.h"
14 #include "SkSurface.h"
15
16 ///////////////////////////////////////////////////////////////////////////////
17
18 static void show_bounds(SkCanvas* canvas, const SkIRect& subset, const SkIRect& clip) {
19 SkIRect rects[] { subset, clip };
20 SkColor colors[] { SK_ColorRED, SK_ColorBLUE };
21
22 SkPaint paint;
23 paint.setStyle(SkPaint::kStroke_Style);
24
25 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
26 paint.setColor(colors[i]);
27 canvas->drawRect(SkRect::Make(rects[i]), paint);
28 }
29 }
30
31 // In this GM, we're going to feed the inner portion of a 100x100 checkboard
32 // (i.e., strip off a 25-wide border) through the makeWithFilter method.
33 // We'll then draw the appropriate subset of the result to the screen at the
34 // given offset.
35 class ImageMakeWithFilterGM : public skiagm::GM {
36 public:
37 ImageMakeWithFilterGM () {}
38
39 protected:
40 SkString onShortName() override {
41 return SkString("imagemakewithfilter");
42 }
43
44 SkISize onISize() override { return SkISize::Make(320, 100); }
45
46 void onDraw(SkCanvas* canvas) override {
47 auto cf = SkColorFilter::MakeModeFilter(SK_ColorGREEN, SkXfermode::kSrc_ Mode);
48 sk_sp<SkImageFilter> filters[] = {
49 SkColorFilterImageFilter::Make(std::move(cf), nullptr),
50 SkBlurImageFilter::Make(2.0f, 2.0f, nullptr),
51 SkDropShadowImageFilter::Make(
52 10.0f, 5.0f, 3.0f, 3.0f, SK_ColorBLUE,
53 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
54 nullptr),
55 };
56
57 SkIRect clipBounds[] {
58 { -20, -20, 100, 100 },
59 { 0, 0, 75, 75 },
60 { 20, 20, 100, 100 },
61 { -20, -20, 50, 50 },
62 { 20, 20, 50, 50 },
63 };
64
65 SkImageInfo info = SkImageInfo::MakeN32(100, 100, kPremul_SkAlphaType);
66 SkScalar MARGIN = SkIntToScalar(40);
67 SkScalar DX = info.width() + MARGIN;
68 SkScalar DY = info.height() + MARGIN;
69
70 canvas->translate(MARGIN, MARGIN);
71
72 sk_sp<SkSurface> surface = canvas->makeSurface(info);
73 if (!surface) {
74 surface = SkSurface::MakeRaster(info);
75 }
76 sk_tool_utils::draw_checkerboard(surface->getCanvas());
77 sk_sp<SkImage> source = surface->makeImageSnapshot();
78
79 for (auto clipBound : clipBounds) {
80 canvas->save();
81 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
82 SkIRect subset = SkIRect::MakeXYWH(25, 25, 50, 50);
83 SkIRect outSubset;
84 SkIPoint offset;
85 sk_sp<SkImage> result = source->makeWithFilter(filters[i].get(), subset, clipBound,
86 &outSubset, &offs et);
87 SkASSERT(result);
88 SkASSERT(source->isTextureBacked() == result->isTextureBacked()) ;
89 result = result->makeSubset(outSubset);
90 canvas->drawImage(result.get(), SkIntToScalar(offset.fX), SkIntT oScalar(offset.fY));
91 show_bounds(canvas, SkIRect::MakeXYWH(offset.x(), offset.y(), ou tSubset.width(),
92 outSubset.height()), clipB ound);
93 canvas->translate(DX, 0);
94 }
95 canvas->restore();
96 canvas->translate(0, DY);
97 }
98 }
99
100 private:
101 typedef GM INHERITED;
102 };
103 DEF_GM( return new ImageMakeWithFilterGM; )
OLDNEW
« no previous file with comments | « no previous file | include/core/SkImage.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698