2013-10-07 18:00:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 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-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBlendMode.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontTypes.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"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#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"
|
|
|
|
#include "include/effects/SkLumaColorFilter.h"
|
2021-02-09 21:34:29 +00:00
|
|
|
#include "include/effects/SkRuntimeEffect.h"
|
2021-11-03 18:12:01 +00:00
|
|
|
#include "src/core/SkColorFilterPriv.h"
|
2021-02-09 21:34:29 +00:00
|
|
|
#include "tools/Resources.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2013-10-07 18:00:17 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-10-07 18:00:17 +00:00
|
|
|
static SkScalar kSize = 80;
|
|
|
|
static SkScalar kInset = 10;
|
|
|
|
static SkColor kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0);
|
2015-07-17 20:20:48 +00:00
|
|
|
static SkColor kColor2 = SkColorSetARGB(0xff, 0x82, 0xff, 0);
|
2013-10-07 18:00:17 +00:00
|
|
|
|
|
|
|
static void draw_label(SkCanvas* canvas, const char* label,
|
|
|
|
const SkPoint& offset) {
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface());
|
2018-12-22 03:22:31 +00:00
|
|
|
font.setEdging(SkFont::Edging::kAlias);
|
|
|
|
|
2013-10-07 18:00:17 +00:00
|
|
|
size_t len = strlen(label);
|
|
|
|
|
2019-05-07 19:38:46 +00:00
|
|
|
SkScalar width = font.measureText(label, len, SkTextEncoding::kUTF8);
|
|
|
|
canvas->drawSimpleText(label, len, SkTextEncoding::kUTF8, offset.x() - width / 2, offset.y(),
|
2018-12-22 03:22:31 +00:00
|
|
|
font, SkPaint());
|
2013-10-07 18:00:17 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
static void draw_scene(SkCanvas* canvas, const sk_sp<SkColorFilter>& filter, SkBlendMode mode,
|
2016-03-13 21:13:58 +00:00
|
|
|
const sk_sp<SkShader>& s1, const sk_sp<SkShader>& s2) {
|
2013-10-07 18:00:17 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
SkRect r, c, bounds = SkRect::MakeWH(kSize, kSize);
|
|
|
|
|
|
|
|
c = bounds;
|
|
|
|
c.fRight = bounds.centerX();
|
2013-10-10 20:13:51 +00:00
|
|
|
paint.setARGB(0x20, 0, 0, 0xff);
|
2013-10-07 18:00:17 +00:00
|
|
|
canvas->drawRect(bounds, paint);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(&bounds, nullptr);
|
2013-10-07 18:00:17 +00:00
|
|
|
|
|
|
|
r = bounds;
|
|
|
|
r.inset(kInset, 0);
|
|
|
|
paint.setShader(s1);
|
|
|
|
paint.setColor(s1 ? SK_ColorBLACK : SkColorSetA(kColor1, 0x80));
|
|
|
|
canvas->drawOval(r, paint);
|
|
|
|
if (!s1) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(c);
|
|
|
|
paint.setColor(kColor1);
|
|
|
|
canvas->drawOval(r, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint xferPaint;
|
2016-10-06 00:33:02 +00:00
|
|
|
xferPaint.setBlendMode(mode);
|
2013-10-07 18:00:17 +00:00
|
|
|
canvas->saveLayer(&bounds, &xferPaint);
|
|
|
|
|
|
|
|
r = bounds;
|
|
|
|
r.inset(0, kInset);
|
|
|
|
paint.setShader(s2);
|
|
|
|
paint.setColor(s2 ? SK_ColorBLACK : SkColorSetA(kColor2, 0x80));
|
|
|
|
paint.setColorFilter(filter);
|
|
|
|
canvas->drawOval(r, paint);
|
|
|
|
if (!s2) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(c);
|
|
|
|
paint.setColor(kColor2);
|
|
|
|
canvas->drawOval(r, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
class LumaFilterGM : public skiagm::GM {
|
2020-11-05 15:05:19 +00:00
|
|
|
protected:
|
|
|
|
void onOnceBeforeDraw() override {
|
2013-10-07 18:00:17 +00:00
|
|
|
SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) };
|
|
|
|
SkColor g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) };
|
|
|
|
SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } };
|
|
|
|
SkPoint g2Points[] = { { 0, 0 }, { kSize, 0 } };
|
|
|
|
SkScalar pos[] = { 0.2f, 1.0f };
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
fFilter = SkLumaColorFilter::Make();
|
2016-03-13 21:13:58 +00:00
|
|
|
fGr1 = SkGradientShader::MakeLinear(g1Points, g1Colors, pos, SK_ARRAY_COUNT(g1Colors),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp);
|
2016-03-13 21:13:58 +00:00
|
|
|
fGr2 = SkGradientShader::MakeLinear(g2Points, g2Colors, pos, SK_ARRAY_COUNT(g2Colors),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp);
|
2013-10-07 18:00:17 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-10-07 18:00:17 +00:00
|
|
|
return SkString("lumafilter");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2013-10-07 18:00:17 +00:00
|
|
|
return SkISize::Make(600, 420);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-10-06 00:33:02 +00:00
|
|
|
SkBlendMode modes[] = {
|
|
|
|
SkBlendMode::kSrcOver,
|
|
|
|
SkBlendMode::kDstOver,
|
|
|
|
SkBlendMode::kSrcATop,
|
|
|
|
SkBlendMode::kDstATop,
|
|
|
|
SkBlendMode::kSrcIn,
|
|
|
|
SkBlendMode::kDstIn,
|
|
|
|
};
|
2013-10-07 18:00:17 +00:00
|
|
|
struct {
|
2016-03-13 21:13:58 +00:00
|
|
|
const sk_sp<SkShader>& fShader1;
|
|
|
|
const sk_sp<SkShader>& fShader2;
|
2013-10-07 18:00:17 +00:00
|
|
|
} shaders[] = {
|
2015-08-27 14:41:13 +00:00
|
|
|
{ nullptr, nullptr },
|
|
|
|
{ nullptr, fGr2 },
|
|
|
|
{ fGr1, nullptr },
|
2013-10-07 18:00:17 +00:00
|
|
|
{ fGr1, fGr2 },
|
|
|
|
};
|
|
|
|
|
|
|
|
SkScalar gridStep = kSize + 2 * kInset;
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
|
2016-11-15 21:44:34 +00:00
|
|
|
draw_label(canvas, SkBlendMode_Name(modes[i]),
|
2013-10-07 18:00:17 +00:00
|
|
|
SkPoint::Make(gridStep * (0.5f + i), 20));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(shaders); ++i) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(kInset, gridStep * i + 30);
|
|
|
|
for (size_t m = 0; m < SK_ARRAY_COUNT(modes); ++m) {
|
|
|
|
draw_scene(canvas, fFilter, modes[m], shaders[i].fShader1,
|
|
|
|
shaders[i].fShader2);
|
|
|
|
canvas->translate(gridStep, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-03-22 17:17:23 +00:00
|
|
|
sk_sp<SkColorFilter> fFilter;
|
|
|
|
sk_sp<SkShader> fGr1, fGr2;
|
2013-10-07 18:00:17 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2013-10-07 18:00:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new LumaFilterGM;)
|
2021-02-09 21:34:29 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(AlternateLuma, canvas, 384,128) {
|
|
|
|
sk_sp<SkImage> img = GetResourceAsImage("images/mandrill_128.png");
|
|
|
|
if (!img) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normal luma colorfilter on the left.
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColorFilter(SkLumaColorFilter::Make());
|
|
|
|
canvas->drawImage(img, 0,0, SkSamplingOptions{}, &paint);
|
|
|
|
canvas->translate(128,0);
|
|
|
|
|
|
|
|
// Original image in the middle for reference.
|
|
|
|
canvas->drawImage(img, 0,0);
|
|
|
|
canvas->translate(128,0);
|
|
|
|
|
2021-07-09 19:29:10 +00:00
|
|
|
// Here, RGB holds CIE XYZ. Splatting the G (Y) channel should result in (near) greyscale.
|
2021-04-15 17:52:43 +00:00
|
|
|
auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
|
2021-07-09 19:29:10 +00:00
|
|
|
"half4 main(half4 inColor) { return inColor.ggga; }"});
|
2021-02-09 21:34:29 +00:00
|
|
|
SkASSERT(effect && err.isEmpty());
|
|
|
|
|
2021-04-12 21:17:19 +00:00
|
|
|
sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
|
2021-02-09 21:34:29 +00:00
|
|
|
SkASSERT(filter);
|
|
|
|
|
|
|
|
SkAlphaType unpremul = kUnpremul_SkAlphaType;
|
2021-11-03 18:12:01 +00:00
|
|
|
paint.setColorFilter(SkColorFilterPriv::WithWorkingFormat(std::move(filter),
|
|
|
|
&SkNamedTransferFn::kLinear,
|
|
|
|
&SkNamedGamut::kXYZ,
|
|
|
|
&unpremul));
|
2021-02-09 21:34:29 +00:00
|
|
|
canvas->drawImage(img, 0,0, SkSamplingOptions{}, &paint);
|
|
|
|
}
|