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" 8 #include "include/core/SkCanvas.h" 9 #include "include/core/SkColor.h" 10 #include "include/core/SkImage.h" 11 #include "include/core/SkM44.h" 12 #include "include/core/SkMatrix.h" 13 #include "include/core/SkSurface.h" 14 #include "include/effects/SkGradientShader.h" 15 #include "tools/timer/TimeUtils.h" 16 17 // Adapted from https://codepen.io/adamdupuis/pen/qLYzqB 18 class CrBug224618GM : public skiagm::GM { 19 public: CrBug224618GM()20 CrBug224618GM() : fTime(0.f) {} 21 22 protected: getName() const23 SkString getName() const override { return SkString("crbug_224618"); } 24 getISize()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 onAnimate(double nanos)29 bool onAnimate(double nanos) override { 30 fTime = TimeUtils::Scaled(1e-9f * nanos, 0.5f); 31 return true; 32 } 33 onOnceBeforeDraw()34 void onOnceBeforeDraw() override { 35 static const SkColor kColors[2] = {SK_ColorTRANSPARENT, SkColorSetARGB(128, 255, 255, 255)}; 36 sk_sp<SkShader> gradient = SkGradientShader::MakeRadial( 37 {200.f, 200.f}, 25.f, kColors, nullptr, 2, SkTileMode::kMirror, 38 SkGradientShader::kInterpolateColorsInPremul_Flag, nullptr); 39 40 sk_sp<SkSurface> surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(400, 400)); 41 42 SkPaint bgPaint; 43 bgPaint.setShader(gradient); 44 surface->getCanvas()->drawPaint(bgPaint); 45 46 fCubeImage = surface->makeImageSnapshot(); 47 } 48 onDraw(SkCanvas * canvas)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] = { 71 SK_ColorRED, 72 SK_ColorGREEN, 73 SK_ColorBLUE, 74 SK_ColorYELLOW, 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), 100 SkSamplingOptions(SkFilterMode::kLinear), &fillPaint, 101 SkCanvas::kFast_SrcRectConstraint); 102 103 canvas->restore(); 104 } 105 } 106 private: 107 static const int kMaxVW = 800; 108 static const int kMinVW = 300; 109 110 SkScalar fTime; 111 sk_sp<SkImage> fCubeImage; 112 }; 113 114 DEF_GM(return new CrBug224618GM();) 115