Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Typedefs | Functions | Variables
compositor_quads.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorFilter.h"
#include "include/core/SkData.h"
#include "include/core/SkFont.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageFilter.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkMaskFilter.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkShader.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkTileMode.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkTypes.h"
#include "include/effects/SkColorMatrix.h"
#include "include/effects/SkGradientShader.h"
#include "include/effects/SkImageFilters.h"
#include "include/effects/SkShaderMaskFilter.h"
#include "include/private/base/SkTArray.h"
#include "src/core/SkLineClipper.h"
#include "tools/DecodeUtils.h"
#include "tools/Resources.h"
#include "tools/ToolUtils.h"
#include "tools/fonts/FontToolUtils.h"
#include "tools/gpu/YUVUtils.h"
#include <array>
#include <memory>
#include <utility>

Go to the source code of this file.

Classes

class  ClipTileRenderer
 
class  CompositorGM
 
class  DebugTileRenderer
 
class  SolidColorRenderer
 
class  TextureSetRenderer
 
class  YUVTextureSetRenderer
 

Typedefs

using ClipTileRendererArray = TArray< sk_sp< ClipTileRenderer > >
 

Functions

static void clipping_line_segment (const SkPoint &p0, const SkPoint &p1, SkPoint line[2])
 
static bool intersect_line_segments (const SkPoint &p0, const SkPoint &p1, const SkPoint &l0, const SkPoint &l1, SkPoint *intersect)
 
static void draw_outset_line (SkCanvas *canvas, const SkMatrix &local, const SkPoint pts[2], const SkPaint &paint)
 
static void draw_tile_boundaries (SkCanvas *canvas, const SkMatrix &local)
 
static void draw_clipping_boundaries (SkCanvas *canvas, const SkMatrix &local)
 
static void draw_text (SkCanvas *canvas, const char *text)
 
static ClipTileRendererArray make_debug_renderers ()
 
static ClipTileRendererArray make_solid_color_renderers ()
 
static ClipTileRendererArray make_shader_renderers ()
 
static ClipTileRendererArray make_image_renderers ()
 
static ClipTileRendererArray make_filtered_renderers ()
 

Variables

static constexpr SkScalar kTileWidth = 40
 
static constexpr SkScalar kTileHeight = 30
 
static constexpr int kRowCount = 4
 
static constexpr int kColCount = 3
 
static constexpr SkPoint kClipP1 = {1.75f * kTileWidth, 0.8f * kTileHeight}
 
static constexpr SkPoint kClipP2 = {0.6f * kTileWidth, 2.f * kTileHeight}
 
static constexpr SkPoint kClipP3 = {2.9f * kTileWidth, 3.5f * kTileHeight}
 
static constexpr int kMatrixCount = 5
 

Typedef Documentation

◆ ClipTileRendererArray

Definition at line 53 of file compositor_quads.cpp.

Function Documentation

◆ clipping_line_segment()

static void clipping_line_segment ( const SkPoint p0,
const SkPoint p1,
SkPoint  line[2] 
)
static

Definition at line 78 of file compositor_quads.cpp.

78 {
79 SkVector v = p1 - p0;
80 // 10f was chosen as a balance between large enough to scale the currently set clip
81 // points outside of the tile grid, but small enough to preserve precision.
82 line[0] = p0 - v * 10.f;
83 line[1] = p1 + v * 10.f;
84}

◆ draw_clipping_boundaries()

static void draw_clipping_boundaries ( SkCanvas canvas,
const SkMatrix local 
)
static

Definition at line 167 of file compositor_quads.cpp.

