2015-01-14 21:16:26 +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-03-20 16:55:08 +00:00
|
|
|
#include "AnimTimer.h"
|
2015-01-14 21:16:26 +00:00
|
|
|
#include "Resources.h"
|
2018-08-08 15:36:17 +00:00
|
|
|
#include "Sample.h"
|
2015-01-14 21:16:26 +00:00
|
|
|
#include "SkCanvas.h"
|
2019-03-20 16:55:08 +00:00
|
|
|
#include "SkData.h"
|
2019-01-08 19:00:08 +00:00
|
|
|
#include "SkFont.h"
|
2015-11-09 16:36:05 +00:00
|
|
|
#include "SkGradientShader.h"
|
2019-03-20 16:55:08 +00:00
|
|
|
#include "SkInterpolator.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkPath.h"
|
2015-01-14 21:16:26 +00:00
|
|
|
#include "SkRandom.h"
|
2019-03-20 16:55:08 +00:00
|
|
|
#include "SkSurface.h"
|
2015-01-14 21:16:26 +00:00
|
|
|
#include "SkTime.h"
|
|
|
|
|
2018-09-05 18:41:40 +00:00
|
|
|
static sk_sp<SkSurface> make_surface(SkCanvas* canvas, const SkImageInfo& info) {
|
|
|
|
auto surface = canvas->makeSurface(info);
|
|
|
|
if (!surface) {
|
|
|
|
surface = SkSurface::MakeRaster(info);
|
|
|
|
}
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
2016-03-09 22:47:34 +00:00
|
|
|
static sk_sp<SkShader> make_shader(const SkRect& bounds) {
|
2017-12-08 15:21:31 +00:00
|
|
|
sk_sp<SkImage> image(GetResourceAsImage("images/mandrill_128.png"));
|
2017-04-28 15:12:19 +00:00
|
|
|
return image ? image->makeShader() : nullptr;
|
2015-11-09 16:36:05 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 21:16:26 +00:00
|
|
|
#define N 128
|
2015-01-18 03:36:08 +00:00
|
|
|
#define ANGLE_DELTA 3
|
|
|
|
#define SCALE_DELTA (SK_Scalar1 / 32)
|
2015-01-14 21:16:26 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_image() {
|
2015-01-18 03:36:08 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32(N, N, kOpaque_SkAlphaType);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRaster(info));
|
2015-01-14 21:16:26 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
2015-01-18 03:36:08 +00:00
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
2015-01-14 21:16:26 +00:00
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
path.setFillType(SkPath::kEvenOdd_FillType);
|
|
|
|
|
|
|
|
path.addRect(SkRect::MakeWH(N/2, N));
|
|
|
|
path.addRect(SkRect::MakeWH(N, N/2));
|
|
|
|
path.moveTo(0, 0); path.lineTo(N, 0); path.lineTo(0, N); path.close();
|
|
|
|
|
2015-11-09 16:36:05 +00:00
|
|
|
SkPaint paint;
|
2016-03-09 22:47:34 +00:00
|
|
|
paint.setShader(make_shader(SkRect::MakeWH(N, N)));
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-11-09 16:36:05 +00:00
|
|
|
canvas->drawPath(path, paint);
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-01-14 21:16:26 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> zoom_up(SkSurface* origSurf, SkImage* orig) {
|
2015-11-09 16:36:05 +00:00
|
|
|
const SkScalar S = 16; // amount to scale up
|
2015-01-18 03:36:08 +00:00
|
|
|
const int D = 2; // dimension scaling for the offscreen
|
|
|
|
// since we only view the center, don't need to produce the entire thing
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-01-18 03:36:08 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32(orig->width() * D, orig->height() * D,
|
|
|
|
kOpaque_SkAlphaType);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(origSurf->makeSurface(info));
|
2015-01-18 03:36:08 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
|
|
|
canvas->scale(S, S);
|
|
|
|
canvas->translate(-SkScalarHalf(orig->width()) * (S - D) / S,
|
|
|
|
-SkScalarHalf(orig->height()) * (S - D) / S);
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->drawImage(orig, 0, 0, nullptr);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-01-18 03:36:08 +00:00
|
|
|
if (S > 3) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorWHITE);
|
|
|
|
for (int i = 1; i < orig->height(); ++i) {
|
|
|
|
SkScalar y = SkIntToScalar(i);
|
|
|
|
canvas->drawLine(0, y, SkIntToScalar(orig->width()), y, paint);
|
|
|
|
}
|
|
|
|
for (int i = 1; i < orig->width(); ++i) {
|
|
|
|
SkScalar x = SkIntToScalar(i);
|
|
|
|
canvas->drawLine(x, 0, x, SkIntToScalar(orig->height()), paint);
|
|
|
|
}
|
|
|
|
}
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-01-18 03:36:08 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 21:16:26 +00:00
|
|
|
struct AnimValue {
|
|
|
|
SkScalar fValue;
|
|
|
|
SkScalar fMin;
|
|
|
|
SkScalar fMax;
|
|
|
|
SkScalar fMod;
|
|
|
|
|
|
|
|
operator SkScalar() const { return fValue; }
|
|
|
|
|
|
|
|
void set(SkScalar value, SkScalar min, SkScalar max) {
|
|
|
|
fValue = value;
|
|
|
|
fMin = min;
|
|
|
|
fMax = max;
|
|
|
|
fMod = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setMod(SkScalar value, SkScalar mod) {
|
|
|
|
fValue = value;
|
|
|
|
fMin = 0;
|
|
|
|
fMax = 0;
|
|
|
|
fMod = mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkScalar inc(SkScalar delta) {
|
|
|
|
fValue += delta;
|
|
|
|
return this->fixUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkScalar fixUp() {
|
|
|
|
if (fMod) {
|
|
|
|
fValue = SkScalarMod(fValue, fMod);
|
|
|
|
} else {
|
|
|
|
if (fValue > fMax) {
|
|
|
|
fValue = fMax;
|
|
|
|
} else if (fValue < fMin) {
|
|
|
|
fValue = fMin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fValue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-18 03:36:08 +00:00
|
|
|
static void draw_box_frame(SkCanvas* canvas, int width, int height) {
|
|
|
|
SkPaint p;
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
SkRect r = SkRect::MakeIWH(width, height);
|
|
|
|
r.inset(0.5f, 0.5f);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
canvas->drawLine(r.left(), r.top(), r.right(), r.bottom(), p);
|
|
|
|
canvas->drawLine(r.left(), r.bottom(), r.right(), r.top(), p);
|
|
|
|
}
|
2015-01-14 21:16:26 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class FilterQualityView : public Sample {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fImage;
|
|
|
|
AnimValue fScale, fAngle;
|
|
|
|
SkSize fCell;
|
|
|
|
SkInterpolator fTrans;
|
|
|
|
SkMSec fCurrTime;
|
|
|
|
bool fShowFatBits;
|
2015-01-14 21:16:26 +00:00
|
|
|
|
|
|
|
public:
|
2017-07-12 19:52:56 +00:00
|
|
|
FilterQualityView() : fTrans(2, 2), fShowFatBits(true) {
|
2015-01-18 03:36:08 +00:00
|
|
|
fCell.set(256, 256);
|
|
|
|
|
2015-01-14 21:16:26 +00:00
|
|
|
fScale.set(1, SK_Scalar1 / 8, 1);
|
|
|
|
fAngle.setMod(0, 360);
|
|
|
|
|
|
|
|
SkScalar values[2];
|
|
|
|
fTrans.setMirror(true);
|
|
|
|
fTrans.setReset(true);
|
|
|
|
|
2015-02-06 06:02:37 +00:00
|
|
|
fCurrTime = 0;
|
|
|
|
|
2015-01-14 21:16:26 +00:00
|
|
|
fTrans.setRepeatCount(999);
|
|
|
|
values[0] = values[1] = 0;
|
2015-02-06 06:02:37 +00:00
|
|
|
fTrans.setKeyFrame(0, fCurrTime, values);
|
2015-01-14 21:16:26 +00:00
|
|
|
values[0] = values[1] = 1;
|
2015-02-06 06:02:37 +00:00
|
|
|
fTrans.setKeyFrame(1, fCurrTime + 2000, values);
|
2015-01-14 21:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "FilterQuality");
|
2015-01-14 21:16:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SkUnichar uni;
|
2018-08-08 15:36:17 +00:00
|
|
|
if (Sample::CharQ(*evt, &uni)) {
|
2015-01-14 21:16:26 +00:00
|
|
|
switch (uni) {
|
2017-11-22 21:36:07 +00:00
|
|
|
case '1': fAngle.inc(-ANGLE_DELTA); return true;
|
|
|
|
case '2': fAngle.inc( ANGLE_DELTA); return true;
|
|
|
|
case '3': fScale.inc(-SCALE_DELTA); return true;
|
|
|
|
case '4': fScale.inc( SCALE_DELTA); return true;
|
|
|
|
case '5': fShowFatBits = !fShowFatBits; return true;
|
2015-01-14 21:16:26 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
2015-03-16 17:08:34 +00:00
|
|
|
void drawTheImage(SkCanvas* canvas, const SkISize& size, SkFilterQuality filter,
|
2015-01-18 03:36:08 +00:00
|
|
|
SkScalar dx, SkScalar dy) {
|
2015-01-14 21:16:26 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2015-03-16 17:08:34 +00:00
|
|
|
paint.setFilterQuality(filter);
|
2015-01-14 21:16:26 +00:00
|
|
|
|
2015-01-18 03:36:08 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
canvas->translate(dx, dy);
|
|
|
|
|
|
|
|
canvas->translate(SkScalarHalf(size.width()), SkScalarHalf(size.height()));
|
2015-01-14 21:16:26 +00:00
|
|
|
canvas->scale(fScale, fScale);
|
|
|
|
canvas->rotate(fAngle);
|
2016-03-17 17:51:11 +00:00
|
|
|
canvas->drawImage(fImage.get(), -SkScalarHalf(fImage->width()), -SkScalarHalf(fImage->height()),
|
2015-01-14 21:16:26 +00:00
|
|
|
&paint);
|
2015-01-18 03:36:08 +00:00
|
|
|
|
|
|
|
if (false) {
|
|
|
|
acr.restore();
|
|
|
|
draw_box_frame(canvas, size.width(), size.height());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 17:08:34 +00:00
|
|
|
void drawHere(SkCanvas* canvas, SkFilterQuality filter, SkScalar dx, SkScalar dy) {
|
2015-01-18 03:36:08 +00:00
|
|
|
SkCanvas* origCanvas = canvas;
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
SkISize size = SkISize::Make(fImage->width(), fImage->height());
|
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
sk_sp<SkSurface> surface;
|
2015-01-18 03:36:08 +00:00
|
|
|
if (fShowFatBits) {
|
|
|
|
// scale up so we don't clip rotations
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32(fImage->width() * 2, fImage->height() * 2,
|
|
|
|
kOpaque_SkAlphaType);
|
2018-09-05 18:41:40 +00:00
|
|
|
surface = make_surface(canvas, info);
|
2015-01-18 03:36:08 +00:00
|
|
|
canvas = surface->getCanvas();
|
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
|
|
|
size.set(info.width(), info.height());
|
|
|
|
} else {
|
|
|
|
canvas->translate(SkScalarHalf(fCell.width() - fImage->width()),
|
|
|
|
SkScalarHalf(fCell.height() - fImage->height()));
|
|
|
|
}
|
|
|
|
this->drawTheImage(canvas, size, filter, dx, dy);
|
|
|
|
|
|
|
|
if (surface) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> orig(surface->makeImageSnapshot());
|
2016-03-24 01:59:25 +00:00
|
|
|
sk_sp<SkImage> zoomed(zoom_up(surface.get(), orig.get()));
|
2016-03-17 17:51:11 +00:00
|
|
|
origCanvas->drawImage(zoomed.get(),
|
2015-01-18 03:36:08 +00:00
|
|
|
SkScalarHalf(fCell.width() - zoomed->width()),
|
|
|
|
SkScalarHalf(fCell.height() - zoomed->height()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawBorders(SkCanvas* canvas) {
|
|
|
|
SkPaint p;
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setColor(SK_ColorBLUE);
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeWH(fCell.width() * 2, fCell.height() * 2);
|
|
|
|
r.inset(SK_ScalarHalf, SK_ScalarHalf);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
canvas->drawLine(r.left(), r.centerY(), r.right(), r.centerY(), p);
|
|
|
|
canvas->drawLine(r.centerX(), r.top(), r.centerX(), r.bottom(), p);
|
2015-01-14 21:16:26 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 19:52:56 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
fImage = make_image();
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2015-01-18 03:36:08 +00:00
|
|
|
fCell.set(this->height() / 2, this->height() / 2);
|
|
|
|
|
2015-01-14 21:16:26 +00:00
|
|
|
SkScalar trans[2];
|
2015-02-06 06:02:37 +00:00
|
|
|
fTrans.timeToValues(fCurrTime, trans);
|
2015-01-14 21:16:26 +00:00
|
|
|
|
2015-01-18 03:36:08 +00:00
|
|
|
for (int y = 0; y < 2; ++y) {
|
|
|
|
for (int x = 0; x < 2; ++x) {
|
|
|
|
int index = y * 2 + x;
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->translate(fCell.width() * x, fCell.height() * y);
|
|
|
|
SkRect r = SkRect::MakeWH(fCell.width(), fCell.height());
|
|
|
|
r.inset(4, 4);
|
|
|
|
canvas->clipRect(r);
|
2015-03-16 17:08:34 +00:00
|
|
|
this->drawHere(canvas, SkFilterQuality(index), trans[0], trans[1]);
|
2015-01-18 03:36:08 +00:00
|
|
|
}
|
2015-01-14 21:16:26 +00:00
|
|
|
}
|
|
|
|
|
2015-01-18 03:36:08 +00:00
|
|
|
this->drawBorders(canvas);
|
|
|
|
|
|
|
|
const SkScalar textX = fCell.width() * 2 + 30;
|
|
|
|
|
2019-01-08 19:00:08 +00:00
|
|
|
SkFont font(nullptr, 36);
|
2015-01-14 21:16:26 +00:00
|
|
|
SkPaint paint;
|
2019-01-08 19:00:08 +00:00
|
|
|
canvas->drawString(SkStringPrintf("%.8g", (float)fScale), textX, 100, font, paint);
|
|
|
|
canvas->drawString(SkStringPrintf("%.8g", (float)fAngle), textX, 150, font, paint);
|
|
|
|
canvas->drawString(SkStringPrintf("%.8g", trans[0] ), textX, 200, font, paint);
|
|
|
|
canvas->drawString(SkStringPrintf("%.8g", trans[1] ), textX, 250, font, paint);
|
2015-02-06 06:02:37 +00:00
|
|
|
}
|
2015-01-18 03:36:08 +00:00
|
|
|
|
2019-03-20 16:55:08 +00:00
|
|
|
bool onAnimate(const AnimTimer& timer) override {
|
2015-02-06 06:02:37 +00:00
|
|
|
fCurrTime = timer.msec();
|
|
|
|
return true;
|
2015-01-18 03:36:08 +00:00
|
|
|
}
|
2015-01-14 21:16:26 +00:00
|
|
|
|
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2015-01-14 21:16:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new FilterQualityView(); )
|