skia2/gm/imagescalealigned.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

161 lines
5.6 KiB
C++

/*
* Copyright 2015 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/SkColor.h"
#include "include/core/SkImage.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTypes.h"
#include "include/private/SkTArray.h"
class ImageScaleAlignedGM : public skiagm::GM {
protected:
void onOnceBeforeDraw() override {
const SkVector vectors[] = { { 1, 0 }, { 0, 1 } };
for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
auto& set = fSets.push_back();
set.fVector = vectors[i];
set.fImages.push_back() = MakeImage(vectors[i], SK_ColorGREEN);
set.fScales.push_back() = 1;
set.fImages.push_back() = MakeImage(vectors[i], SK_ColorRED);
set.fScales.push_back() = kStretchFactor;
set.fImages.push_back() = MakeImage(vectors[i], SK_ColorGREEN);
set.fScales.push_back() = 1;
}
}
SkString onShortName() override {
return SkString("image_scale_aligned");
}
SkISize onISize() override {
return SkISize::Make(580, 780);
}
void onDraw(SkCanvas* canvas) override {
struct {
SkPoint offset;
SkVector scale;
} cfgs[] = {
{{ 10, 10 }, { 1, 1 }},
{{ 300.5f, 10 }, { 1, 1 }},
{{ 10, 200.5f }, { 1, 1 }},
{{ 300.5f, 200.5f }, { 1, 1 }},
{{ 10.5f, 400.5f }, { 1, 1 }},
{{ 550.5f, 400.5f }, { -1, 1 }},
{{ 10.5f, 750.5f }, { 1, -1 }},
{{ 550.5f, 750.5f }, { -1, -1 }},
};
for (size_t i = 0; i < SK_ARRAY_COUNT(cfgs); ++i) {
SkAutoCanvasRestore acr(canvas, true);
canvas->translate(cfgs[i].offset.x(), cfgs[i].offset.y());
canvas->scale(cfgs[i].scale.x(), cfgs[i].scale.y());
drawSets(canvas);
}
}
private:
struct ImageSet {
SkSTArray<3, sk_sp<SkImage>, true> fImages;
SkSTArray<3, SkScalar> fScales;
SkVector fVector;
};
static sk_sp<SkImage> MakeImage(const SkVector& vec, SkColor color) {
const SkPoint start = SkPoint::Make(vec.y() * kSegLen / 2, vec.x() * kSegLen / 2);
const SkPoint end = SkPoint::Make(start.x() + vec.x() * (kSegLen - 1),
start.y() + vec.y() * (kSegLen - 1));
auto surface(SkSurface::MakeRasterN32Premul(kSegLen, kSegLen));
surface->getCanvas()->clear(SK_ColorTRANSPARENT);
SkPaint paint;
paint.setAntiAlias(true);
const SkRect border = SkRect::MakeIWH(kSegLen, kSegLen).makeInset(.5f, .5f);
paint.setColor(SK_ColorBLUE);
paint.setStyle(SkPaint::kStroke_Style);
surface->getCanvas()->drawRect(border, paint);
paint.setColor(SK_ColorBLACK);
surface->getCanvas()->drawLine(start, end, paint);
paint.reset();
paint.setColor(color);
const SkPoint pts[] = { start, end };
surface->getCanvas()->drawPoints(SkCanvas::kPoints_PointMode, 2, pts, paint);
return surface->makeImageSnapshot();
}
void drawSets(SkCanvas* canvas) const {
SkAutoCanvasRestore acr(canvas, true);
const SkSamplingOptions samplings[] = {
SkSamplingOptions(SkFilterMode::kNearest),
SkSamplingOptions(SkFilterMode::kLinear),
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
SkSamplingOptions(SkCubicResampler::Mitchell()),
};
const bool AAs[] = { false, true };
SkPaint paint;
for (int i = 0; i < fSets.count(); ++i) {
auto& set = fSets[i];
SkPoint lastPt;
for (size_t j = 0; j < SK_ARRAY_COUNT(AAs); ++j) {
paint.setAntiAlias(AAs[j]);
for (size_t k = 0; k < SK_ARRAY_COUNT(samplings); ++k) {
lastPt = drawSet(canvas, set, samplings[k], paint);
canvas->translate((kSegLen + 4) * set.fVector.y(),
(kSegLen + 4) * set.fVector.x());
}
}
canvas->translate(lastPt.x() + kSegLen,
- SkIntToScalar(kSegLen + 4) * SK_ARRAY_COUNT(samplings) * SK_ARRAY_COUNT(AAs));
}
}
SkPoint drawSet(SkCanvas* canvas, const ImageSet& set, const SkSamplingOptions& sampling,
const SkPaint& paint) const {
SkASSERT(set.fImages.count() == set.fScales.count());
SkPoint pt = SkPoint::Make(0, 0);
for (int i = 0; i < set.fImages.count(); ++i) {
auto& img = set.fImages[i];
const SkRect dst =
SkRect::MakeXYWH(pt.x(), pt.y(),
img->width() * (1 + (set.fScales[i] - 1) * set.fVector.x()),
img->height() * (1 + (set.fScales[i] - 1) * set.fVector.y()));
canvas->drawImageRect(img.get(), dst, sampling, &paint);
pt.offset(dst.width() * set.fVector.x(), dst.height() * set.fVector.y());
}
return pt;
}
inline static constexpr unsigned kSegLen = 15;
inline static constexpr unsigned kStretchFactor = 4;
SkSTArray<2, ImageSet> fSets;
using INHERITED = GM;
};
DEF_GM(return new ImageScaleAlignedGM();)