| 1 |
/* |
| 2 |
* Copyright 2013 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBlendMode.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkColorFilter.h" |
| 13 |
#include "include/core/SkColorSpace.h" |
| 14 |
#include "include/core/SkFont.h" |
| 15 |
#include "include/core/SkFontTypes.h" |
| 16 |
#include "include/core/SkPaint.h" |
| 17 |
#include "include/core/SkPoint.h" |
| 18 |
#include "include/core/SkRect.h" |
| 19 |
#include "include/core/SkRefCnt.h" |
| 20 |
#include "include/core/SkScalar.h" |
| 21 |
#include "include/core/SkShader.h" |
| 22 |
#include "include/core/SkSize.h" |
| 23 |
#include "include/core/SkString.h" |
| 24 |
#include "include/core/SkTileMode.h" |
| 25 |
#include "include/core/SkTypeface.h" |
| 26 |
#include "include/core/SkTypes.h" |
| 27 |
#include "include/effects/SkGradient.h" |
| 28 |
#include "include/effects/SkLumaColorFilter.h" |
| 29 |
#include "include/effects/SkRuntimeEffect.h" |
| 30 |
#include "src/core/SkColorFilterPriv.h" |
| 31 |
#include "tools/DecodeUtils.h" |
| 32 |
#include "tools/Resources.h" |
| 33 |
#include "tools/ToolUtils.h" |
| 34 |
#include "tools/fonts/FontToolUtils.h" |
| 35 |
|
| 36 |
#include <string.h> |
| 37 |
|
| 38 |
static const float kSize = 80; |
| 39 |
static const float kInset = 10; |
| 40 |
static const SkColor4f kColor1 = { 1, 1, 0, 1}; |
| 41 |
static const SkColor4f kColor2 = {0x82/255.f, 1, 0, 1}; |
| 42 |
|
| 43 |
static void draw_label(SkCanvas* canvas, const char* label, |
| 44 |
const SkPoint& offset) { |
| 45 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 46 |
font.setEdging(SkFont::Edging::kAlias); |
| 47 |
|
| 48 |
size_t len = strlen(label); |
| 49 |
|
| 50 |
SkScalar width = font.measureText(label, len, SkTextEncoding::kUTF8); |
| 51 |
canvas->drawSimpleText(label, len, SkTextEncoding::kUTF8, offset.x() - width / 2, offset.y(), |
| 52 |
font, SkPaint()); |
| 53 |
} |
| 54 |
|
| 55 |
static void draw_scene(SkCanvas* canvas, const sk_sp<SkColorFilter>& filter, SkBlendMode mode, |
| 56 |
const sk_sp<SkShader>& s1, const sk_sp<SkShader>& s2) { |
| 57 |
SkPaint paint; |
| 58 |
paint.setAntiAlias(true); |
| 59 |
SkRect r, c, bounds = SkRect::MakeWH(kSize, kSize); |
| 60 |
|
| 61 |
c = bounds; |
| 62 |
c.fRight = bounds.centerX(); |
| 63 |
paint.setARGB(0x20, 0, 0, 0xff); |
| 64 |
canvas->drawRect(bounds, paint); |
| 65 |
|
| 66 |
canvas->saveLayer(&bounds, nullptr); |
| 67 |
|
| 68 |
r = bounds; |
| 69 |
r.inset(kInset, 0); |
| 70 |
paint.setShader(s1); |
| 71 |
paint.setColor(s1 ? SkColors::kBlack : kColor1.withAlphaByte(0x80)); |
| 72 |
canvas->drawOval(r, paint); |
| 73 |
if (!s1) { |
| 74 |
canvas->save(); |
| 75 |
canvas->clipRect(c); |
| 76 |
paint.setColor(kColor1); |
| 77 |
canvas->drawOval(r, paint); |
| 78 |
canvas->restore(); |
| 79 |
} |
| 80 |
|
| 81 |
SkPaint xferPaint; |
| 82 |
xferPaint.setBlendMode(mode); |
| 83 |
canvas->saveLayer(&bounds, &xferPaint); |
| 84 |
|
| 85 |
r = bounds; |
| 86 |
r.inset(0, kInset); |
| 87 |
paint.setShader(s2); |
| 88 |
paint.setColor(s2 ? SkColors::kBlack : kColor2.withAlphaByte(0x80)); |
| 89 |
paint.setColorFilter(filter); |
| 90 |
canvas->drawOval(r, paint); |
| 91 |
if (!s2) { |
| 92 |
canvas->save(); |
| 93 |
canvas->clipRect(c); |
| 94 |
paint.setColor(kColor2); |
| 95 |
canvas->drawOval(r, paint); |
| 96 |
canvas->restore(); |
| 97 |
} |
| 98 |
|
| 99 |
canvas->restore(); |
| 100 |
canvas->restore(); |
| 101 |
} |
| 102 |
|
| 103 |
class LumaFilterGM : public skiagm::GM { |
| 104 |
protected: |
| 105 |
void onOnceBeforeDraw() override { |
| 106 |
SkColor4f g1Colors[] = { kColor1, kColor1.withAlphaByte(0x20) }; |
| 107 |
SkColor4f g2Colors[] = { kColor2, kColor2.withAlphaByte(0x20) }; |
| 108 |
SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } }; |
| 109 |
SkPoint g2Points[] = { { 0, 0 }, { kSize, 0 } }; |
| 110 |
SkScalar pos[] = { 0.2f, 1.0f }; |
| 111 |
|
| 112 |
fFilter = SkLumaColorFilter::Make(); |
| 113 |
fGr1 = SkShaders::LinearGradient(g1Points, {{g1Colors, pos, SkTileMode::kClamp}, {}}); |
| 114 |
fGr2 = SkShaders::LinearGradient(g2Points, {{g2Colors, pos, SkTileMode::kClamp}, {}}); |
| 115 |
} |
| 116 |
|
| 117 |
SkString getName() const override { return SkString("lumafilter"); } |
| 118 |
|
| 119 |
SkISize getISize() override { return SkISize::Make(600, 420); } |
| 120 |
|
| 121 |
void onDraw(SkCanvas* canvas) override { |
| 122 |
SkBlendMode modes[] = { |
| 123 |
SkBlendMode::kSrcOver, |
| 124 |
SkBlendMode::kDstOver, |
| 125 |
SkBlendMode::kSrcATop, |
| 126 |
SkBlendMode::kDstATop, |
| 127 |
SkBlendMode::kSrcIn, |
| 128 |
SkBlendMode::kDstIn, |
| 129 |
}; |
| 130 |
struct { |
| 131 |
const sk_sp<SkShader>& fShader1; |
| 132 |
const sk_sp<SkShader>& fShader2; |
| 133 |
} shaders[] = { |
| 134 |
{ nullptr, nullptr }, |
| 135 |
{ nullptr, fGr2 }, |
| 136 |
{ fGr1, nullptr }, |
| 137 |
{ fGr1, fGr2 }, |
| 138 |
}; |
| 139 |
|
| 140 |
SkScalar gridStep = kSize + 2 * kInset; |
| 141 |
for (size_t i = 0; i < std::size(modes); ++i) { |
| 142 |
draw_label(canvas, SkBlendMode_Name(modes[i]), |
| 143 |
SkPoint::Make(gridStep * (0.5f + i), 20)); |
| 144 |
} |
| 145 |
|
| 146 |
for (size_t i = 0; i < std::size(shaders); ++i) { |
| 147 |
canvas->save(); |
| 148 |
canvas->translate(kInset, gridStep * i + 30); |
| 149 |
for (size_t m = 0; m < std::size(modes); ++m) { |
| 150 |
draw_scene(canvas, fFilter, modes[m], shaders[i].fShader1, shaders[i].fShader2); |
| 151 |
canvas->translate(gridStep, 0); |
| 152 |
} |
| 153 |
canvas->restore(); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
private: |
| 158 |
sk_sp<SkColorFilter> fFilter; |
| 159 |
sk_sp<SkShader> fGr1, fGr2; |
| 160 |
|
| 161 |
using INHERITED = skiagm::GM; |
| 162 |
}; |
| 163 |
|
| 164 |
////////////////////////////////////////////////////////////////////////////// |
| 165 |
|
| 166 |
DEF_GM(return new LumaFilterGM;) |
| 167 |
|
| 168 |
DEF_SIMPLE_GM(AlternateLuma, canvas, 384,128) { |
| 169 |
sk_sp<SkImage> img = ToolUtils::GetResourceAsImage("images/mandrill_128.png"); |
| 170 |
if (!img) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
// Normal luma colorfilter on the left. |
| 175 |
SkPaint paint; |
| 176 |
paint.setColorFilter(SkLumaColorFilter::Make()); |
| 177 |
canvas->drawImage(img, 0,0, SkFilterMode::kNearest, &paint); |
| 178 |
canvas->translate(128,0); |
| 179 |
|
| 180 |
// Original image in the middle for reference. |
| 181 |
canvas->drawImage(img, 0,0); |
| 182 |
canvas->translate(128,0); |
| 183 |
|
| 184 |
// Here, RGB holds CIE XYZ. Splatting the G (Y) channel should result in (near) greyscale. |
| 185 |
auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{ |
| 186 |
"half4 main(half4 inColor) { return inColor.ggga; }"}); |
| 187 |
SkASSERT(effect && err.isEmpty()); |
| 188 |
|
| 189 |
sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty()); |
| 190 |
SkASSERT(filter); |
| 191 |
|
| 192 |
SkAlphaType unpremul = kUnpremul_SkAlphaType; |
| 193 |
paint.setColorFilter(SkColorFilterPriv::WithWorkingFormat(std::move(filter), |
| 194 |
&SkNamedTransferFn::kLinear, |
| 195 |
&SkNamedGamut::kXYZ, |
| 196 |
&unpremul)); |
| 197 |
canvas->drawImage(img, 0,0, SkFilterMode::kNearest, &paint); |
| 198 |
} |