blob: 2bb5180de184c1ec31f0f0c5997082a1c55ad5da [file] [log] [blame]
tfarina11a005e2014-06-26 13:07:05 -07001/*
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
Mike Klein8aa0edf2020-10-16 11:04:18 -05008#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/utils/SkInterpolator.h"
tfarina11a005e2014-06-26 13:07:05 -070010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "tests/Test.h"
tfarina11a005e2014-06-26 13:07:05 -070012
13static SkScalar* iset(SkScalar array[3], int a, int b, int c) {
14 array[0] = SkIntToScalar(a);
15 array[1] = SkIntToScalar(b);
16 array[2] = SkIntToScalar(c);
17 return array;
18}
19
20DEF_TEST(Interpolator, reporter) {
21 SkInterpolator inter(3, 2);
22 SkScalar v1[3], v2[3], v[3];
23 SkInterpolator::Result result;
24
John Stilesfe0de302020-08-14 10:52:06 -040025 inter.setKeyFrame(0, 100, iset(v1, 10, 20, 30), nullptr);
tfarina11a005e2014-06-26 13:07:05 -070026 inter.setKeyFrame(1, 200, iset(v2, 110, 220, 330));
27
28 result = inter.timeToValues(0, v);
29 REPORTER_ASSERT(reporter, result == SkInterpolator::kFreezeStart_Result);
30 REPORTER_ASSERT(reporter, memcmp(v, v1, sizeof(v)) == 0);
31
32 result = inter.timeToValues(99, v);
33 REPORTER_ASSERT(reporter, result == SkInterpolator::kFreezeStart_Result);
34 REPORTER_ASSERT(reporter, memcmp(v, v1, sizeof(v)) == 0);
35
36 result = inter.timeToValues(100, v);
37 REPORTER_ASSERT(reporter, result == SkInterpolator::kNormal_Result);
38 REPORTER_ASSERT(reporter, memcmp(v, v1, sizeof(v)) == 0);
39
40 result = inter.timeToValues(200, v);
41 REPORTER_ASSERT(reporter, result == SkInterpolator::kNormal_Result);
42 REPORTER_ASSERT(reporter, memcmp(v, v2, sizeof(v)) == 0);
43
44 result = inter.timeToValues(201, v);
45 REPORTER_ASSERT(reporter, result == SkInterpolator::kFreezeEnd_Result);
46 REPORTER_ASSERT(reporter, memcmp(v, v2, sizeof(v)) == 0);
47
48 result = inter.timeToValues(150, v);
49 REPORTER_ASSERT(reporter, result == SkInterpolator::kNormal_Result);
50
51// Found failing when we re-enabled this test:
52#if 0
53 SkScalar vv[3];
54 REPORTER_ASSERT(reporter, memcmp(v, iset(vv, 60, 120, 180), sizeof(v)) == 0);
55#endif
56
57 result = inter.timeToValues(125, v);
58 REPORTER_ASSERT(reporter, result == SkInterpolator::kNormal_Result);
59 result = inter.timeToValues(175, v);
60 REPORTER_ASSERT(reporter, result == SkInterpolator::kNormal_Result);
61
caryclark65448172016-01-07 11:33:15 -080062 for (SkScalar val = -0.1f; val <= 1.1f; val += 0.1f) {
63 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkTPin(0.f, val, 1.f),
64 SkUnitCubicInterp(val, 1.f/3, 1.f/3, 2.f/3, 2.f/3)));
65 }
66
67 // These numbers come from
68 // http://www.w3.org/TR/css3-transitions/#transition-timing-function_tag.
69 const SkScalar testTransitions[][4] = {
70 { 0.25f, 0.1f, 0.25f, 1 }, // ease
71 { 0.42f, 0, 1, 1 }, // ease in
72 { 0, 0, 0.58f, 1 }, // ease out
73 { 0.42f, 0, 0.58f, 1 }, // ease in out
74 };
75
76 const SkScalar expectedOutput[][5] = {
77 { 0.0947876f, 0.513367f, 0.80249f, 0.940796f, 0.994263f }, // ease
78 { 0.0170288f, 0.129639f, 0.31543f, 0.554749f, 0.839417f }, // ease in
79 { 0.160583f, 0.445251f, 0.684692f, 0.870361f, 0.982971f }, // ease out
80 { 0.0197144f, 0.187439f, 0.500122f, 0.812561f, 0.980286f }, // ease in out
81 };
82
83 int i = 0;
84 for (const SkScalar* t : testTransitions) {
85 int j = 0;
86 for (SkScalar val = 0.1f; val < 1; val += 0.2f) {
87 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(expectedOutput[i][j++],
88 SkUnitCubicInterp(val, t[0], t[1], t[2], t[3])));
89 }
90 ++i;
caryclark65448172016-01-07 11:33:15 -080091 }
tfarina11a005e2014-06-26 13:07:05 -070092}