2015-04-14 15:19:01 +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-04-30 17:44:26 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2021-01-13 18:14:23 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-04-30 17:44:26 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2021-01-19 22:58:25 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2015-04-14 15:19:01 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
// This GM exercises HighQuality anisotropic filtering.
|
|
|
|
class AnisotropicGM : public GM {
|
|
|
|
public:
|
2021-02-05 17:55:38 +00:00
|
|
|
AnisotropicGM() : fSampling(SkCubicResampler::Mitchell()) {
|
2018-08-16 14:17:03 +00:00
|
|
|
this->setBGColor(0xFFCCCCCC);
|
2015-04-14 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
SkString onShortName() override { return SkString("anisotropic_hq"); }
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(2*kImageSize + 3*kSpacer,
|
|
|
|
kNumVertImages*kImageSize + (kNumVertImages+1)*kSpacer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an image consisting of lines radiating from its center
|
|
|
|
void onOnceBeforeDraw() override {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr int kNumLines = 100;
|
|
|
|
constexpr SkScalar kAngleStep = 360.0f / kNumLines;
|
|
|
|
constexpr int kInnerOffset = 10;
|
2015-04-14 15:19:01 +00:00
|
|
|
|
2021-01-19 22:58:25 +00:00
|
|
|
auto info = SkImageInfo::MakeN32(kImageSize, kImageSize, kOpaque_SkAlphaType);
|
|
|
|
auto surf = SkSurface::MakeRaster(info);
|
|
|
|
auto canvas = surf->getCanvas();
|
2015-04-14 15:19:01 +00:00
|
|
|
|
2021-01-19 22:58:25 +00:00
|
|
|
canvas->clear(SK_ColorWHITE);
|
2015-04-14 15:19:01 +00:00
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
|
|
|
|
SkScalar angle = 0.0f, sin, cos;
|
|
|
|
|
2021-01-19 22:58:25 +00:00
|
|
|
canvas->translate(kImageSize/2.0f, kImageSize/2.0f);
|
2015-04-14 15:19:01 +00:00
|
|
|
for (int i = 0; i < kNumLines; ++i, angle += kAngleStep) {
|
2019-04-02 14:59:28 +00:00
|
|
|
sin = SkScalarSin(angle);
|
|
|
|
cos = SkScalarCos(angle);
|
2021-01-19 22:58:25 +00:00
|
|
|
canvas->drawLine(cos * kInnerOffset, sin * kInnerOffset,
|
|
|
|
cos * kImageSize/2, sin * kImageSize/2, p);
|
2015-04-14 15:19:01 +00:00
|
|
|
}
|
2021-01-19 22:58:25 +00:00
|
|
|
fImage = surf->makeImageSnapshot();
|
2015-04-14 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void draw(SkCanvas* canvas, int x, int y, int xSize, int ySize) {
|
|
|
|
SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
|
|
|
|
SkIntToScalar(xSize), SkIntToScalar(ySize));
|
2021-01-22 20:26:41 +00:00
|
|
|
canvas->drawImageRect(fImage, r, fSampling);
|
2015-04-14 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
SkScalar gScales[] = { 0.9f, 0.8f, 0.75f, 0.6f, 0.5f, 0.4f, 0.25f, 0.2f, 0.1f };
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-04-14 15:19:01 +00:00
|
|
|
SkASSERT(kNumVertImages-1 == (int)SK_ARRAY_COUNT(gScales)/2);
|
|
|
|
|
|
|
|
// Minimize vertically
|
|
|
|
for (int i = 0; i < (int)SK_ARRAY_COUNT(gScales); ++i) {
|
2021-01-19 22:58:25 +00:00
|
|
|
int height = SkScalarFloorToInt(fImage->height() * gScales[i]);
|
2015-04-14 15:19:01 +00:00
|
|
|
|
|
|
|
int yOff;
|
|
|
|
if (i <= (int)SK_ARRAY_COUNT(gScales)/2) {
|
2021-01-19 22:58:25 +00:00
|
|
|
yOff = kSpacer + i * (fImage->height() + kSpacer);
|
2015-04-14 15:19:01 +00:00
|
|
|
} else {
|
|
|
|
// Position the more highly squashed images with their less squashed counterparts
|
2021-01-19 22:58:25 +00:00
|
|
|
yOff = (SK_ARRAY_COUNT(gScales) - i) * (fImage->height() + kSpacer) - height;
|
2015-04-14 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 22:58:25 +00:00
|
|
|
this->draw(canvas, kSpacer, yOff, fImage->width(), height);
|
2015-04-14 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Minimize horizontally
|
|
|
|
for (int i = 0; i < (int)SK_ARRAY_COUNT(gScales); ++i) {
|
2021-01-19 22:58:25 +00:00
|
|
|
int width = SkScalarFloorToInt(fImage->width() * gScales[i]);
|
2015-04-14 15:19:01 +00:00
|
|
|
|
|
|
|
int xOff, yOff;
|
|
|
|
if (i <= (int)SK_ARRAY_COUNT(gScales)/2) {
|
2021-01-19 22:58:25 +00:00
|
|
|
xOff = fImage->width() + 2*kSpacer;
|
|
|
|
yOff = kSpacer + i * (fImage->height() + kSpacer);
|
2015-04-14 15:19:01 +00:00
|
|
|
} else {
|
|
|
|
// Position the more highly squashed images with their less squashed counterparts
|
2021-01-19 22:58:25 +00:00
|
|
|
xOff = fImage->width() + 2*kSpacer + fImage->width() - width;
|
|
|
|
yOff = kSpacer + (SK_ARRAY_COUNT(gScales) - i - 1) * (fImage->height() + kSpacer);
|
2015-04-14 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 22:58:25 +00:00
|
|
|
this->draw(canvas, xOff, yOff, width, fImage->height());
|
2015-04-14 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kImageSize = 256;
|
|
|
|
static constexpr int kSpacer = 10;
|
|
|
|
static constexpr int kNumVertImages = 5;
|
2015-04-14 15:19:01 +00:00
|
|
|
|
2021-01-19 22:58:25 +00:00
|
|
|
sk_sp<SkImage> fImage;
|
|
|
|
SkSamplingOptions fSampling;
|
2015-04-14 15:19:01 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2015-04-14 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new AnisotropicGM;)
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|