2015-09-03 15:32:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPath.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2015-09-03 15:32:08 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_image(SkCanvas* origCanvas, int w, int h) {
|
2015-09-03 15:32:08 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
|
2019-03-20 16:12:10 +00:00
|
|
|
auto surface(ToolUtils::makeSurface(origCanvas, info));
|
2015-09-03 15:32:08 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::draw_checkerboard(canvas, SK_ColorRED, SK_ColorGREEN, w / 10);
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-09-03 15:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class PerspShadersGM : public GM {
|
|
|
|
public:
|
|
|
|
PerspShadersGM(bool doAA) : fDoAA(doAA) { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
SkString name;
|
|
|
|
name.printf("persp_shaders_%s",
|
|
|
|
fDoAA ? "aa" : "bw");
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kCellSize*kNumCols, kCellSize*kNumRows);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
2021-01-23 23:10:39 +00:00
|
|
|
fBitmapImage = ToolUtils::create_checkerboard_image(
|
2019-03-20 16:12:10 +00:00
|
|
|
kCellSize, kCellSize, SK_ColorBLUE, SK_ColorYELLOW, kCellSize / 10);
|
2015-09-03 15:32:08 +00:00
|
|
|
|
|
|
|
SkPoint pts1[] = {
|
|
|
|
{ 0, 0 },
|
|
|
|
{ SkIntToScalar(kCellSize), SkIntToScalar(kCellSize) }
|
|
|
|
};
|
|
|
|
SkPoint pts2[] = {
|
|
|
|
{ 0, 0 },
|
|
|
|
{ 0, SkIntToScalar(kCellSize) }
|
|
|
|
};
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkColor colors[] = {
|
2015-09-03 15:32:08 +00:00
|
|
|
SK_ColorRED, SK_ColorGREEN, SK_ColorRED, SK_ColorGREEN, SK_ColorRED
|
|
|
|
};
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar pos[] = { 0, 0.25f, 0.5f, 0.75f, SK_Scalar1 };
|
2015-09-03 15:32:08 +00:00
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
fLinearGrad1 = SkGradientShader::MakeLinear(pts1, colors, pos, SK_ARRAY_COUNT(colors),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp);
|
2016-03-13 21:13:58 +00:00
|
|
|
fLinearGrad2 = SkGradientShader::MakeLinear(pts2, colors, pos, SK_ARRAY_COUNT(colors),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp);
|
2015-09-03 15:32:08 +00:00
|
|
|
|
|
|
|
fPerspMatrix.reset();
|
|
|
|
fPerspMatrix.setPerspY(SK_Scalar1 / 50);
|
|
|
|
|
|
|
|
fPath.moveTo(0, 0);
|
|
|
|
fPath.lineTo(0, SkIntToScalar(kCellSize));
|
|
|
|
fPath.lineTo(kCellSize/2.0f, kCellSize/2.0f);
|
|
|
|
fPath.lineTo(SkIntToScalar(kCellSize), SkIntToScalar(kCellSize));
|
|
|
|
fPath.lineTo(SkIntToScalar(kCellSize), 0);
|
|
|
|
fPath.close();
|
|
|
|
}
|
|
|
|
|
2021-01-23 23:10:39 +00:00
|
|
|
void drawRow(SkCanvas* canvas, const SkSamplingOptions& sampling) {
|
2015-09-03 15:32:08 +00:00
|
|
|
SkPaint filterPaint;
|
|
|
|
filterPaint.setAntiAlias(fDoAA);
|
|
|
|
|
|
|
|
SkPaint pathPaint;
|
2021-01-23 23:10:39 +00:00
|
|
|
pathPaint.setShader(fBitmapImage->makeShader(sampling));
|
2015-09-03 15:32:08 +00:00
|
|
|
pathPaint.setAntiAlias(fDoAA);
|
|
|
|
|
|
|
|
SkPaint gradPaint1;
|
|
|
|
gradPaint1.setShader(fLinearGrad1);
|
|
|
|
gradPaint1.setAntiAlias(fDoAA);
|
|
|
|
SkPaint gradPaint2;
|
|
|
|
gradPaint2.setShader(fLinearGrad2);
|
|
|
|
gradPaint2.setAntiAlias(fDoAA);
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(kCellSize), SkIntToScalar(kCellSize));
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(fPerspMatrix);
|
2021-01-23 23:10:39 +00:00
|
|
|
canvas->drawImageRect(fBitmapImage, r, sampling, &filterPaint);
|
2015-09-03 15:32:08 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(kCellSize), 0);
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(fPerspMatrix);
|
2021-01-23 23:10:39 +00:00
|
|
|
canvas->drawImage(fImage.get(), 0, 0, sampling, &filterPaint);
|
2015-09-03 15:32:08 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(kCellSize), 0);
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(fPerspMatrix);
|
|
|
|
canvas->drawRect(r, pathPaint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(kCellSize), 0);
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(fPerspMatrix);
|
|
|
|
canvas->drawPath(fPath, pathPaint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(kCellSize), 0);
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(fPerspMatrix);
|
|
|
|
canvas->drawRect(r, gradPaint1);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(kCellSize), 0);
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(fPerspMatrix);
|
|
|
|
canvas->drawPath(fPath, gradPaint2);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2020-07-10 15:27:43 +00:00
|
|
|
if (!fImage || !fImage->isValid(canvas->recordingContext())) {
|
2016-03-17 17:51:11 +00:00
|
|
|
fImage = make_image(canvas, kCellSize, kCellSize);
|
2015-09-03 15:32:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 23:10:39 +00:00
|
|
|
this->drawRow(canvas, SkSamplingOptions(SkFilterMode::kNearest));
|
2015-09-03 15:32:08 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(kCellSize));
|
2021-01-23 23:10:39 +00:00
|
|
|
this->drawRow(canvas, SkSamplingOptions(SkFilterMode::kLinear));
|
2015-09-03 15:32:08 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(kCellSize));
|
2021-01-23 23:10:39 +00:00
|
|
|
this->drawRow(canvas, SkSamplingOptions(SkFilterMode::kLinear,
|
|
|
|
SkMipmapMode::kNearest));
|
2015-09-03 15:32:08 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(kCellSize));
|
2021-02-05 17:55:38 +00:00
|
|
|
this->drawRow(canvas, SkSamplingOptions(SkCubicResampler::Mitchell()));
|
2015-09-03 15:32:08 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(kCellSize));
|
2022-04-29 13:15:19 +00:00
|
|
|
this->drawRow(canvas, SkSamplingOptions::Aniso(16));
|
|
|
|
canvas->translate(0, SkIntToScalar(kCellSize));
|
2015-09-03 15:32:08 +00:00
|
|
|
}
|
|
|
|
private:
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr int kCellSize = 50;
|
2022-04-29 13:15:19 +00:00
|
|
|
inline static constexpr int kNumRows = 5;
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr int kNumCols = 6;
|
2015-09-03 15:32:08 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
bool fDoAA;
|
|
|
|
SkPath fPath;
|
|
|
|
sk_sp<SkShader> fLinearGrad1;
|
|
|
|
sk_sp<SkShader> fLinearGrad2;
|
|
|
|
SkMatrix fPerspMatrix;
|
|
|
|
sk_sp<SkImage> fImage;
|
2021-01-23 23:10:39 +00:00
|
|
|
sk_sp<SkImage> fBitmapImage;
|
2015-09-03 15:32:08 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2015-09-03 15:32:08 +00:00
|
|
|
};
|
2019-12-05 16:55:52 +00:00
|
|
|
DEF_GM(return new PerspShadersGM(true);)
|
|
|
|
DEF_GM(return new PerspShadersGM(false);)
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|
2015-09-03 15:32:08 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-12-05 16:55:52 +00:00
|
|
|
#include "tools/Resources.h"
|
|
|
|
|
|
|
|
static SkPath make_path() {
|
|
|
|
SkRandom rand;
|
2019-12-05 22:29:45 +00:00
|
|
|
auto rand_pt = [&rand]() {
|
|
|
|
auto x = rand.nextF();
|
|
|
|
auto y = rand.nextF();
|
|
|
|
return SkPoint{x * 400, y * 400};
|
|
|
|
};
|
2019-12-05 16:55:52 +00:00
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
2019-12-05 22:29:45 +00:00
|
|
|
SkPoint pts[6];
|
|
|
|
for (auto& p : pts) {
|
|
|
|
p = rand_pt();
|
|
|
|
}
|
|
|
|
path.moveTo(pts[0]).quadTo(pts[1], pts[2]).quadTo(pts[3], pts[4]).lineTo(pts[5]);
|
2019-12-05 16:55:52 +00:00
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(perspective_clip, canvas, 800, 800) {
|
|
|
|
SkPath path = make_path();
|
|
|
|
auto shader = GetResourceAsImage("images/mandrill_128.png")
|
2020-12-09 02:58:35 +00:00
|
|
|
->makeShader(SkSamplingOptions(), SkMatrix::Scale(3, 3));
|
2019-12-05 16:55:52 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor({0.75, 0.75, 0.75, 1});
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
|
|
|
|
// This is a crazy perspective matrix, derived from halfplanes3, to draw a shape where
|
|
|
|
// part of it is "behind" the viewer, hence showing the need for "half-plane" clipping
|
|
|
|
// when in perspective.
|
|
|
|
SkMatrix mx;
|
|
|
|
const SkScalar array[] = {
|
|
|
|
-1.7866f, 1.3357f, 273.0295f,
|
|
|
|
-1.0820f, 1.3186f, 135.5196f,
|
|
|
|
-0.0047f, -0.0015f, 2.1485f,
|
|
|
|
};
|
|
|
|
mx.set9(array);
|
|
|
|
|
|
|
|
paint.setShader(shader);
|
|
|
|
canvas->concat(mx);
|
|
|
|
canvas->drawPath(path, paint);
|
2015-09-03 15:32:08 +00:00
|
|
|
}
|