167 {
169 paint.setAntiAlias(true);
170 paint.setColor(SK_ColorGREEN);
172 paint.setStrokeWidth(0.f);
173
174 // Clip the "infinite" line segments to a rectangular region outside the tile grid
176
177 // Draw p1 to p2
178 SkPoint line[2];
179 SkPoint clippedLine[2];
181 SkAssertResult(SkLineClipper::IntersectLine(line, border, clippedLine));
182 draw_outset_line(canvas, local, clippedLine, paint);
183
184 // Draw p2 to p3
186 SkAssertResult(SkLineClipper::IntersectLine(line, border, clippedLine));
187 draw_outset_line(canvas, local, clippedLine, paint);
188
189 // Draw p3 to p1
191 SkAssertResult(SkLineClipper::IntersectLine(line, border, clippedLine));
192 draw_outset_line(canvas, local, clippedLine, paint);
193}
#define SkAssertResult(cond)
Definition SkAssert.h:123
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
static bool IntersectLine(const SkPoint src[2], const SkRect &clip, SkPoint dst[2])
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
const Paint & paint
static void clipping_line_segment(const SkPoint &p0, const SkPoint &p1, SkPoint line[2])
static constexpr SkPoint kClipP2
static constexpr SkScalar kTileHeight
static constexpr SkScalar kTileWidth
static constexpr int kRowCount
static void draw_outset_line(SkCanvas *canvas, const SkMatrix &local, const SkPoint pts[2], const SkPaint &paint)
static constexpr SkPoint kClipP3
static constexpr int kColCount
static constexpr SkPoint kClipP1
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609

◆ draw_outset_line()

static void draw_outset_line ( SkCanvas canvas,
const SkMatrix local,
const SkPoint  pts[2],
const SkPaint paint 
)
static

Definition at line 139 of file compositor_quads.cpp.

140 {
141 static constexpr SkScalar kLineOutset = 10.f;
142 SkPoint mapped[2];
143 local.mapPoints(mapped, pts, 2);
144 SkVector v = mapped[1] - mapped[0];
145 v.setLength(v.length() + kLineOutset);
146 canvas->drawLine(mapped[1] - v, mapped[0] + v, paint);
147}
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
float SkScalar
Definition extension.cpp:12
bool setLength(float length)
Definition SkPoint.cpp:30
float length() const

◆ draw_text()

static void draw_text ( SkCanvas canvas,
const char *  text 
)
static

Definition at line 195 of file compositor_quads.cpp.

195 {
197 canvas->drawString(text, 0, 0, font, SkPaint());
198}
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition SkCanvas.h:1803
std::u16string text
sk_sp< SkTypeface > DefaultPortableTypeface()
font
Font Metadata and Metrics.

◆ draw_tile_boundaries()

static void draw_tile_boundaries ( SkCanvas canvas,
const SkMatrix local 
)
static

Definition at line 150 of file compositor_quads.cpp.

150 {
152 paint.setAntiAlias(true);
153 paint.setColor(SK_ColorRED);
155 paint.setStrokeWidth(0.f);
156 for (int x = 1; x < kColCount; ++x) {
157 SkPoint pts[] = {{x * kTileWidth, 0}, {x * kTileWidth, kRowCount * kTileHeight}};
158 draw_outset_line(canvas, local, pts, paint);
159 }
160 for (int y = 1; y < kRowCount; ++y) {
161 SkPoint pts[] = {{0, y * kTileHeight}, {kTileWidth * kColCount, y * kTileHeight}};
162 draw_outset_line(canvas, local, pts, paint);
163 }
164}
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
double y
double x

◆ intersect_line_segments()

static bool intersect_line_segments ( const SkPoint p0,
const SkPoint p1,
const SkPoint l0,
const SkPoint l1,
SkPoint intersect 
)
static

Definition at line 88 of file compositor_quads.cpp.

