skia2/gm/perspimages.cpp
Brian Salomon 9fa47cc1c6 Make class members that are static constexpr also be inline.
This is in prep for compiling with -std=c++14 and -Wno-c++17-extensions
when building with clang. Chrome has encountered problems with
third_party headers that are included both in Skia and other Chrome
sources that produce different code based on whether preprocessor macros
indicate a C++14 or C++17 compilation.

In C++17 they are already inline implicitly. When compiling with C++14
we can get linker errors unless they're explicitly inlined or defined
outside the class. With -Wno-c++17-extensions we can explicitly inline
them in the C++14 build because the warning that would be generated
about using a C++17 language extension is suppressed.

We cannot do this in public headers because we support compiling with
C++14 without suppressing the C++17 language extension warnings.

Bug: chromium:1257145
Change-Id: Iaf5f4c62a398f98dd4ca9b7dfb86f2d5cab21d66
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/457498
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
2021-10-11 16:22:59 +00:00

135 lines
5.0 KiB
C++

/*
* Copyright 2018 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/core/SkImage.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/private/SkTArray.h"
#include "include/private/SkTDArray.h"
#include "tools/Resources.h"
#include <initializer_list>
static sk_sp<SkImage> make_image1() { return GetResourceAsImage("images/mandrill_128.png"); }
static sk_sp<SkImage> make_image2() {
return GetResourceAsImage("images/brickwork-texture.jpg")->makeSubset({0, 0, 128, 128});
}
namespace skiagm {
class PerspImages : public GM {
public:
PerspImages() = default;
protected:
SkString onShortName() override { return SkString("persp_images"); }
SkISize onISize() override { return SkISize::Make(1150, 1280); }
void onOnceBeforeDraw() override {
fImages.push_back(make_image1());
fImages.push_back(make_image2());
}
void onDraw(SkCanvas* canvas) override {
SkTDArray<SkMatrix> matrices;
matrices.push()->setAll(1.f, 0.f, 0.f,
0.f, 1.f, 0.f,
0.f, 0.005f, 1.f);
matrices.push()->setAll(1.f, 0.f, 0.f,
0.f, 1.f, 0.f,
0.007f, -0.005f, 1.f);
matrices[1].preSkew(0.2f, -0.1f);
matrices[1].preRotate(-65.f);
matrices[1].preScale(1.2f, .8f);
matrices[1].postTranslate(0.f, 60.f);
SkPaint paint;
int n = 0;
SkRect bounds = SkRect::MakeEmpty();
for (const auto& img : fImages) {
SkRect imgB = SkRect::MakeWH(img->width(), img->height());
for (const auto& m : matrices) {
SkRect temp;
m.mapRect(&temp, imgB);
bounds.join(temp);
}
}
canvas->translate(-bounds.fLeft + 10.f, -bounds.fTop + 10.f);
canvas->save();
enum class DrawType {
kDrawImage,
kDrawImageRectStrict,
kDrawImageRectFast,
};
for (auto type :
{DrawType::kDrawImage, DrawType::kDrawImageRectStrict, DrawType::kDrawImageRectFast}) {
for (const auto& m : matrices) {
for (auto aa : {false, true}) {
paint.setAntiAlias(aa);
for (auto sampling : {
SkSamplingOptions(SkFilterMode::kNearest),
SkSamplingOptions(SkFilterMode::kLinear),
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
SkSamplingOptions(SkCubicResampler::Mitchell())}) {
for (const auto& img : fImages) {
canvas->save();
canvas->concat(m);
SkRect src = {img->width() / 4.f, img->height() / 4.f,
3.f * img->width() / 4.f, 3.f * img->height() / 4};
SkRect dst = {0, 0,
3.f / 4.f * img->width(), 3.f / 4.f * img->height()};
switch (type) {
case DrawType::kDrawImage:
canvas->drawImage(img, 0, 0, sampling, &paint);
break;
case DrawType::kDrawImageRectStrict:
canvas->drawImageRect(img, src, dst, sampling, &paint,
SkCanvas::kStrict_SrcRectConstraint);
break;
case DrawType::kDrawImageRectFast:
canvas->drawImageRect(img, src, dst, sampling, &paint,
SkCanvas::kFast_SrcRectConstraint);
break;
}
canvas->restore();
++n;
if (n < 8) {
canvas->translate(bounds.width() + 10.f, 0);
} else {
canvas->restore();
canvas->translate(0, bounds.height() + 10.f);
canvas->save();
n = 0;
}
}
}
}
}
}
canvas->restore();
}
private:
inline static constexpr int kNumImages = 4;
SkTArray<sk_sp<SkImage>> fImages;
using INHERITED = GM;
};
//////////////////////////////////////////////////////////////////////////////
DEF_GM(return new PerspImages();)
} // namespace skiagm