blob: d9fb418419f73216d5e161cb053555b674a18226 [file] [log] [blame]
Michael Ludwig08f1a252020-01-22 14:32:11 -05001/*
2 * Copyright 2020 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#include "gm/gm.h"
8#include "include/core/SkCanvas.h"
9#include "include/core/SkColor.h"
10#include "include/core/SkMatrix.h"
Mike Reeddb301b72020-01-25 20:37:15 -050011#include "include/core/SkMatrix44.h"
Michael Ludwig08f1a252020-01-22 14:32:11 -050012#include "include/private/SkM44.h"
13#include "tools/timer/TimeUtils.h"
14
Mike Reeddb301b72020-01-25 20:37:15 -050015static SkM44 rotate_axis_angle(SkScalar x, SkScalar y, SkScalar z, SkScalar radians) {
16 // SkM44 doesn't expose any rotation factories yet
17 SkMatrix44 m;
18 m.setRotateAboutUnit(x, y, z, radians);
19
20 float a[16];
21 m.asColMajorf(a);
22 SkM44 m4;
23 m4.setColMajor(a);
24 return m4;
25}
26
Michael Ludwig08f1a252020-01-22 14:32:11 -050027// Adapted from https://codepen.io/adamdupuis/pen/qLYzqB
28class CrBug224618GM : public skiagm::GM {
29public:
30 CrBug224618GM() : fTime(0.f) {}
31
32protected:
33 SkString onShortName() override {
34 return SkString("crbug_224618");
35 }
36
37 SkISize onISize() override {
38 return SkISize::Make(kMaxVW, kMaxVW);
39 }
40
41 // This animates the FOV in viewer, to ensure the panorama covering rects are stable across
42 // a variety of perspective matrices
43 bool onAnimate(double nanos) override {
44 fTime = TimeUtils::Scaled(1e-9f * nanos, 0.5f);
45 return true;
46 }
47
48 void onDraw(SkCanvas* canvas) override {
49 SkScalar viewportWidth = SkScalarMod(fTime, 10.f) / 10.f * (kMaxVW - kMinVW) + kMinVW;
50 SkScalar radius = viewportWidth / 2.f; // round?
51 // See https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/perspective
52 SkM44 proj{1.f, 0.f, 0.f, 0.f,
53 0.f, 1.f, 0.f, 0.f,
54 0.f, 0.f, 1.f, 0.f,
55 0.f, 0.f, -1.f / radius, 1.f};
56 SkM44 zoom = SkM44::Translate(0.f, 0.f, radius);
57 SkM44 postZoom = SkM44::Translate(0.f, 0.f, -radius - 1.f);
Mike Reeddb301b72020-01-25 20:37:15 -050058 SkM44 rotateHorizontal = rotate_axis_angle(0.f, 1.f, 0.f, 2.356194490192345f);
Michael Ludwig08f1a252020-01-22 14:32:11 -050059
60 // w in degrees will need to be converted to radians
61 SkV4 axisAngles[6] = {
62 {0.f, 1.f, 0.f, -90.f}, // rotateY(-90deg)
63 {1.f, 0.f, 0.f, 0.f}, // <none>
64 {0.f, 1.f, 0.f, 90.f}, // rotateY(90deg)
65 {0.f, 1.f, 0.f, 180.f}, // rotateY(180deg)
66 {1.f, 0.f, 0.f, -90.f}, // rotateX(-90deg)
67 {1.f, 0.f, 0.f, 90.f}, // rotateX(90deg)
68 };
69 SkColor faceColors[6] = {
70 SK_ColorRED,
71 SK_ColorGREEN,
72 SK_ColorBLUE,
73 SK_ColorYELLOW,
74 SkColorSetARGB(0xFF, 0xFF, 0xA5, 0x00), // orange css
75 SkColorSetARGB(0xFF, 0x80, 0x00, 0x80) // purple css
76 };
77 for (int i = 0; i < 6; ++i) {
Mike Reeddb301b72020-01-25 20:37:15 -050078 SkM44 model = rotate_axis_angle(axisAngles[i].x, axisAngles[i].y, axisAngles[i].z,
Michael Ludwig08f1a252020-01-22 14:32:11 -050079 SkDegreesToRadians(axisAngles[i].w));
80 model = SkM44::Translate(radius, radius) * proj * // project and place content
81 zoom * rotateHorizontal * model * postZoom * // main model matrix
82 SkM44::Translate(-radius, -radius); // center content
83
84 canvas->save();
85 canvas->experimental_concat44(model);
86
87 SkPaint fillPaint;
88 fillPaint.setAntiAlias(true);
89 fillPaint.setColor(faceColors[i]);
90
91 canvas->drawRect(SkRect::MakeWH(viewportWidth, viewportWidth), fillPaint);
92 canvas->restore();
93 }
94 }
95private:
96 static const int kMaxVW = 800;
97 static const int kMinVW = 300;
98 SkScalar fTime;
99};
100
101DEF_GM(return new CrBug224618GM();)