89 {
90 static constexpr SkScalar kHorizontalTolerance = 0.01f; // Pretty conservative
91
92 // Use doubles for accuracy, since the clipping strategy used below can create T
93 // junctions, and lower precision could artificially create gaps
94 double pY = (double) p1.fY - (double) p0.fY;
95 double pX = (double) p1.fX - (double) p0.fX;
96 double lY = (double) l1.fY - (double) l0.fY;
97 double lX = (double) l1.fX - (double) l0.fX;
98 double plY = (double) p0.fY - (double) l0.fY;
99 double plX = (double) p0.fX - (double) l0.fX;
100 if (SkScalarNearlyZero(pY, kHorizontalTolerance)) {
101 if (SkScalarNearlyZero(lY, kHorizontalTolerance)) {
102 // Two horizontal lines
103 return false;
104 } else {
105 // Recalculate but swap p and l
106 return intersect_line_segments(l0, l1, p0, p1, intersect);
107 }
108 }
109
110 // Up to now, the line segments do not form an invalid intersection
111 double lNumerator = plX * pY - plY * pX;
112 double lDenom = lX * pY - lY * pX;
113 if (SkScalarNearlyZero(lDenom)) {
114 // Parallel or identical
115 return false;
116 }
117
118 // Calculate alphaL that provides the intersection point along (l0-l1), e.g. l0+alphaL*(l1-l0)
119 double alphaL = lNumerator / lDenom;
120 if (alphaL < 0.0 || alphaL > 1.0) {
121 // Outside of the l segment
122 return false;
123 }
124
125 // Calculate alphaP from the valid alphaL (since it could be outside p segment)
126 // double alphaP = (alphaL * l.fY - pl.fY) / p.fY;
127 double alphaP = (alphaL * lY - plY) / pY;
128 if (alphaP < 0.0 || alphaP > 1.0) {
129 // Outside of p segment
130 return false;
131 }
132
133 // Is valid, so calculate the actual intersection point
134 *intersect = l1 * SkScalar(alphaL) + l0 * SkScalar(1.0 - alphaL);
135 return true;
136}
static bool intersect(const SkPoint &p0, const SkPoint &n0, const SkPoint &p1, const SkPoint &n1, SkScalar *t)
static bool SkScalarNearlyZero(SkScalar x, SkScalar tolerance=SK_ScalarNearlyZero)
Definition SkScalar.h:101
static bool intersect_line_segments(const SkPoint &p0, const SkPoint &p1, const SkPoint &l0, const SkPoint &l1, SkPoint *intersect)
float fX
x-axis value
float fY
y-axis value

◆ make_debug_renderers()

static ClipTileRendererArray make_debug_renderers ( )
static

Definition at line 995 of file compositor_quads.cpp.

995 {
999}
static sk_sp< ClipTileRenderer > MakeNonAA()
static sk_sp< ClipTileRenderer > Make()
static sk_sp< ClipTileRenderer > MakeAA()

◆ make_filtered_renderers()

static ClipTileRendererArray make_filtered_renderers ( )
static

Definition at line 1031 of file compositor_quads.cpp.

