Flutter Engine
The Flutter Engine
imagesource.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 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"
15#include "include/core/SkRect.h"
18#include "include/core/SkSize.h"
21#include "tools/ToolUtils.h"
23
24#include <utility>
25
26// This GM exercises the SkImageSource ImageFilter class.
27
28static void fill_rect_filtered(SkCanvas* canvas,
29 const SkRect& clipRect,
30 sk_sp<SkImageFilter> filter) {
32 paint.setImageFilter(std::move(filter));
33 canvas->save();
34 canvas->clipRect(clipRect);
35 canvas->drawPaint(paint);
36 canvas->restore();
37}
38
39class ImageSourceGM : public skiagm::GM {
40public:
42
43protected:
44 SkString getName() const override { return SkString("imagesource"); }
45
46 SkISize getISize() override { return SkISize::Make(500, 150); }
47
48 void onOnceBeforeDraw() override {
49 fImage = ToolUtils::CreateStringImage(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
50 }
51
52 void onDraw(SkCanvas* canvas) override {
53 canvas->clear(SK_ColorBLACK);
54
55 const SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30);
56 const SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60);
57 const SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100);
58 const SkRect bounds = SkRect::MakeIWH(fImage->width(), fImage->height());
59 const SkSamplingOptions sampling({1/3.0f, 1/3.0f});
60
61 {
62 // Draw an unscaled bitmap.
64 fill_rect_filtered(canvas, clipRect, std::move(imageSource));
65 canvas->translate(SkIntToScalar(100), 0);
66 }
67 {
68 // Draw an unscaled subset of the source bitmap (srcRect -> srcRect).
69 sk_sp<SkImageFilter> imageSourceSrcRect(
70 SkImageFilters::Image(fImage, srcRect, srcRect, sampling));
71 fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRect));
72 canvas->translate(SkIntToScalar(100), 0);
73 }
74 {
75 // Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect).
76 sk_sp<SkImageFilter> imageSourceSrcRectDstRect(
77 SkImageFilters::Image(fImage, srcRect, dstRect, sampling));
78 fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRectDstRect));
79 canvas->translate(SkIntToScalar(100), 0);
80 }
81 {
82 // Draw the entire bitmap scaled to a destination rect (bounds -> dstRect).
83 sk_sp<SkImageFilter> imageSourceDstRectOnly(
84 SkImageFilters::Image(fImage, bounds, dstRect, sampling));
85 fill_rect_filtered(canvas, clipRect, std::move(imageSourceDstRectOnly));
86 canvas->translate(SkIntToScalar(100), 0);
87 }
88 }
89
90private:
91 sk_sp<SkImage> fImage;
92 using INHERITED = GM;
93};
94
95///////////////////////////////////////////////////////////////////////////////
96
97DEF_GM( return new ImageSourceGM; )
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
#define SkIntToScalar(x)
Definition: SkScalar.h:57
void onDraw(SkCanvas *canvas) override
Definition: imagesource.cpp:52
void onOnceBeforeDraw() override
Definition: imagesource.cpp:48
SkString getName() const override
Definition: imagesource.cpp:44
SkISize getISize() override
Definition: imagesource.cpp:46
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawPaint(const SkPaint &paint)
Definition: SkCanvas.cpp:1668
void clear(SkColor color)
Definition: SkCanvas.h:1199
int save()
Definition: SkCanvas.cpp:447
static sk_sp< SkImageFilter > Image(sk_sp< SkImage > image, const SkRect &srcRect, const SkRect &dstRect, const SkSamplingOptions &sampling)
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
const Paint & paint
Definition: color_source.cc:38
#define DEF_GM(CODE)
Definition: gm.h:40
static void fill_rect_filtered(SkCanvas *canvas, const SkRect &clipRect, sk_sp< SkImageFilter > filter)
Definition: imagesource.cpp:28
Optional< SkRect > bounds
Definition: SkRecords.h:189
clipRect(r.rect, r.opAA.op(), r.opAA.aa())) template<> void Draw
SkSamplingOptions sampling
Definition: SkRecords.h:337
sk_sp< SkImage > CreateStringImage(int w, int h, SkColor c, int x, int y, int textSize, const char *str)
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkRect MakeIWH(int w, int h)
Definition: SkRect.h:623
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659