2011-12-10 19:36:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 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"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-04-30 17:44:26 +00:00
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFont.h"
|
2019-04-30 17:44:26 +00:00
|
|
|
#include "include/core/SkFontTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-04-30 17:44:26 +00:00
|
|
|
#include "include/core/SkImageFilter.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"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkShader.h"
|
2019-04-30 17:44:26 +00:00
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-04-30 17:44:26 +00:00
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2011-12-10 19:36:56 +00:00
|
|
|
|
2019-04-30 17:44:26 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2021-07-08 14:43:31 +00:00
|
|
|
static sk_sp<SkImage> make_src(int w, int h) {
|
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(w, h));
|
2016-09-29 19:25:26 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
2011-12-10 19:36:56 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
2021-07-08 14:43:31 +00:00
|
|
|
SkPoint pts[] = { {0, 0}, {SkIntToScalar(w), SkIntToScalar(h)} };
|
2011-12-10 19:36:56 +00:00
|
|
|
SkColor colors[] = {
|
2013-05-28 19:43:05 +00:00
|
|
|
SK_ColorTRANSPARENT, SK_ColorGREEN, SK_ColorCYAN,
|
|
|
|
SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE,
|
2011-12-10 19:36:56 +00:00
|
|
|
};
|
2016-03-09 02:50:00 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp));
|
2016-09-29 19:25:26 +00:00
|
|
|
canvas->drawPaint(paint);
|
|
|
|
return surface->makeImageSnapshot();
|
2011-12-10 19:36:56 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 14:43:31 +00:00
|
|
|
static sk_sp<SkImage> make_dst(int w, int h) {
|
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(w, h));
|
2016-09-29 19:25:26 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
2011-12-10 19:36:56 +00:00
|
|
|
SkPaint paint;
|
2021-07-08 14:43:31 +00:00
|
|
|
SkPoint pts[] = { {0, SkIntToScalar(h)}, {SkIntToScalar(w), 0} };
|
2011-12-10 19:36:56 +00:00
|
|
|
SkColor colors[] = {
|
2015-07-17 16:39:16 +00:00
|
|
|
SK_ColorBLUE, SK_ColorYELLOW, SK_ColorBLACK, SK_ColorGREEN,
|
2018-08-16 14:17:03 +00:00
|
|
|
SK_ColorGRAY,
|
2011-12-10 19:36:56 +00:00
|
|
|
};
|
2016-03-09 02:50:00 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp));
|
2016-09-29 19:25:26 +00:00
|
|
|
canvas->drawPaint(paint);
|
|
|
|
return surface->makeImageSnapshot();
|
2011-12-10 19:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) {
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 24);
|
2018-12-12 23:10:38 +00:00
|
|
|
font.setEdging(SkFont::Edging::kAntiAlias);
|
2011-12-10 19:36:56 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
SkString str;
|
|
|
|
str.appendScalar(k[i]);
|
2019-05-07 19:38:46 +00:00
|
|
|
SkScalar width = font.measureText(str.c_str(), str.size(), SkTextEncoding::kUTF8);
|
2019-01-07 14:36:09 +00:00
|
|
|
canvas->drawString(str, x, y + font.getSize(), font, paint);
|
2011-12-12 22:01:06 +00:00
|
|
|
x += width + SkIntToScalar(10);
|
2011-12-10 19:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ArithmodeGM : public skiagm::GM {
|
2019-07-18 15:51:19 +00:00
|
|
|
SkString onShortName() override { return SkString("arithmode"); }
|
2011-12-10 19:36:56 +00:00
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
SkISize onISize() override { return {640, 572}; }
|
2011-12-10 19:36:56 +00:00
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2021-07-08 14:43:31 +00:00
|
|
|
constexpr int WW = 100,
|
|
|
|
HH = 32;
|
|
|
|
|
|
|
|
sk_sp<SkImage> src = make_src(WW, HH);
|
|
|
|
sk_sp<SkImage> dst = make_dst(WW, HH);
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> srcFilter = SkImageFilters::Image(src);
|
|
|
|
sk_sp<SkImageFilter> dstFilter = SkImageFilters::Image(dst);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar one = SK_Scalar1;
|
|
|
|
constexpr SkScalar K[] = {
|
2011-12-10 19:36:56 +00:00
|
|
|
0, 0, 0, 0,
|
2011-12-12 22:01:06 +00:00
|
|
|
0, 0, 0, one,
|
|
|
|
0, one, 0, 0,
|
|
|
|
0, 0, one, 0,
|
|
|
|
0, one, one, 0,
|
|
|
|
0, one, -one, 0,
|
|
|
|
0, one/2, one/2, 0,
|
|
|
|
0, one/2, one/2, one/4,
|
|
|
|
0, one/2, one/2, -one/4,
|
|
|
|
one/4, one/2, one/2, 0,
|
|
|
|
-one/4, one/2, one/2, 0,
|
2011-12-10 19:36:56 +00:00
|
|
|
};
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-12-10 19:36:56 +00:00
|
|
|
const SkScalar* k = K;
|
|
|
|
const SkScalar* stop = k + SK_ARRAY_COUNT(K);
|
2016-09-29 19:25:26 +00:00
|
|
|
const SkRect rect = SkRect::MakeWH(WW, HH);
|
|
|
|
SkScalar gap = SkIntToScalar(WW + 20);
|
2011-12-10 19:36:56 +00:00
|
|
|
while (k < stop) {
|
2016-09-29 19:25:26 +00:00
|
|
|
{
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->drawImage(src, 0, 0);
|
|
|
|
canvas->translate(gap, 0);
|
|
|
|
canvas->drawImage(dst, 0, 0);
|
|
|
|
canvas->translate(gap, 0);
|
|
|
|
SkPaint paint;
|
2019-08-02 19:21:23 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Arithmetic(k[0], k[1], k[2], k[3], true,
|
|
|
|
dstFilter, srcFilter, nullptr));
|
2016-09-29 19:25:26 +00:00
|
|
|
canvas->saveLayer(&rect, &paint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(gap, 0);
|
|
|
|
show_k_text(canvas, 0, 0, k);
|
|
|
|
}
|
|
|
|
|
2011-12-10 19:36:56 +00:00
|
|
|
k += 4;
|
2016-09-29 19:25:26 +00:00
|
|
|
canvas->translate(0, HH + 12);
|
2011-12-10 19:36:56 +00:00
|
|
|
}
|
2015-10-19 21:41:11 +00:00
|
|
|
|
|
|
|
// Draw two special cases to test enforcePMColor. In these cases, we
|
|
|
|
// draw the dst bitmap twice, the first time it is halved and inverted,
|
|
|
|
// leading to invalid premultiplied colors. If we enforcePMColor, these
|
|
|
|
// invalid values should be clamped, and will not contribute to the
|
|
|
|
// second draw.
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
const bool enforcePMColor = (i == 0);
|
2016-09-29 19:25:26 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->translate(gap, 0);
|
|
|
|
canvas->drawImage(dst, 0, 0);
|
|
|
|
canvas->translate(gap, 0);
|
|
|
|
|
|
|
|
sk_sp<SkImageFilter> bg =
|
2019-08-02 19:21:23 +00:00
|
|
|
SkImageFilters::Arithmetic(0, 0, -one / 2, 1, enforcePMColor, dstFilter,
|
|
|
|
nullptr, nullptr);
|
2016-09-29 19:25:26 +00:00
|
|
|
SkPaint p;
|
2019-08-02 19:21:23 +00:00
|
|
|
p.setImageFilter(SkImageFilters::Arithmetic(0, one / 2, -one, 1, true,
|
|
|
|
std::move(bg), dstFilter, nullptr));
|
2016-09-29 19:25:26 +00:00
|
|
|
canvas->saveLayer(&rect, &p);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(gap, 0);
|
|
|
|
|
|
|
|
// Label
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 24);
|
2016-09-29 19:25:26 +00:00
|
|
|
SkString str(enforcePMColor ? "enforcePM" : "no enforcePM");
|
2019-01-05 21:35:13 +00:00
|
|
|
canvas->drawString(str, 0, font.getSize(), font, SkPaint());
|
2016-09-29 19:25:26 +00:00
|
|
|
}
|
|
|
|
canvas->translate(0, HH + 12);
|
2015-10-19 21:41:11 +00:00
|
|
|
}
|
2011-12-10 19:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2011-12-10 19:36:56 +00:00
|
|
|
};
|
2021-07-08 14:43:31 +00:00
|
|
|
DEF_GM( return new ArithmodeGM; )
|
2011-12-10 19:36:56 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-07-08 14:43:31 +00:00
|
|
|
#include "include/effects/SkBlenders.h"
|
|
|
|
|
|
|
|
class Arithmode2GM : public skiagm::GM {
|
|
|
|
float fK1, fK2, fK3, fK4;
|
|
|
|
sk_sp<SkImage> fSrc, fDst, fChecker;
|
|
|
|
|
|
|
|
SkString onShortName() override { return SkString("arithmode_blender"); }
|
|
|
|
|
|
|
|
SkISize onISize() override { return {430, 430}; }
|
|
|
|
|
|
|
|
enum {
|
|
|
|
W = 200,
|
|
|
|
H = 200,
|
|
|
|
};
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
// need something interesting, in case we're drawn w/o calling animate()
|
|
|
|
fK1 = -0.25f;
|
|
|
|
fK2 = 0.25f;
|
|
|
|
fK3 = 0.25f;
|
|
|
|
fK4 = 0;
|
|
|
|
|
|
|
|
fSrc = make_src(W, H);
|
|
|
|
fDst = make_dst(W, H);
|
|
|
|
|
|
|
|
fChecker = ToolUtils::create_checkerboard_image(W, H, 0xFF999999, 0xFFCCCCCC, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onAnimate(double nanos) override {
|
|
|
|
double theta = nanos * 1e-6 * 0.001;
|
|
|
|
fK1 = sin(theta + 0) * 0.25;
|
|
|
|
fK2 = cos(theta + 1) * 0.25;
|
|
|
|
fK3 = sin(theta + 2) * 0.25;
|
|
|
|
fK4 = 0.5;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
const SkRect rect = SkRect::MakeWH(W, H);
|
|
|
|
|
|
|
|
canvas->drawImage(fSrc, 10, 10);
|
|
|
|
canvas->drawImage(fDst, 10, 10 + fSrc->height() + 10);
|
|
|
|
|
|
|
|
canvas->translate(10 + fSrc->width() + 10, 10);
|
|
|
|
canvas->drawImage(fChecker, 0, 0);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setBlender(SkBlenders::Arithmetic(fK1, fK2, fK3, fK4, true));
|
|
|
|
|
|
|
|
canvas->saveLayer(&rect, nullptr);
|
|
|
|
canvas->drawImage(fDst, 0, 0);
|
|
|
|
canvas->drawImage(fSrc, 0, 0, SkSamplingOptions(), &paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
using INHERITED = GM;
|
|
|
|
};
|
|
|
|
DEF_GM( return new Arithmode2GM; )
|