| 1 |
/* |
| 2 |
* Copyright 2015 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/SkFont.h" |
| 13 |
#include "include/core/SkImageInfo.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkPoint.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkRefCnt.h" |
| 18 |
#include "include/core/SkScalar.h" |
| 19 |
#include "include/core/SkShader.h" |
| 20 |
#include "include/core/SkSize.h" |
| 21 |
#include "include/core/SkString.h" |
| 22 |
#include "include/core/SkSurface.h" |
| 23 |
#include "include/core/SkTileMode.h" |
| 24 |
#include "include/core/SkTypeface.h" |
| 25 |
#include "include/core/SkTypes.h" |
| 26 |
#include "include/effects/SkGradient.h" |
| 27 |
#include "tools/ToolUtils.h" |
| 28 |
#include "tools/fonts/FontToolUtils.h" |
| 29 |
|
| 30 |
namespace skiagm { |
| 31 |
|
| 32 |
constexpr int kColWidth = 180; |
| 33 |
constexpr int kNumCols = 4; |
| 34 |
constexpr int kWidth = kColWidth * kNumCols; |
| 35 |
constexpr int kHeight = 750; |
| 36 |
|
| 37 |
static sk_sp<SkShader> make_shader(const SkRect& bounds) { |
| 38 |
const SkPoint pts[] = { |
| 39 |
{ bounds.left(), bounds.top() }, |
| 40 |
{ bounds.right(), bounds.bottom() }, |
| 41 |
}; |
| 42 |
const SkColor4f colors[] = { |
| 43 |
SkColors::kRed, SkColors::kGreen, |
| 44 |
}; |
| 45 |
return SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kRepeat}, {}}); |
| 46 |
} |
| 47 |
|
| 48 |
class LcdBlendGM : public skiagm::GM { |
| 49 |
public: |
| 50 |
LcdBlendGM() { |
| 51 |
const int kPointSize = 25; |
| 52 |
fTextHeight = SkIntToScalar(kPointSize); |
| 53 |
} |
| 54 |
|
| 55 |
protected: |
| 56 |
SkString getName() const override { return SkString("lcdblendmodes"); } |
| 57 |
|
| 58 |
void onOnceBeforeDraw() override { |
| 59 |
fCheckerboard = ToolUtils::create_checkerboard_shader(SK_ColorBLACK, SK_ColorWHITE, 4); |
| 60 |
} |
| 61 |
|
| 62 |
SkISize getISize() override { return SkISize::Make(kWidth, kHeight); } |
| 63 |
|
| 64 |
void onDraw(SkCanvas* canvas) override { |
| 65 |
SkPaint p; |
| 66 |
p.setAntiAlias(false); |
| 67 |
p.setStyle(SkPaint::kFill_Style); |
| 68 |
p.setShader(fCheckerboard); |
| 69 |
SkRect r = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); |
| 70 |
canvas->drawRect(r, p); |
| 71 |
|
| 72 |
SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
| 73 |
SkSurfaceProps props = SkSurfaceProps(0, kRGB_H_SkPixelGeometry); |
| 74 |
auto surface(ToolUtils::makeSurface(canvas, info, &props)); |
| 75 |
|
| 76 |
SkCanvas* surfCanvas = surface->getCanvas(); |
| 77 |
this->drawColumn(surfCanvas, SK_ColorBLACK, SK_ColorWHITE, false); |
| 78 |
surfCanvas->translate(SkIntToScalar(kColWidth), 0); |
| 79 |
this->drawColumn(surfCanvas, SK_ColorWHITE, SK_ColorBLACK, false); |
| 80 |
surfCanvas->translate(SkIntToScalar(kColWidth), 0); |
| 81 |
this->drawColumn(surfCanvas, SK_ColorGREEN, SK_ColorMAGENTA, false); |
| 82 |
surfCanvas->translate(SkIntToScalar(kColWidth), 0); |
| 83 |
this->drawColumn(surfCanvas, SK_ColorCYAN, SK_ColorMAGENTA, true); |
| 84 |
|
| 85 |
SkPaint surfPaint; |
| 86 |
surfPaint.setBlendMode(SkBlendMode::kSrcOver); |
| 87 |
surface->draw(canvas, 0, 0, SkSamplingOptions(), &surfPaint); |
| 88 |
} |
| 89 |
|
| 90 |
void drawColumn(SkCanvas* canvas, SkColor backgroundColor, SkColor textColor, bool useGrad) { |
| 91 |
const SkBlendMode gModes[] = { |
| 92 |
SkBlendMode::kClear, |
| 93 |
SkBlendMode::kSrc, |
| 94 |
SkBlendMode::kDst, |
| 95 |
SkBlendMode::kSrcOver, |
| 96 |
SkBlendMode::kDstOver, |
| 97 |
SkBlendMode::kSrcIn, |
| 98 |
SkBlendMode::kDstIn, |
| 99 |
SkBlendMode::kSrcOut, |
| 100 |
SkBlendMode::kDstOut, |
| 101 |
SkBlendMode::kSrcATop, |
| 102 |
SkBlendMode::kDstATop, |
| 103 |
SkBlendMode::kXor, |
| 104 |
SkBlendMode::kPlus, |
| 105 |
SkBlendMode::kModulate, |
| 106 |
SkBlendMode::kScreen, |
| 107 |
SkBlendMode::kOverlay, |
| 108 |
SkBlendMode::kDarken, |
| 109 |
SkBlendMode::kLighten, |
| 110 |
SkBlendMode::kColorDodge, |
| 111 |
SkBlendMode::kColorBurn, |
| 112 |
SkBlendMode::kHardLight, |
| 113 |
SkBlendMode::kSoftLight, |
| 114 |
SkBlendMode::kDifference, |
| 115 |
SkBlendMode::kExclusion, |
| 116 |
SkBlendMode::kMultiply, |
| 117 |
SkBlendMode::kHue, |
| 118 |
SkBlendMode::kSaturation, |
| 119 |
SkBlendMode::kColor, |
| 120 |
SkBlendMode::kLuminosity, |
| 121 |
}; |
| 122 |
// Draw background rect |
| 123 |
SkPaint backgroundPaint; |
| 124 |
backgroundPaint.setColor(backgroundColor); |
| 125 |
canvas->drawRect(SkRect::MakeIWH(kColWidth, kHeight), backgroundPaint); |
| 126 |
SkScalar y = fTextHeight; |
| 127 |
for (size_t m = 0; m < std::size(gModes); m++) { |
| 128 |
SkPaint paint; |
| 129 |
paint.setColor(textColor); |
| 130 |
paint.setBlendMode(gModes[m]); |
| 131 |
SkFont font(ToolUtils::DefaultPortableTypeface(), fTextHeight); |
| 132 |
font.setSubpixel(true); |
| 133 |
font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 134 |
if (useGrad) { |
| 135 |
SkRect r; |
| 136 |
r.setXYWH(0, y - fTextHeight, SkIntToScalar(kColWidth), fTextHeight); |
| 137 |
paint.setShader(make_shader(r)); |
| 138 |
} |
| 139 |
SkString string(SkBlendMode_Name(gModes[m])); |
| 140 |
canvas->drawString(string, 0, y, font, paint); |
| 141 |
y+=fTextHeight; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
private: |
| 146 |
SkScalar fTextHeight; |
| 147 |
sk_sp<SkShader> fCheckerboard; |
| 148 |
using INHERITED = skiagm::GM; |
| 149 |
}; |
| 150 |
|
| 151 |
////////////////////////////////////////////////////////////////////////////// |
| 152 |
|
| 153 |
DEF_GM( return new LcdBlendGM; ) |
| 154 |
} // namespace skiagm |