1031 {
1032 sk_sp<SkImage> mandrill = ToolUtils::GetResourceAsImage("images/mandrill_512.png");
1033
1034 SkColorMatrix cm;
1035 cm.setSaturation(10);
1037 sk_sp<SkImageFilter> imageFilter = SkImageFilters::Dilate(8, 8, nullptr);
1038
1039 static constexpr SkColor kAlphas[] = { SK_ColorTRANSPARENT, SK_ColorBLACK };
1040 auto alphaGradient = SkGradientShader::MakeRadial(
1041 {0.5f * kTileWidth * kColCount, 0.5f * kTileHeight * kRowCount},
1042 0.25f * kTileWidth * kColCount, kAlphas, nullptr, 2, SkTileMode::kClamp);
1043 sk_sp<SkMaskFilter> maskFilter = SkShaderMaskFilter::Make(std::move(alphaGradient));
1044
1045 return ClipTileRendererArray{
1046 TextureSetRenderer::MakeAlpha(mandrill, 0.5f),
1047 TextureSetRenderer::MakeColorFilter("Saturation", mandrill, std::move(colorFilter)),
1048
1049 // NOTE: won't draw correctly until SkCanvas' AutoLoopers are used to handle image filters
1050 TextureSetRenderer::MakeImageFilter("Dilate", mandrill, std::move(imageFilter)),
1051
1052 // NOTE: blur mask filters do work (tested locally), but visually they don't make much
1053 // sense, since each quad is blurred independently
1054 TextureSetRenderer::MakeMaskFilter("Shader", mandrill, std::move(maskFilter))};
1055}
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorTRANSPARENT
Definition SkColor.h:99
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
void setSaturation(float sat)
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkImageFilter > Dilate(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkMaskFilter > Make(sk_sp< SkShader > shader)
static sk_sp< ClipTileRenderer > MakeColorFilter(const char *name, sk_sp< SkImage > image, sk_sp< SkColorFilter > filter)
static sk_sp< ClipTileRenderer > MakeImageFilter(const char *name, sk_sp< SkImage > image, sk_sp< SkImageFilter > filter)
static sk_sp< ClipTileRenderer > MakeAlpha(sk_sp< SkImage > image, SkScalar alpha)
static sk_sp< ClipTileRenderer > MakeMaskFilter(const char *name, sk_sp< SkImage > image, sk_sp< SkMaskFilter > filter)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25

◆ make_image_renderers()

static ClipTileRendererArray make_image_renderers ( )
static

Definition at line 1022 of file compositor_quads.cpp.

1022 {
1023 sk_sp<SkImage> mandrill = ToolUtils::GetResourceAsImage("images/mandrill_512.png");
1024 sk_sp<SkData> mandrillJpeg = GetResourceAsData("images/mandrill_h1v1.jpg");
1029}
sk_sp< SkData > GetResourceAsData(const char *resource)
Definition Resources.cpp:42
static sk_sp< ClipTileRenderer > MakeUnbatched(sk_sp< SkImage > image)
static sk_sp< ClipTileRenderer > MakeBatched(sk_sp< SkImage > image, int transformCount)
static sk_sp< ClipTileRenderer > MakeFromJPEG(sk_sp< SkData > imageData)
static constexpr int kMatrixCount

◆ make_shader_renderers()

static ClipTileRendererArray make_shader_renderers ( )
static

Definition at line 1005 of file compositor_quads.cpp.

1005 {
1006 static constexpr SkPoint kPts[] = { {0.f, 0.f}, {0.25f * kTileWidth, 0.25f * kTileHeight} };
1007 static constexpr SkColor kColors[] = { SK_ColorBLUE, SK_ColorWHITE };
1008 auto gradient = SkGradientShader::MakeLinear(kPts, kColors, nullptr, 2,
1010
1012 SkBitmap bm;
1013 bm.allocPixels(info);
1016
1017 return ClipTileRendererArray{
1018 TextureSetRenderer::MakeShader("Gradient", image, gradient, false),
1019 TextureSetRenderer::MakeShader("Local Gradient", image, gradient, true)};
1020}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition SkColorType.h:21
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:258
sk_sp< SkImage > asImage() const
Definition SkBitmap.cpp:645
void eraseColor(SkColor4f) const
Definition SkBitmap.cpp:442
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< ClipTileRenderer > MakeShader(const char *name, sk_sp< SkImage > image, sk_sp< SkShader > shader, bool local)
sk_sp< SkImage > image
Definition examples.cpp:29
const DlColor kColors[]
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)

◆ make_solid_color_renderers()

static ClipTileRendererArray make_solid_color_renderers ( )
static

Definition at line 1001 of file compositor_quads.cpp.

1001 {
1002 return ClipTileRendererArray{SolidColorRenderer::Make({.2f, .8f, .3f, 1.f})};
1003}
static sk_sp< ClipTileRenderer > Make(const SkColor4f &color)

Variable Documentation

◆ kClipP1

constexpr SkPoint kClipP1 = {1.75f * kTileWidth, 0.8f * kTileHeight}
staticconstexpr

Definition at line 68 of file compositor_quads.cpp.

68{1.75f * kTileWidth, 0.8f * kTileHeight};

◆ kClipP2

constexpr SkPoint kClipP2 = {0.6f * kTileWidth, 2.f * kTileHeight}
staticconstexpr

Definition at line 69 of file compositor_quads.cpp.

69{0.6f * kTileWidth, 2.f * kTileHeight};

◆ kClipP3

constexpr SkPoint kClipP3 = {2.9f * kTileWidth, 3.5f * kTileHeight}
staticconstexpr

Definition at line 70 of file compositor_quads.cpp.

70{2.9f * kTileWidth, 3.5f * kTileHeight};

◆ kColCount

constexpr int kColCount = 3
staticconstexpr

Definition at line 63 of file compositor_quads.cpp.

◆ kMatrixCount

constexpr int kMatrixCount = 5
staticconstexpr

Definition at line 420 of file compositor_quads.cpp.

◆ kRowCount

constexpr int kRowCount = 4
staticconstexpr

Definition at line 62 of file compositor_quads.cpp.

◆ kTileHeight

constexpr SkScalar kTileHeight = 30
staticconstexpr

Definition at line 60 of file compositor_quads.cpp.

◆ kTileWidth

constexpr SkScalar kTileWidth = 40
staticconstexpr

Definition at line 59 of file compositor_quads.cpp.