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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2017-01-09 15:48:23 +00:00
|
|
|
#include "SkArithmeticImageFilter.h"
|
2011-12-10 19:36:56 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkColorPriv.h"
|
2016-09-29 19:25:26 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkImageSource.h"
|
2011-12-10 19:36:56 +00:00
|
|
|
#include "SkShader.h"
|
2016-09-29 19:25:26 +00:00
|
|
|
#include "SkSurface.h"
|
2011-12-10 19:36:56 +00:00
|
|
|
|
|
|
|
#define WW 100
|
|
|
|
#define HH 32
|
|
|
|
|
2016-09-29 19:25:26 +00:00
|
|
|
static sk_sp<SkImage> make_src() {
|
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(WW, HH));
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
2011-12-10 19:36:56 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
2012-01-06 19:22:11 +00:00
|
|
|
SkPoint pts[] = { {0, 0}, {SkIntToScalar(WW), SkIntToScalar(HH)} };
|
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),
|
|
|
|
SkShader::kClamp_TileMode));
|
2016-09-29 19:25:26 +00:00
|
|
|
canvas->drawPaint(paint);
|
|
|
|
return surface->makeImageSnapshot();
|
2011-12-10 19:36:56 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 19:25:26 +00:00
|
|
|
static sk_sp<SkImage> make_dst() {
|
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(WW, HH));
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
2011-12-10 19:36:56 +00:00
|
|
|
SkPaint paint;
|
2012-01-06 19:22:11 +00:00
|
|
|
SkPoint pts[] = { {0, SkIntToScalar(HH)}, {SkIntToScalar(WW), 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,
|
|
|
|
sk_tool_utils::color_to_565(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),
|
|
|
|
SkShader::kClamp_TileMode));
|
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[]) {
|
|
|
|
SkPaint paint;
|
2011-12-12 22:01:06 +00:00
|
|
|
paint.setTextSize(SkIntToScalar(24));
|
2011-12-10 19:36:56 +00:00
|
|
|
paint.setAntiAlias(true);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2011-12-10 19:36:56 +00:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
SkString str;
|
|
|
|
str.appendScalar(k[i]);
|
|
|
|
SkScalar width = paint.measureText(str.c_str(), str.size());
|
2011-12-12 22:01:06 +00:00
|
|
|
canvas->drawText(str.c_str(), str.size(), x, y + paint.getTextSize(), paint);
|
|
|
|
x += width + SkIntToScalar(10);
|
2011-12-10 19:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ArithmodeGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
ArithmodeGM () {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("arithmode");
|
|
|
|
}
|
|
|
|
|
2015-10-19 21:41:11 +00:00
|
|
|
virtual SkISize onISize() { return SkISize::Make(640, 572); }
|
2011-12-10 19:36:56 +00:00
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
2016-09-29 19:25:26 +00:00
|
|
|
sk_sp<SkImage> src = make_src();
|
|
|
|
sk_sp<SkImage> dst = make_dst();
|
|
|
|
sk_sp<SkImageFilter> srcFilter = SkImageSource::Make(src);
|
|
|
|
sk_sp<SkImageFilter> dstFilter = SkImageSource::Make(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;
|
2017-01-09 15:48:23 +00:00
|
|
|
paint.setImageFilter(SkArithmeticImageFilter::Make(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 =
|
2017-01-09 15:48:23 +00:00
|
|
|
SkArithmeticImageFilter::Make(0, 0, -one / 2, 1, enforcePMColor, dstFilter);
|
2016-09-29 19:25:26 +00:00
|
|
|
SkPaint p;
|
2017-01-09 15:48:23 +00:00
|
|
|
p.setImageFilter(SkArithmeticImageFilter::Make(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
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setTextSize(SkIntToScalar(24));
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
|
|
|
SkString str(enforcePMColor ? "enforcePM" : "no enforcePM");
|
|
|
|
canvas->drawText(str.c_str(), str.size(), 0, paint.getTextSize(), paint);
|
|
|
|
}
|
|
|
|
canvas->translate(0, HH + 12);
|
2015-10-19 21:41:11 +00:00
|
|
|
}
|
2011-12-10 19:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-12-10 21:31:59 +00:00
|
|
|
DEF_GM( return new ArithmodeGM; )
|