blob: 076a1aa703d35d894d9866209af3e249f0b91d80 [file] [log] [blame]
Brian Osman7c979f52019-02-12 13:27:51 -05001/*
2* Copyright 2019 Google LLC
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#ifndef SkCurve_DEFINED
9#define SkCurve_DEFINED
10
Brian Osman125daa42019-02-20 12:25:20 -050011#include "SkColor.h"
Brian Osman7c979f52019-02-12 13:27:51 -050012#include "SkScalar.h"
Brian Osman8b6283f2019-02-14 16:55:21 -050013#include "SkTArray.h"
Brian Osman7c979f52019-02-12 13:27:51 -050014
15class SkFieldVisitor;
16class SkRandom;
17
Brian Osmane12e4992019-02-19 15:39:18 -050018/**
19 * SkCurve implements a keyframed 1D function, useful for animating values over time. This pattern
20 * is common in digital content creation tools. An SkCurve might represent rotation, scale, opacity,
21 * or any other scalar quantity.
22 *
23 * An SkCurve has a logical domain of [0, 1], and is made of one or more SkCurveSegments.
24 * Each segment describes the behavior of the curve in some sub-domain. For an SkCurve with N
25 * segments, there are (N - 1) intermediate x-values that subdivide the domain. The first and last
26 * x-values are implicitly 0 and 1:
27 *
28 * 0 ... x[0] ... x[1] ... ... 1
29 * Segment_0 Segment_1 ... Segment_N-1
30 *
31 * Each segment describes a function over [0, 1] - x-values are re-normalized to the segment's
32 * domain when being evaluated. The segments are cubic polynomials, defined by four values (fMin).
33 * These are the values at x=0 and x=1, as well as control points at x=1/3 and x=2/3.
34 *
35 * For segments with fConstant == true, only the first value is used (fMin[0]).
36 *
37 * Each segment has two additional features for creating interesting (and varied) animation:
38 * - A segment can be ranged. Ranged segments have two sets of coefficients, and a random value
39 * taken from the SkRandom will be used to lerp betwen them. Typically, the SkRandom passed to
40 * eval will be in the same state at each call, so this value will be stable. That causes a
41 * ranged SkCurve to produce a single smooth cubic function somewhere within the range defined
42 * by fMin and fMax.
43 * - A segment can be bidirectional. In that case, after a value is computed, it will be negated
44 * 50% of the time.
45 */
46
Brian Osman34d13312019-02-27 11:19:19 -050047enum SkCurveSegmentType {
48 kConstant_SegmentType,
49 kLinear_SegmentType,
50 kCubic_SegmentType,
51};
52
Brian Osman8b6283f2019-02-14 16:55:21 -050053struct SkCurveSegment {
Brian Osman1b20cd82019-02-25 14:15:02 -050054 SkScalar eval(SkScalar x, SkScalar t, bool negate) const;
Brian Osman8b6283f2019-02-14 16:55:21 -050055 void visitFields(SkFieldVisitor* v);
56
57 void setConstant(SkScalar c) {
Brian Osman34d13312019-02-27 11:19:19 -050058 fType = kConstant_SegmentType;
59 fRanged = false;
Brian Osman8b6283f2019-02-14 16:55:21 -050060 fMin[0] = c;
61 }
62
63 SkScalar fMin[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
64 SkScalar fMax[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
65
Brian Osman34d13312019-02-27 11:19:19 -050066 int fType = kConstant_SegmentType;
Brian Osman8b6283f2019-02-14 16:55:21 -050067 bool fRanged = false;
68 bool fBidirectional = false;
69};
Brian Osman7c979f52019-02-12 13:27:51 -050070
71struct SkCurve {
72 SkCurve(SkScalar c = 0.0f) {
Brian Osman8b6283f2019-02-14 16:55:21 -050073 fSegments.push_back().setConstant(c);
Brian Osman7c979f52019-02-12 13:27:51 -050074 }
75
Brian Osmane12e4992019-02-19 15:39:18 -050076 // Evaluate this curve at x, using random for curves that have ranged or bidirectional segments.
Brian Osman8b6283f2019-02-14 16:55:21 -050077 SkScalar eval(SkScalar x, SkRandom& random) const;
Brian Osman7c979f52019-02-12 13:27:51 -050078 void visitFields(SkFieldVisitor* v);
Brian Osmane12e4992019-02-19 15:39:18 -050079
Brian Osmane12e4992019-02-19 15:39:18 -050080 // It should always be true that (fXValues.count() + 1) == fSegments.count()
Brian Osman8b6283f2019-02-14 16:55:21 -050081 SkTArray<SkScalar, true> fXValues;
82 SkTArray<SkCurveSegment, true> fSegments;
Brian Osman7c979f52019-02-12 13:27:51 -050083};
84
Brian Osman125daa42019-02-20 12:25:20 -050085/**
86 * SkColorCurve is similar to SkCurve, but keyframes 4D values - specifically colors. Because
87 * negative colors rarely make sense, SkColorCurves do not support bidirectional segments, but
88 * support all other features (including cubic interpolation).
89 */
90
91struct SkColorCurveSegment {
92 SkColorCurveSegment() {
93 for (int i = 0; i < 4; ++i) {
94 fMin[i] = { 1.0f, 1.0f, 1.0f, 1.0f };
95 fMax[i] = { 1.0f, 1.0f, 1.0f, 1.0f };
96 }
97 }
98
99 SkColor4f eval(SkScalar x, SkRandom& random) const;
100 void visitFields(SkFieldVisitor* v);
101
102 void setConstant(SkColor4f c) {
Brian Osman34d13312019-02-27 11:19:19 -0500103 fType = kConstant_SegmentType;
Brian Osman125daa42019-02-20 12:25:20 -0500104 fRanged = false;
105 fMin[0] = c;
106 }
107
108 SkColor4f fMin[4];
109 SkColor4f fMax[4];
110
Brian Osman34d13312019-02-27 11:19:19 -0500111 int fType = kConstant_SegmentType;
Brian Osman125daa42019-02-20 12:25:20 -0500112 bool fRanged = false;
113};
114
115struct SkColorCurve {
116 SkColorCurve(SkColor4f c = { 1.0f, 1.0f, 1.0f, 1.0f }) {
117 fSegments.push_back().setConstant(c);
118 }
119
120 SkColor4f eval(SkScalar x, SkRandom& random) const;
121 void visitFields(SkFieldVisitor* v);
122
123 SkTArray<SkScalar, true> fXValues;
124 SkTArray<SkColorCurveSegment, true> fSegments;
125};
126
Brian Osman7c979f52019-02-12 13:27:51 -0500127#endif // SkCurve_DEFINED