skia2/gm/lumafilter.cpp
reed d053ce9c54 Reland of [2] of "switch colorfilters to sk_sp (patchset #11 id:200001 of https://codereview.chromium.o… (patchset #1 id:1 of https://codereview.chromium.org/1821103004/ )
Reason for revert:
guard has now landed in chrome

Original issue's description:
> Revert of Revert[2] of "switch colorfilters to sk_sp (patchset #11 id:200001 of https://codereview.chromium.o… (patchset #3 id:40001 of https://codereview.chromium.org/1825073002/ )
>
> Reason for revert:
> CreateModeFilter not compiling
>
> Original issue's description:
> > Revert[2] of "switch colorfilters to sk_sp (patchset #11 id:200001 of https://codereview.chromium.org/1822623002/ )"
> >
> > Fixed legacy withColorFilter to call new(er) make method
> >
> > This reverts commit 1eb81db650.
> >
> > BUG=skia:
> > GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1825073002
> >
> > TBR=
> >
> > Committed: https://skia.googlesource.com/skia/+/4c9776b046dd5e9e46e2d1ce35154855c8fcb381
>
> TBR=
> # Skipping CQ checks because original CL landed less than 1 days ago.
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
> BUG=skia:
>
> Committed: https://skia.googlesource.com/skia/+/d6889293dd0942f27f9593f679722c956831f2c4

TBR=
# Not skipping CQ checks because original CL landed more than 1 days ago.
BUG=skia:

Review URL: https://codereview.chromium.org/1827433002
2016-03-22 10:17:23 -07:00

149 lines
4.6 KiB
C++

/*
* Copyright 2013 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"
#include "SkCanvas.h"
#include "SkGradientShader.h"
#include "SkLumaColorFilter.h"
static SkScalar kSize = 80;
static SkScalar kInset = 10;
static SkColor kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0);
static SkColor kColor2 = SkColorSetARGB(0xff, 0x82, 0xff, 0);
static void draw_label(SkCanvas* canvas, const char* label,
const SkPoint& offset) {
SkPaint paint;
sk_tool_utils::set_portable_typeface(&paint);
size_t len = strlen(label);
SkScalar width = paint.measureText(label, len);
canvas->drawText(label, len, offset.x() - width / 2, offset.y(),
paint);
}
static void draw_scene(SkCanvas* canvas, const sk_sp<SkColorFilter>& filter, SkXfermode::Mode mode,
const sk_sp<SkShader>& s1, const sk_sp<SkShader>& s2) {
SkPaint paint;
paint.setAntiAlias(true);
SkRect r, c, bounds = SkRect::MakeWH(kSize, kSize);
c = bounds;
c.fRight = bounds.centerX();
paint.setARGB(0x20, 0, 0, 0xff);
canvas->drawRect(bounds, paint);
canvas->saveLayer(&bounds, nullptr);
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;
xferPaint.setXfermodeMode(mode);
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 {
public:
LumaFilterGM() {
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 };
fFilter = SkLumaColorFilter::Make();
fGr1 = SkGradientShader::MakeLinear(g1Points, g1Colors, pos, SK_ARRAY_COUNT(g1Colors),
SkShader::kClamp_TileMode);
fGr2 = SkGradientShader::MakeLinear(g2Points, g2Colors, pos, SK_ARRAY_COUNT(g2Colors),
SkShader::kClamp_TileMode);
}
protected:
SkString onShortName() override {
return SkString("lumafilter");
}
SkISize onISize() override {
return SkISize::Make(600, 420);
}
void onDraw(SkCanvas* canvas) override {
SkXfermode::Mode modes[] = { SkXfermode::kSrcOver_Mode,
SkXfermode::kDstOver_Mode,
SkXfermode::kSrcATop_Mode,
SkXfermode::kDstATop_Mode,
SkXfermode::kSrcIn_Mode,
SkXfermode::kDstIn_Mode,
};
struct {
const sk_sp<SkShader>& fShader1;
const sk_sp<SkShader>& fShader2;
} shaders[] = {
{ nullptr, nullptr },
{ nullptr, fGr2 },
{ fGr1, nullptr },
{ fGr1, fGr2 },
};
SkScalar gridStep = kSize + 2 * kInset;
for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
draw_label(canvas, SkXfermode::ModeName(modes[i]),
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:
sk_sp<SkColorFilter> fFilter;
sk_sp<SkShader> fGr1, fGr2;
typedef skiagm::GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
DEF_GM(return new LumaFilterGM;)