Flutter Engine
The Flutter Engine
Public Member Functions | List of all members
FilterBoundsSample Class Reference
Inheritance diagram for FilterBoundsSample:
Slide SkRefCnt SkRefCntBase

Public Member Functions

 FilterBoundsSample ()
 
void load (SkScalar w, SkScalar h) override
 
void draw (SkCanvas *canvas) override
 
- Public Member Functions inherited from Slide
virtual SkISize getDimensions () const
 
virtual void gpuTeardown ()
 
virtual void draw (SkCanvas *canvas)=0
 
virtual bool animate (double nanos)
 
virtual void load (SkScalar winWidth, SkScalar winHeight)
 
virtual void resize (SkScalar winWidth, SkScalar winHeight)
 
virtual void unload ()
 
virtual bool onChar (SkUnichar c)
 
virtual bool onMouse (SkScalar x, SkScalar y, skui::InputState state, skui::ModifierKey modifiers)
 
virtual bool onGetControls (SkMetaData *)
 
virtual void onSetControls (const SkMetaData &)
 
const SkStringgetName ()
 
- Public Member Functions inherited from SkRefCntBase
 SkRefCntBase ()
 
virtual ~SkRefCntBase ()
 
bool unique () const
 
void ref () const
 
void unref () const
 

Additional Inherited Members

- Protected Attributes inherited from Slide
SkString fName
 

Detailed Description

Definition at line 176 of file FilterBoundsSlide.cpp.

Constructor & Destructor Documentation

◆ FilterBoundsSample()

FilterBoundsSample::FilterBoundsSample ( )
inline

Definition at line 178 of file FilterBoundsSlide.cpp.

178{ fName = "FilterBounds"; }
SkString fName
Definition: Slide.h:54

Member Function Documentation

◆ draw()

void FilterBoundsSample::draw ( SkCanvas canvas)
inlineoverridevirtual

Implements Slide.

Definition at line 186 of file FilterBoundsSlide.cpp.

