skia2/gm/3d.cpp
Mike Reed a3a704afa3 Override didTranslate, and add virtual for didScale
This completes pushing through the new virtual didConcat44() to our
subclasses, and introduces didScale() for future optimizations. We
don't call didScale yet, until external subclasses are also updated.

This was derived from https://skia-review.googlesource.com/c/skia/+/263349

bug: skia: 9768
Change-Id: Ia26b48e76e323037082e8f2ee83673c26b99ebed
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/263702
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
2020-01-11 02:11:26 +00:00

82 lines
2.0 KiB
C++

/*
* Copyright 2020 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm/gm.h"
#include "include/core/SkCanvas.h"
#include "include/utils/Sk3D.h"
struct Info {
float fNear = 0.05f;
float fFar = 4;
float fAngle = SK_ScalarPI / 4;
SkPoint3 fEye { 0, 0, 1.0f/tan(fAngle/2) - 1 };
SkPoint3 fCOA { 0, 0, 0 };
SkPoint3 fUp { 0, 1, 0 };
};
static SkMatrix44 inv(const SkMatrix44& m) {
SkMatrix44 inverse;
m.invert(&inverse);
return inverse;
}
static SkMatrix44 make_ctm(const Info& info, const SkMatrix44& model, SkSize size) {
SkMatrix44 camera,
perspective,
viewport;
SkScalar w = size.width();
SkScalar h = size.height();
Sk3Perspective(&perspective, info.fNear, info.fFar, info.fAngle);
Sk3LookAt(&camera, info.fEye, info.fCOA, info.fUp);
viewport.setScale(w*0.5f, h*0.5f, 1);//.postTranslate(r.centerX(), r.centerY(), 0);
return viewport * perspective * camera * model * inv(viewport);
}
#include "include/core/SkPicture.h"
#include "include/core/SkPictureRecorder.h"
static void do_draw(SkCanvas* canvas, SkColor color) {
SkAutoCanvasRestore acr(canvas, true);
Info info;
SkMatrix44 m;
m.setRotateDegreesAbout(0, 1, 0, 30);
canvas->concat(make_ctm(info, m, {300, 300}));
canvas->translate(150, 150);
SkPaint paint;
paint.setColor(color);
canvas->drawRect({-100, -100, 100, 100}, paint);
}
/*
* Test calling drawables w/ translate and matrices
*/
DEF_SIMPLE_GM(sk3d_simple, real_canvas, 300, 300) {
do_draw(real_canvas, 0xFFFF0000);
SkPictureRecorder recorder;
SkCanvas* canvas = recorder.beginRecording(300, 300);
do_draw(canvas, 0x880000FF);
auto pic = recorder.finishRecordingAsPicture();
if (true) {
real_canvas->drawPicture(pic);
} else {
auto data = pic->serialize();
auto pic2 = SkPicture::MakeFromData(data.get());
real_canvas->drawPicture(pic2);
}
}