blob: 3c692210f15f3370ca14b34193ede61639933486 [file] [log] [blame]
Michael Ludwig784184a2019-04-30 13:28:26 -04001/*
2 * Copyright 2019 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"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkFilterQuality.h"
Michael Ludwig784184a2019-04-30 13:28:26 -040012#include "include/core/SkFont.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkFontTypes.h"
Michael Ludwig784184a2019-04-30 13:28:26 -040014#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkMatrix.h"
16#include "include/core/SkMatrix44.h"
17#include "include/core/SkPaint.h"
Michael Ludwig784184a2019-04-30 13:28:26 -040018#include "include/core/SkRRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040019#include "include/core/SkRect.h"
20#include "include/core/SkRefCnt.h"
21#include "include/core/SkScalar.h"
22#include "include/core/SkSize.h"
23#include "include/core/SkString.h"
Michael Ludwig784184a2019-04-30 13:28:26 -040024#include "include/core/SkSurface.h"
25#include "tools/timer/AnimTimer.h"
26
27// Mimics https://output.jsbin.com/falefice/1/quiet?CC_POSTER_CIRCLE, which can't be captured as
28// an SKP due to many 3D layers being composited post-SKP capture.
29// See skbug.com/9028
30class PosterCircleGM : public skiagm::GM {
31public:
32 PosterCircleGM() : fTime(0.f) {}
33
34protected:
35
36 SkString onShortName() override {
37 return SkString("poster_circle");
38 }
39
40 SkISize onISize() override {
41 return SkISize::Make(kStageWidth, kStageHeight + 50);
42 }
43
44 bool onAnimate(const AnimTimer& timer) override {
45#if 0
46 if (timer.isRunning()) {
47 // Use a fixed timestep
48 fTime += 5e-4f;
49 }
50#else
51 fTime = timer.scaled(0.5f);
52#endif
53 return true;
54 }
55
56 void onOnceBeforeDraw() override {
57 SkFont font;
58 font.setEdging(SkFont::Edging::kAntiAlias);
59 font.setEmbolden(true);
60 font.setSize(24.f);
61
62 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(kPosterSize, kPosterSize);
63 for (int i = 0; i < kNumAngles; ++i) {
64 SkCanvas* canvas = surface->getCanvas();
65
66 SkPaint fillPaint;
67 fillPaint.setAntiAlias(true);
68 fillPaint.setColor(i % 2 == 0 ? SkColorSetRGB(0x99, 0x5C, 0x7F)
69 : SkColorSetRGB(0x83, 0x5A, 0x99));
70 canvas->drawRRect(SkRRect::MakeRectXY(SkRect::MakeWH(kPosterSize, kPosterSize),
71 10.f, 10.f), fillPaint);
72
73 SkString label;
74 label.printf("%d", i);
75 SkRect labelBounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040076 font.measureText(label.c_str(), label.size(), SkTextEncoding::kUTF8, &labelBounds);
Michael Ludwig784184a2019-04-30 13:28:26 -040077 SkScalar labelX = 0.5f * kPosterSize - 0.5f * labelBounds.width();
78 SkScalar labelY = 0.5f * kPosterSize + 0.5f * labelBounds.height();
79
80
81 SkPaint labelPaint;
82 labelPaint.setAntiAlias(true);
83 canvas->drawString(label, labelX, labelY, font, labelPaint);
84
85 fPosterImages[i] = surface->makeImageSnapshot();
86 }
87 }
88
89 void onDraw(SkCanvas* canvas) override {
90 // See https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/perspective
91 // for projection matrix when --webkit-perspective: 800px is used.
92 SkMatrix44 proj(SkMatrix44::kIdentity_Constructor);
93 proj.set(3, 2, -1.f / 800.f);
94
95 for (int pass = 0; pass < 2; ++pass) {
96 // Want to draw 90 to 270 first (the back), then 270 to 90 (the front), but do all 3
97 // rings backsides, then their frontsides since the front projections overlap across
98 // rings. Note: we skip the poster circle's x axis rotation because that complicates the
99 // back-to-front drawing order and it isn't necessary to trigger draws aligned with Z.
100 bool drawFront = pass > 0;
101
102 for (int y = 0; y < 3; ++y) {
103 float ringY = (y - 1) * (kPosterSize + 10.f);
104 for (int i = 0; i < kNumAngles; ++i) {
105 // Add an extra 45 degree rotation, which triggers the bug by aligning some of
106 // the posters with the z axis.
107 SkScalar yDuration = 5.f - y;
108 SkScalar yRotation = SkScalarMod(kAngleStep * i +
109 360.f * SkScalarMod(fTime / yDuration, yDuration), 360.f);
110 // These rotation limits were chosen manually to line up with current projection
111 static constexpr SkScalar kBackMinAngle = 70.f;
112 static constexpr SkScalar kBackMaxAngle = 290.f;
113 if (drawFront) {
114 if (yRotation >= kBackMinAngle && yRotation <= kBackMaxAngle) {
115 // Back portion during a front draw
116 continue;
117 }
118 } else {
119 if (yRotation < kBackMinAngle || yRotation > kBackMaxAngle) {
120 // Front portion during a back draw
121 continue;
122 }
123 }
124
125 canvas->save();
126
127 // Matrix matches transform: rotateY(<angle>deg) translateZ(200px); nested in an
128 // element with the perspective projection matrix above.
129 SkMatrix44 model;
130 // No post/preRotate, so start with rotation matrix and adjust from there
131 model.setRotateAboutUnit(0.f, 1.f, 0.f, SkDegreesToRadians(yRotation));
132 model.preTranslate(0.f, 0.f, kRingRadius); // *before* rotation
133 model.postTranslate(0.f, ringY, 0.f); // *after* rotation
134 model.postConcat(proj);
135 model.postTranslate(0.5f * kStageWidth, 0.5f * kStageHeight + 25, 0.f);
136
137 // Flatten the 4x4 matrix by discarding the 3rd row and column
138 canvas->concat(SkMatrix::MakeAll(
139 model.get(0, 0), model.get(0, 1), model.get(0, 3),
140 model.get(1, 0), model.get(1, 1), model.get(1, 3),
141 model.get(3, 0), model.get(3, 1), model.get(3, 3)));
142
143 SkRect poster = SkRect::MakeLTRB(-0.5f * kPosterSize, -0.5f * kPosterSize,
144 0.5f * kPosterSize, 0.5f * kPosterSize);
145 SkPaint fillPaint;
146 fillPaint.setAntiAlias(true);
147 fillPaint.setAlphaf(0.7f);
148 fillPaint.setFilterQuality(kLow_SkFilterQuality);
149 canvas->drawImageRect(fPosterImages[i], poster, &fillPaint);
150
151 canvas->restore();
152 }
153 }
154 }
155 }
156
157private:
158 static const int kAngleStep = 30;
159 static const int kNumAngles = 12; // 0 through 330 degrees
160
161 static const int kStageWidth = 600;
162 static const int kStageHeight = 400;
163 static const int kRingRadius = 200;
164 static const int kPosterSize = 100;
165
166 sk_sp<SkImage> fPosterImages[kNumAngles];
167 SkScalar fTime;
168};
169
170DEF_GM(return new PosterCircleGM();)