186 {
187 // The local content, e.g. what would be submitted to drawRect or the bounds to saveLayer
188 const SkRect localContentRect = SkRect::MakeLTRB(100.f, 20.f, 180.f, 140.f);
189 SkMatrix ctm = canvas->getLocalToDeviceAs3x3();
190
191 // Base rendering of a filter
192 SkPaint blurPaint;
193 blurPaint.setImageFilter(fBlur);
194 canvas->saveLayer(&localContentRect, &blurPaint);
195 canvas->drawImageRect(fImage.get(), localContentRect, localContentRect,
198 canvas->restore();
199
200 // Now visualize the underlying bounds calculations used to determine the layer for the blur
201 SkIRect target = ctm.mapRect(localContentRect).roundOut();
202 if (!target.intersect(SkIRect::MakeWH(canvas->imageInfo().width(),
203 canvas->imageInfo().height()))) {
204 return;
205 }
207 skif::ParameterSpace<SkRect> contentBounds(localContentRect);
208 skif::ParameterSpace<SkPoint> contentCenter({localContentRect.centerX(),
209 localContentRect.centerY()});
210 skif::Mapping mapping;
211 SkAssertResult(mapping.decomposeCTM(ctm, fBlur.get(), contentCenter));
212
213 // Add axis lines, to show perspective distortion
214 canvas->save();
215 canvas->setMatrix(mapping.layerToDevice());
216 canvas->drawPath(create_axis_path(SkRect(mapping.paramToLayer(contentBounds)), 20.f),
218 canvas->restore();
219
220 // Visualize scale factors at the four corners and center of the local rect
221 draw_scale_factors(canvas, mapping, localContentRect);
222
223 // The device content rect, e.g. the clip bounds if 'localContentRect' were used as a clip
224 // before the draw or saveLayer, representing what the filter must cover if it affects
225 // transparent black or doesn't have a local content hint.
226 canvas->setMatrix(SkMatrix::I());
227 canvas->drawRect(ctm.mapRect(localContentRect), line_paint(SK_ColorDKGRAY));
228
229 // Layer bounds for the filter, in the layer space compatible with the filter's matrix
230 // type requirements.
231 skif::LayerSpace<SkIRect> targetOutputInLayer = mapping.deviceToLayer(targetOutput);
232 skif::LayerSpace<SkIRect> hintedLayerBounds = as_IFB(fBlur)->getInputBounds(
233 mapping, targetOutput, contentBounds);
234 skif::LayerSpace<SkIRect> unhintedLayerBounds = as_IFB(fBlur)->getInputBounds(
235 mapping, targetOutput, {});
236
237 canvas->setMatrix(mapping.layerToDevice());
238 canvas->drawRect(SkRect::Make(SkIRect(targetOutputInLayer)),
240 canvas->drawRect(SkRect::Make(SkIRect(hintedLayerBounds)), line_paint(SK_ColorRED));
241 canvas->drawRect(SkRect::Make(SkIRect(unhintedLayerBounds)), line_paint(SK_ColorGREEN));
242
243 // For visualization purposes, we want to show the layer-space output, this is what we get
244 // when contentBounds is provided as a hint in local/parameter space.
245 skif::Mapping layerOnly{mapping.layerMatrix()};
246 std::optional<skif::DeviceSpace<SkIRect>> hintedOutputBounds =
247 as_IFB(fBlur)->getOutputBounds(layerOnly, contentBounds);
248 if (hintedOutputBounds) {
249 canvas->drawRect(SkRect::Make(SkIRect(*hintedOutputBounds)), line_paint(SK_ColorBLUE));
250 }
251
252 canvas->resetMatrix();
253 float y = print_info(canvas,
254 mapping.paramToLayer(contentBounds).roundOut(),
255 targetOutput,
256 hintedOutputBounds,
257 unhintedLayerBounds);
258
259 // Draw color key for layer visualization
260 draw_scale_key(canvas, y);
261 }
static SkPaint line_paint(SkColor color, bool dashed=false)
static SkPath create_axis_path(const SkRect &rect, float axisSpace)
static void draw_scale_key(SkCanvas *canvas, float y)
static void draw_scale_factors(SkCanvas *canvas, const skif::Mapping &mapping, const SkRect &rect)
static float print_info(SkCanvas *canvas, const skif::LayerSpace< SkIRect > &layerContentBounds, const skif::DeviceSpace< SkIRect > &outputBounds, std::optional< skif::DeviceSpace< SkIRect > > hintedOutputBounds, const skif::LayerSpace< SkIRect > &unhintedLayerBounds)
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
constexpr SkColor SK_ColorGRAY
Definition: SkColor.h:113
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
constexpr SkColor SK_ColorDKGRAY
Definition: SkColor.h:108
static SkImageFilter_Base * as_IFB(SkImageFilter *filter)
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
SkMatrix getLocalToDeviceAs3x3() const
Definition: SkCanvas.h:2222
@ kFast_SrcRectConstraint
sample outside bounds; faster
Definition: SkCanvas.h:1543
void resetMatrix()
Definition: SkCanvas.cpp:1355
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
Definition: SkCanvas.cpp:2333
int save()
Definition: SkCanvas.cpp:447
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
void setMatrix(const SkM44 &matrix)
Definition: SkCanvas.cpp:1349
SkImageInfo imageInfo() const
Definition: SkCanvas.cpp:1206
std::optional< skif::DeviceSpace< SkIRect > > getOutputBounds(const skif::Mapping &mapping, const skif::ParameterSpace< SkRect > &contentBounds) const
skif::LayerSpace< SkIRect > getInputBounds(const skif::Mapping &mapping, const skif::DeviceSpace< SkIRect > &desiredOutput, std::optional< skif::ParameterSpace< SkRect > > knownContentBounds) const
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
bool mapRect(SkRect *dst, const SkRect &src, SkApplyPerspectiveClip pc=SkApplyPerspectiveClip::kYes) const
Definition: SkMatrix.cpp:1141
void setImageFilter(sk_sp< SkImageFilter > imageFilter)
T * get() const
Definition: SkRefCnt.h:303
uint32_t * target
double y
Definition: SkRect.h:32
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
int width() const
Definition: SkImageInfo.h:365
int height() const
Definition: SkImageInfo.h:371
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
constexpr float centerX() const
Definition: SkRect.h:776
constexpr float centerY() const
Definition: SkRect.h:785
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646

◆ load()

void FilterBoundsSample::load ( SkScalar  w,
SkScalar  h 
)
inlineoverridevirtual

Reimplemented from Slide.

Definition at line 180 of file FilterBoundsSlide.cpp.

180 {
181 fBlur = SkImageFilters::Blur(8.f, 8.f, nullptr);
183 300, 300, SK_ColorMAGENTA, SK_ColorLTGRAY, 50);
184 }
constexpr SkColor SK_ColorLTGRAY
Definition: SkColor.h:118
constexpr SkColor SK_ColorMAGENTA
Definition: SkColor.h:147
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
sk_sp< SkImage > create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize)
Definition: ToolUtils.cpp:168

The documentation for this class was generated from the following file: