2015-09-03 02:05:10 +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 "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkDrawable.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkRSXform.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "include/utils/SkTextUtils.h"
|
|
|
|
#include "samplecode/Sample.h"
|
2015-09-03 02:05:10 +00:00
|
|
|
|
2017-03-15 16:19:37 +00:00
|
|
|
const SkBlendMode gModes[] = {
|
|
|
|
SkBlendMode::kSrcOver,
|
|
|
|
SkBlendMode::kSrc,
|
|
|
|
SkBlendMode::kSrcIn,
|
|
|
|
SkBlendMode::kSrcOut,
|
|
|
|
SkBlendMode::kSrcATop,
|
|
|
|
SkBlendMode::kDstOver,
|
|
|
|
SkBlendMode::kDstIn,
|
|
|
|
SkBlendMode::kDstOut,
|
|
|
|
SkBlendMode::kDstATop,
|
2015-09-03 02:05:10 +00:00
|
|
|
};
|
|
|
|
const int N_Modes = SK_ARRAY_COUNT(gModes);
|
|
|
|
|
|
|
|
static SkRandom gRand;
|
|
|
|
|
2017-11-22 21:36:07 +00:00
|
|
|
struct ModeButton {
|
2015-09-03 02:05:10 +00:00
|
|
|
SkString fLabel;
|
|
|
|
SkColor fColor;
|
2017-11-22 21:36:07 +00:00
|
|
|
SkRect fRect;
|
2015-09-03 02:05:10 +00:00
|
|
|
|
|
|
|
public:
|
2017-11-22 21:36:07 +00:00
|
|
|
void init(const char label[], const SkRect& rect) {
|
|
|
|
fLabel = label;
|
|
|
|
fRect = rect;
|
2015-09-03 02:05:10 +00:00
|
|
|
fColor = (gRand.nextU() & 0x7F7F7F7F) | SkColorSetARGB(0xFF, 0, 0, 0x80);
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2017-11-22 21:36:07 +00:00
|
|
|
void draw(SkCanvas* canvas) {
|
2015-09-03 02:05:10 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(fColor);
|
2017-11-22 21:36:07 +00:00
|
|
|
canvas->drawRoundRect(fRect, 8, 8, paint);
|
2015-09-03 02:05:10 +00:00
|
|
|
|
|
|
|
paint.setColor(0xFFFFFFFF);
|
2019-01-03 20:45:53 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setSize(16);
|
|
|
|
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
|
|
|
SkTextUtils::DrawString(canvas, fLabel.c_str(), fRect.centerX(), fRect.fTop + 0.68f * fRect.height(),
|
|
|
|
font, paint, SkTextUtils::kCenter_Align);
|
2015-09-03 02:05:10 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 21:36:07 +00:00
|
|
|
bool hitTest(SkScalar x, SkScalar y) {
|
2019-08-22 20:19:50 +00:00
|
|
|
return fRect.intersects({x - 1, y - 1, x + 1, y + 1});
|
2015-09-03 02:05:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ModeDrawable : public SkDrawable {
|
|
|
|
public:
|
2016-10-06 00:33:02 +00:00
|
|
|
ModeDrawable() : fMode(SkBlendMode::kSrcOver), fLoc(SkPoint::Make(0, 0)) {}
|
2015-09-03 02:05:10 +00:00
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
SkBlendMode fMode;
|
|
|
|
SkPoint fLoc;
|
2015-09-03 02:05:10 +00:00
|
|
|
|
|
|
|
bool hitTest(SkScalar x, SkScalar y) {
|
|
|
|
SkRect target = SkRect::MakeXYWH(x - fLoc.x() - 1, y - fLoc.y() - 1, 3, 3);
|
|
|
|
return this->getBounds().intersects(target);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CircDrawable : public ModeDrawable {
|
|
|
|
SkPaint fPaint;
|
|
|
|
SkRect fBounds;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CircDrawable(SkScalar size, SkColor c) {
|
|
|
|
const SkColor colors[] = { 0, c };
|
2016-03-09 02:50:00 +00:00
|
|
|
fPaint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(size/2, size/2), size/2,
|
2015-09-03 02:05:10 +00:00
|
|
|
colors, nullptr, 2,
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp));
|
2015-09-03 02:05:10 +00:00
|
|
|
fBounds = SkRect::MakeWH(size, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkRect onGetBounds() override {
|
|
|
|
return fBounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-10-06 00:33:02 +00:00
|
|
|
fPaint.setBlendMode(fMode);
|
2015-09-03 02:05:10 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(fLoc.x(), fLoc.y());
|
|
|
|
canvas->drawOval(fBounds, fPaint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class XferDemo : public Sample {
|
2015-09-03 02:05:10 +00:00
|
|
|
enum {
|
|
|
|
N = 4
|
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-03 02:05:10 +00:00
|
|
|
SkRect fModeRect[N_Modes];
|
2017-11-22 21:36:07 +00:00
|
|
|
ModeButton fModeButtons[N_Modes];
|
2016-11-04 17:02:54 +00:00
|
|
|
sk_sp<CircDrawable> fDrs[N];
|
2015-09-03 02:05:10 +00:00
|
|
|
CircDrawable* fSelected;
|
|
|
|
|
|
|
|
void addButtons() {
|
|
|
|
SkScalar x = 10;
|
|
|
|
SkScalar y = 10;
|
|
|
|
for (int i = 0; i < N_Modes; ++i) {
|
2017-11-22 21:36:07 +00:00
|
|
|
fModeButtons[i].init(SkBlendMode_Name(gModes[i]), SkRect::MakeXYWH(x, y, 70, 25));
|
2015-09-03 02:05:10 +00:00
|
|
|
fModeRect[i] = SkRect::MakeXYWH(x, y + 28, 70, 2);
|
|
|
|
x += 80;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
XferDemo() {
|
|
|
|
const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorBLACK };
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
fDrs[i].reset(new CircDrawable(200, colors[i]));
|
|
|
|
fDrs[i]->fLoc.set(100.f + i * 100, 100.f + i * 100);
|
2016-10-06 00:33:02 +00:00
|
|
|
fDrs[i]->fMode = SkBlendMode::kSrcOver;
|
2015-09-03 02:05:10 +00:00
|
|
|
}
|
|
|
|
fSelected = nullptr;
|
|
|
|
|
|
|
|
this->addButtons();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("XferDemo"); }
|
2015-09-03 02:05:10 +00:00
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2017-11-22 21:36:07 +00:00
|
|
|
for (int i = 0; i < N_Modes; ++i) {
|
|
|
|
fModeButtons[i].draw(canvas);
|
|
|
|
}
|
|
|
|
|
2015-09-03 02:05:10 +00:00
|
|
|
SkPaint paint;
|
|
|
|
if (fSelected) {
|
|
|
|
for (int i = 0; i < N_Modes; ++i) {
|
2017-03-15 16:19:37 +00:00
|
|
|
if (fSelected->fMode == gModes[i]) {
|
2015-09-03 02:05:10 +00:00
|
|
|
canvas->drawRect(fModeRect[i], paint);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->saveLayer(nullptr, nullptr);
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
fDrs[i]->draw(canvas);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override {
|
2017-11-22 21:36:07 +00:00
|
|
|
// Check mode buttons first
|
|
|
|
for (int i = 0; i < N_Modes; ++i) {
|
|
|
|
if (fModeButtons[i].hitTest(x, y)) {
|
2019-07-12 15:32:43 +00:00
|
|
|
Click* click = new Click();
|
2017-11-22 21:36:07 +00:00
|
|
|
click->fMeta.setS32("mode", i);
|
|
|
|
return click;
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 02:05:10 +00:00
|
|
|
fSelected = nullptr;
|
|
|
|
for (int i = N - 1; i >= 0; --i) {
|
|
|
|
if (fDrs[i]->hitTest(x, y)) {
|
2016-11-04 17:02:54 +00:00
|
|
|
fSelected = fDrs[i].get();
|
2015-09-03 02:05:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 15:32:43 +00:00
|
|
|
return fSelected ? new Click() : nullptr;
|
2015-09-03 02:05:10 +00:00
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-03 02:05:10 +00:00
|
|
|
bool onClick(Click* click) override {
|
2017-11-22 21:36:07 +00:00
|
|
|
int32_t mode;
|
|
|
|
if (click->fMeta.findS32("mode", &mode)) {
|
2019-08-29 14:39:22 +00:00
|
|
|
if (fSelected && skui::InputState::kUp == click->fState) {
|
2017-11-22 21:36:07 +00:00
|
|
|
fSelected->fMode = gModes[mode];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fSelected->fLoc.fX += click->fCurr.fX - click->fPrev.fX;
|
|
|
|
fSelected->fLoc.fY += click->fCurr.fY - click->fPrev.fY;
|
|
|
|
}
|
2015-09-03 02:05:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Sample;
|
2015-09-03 02:05:10 +00:00
|
|
|
};
|
2020-07-24 19:30:49 +00:00
|
|
|
DEF_SAMPLE( return new XferDemo; )
|
2015-09-03 02:05:10 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-07-24 19:30:49 +00:00
|
|
|
#include "tools/Resources.h"
|
|
|
|
|
|
|
|
class CubicResamplerDemo : public Sample {
|
|
|
|
struct Rec {
|
|
|
|
sk_sp<SkImage> fImage;
|
|
|
|
SkRect fBounds;
|
|
|
|
|
2020-11-06 14:50:47 +00:00
|
|
|
void draw(SkCanvas* canvas, SkCubicResampler cubic) const {
|
2020-07-24 19:30:49 +00:00
|
|
|
SkRect r = fBounds;
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
SkMatrix lm = SkMatrix::Translate(r.x(), r.y())
|
|
|
|
* SkMatrix::Scale(10, 10);
|
2020-12-08 18:16:56 +00:00
|
|
|
paint.setShader(fImage->makeShader(SkSamplingOptions(), lm));
|
2020-07-24 19:30:49 +00:00
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
r.offset(r.width() + 10, 0);
|
|
|
|
lm.postTranslate(r.width() + 10, 0);
|
|
|
|
|
2021-01-15 02:59:01 +00:00
|
|
|
paint.setShader(fImage->makeShader(SkSamplingOptions(SkFilterMode::kLinear), lm));
|
2020-07-24 19:30:49 +00:00
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
r.offset(r.width() + 10, 0);
|
|
|
|
lm.postTranslate(r.width() + 10, 0);
|
|
|
|
|
2020-11-20 23:45:36 +00:00
|
|
|
paint.setShader(fImage->makeShader(SkTileMode::kClamp, SkTileMode::kClamp,
|
|
|
|
SkSamplingOptions(cubic), &lm));
|
2020-07-24 19:30:49 +00:00
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
std::vector<Rec> fRecs;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CubicResamplerDemo() {
|
|
|
|
const char* names[] = {
|
|
|
|
"images/mandrill_128.png",
|
|
|
|
"images/rle.bmp",
|
|
|
|
"images/example_4.png",
|
|
|
|
};
|
|
|
|
SkRect r = {10, 10, 200, 200};
|
|
|
|
for (auto name : names) {
|
|
|
|
fRecs.push_back({GetResourceAsImage(name), r});
|
|
|
|
r.offset(0, r.height() + 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
fDomain.setXYWH(r.fLeft + 3*r.width() + 40, 50, 200, 200);
|
|
|
|
fCubic = {.3f, .5f};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString name() override { return SkString("CubicResampler"); }
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
for (const auto& rec : fRecs) {
|
|
|
|
rec.draw(canvas, fCubic);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStroke(true);
|
|
|
|
canvas->drawRect(fDomain, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setStroke(false);
|
2021-01-15 17:26:22 +00:00
|
|
|
SkPoint loc = SkMatrix::RectToRect({0,0,1,1}, fDomain).mapXY(fCubic.B, fCubic.C);
|
2020-07-24 19:30:49 +00:00
|
|
|
canvas->drawCircle(loc.fX, loc.fY, 8, paint);
|
|
|
|
|
|
|
|
SkString str;
|
|
|
|
str.printf("B=%4.2f C=%4.2f", fCubic.B, fCubic.C);
|
|
|
|
SkFont font;
|
|
|
|
font.setSize(25);
|
|
|
|
font.setEdging(SkFont::Edging::kAntiAlias);
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
canvas->drawSimpleText(str.c_str(), str.size(), SkTextEncoding::kUTF8,
|
|
|
|
fDomain.fLeft + 10, fDomain.fBottom + 40, font, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static float pin_unitize(float min, float max, float value) {
|
|
|
|
return (std::min(std::max(value, min), max) - min) / (max - min);
|
|
|
|
}
|
|
|
|
static SkPoint pin_unitize(const SkRect& r, SkPoint p) {
|
|
|
|
return {
|
|
|
|
pin_unitize(r.fLeft, r.fRight, p.fX),
|
|
|
|
pin_unitize(r.fTop, r.fBottom, p.fY),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override {
|
|
|
|
if (fDomain.contains(x, y)) {
|
|
|
|
return new Click([this](Click* click) {
|
|
|
|
auto [B, C] = pin_unitize(fDomain, click->fCurr);
|
|
|
|
fCubic = {B, C};
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkRect fDomain;
|
|
|
|
SkImage::CubicResampler fCubic;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Sample;
|
2020-07-24 19:30:49 +00:00
|
|
|
};
|
|
|
|
DEF_SAMPLE( return new CubicResamplerDemo; )
|