Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
patch.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 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"
14#include "include/core/SkPath.h"
23#include "tools/DecodeUtils.h"
24#include "tools/Resources.h"
25
27 const SkColor colors[] = {
30 };
31 const SkPoint pts[] = { { 100.f / 4.f, 0.f }, { 3.f * 100.f / 4.f, 100.f } };
32
33 return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
35}
36
37static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
38 //draw control points
41 SkPatchUtils::GetBottomCubic(cubics, bottom);
43 SkPatchUtils::GetTopCubic(cubics, top);
48
49 paint.setColor(SK_ColorBLACK);
50 paint.setStrokeWidth(0.5f);
51 SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
52 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
53 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint);
57
58 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top + 1, paint);
61
62 paint.setStrokeWidth(2);
63
64 paint.setColor(SK_ColorRED);
65 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
66
67 paint.setColor(SK_ColorBLUE);
68 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint);
69
70 paint.setColor(SK_ColorCYAN);
71 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint);
72
73 paint.setColor(SK_ColorYELLOW);
75
76 paint.setColor(SK_ColorGREEN);
78}
79
80// The order of the colors and points is clockwise starting at upper-left corner.
82 //top points
83 {100,100},{150,50},{250,150}, {300,100},
84 //right points
85 {250, 150},{350,250},
86 //bottom points
87 {300,300},{250,250},{150,350},{100,300},
88 //left points
89 {50,250},{150,150}
90};
91
93 {0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f}
94};
95
96
97static void dopatch(SkCanvas* canvas, const SkColor colors[], sk_sp<SkImage> img,
98 const SkMatrix* localMatrix) {
100 paint.setColor(SK_ColorGREEN);
101
102 const SkBlendMode modes[] = {
106 };
107
108 SkPoint texStorage[4];
109 const SkPoint* tex = gTexCoords;
110
111 sk_sp<SkShader> shader;
112 if (img) {
113 SkScalar w = img->width();
114 SkScalar h = img->height();
115 shader = img->makeShader(SkSamplingOptions(), localMatrix);
116 texStorage[0].set(0, 0);
117 texStorage[1].set(w, 0);
118 texStorage[2].set(w, h);
119 texStorage[3].set(0, h);
120 tex = texStorage;
121 } else {
122 shader = make_shader();
123 }
124
125 canvas->save();
126 for (int y = 0; y < 3; y++) {
127 for (int x = 0; x < 4; x++) {
128 canvas->save();
129 canvas->translate(x * 350.0f, y * 350.0f);
130 switch (x) {
131 case 0:
132 canvas->drawPatch(gCubics, nullptr, nullptr, modes[y], paint);
133 break;
134 case 1:
135 canvas->drawPatch(gCubics, colors, nullptr, modes[y], paint);
136 break;
137 case 2:
138 paint.setShader(shader);
139 canvas->drawPatch(gCubics, nullptr, tex, modes[y], paint);
140 paint.setShader(nullptr);
141 break;
142 case 3:
143 paint.setShader(shader);
144 canvas->drawPatch(gCubics, colors, tex, modes[y], paint);
145 paint.setShader(nullptr);
146 break;
147 default:
148 break;
149 }
150
152 canvas->restore();
153 }
154 }
155 canvas->restore();
156}
157
158DEF_SIMPLE_GM(patch_primitive, canvas, 1500, 1100) {
159 const SkColor colors[SkPatchUtils::kNumCorners] = {
161 };
162 dopatch(canvas, colors, nullptr, nullptr);
163}
164DEF_SIMPLE_GM(patch_image, canvas, 1500, 1100) {
165 const SkColor colors[SkPatchUtils::kNumCorners] = {
167 };
168 dopatch(canvas, colors, ToolUtils::GetResourceAsImage("images/mandrill_128.png"), nullptr);
169}
170DEF_SIMPLE_GM(patch_image_persp, canvas, 1500, 1100) {
171 const SkColor colors[SkPatchUtils::kNumCorners] = {
173 };
174 SkMatrix localM;
175 localM.reset();
176 localM[6] = 0.00001f; // force perspective
177 dopatch(canvas, colors, ToolUtils::GetResourceAsImage("images/mandrill_128.png"), &localM);
178}
179DEF_SIMPLE_GM(patch_alpha, canvas, 1500, 1100) {
180 const SkColor colors[SkPatchUtils::kNumCorners] = {
181 SK_ColorRED, 0x0000FF00, SK_ColorBLUE, 0x00FF00FF,
182 };
183 dopatch(canvas, colors, nullptr, nullptr);
184}
185
186// These two should look the same (one patch, one simple path)
187DEF_SIMPLE_GM(patch_alpha_test, canvas, 550, 250) {
188 canvas->translate(-75, -75);
189
190 const SkColor colors[SkPatchUtils::kNumCorners] = {
191 0x80FF0000, 0x80FF0000, 0x80FF0000, 0x80FF0000,
192 };
194 canvas->drawPatch(gCubics, colors, nullptr, SkBlendMode::kDst, paint);
195
196 canvas->translate(300, 0);
197
198 SkPath path;
199 path.moveTo(gCubics[0]);
200 path.cubicTo(gCubics[ 1], gCubics[ 2], gCubics[ 3]);
201 path.cubicTo(gCubics[ 4], gCubics[ 5], gCubics[ 6]);
202 path.cubicTo(gCubics[ 7], gCubics[ 8], gCubics[ 9]);
203 path.cubicTo(gCubics[10], gCubics[11], gCubics[ 0]);
204 paint.setColor(colors[0]);
205 canvas->drawPath(path, paint);
206}
207
SkBlendMode
Definition SkBlendMode.h:38
@ kColorDodge
brighten destination to reflect source
constexpr SkColor SK_ColorYELLOW
Definition SkColor.h:139
constexpr SkColor SK_ColorMAGENTA
Definition SkColor.h:147
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorCYAN
Definition SkColor.h:143
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
static bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)
void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint &paint)
void drawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], SkBlendMode mode, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
int save()
Definition SkCanvas.cpp:451
@ kLines_PointMode
draw each pair of points as a line segment
Definition SkCanvas.h:1242
@ kPoints_PointMode
draw each point separately
Definition SkCanvas.h:1241
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)
SkMatrix & reset()
Definition SkMatrix.cpp:49
static void GetTopCubic(const SkPoint cubics[12], SkPoint points[4])
static void GetRightCubic(const SkPoint cubics[12], SkPoint points[4])
static void GetLeftCubic(const SkPoint cubics[12], SkPoint points[4])
static void GetBottomCubic(const SkPoint cubics[12], SkPoint points[4])
const Paint & paint
float SkScalar
Definition extension.cpp:12
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
double y
double x
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25
static void dopatch(SkCanvas *canvas, const SkColor colors[], sk_sp< SkImage > img, const SkMatrix *localMatrix)
Definition patch.cpp:97
const SkPoint gTexCoords[SkPatchUtils::kNumCorners]
Definition patch.cpp:92
const SkPoint gCubics[SkPatchUtils::kNumCtrlPts]
Definition patch.cpp:81
static sk_sp< SkShader > make_shader()
Definition patch.cpp:26
static void draw_control_points(SkCanvas *canvas, const SkPoint cubics[12])
Definition patch.cpp:37
SkScalar w
SkScalar h
void set(float x, float y)