Flutter Engine
The Flutter Engine
crbug_224618.cpp
Go to the documentation of this file.
1/*
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"
11#include "include/core/SkM44.h"
16
17// Adapted from https://codepen.io/adamdupuis/pen/qLYzqB
18class CrBug224618GM : public skiagm::GM {
19public:
20 CrBug224618GM() : fTime(0.f) {}
21
22protected:
23 SkString getName() const override { return SkString("crbug_224618"); }
24
25 SkISize getISize() override { return SkISize::Make(kMaxVW, kMaxVW); }
26
27 // This animates the FOV in viewer, to ensure the panorama covering rects are stable across
28 // a variety of perspective matrices
29 bool onAnimate(double nanos) override {
30 fTime = TimeUtils::Scaled(1e-9f * nanos, 0.5f);
31 return true;
32 }
33
34 void onOnceBeforeDraw() override {
35 static const SkColor kColors[2] = {SK_ColorTRANSPARENT, SkColorSetARGB(128, 255, 255, 255)};
37 {200.f, 200.f}, 25.f, kColors, nullptr, 2, SkTileMode::kMirror,
39
41
42 SkPaint bgPaint;
43 bgPaint.setShader(gradient);
44 surface->getCanvas()->drawPaint(bgPaint);
45
46 fCubeImage = surface->makeImageSnapshot();
47 }
48
49 void onDraw(SkCanvas* canvas) override {
50 SkScalar viewportWidth = SkScalarMod(fTime, 10.f) / 10.f * (kMaxVW - kMinVW) + kMinVW;
51 SkScalar radius = viewportWidth / 2.f; // round?
52 // See https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/perspective
53 SkM44 proj{1.f, 0.f, 0.f, 0.f,
54 0.f, 1.f, 0.f, 0.f,
55 0.f, 0.f, 1.f, 0.f,
56 0.f, 0.f, -1.f / radius, 1.f};
57 SkM44 zoom = SkM44::Translate(0.f, 0.f, radius);
58 SkM44 postZoom = SkM44::Translate(0.f, 0.f, -radius - 1.f);
59 SkM44 rotateHorizontal = SkM44::Rotate({0, 1, 0}, 2.356194490192345f);
60
61 // w in degrees will need to be converted to radians
62 SkV4 axisAngles[6] = {
63 {0.f, 1.f, 0.f, -90.f}, // rotateY(-90deg)
64 {1.f, 0.f, 0.f, 0.f}, // <none>
65 {0.f, 1.f, 0.f, 90.f}, // rotateY(90deg)
66 {0.f, 1.f, 0.f, 180.f}, // rotateY(180deg)
67 {1.f, 0.f, 0.f, -90.f}, // rotateX(-90deg)
68 {1.f, 0.f, 0.f, 90.f}, // rotateX(90deg)
69 };
70 SkColor faceColors[6] = {
75 SkColorSetARGB(0xFF, 0xFF, 0xA5, 0x00), // orange css
76 SkColorSetARGB(0xFF, 0x80, 0x00, 0x80) // purple css
77 };
78
79 for (int i = 0; i < 6; ++i) {
80 SkM44 model = SkM44::Rotate({axisAngles[i].x, axisAngles[i].y, axisAngles[i].z},
81 SkDegreesToRadians(axisAngles[i].w));
82 model = SkM44::Translate(radius, radius) * proj * // project and place content
83 zoom * rotateHorizontal * model * postZoom * // main model matrix
84 SkM44::Translate(-radius, -radius); // center content
85
86 canvas->save();
87 canvas->concat(model);
88
89 SkPaint fillPaint;
90 fillPaint.setAntiAlias(true);
91 fillPaint.setColor(faceColors[i]);
92
93 // Leverages FillRectOp on GPU backend
94 canvas->drawRect(SkRect::MakeWH(viewportWidth, viewportWidth), fillPaint);
95
96 // Leverages TextureOp on GPU backend, to ensure sure both quad paths handle clipping
97 canvas->drawImageRect(fCubeImage.get(),
98 SkRect::MakeWH(fCubeImage->width(), fCubeImage->height()),
99 SkRect::MakeWH(viewportWidth, viewportWidth),
102
103 canvas->restore();
104 }
105 }
106private:
107 static const int kMaxVW = 800;
108 static const int kMinVW = 300;
109
110 SkScalar fTime;
111 sk_sp<SkImage> fCubeImage;
112};
113
114DEF_GM(return new CrBug224618GM();)
constexpr SkColor SK_ColorYELLOW
Definition: SkColor.h:139
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
#define SkDegreesToRadians(degrees)
Definition: SkScalar.h:77
#define SkScalarMod(x, y)
Definition: SkScalar.h:41
bool onAnimate(double nanos) override
SkString getName() const override
void onDraw(SkCanvas *canvas) override
SkISize getISize() override
void onOnceBeforeDraw() override
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
@ kFast_SrcRectConstraint
sample outside bounds; faster
Definition: SkCanvas.h:1543
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
Definition: SkCanvas.cpp:2333
int save()
Definition: SkCanvas.cpp:447
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
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)
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
Definition: SkM44.h:150
static SkM44 Rotate(SkV3 axis, SkScalar radians)
Definition: SkM44.h:239
static SkM44 Translate(SkScalar x, SkScalar y, SkScalar z=0)
Definition: SkM44.h:225
void setColor(SkColor color)
Definition: SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition: SkPaint.h:170
void setShader(sk_sp< SkShader > shader)
T * get() const
Definition: SkRefCnt.h:303
Definition: gm.h:110
VkSurfaceKHR surface
Definition: main.cc:49
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
static float Scaled(float time, float speed, float period=0)
Definition: TimeUtils.h:27
const DlColor kColors[]
SkScalar w
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo MakeN32Premul(int width, int height)
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609
Definition: SkM44.h:98