• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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"
10 
11 struct Info {
12     float   fNear = 0.05f;
13     float   fFar = 4;
14     float   fAngle = SK_ScalarPI / 4;
15 
16     SkV3    fEye { 0, 0, 1.0f/tan(fAngle/2) - 1 };
17     SkV3    fCOA { 0, 0, 0 };
18     SkV3    fUp  { 0, 1, 0 };
19 };
20 
inv(const SkM44 & m)21 static SkM44 inv(const SkM44& m) {
22     SkM44 inverse(SkM44::kUninitialized_Constructor);
23     if (!m.invert(&inverse)) {
24         inverse.setIdentity();
25     }
26     return inverse;
27 }
28 
make_ctm(const Info & info,const SkM44 & model,SkSize size)29 static SkM44 make_ctm(const Info& info, const SkM44& model, SkSize size) {
30     SkM44  camera, perspective, viewport;
31 
32     SkScalar w = size.width();
33     SkScalar h = size.height();
34 
35     perspective = Sk3Perspective(info.fNear, info.fFar, info.fAngle);
36     camera = Sk3LookAt(info.fEye, info.fCOA, info.fUp);
37     viewport.setScale(w*0.5f, h*0.5f, 1);//.postTranslate(r.centerX(), r.centerY(), 0);
38 
39     return viewport * perspective * camera * model * inv(viewport);
40 }
41 
42 #include "include/core/SkPicture.h"
43 #include "include/core/SkPictureRecorder.h"
44 
do_draw(SkCanvas * canvas,SkColor color)45 static void do_draw(SkCanvas* canvas, SkColor color) {
46     SkAutoCanvasRestore acr(canvas, true);
47 
48     Info info;
49 
50     SkM44 m = SkM44::Rotate({0, 1, 0}, SK_ScalarPI/6);
51 
52     canvas->concat44(make_ctm(info, m, {300, 300}));
53 
54     canvas->translate(150, 150);
55     SkPaint paint;
56     paint.setColor(color);
57     canvas->drawRect({-100, -100, 100, 100}, paint);
58 }
59 
60 /*
61  *  Test calling drawables w/ translate and matrices
62  */
63 DEF_SIMPLE_GM(sk3d_simple, real_canvas, 300, 300) {
64     do_draw(real_canvas, 0xFFFF0000);
65 
66     SkPictureRecorder recorder;
67     SkCanvas* canvas = recorder.beginRecording(300, 300);
68 
69     do_draw(canvas, 0x880000FF);
70 
71     auto pic = recorder.finishRecordingAsPicture();
72     if (true) {
73         real_canvas->drawPicture(pic);
74     } else {
75         auto data = pic->serialize();
76         auto pic2 = SkPicture::MakeFromData(data.get());
77         real_canvas->drawPicture(pic2);
78     }
79 }
80