Flutter Engine
The Flutter Engine
gradient_matrix.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"
14#include "include/core/SkRect.h"
21
22constexpr SkColor gColors[] = {
24};
25
26// These annoying defines are necessary, because the only other alternative
27// is to use SkIntToScalar(...) everywhere.
28constexpr SkScalar sZero = 0;
31
32// These arrays define the gradient stop points
33// as x1, y1, x2, y2 per gradient to draw.
34constexpr SkPoint linearPts[][2] = {
35 {{sZero, sZero}, {sOne, sZero}},
36 {{sZero, sZero}, {sZero, sOne}},
37 {{sOne, sZero}, {sZero, sZero}},
38 {{sZero, sOne}, {sZero, sZero}},
39
40 {{sZero, sZero}, {sOne, sOne}},
41 {{sOne, sOne}, {sZero, sZero}},
42 {{sOne, sZero}, {sZero, sOne}},
43 {{sZero, sOne}, {sOne, sZero}}
44};
45
46constexpr SkPoint radialPts[][2] = {
47 {{sZero, sHalf}, {sOne, sHalf}},
48 {{sHalf, sZero}, {sHalf, sOne}},
49 {{sOne, sHalf}, {sZero, sHalf}},
50 {{sHalf, sOne}, {sHalf, sZero}},
51
52 {{sZero, sZero}, {sOne, sOne}},
53 {{sOne, sOne}, {sZero, sZero}},
54 {{sOne, sZero}, {sZero, sOne}},
55 {{sZero, sOne}, {sOne, sZero}}
56};
57
58// These define the pixels allocated to each gradient image.
61
62constexpr int IMAGES_X = 4; // number of images per row
63
64static sk_sp<SkShader> make_linear_gradient(const SkPoint pts[2], const SkMatrix& localMatrix) {
66 SkTileMode::kClamp, 0, &localMatrix);
67}
68
69static sk_sp<SkShader> make_radial_gradient(const SkPoint pts[2], const SkMatrix& localMatrix) {
71 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
72 SkScalarAve(pts[0].fY, pts[1].fY));
73 float radius = (center - pts[0]).length();
75 SkTileMode::kClamp, 0, &localMatrix);
76}
77
78static void draw_gradients(SkCanvas* canvas,
79 sk_sp<SkShader> (*makeShader)(const SkPoint[2], const SkMatrix&),
80 const SkPoint ptsArray[][2], int numImages) {
81 // Use some nice prime numbers for the rectangle and matrix with
82 // different scaling along the x and y axes (which is the bug this
83 // test addresses, where incorrect order of operations mixed up the axes)
84 SkRect rectGrad = {
86 SkIntToScalar(181), SkIntToScalar(167) };
87 SkMatrix shaderMat;
88 shaderMat.setScale(rectGrad.width(), rectGrad.height());
89 shaderMat.postTranslate(rectGrad.left(), rectGrad.top());
90
91 canvas->save();
92 for (int i = 0; i < numImages; i++) {
93 // Advance line downwards if necessary.
94 if (i % IMAGES_X == 0 && i != 0) {
95 canvas->restore();
96 canvas->translate(0, TESTGRID_Y);
97 canvas->save();
98 }
99
101 paint.setShader(makeShader(*ptsArray, shaderMat));
102 canvas->drawRect(rectGrad, paint);
103
104 // Advance to next position.
105 canvas->translate(TESTGRID_X, 0);
106 ptsArray++;
107 }
108 canvas->restore();
109}
110
111DEF_SIMPLE_GM_BG(gradient_matrix, canvas, 800, 800, 0xFFDDDDDD) {
114
115 canvas->translate(0, TESTGRID_Y);
116
119}
constexpr SkColor SK_ColorYELLOW
Definition: SkColor.h:139
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
#define SK_Scalar1
Definition: SkScalar.h:18
#define SkScalarAve(a, b)
Definition: SkScalar.h:74
#define SK_ScalarHalf
Definition: SkScalar.h:19
#define SkIntToScalar(x)
Definition: SkScalar.h:57
static SkScalar center(float pos0, float pos1)
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
int save()
Definition: SkCanvas.cpp:447
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< 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 & postTranslate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.cpp:281
SkMatrix & setScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:296
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
constexpr SkPoint linearPts[][2]
constexpr SkColor gColors[]
constexpr SkScalar TESTGRID_X
constexpr SkScalar sZero
constexpr SkPoint radialPts[][2]
constexpr SkScalar TESTGRID_Y
DEF_SIMPLE_GM_BG(gradient_matrix, canvas, 800, 800, 0xFFDDDDDD)
static sk_sp< SkShader > make_linear_gradient(const SkPoint pts[2], const SkMatrix &localMatrix)
constexpr SkScalar sOne
static void draw_gradients(SkCanvas *canvas, sk_sp< SkShader >(*makeShader)(const SkPoint[2], const SkMatrix &), const SkPoint ptsArray[][2], int numImages)
constexpr SkScalar sHalf
constexpr int IMAGES_X
static sk_sp< SkShader > make_radial_gradient(const SkPoint pts[2], const SkMatrix &localMatrix)
size_t length
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
constexpr float left() const
Definition: SkRect.h:734
constexpr float top() const
Definition: SkRect.h:741
constexpr float height() const
Definition: SkRect.h:769
constexpr float width() const
Definition: SkRect.h:762