Flutter Engine
The Flutter Engine
resizeimagefilter.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"
22
23#include <utility>
24
25namespace skiagm {
26
27class ResizeGM : public GM {
28public:
30 this->setBGColor(0x00000000);
31 }
32
33protected:
34 SkString getName() const override { return SkString("resizeimagefilter"); }
35
36 void draw(SkCanvas* canvas,
37 const SkRect& rect,
38 const SkSize& deviceSize,
41 SkRect dstRect;
42 canvas->getLocalToDeviceAs3x3().mapRect(&dstRect, rect);
43 canvas->save();
44 SkScalar deviceScaleX = deviceSize.width() / dstRect.width();
45 SkScalar deviceScaleY = deviceSize.height() / dstRect.height();
46 canvas->translate(rect.x(), rect.y());
47 canvas->scale(deviceScaleX, deviceScaleY);
48 canvas->translate(-rect.x(), -rect.y());
50 matrix.setScale(SkScalarInvert(deviceScaleX), SkScalarInvert(deviceScaleY));
53 std::move(input)));
54 SkPaint filteredPaint;
55 filteredPaint.setImageFilter(std::move(filter));
56 canvas->saveLayer(&rect, &filteredPaint);
58 paint.setColor(0xFF00FF00);
59 SkRect ovalRect = rect;
60 ovalRect.inset(SkIntToScalar(4), SkIntToScalar(4));
61 canvas->drawOval(ovalRect, paint);
62 canvas->restore(); // for saveLayer
63 canvas->restore();
64 }
65
66 SkISize getISize() override { return SkISize::Make(630, 100); }
67
68 void onDraw(SkCanvas* canvas) override {
69 canvas->clear(SK_ColorBLACK);
70
71 const SkSamplingOptions samplings[] = {
77 };
78 const SkRect srcRect = SkRect::MakeWH(96, 96);
79 const SkSize deviceSize = SkSize::Make(16, 16);
80
81 for (const auto& sampling : samplings) {
82 this->draw(canvas, srcRect, deviceSize, sampling, nullptr);
83 canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
84 }
85
86 {
88 SkCanvas* surfaceCanvas = surface->getCanvas();
89 surfaceCanvas->clear(0x000000);
90 {
92 paint.setColor(0xFF00FF00);
93 SkRect ovalRect = SkRect::MakeWH(16, 16);
94 ovalRect.inset(SkIntToScalar(2)/3, SkIntToScalar(2)/3);
95 surfaceCanvas->drawOval(ovalRect, paint);
96 }
97 sk_sp<SkImage> image(surface->makeImageSnapshot());
98 SkRect inRect = SkRect::MakeXYWH(-4, -4, 20, 20);
99 SkRect outRect = SkRect::MakeXYWH(-24, -24, 120, 120);
101 SkImageFilters::Image(std::move(image), inRect, outRect,
102 SkSamplingOptions({1/3.0f, 1/3.0f})));
103 this->draw(canvas, srcRect, deviceSize, samplings[3], std::move(source));
104 }
105 }
106
107private:
108 using INHERITED = GM;
109};
110
111//////////////////////////////////////////////////////////////////////////////
112
113DEF_GM(return new ResizeGM; )
114
115} // namespace skiagm
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
#define SkScalarInvert(x)
Definition: SkScalar.h:73
#define SkIntToScalar(x)
Definition: SkScalar.h:57
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void drawOval(const SkRect &oval, const SkPaint &paint)
Definition: SkCanvas.cpp:1698
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
SkMatrix getLocalToDeviceAs3x3() const
Definition: SkCanvas.h:2222
void clear(SkColor color)
Definition: SkCanvas.h:1199
int save()
Definition: SkCanvas.cpp:447
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
static sk_sp< SkImageFilter > Image(sk_sp< SkImage > image, const SkRect &srcRect, const SkRect &dstRect, const SkSamplingOptions &sampling)
static sk_sp< SkImageFilter > MatrixTransform(const SkMatrix &matrix, const SkSamplingOptions &sampling, sk_sp< SkImageFilter > input)
bool mapRect(SkRect *dst, const SkRect &src, SkApplyPerspectiveClip pc=SkApplyPerspectiveClip::kYes) const
Definition: SkMatrix.cpp:1141
void setImageFilter(sk_sp< SkImageFilter > imageFilter)
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
void setBGColor(SkColor)
Definition: gm.cpp:159
SkString getName() const override
void onDraw(SkCanvas *canvas) override
void draw(SkCanvas *canvas, const SkRect &rect, const SkSize &deviceSize, const SkSamplingOptions &sampling, sk_sp< SkImageFilter > input)
SkISize getISize() override
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
SkBitmap source
Definition: examples.cpp:28
float SkScalar
Definition: extension.cpp:12
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
SkSamplingOptions sampling
Definition: SkRecords.h:337
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
SkSamplingOptions(SkFilterMode::kLinear))
static constexpr SkCubicResampler Mitchell()
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo MakeN32Premul(int width, int height)
void inset(float dx, float dy)
Definition: SkRect.h:1060
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
constexpr float height() const
Definition: SkRect.h:769
constexpr float width() const
Definition: SkRect.h:762
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609
static constexpr SkSamplingOptions Aniso(int maxAniso)
Definition: SkSize.h:52
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition: SkSize.h:56
SkScalar width() const
Definition: SkSize.h:76
SkScalar height() const
Definition: SkSize.h:77