2015-09-15 16:31:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-03-29 16:03:52 +00:00
|
|
|
/*
|
2015-09-15 16:31:40 +00:00
|
|
|
* Tests text rendering with LCD and the various blend modes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2015-09-15 16:31:40 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
2015-09-15 20:13:50 +00:00
|
|
|
#include "SkSurface.h"
|
2015-09-15 16:31:40 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr int kColWidth = 180;
|
|
|
|
constexpr int kNumCols = 4;
|
|
|
|
constexpr int kWidth = kColWidth * kNumCols;
|
|
|
|
constexpr int kHeight = 750;
|
2015-09-15 16:31:40 +00:00
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> make_shader(const SkRect& bounds) {
|
2015-09-15 16:31:40 +00:00
|
|
|
const SkPoint pts[] = {
|
|
|
|
{ bounds.left(), bounds.top() },
|
|
|
|
{ bounds.right(), bounds.bottom() },
|
|
|
|
};
|
|
|
|
const SkColor colors[] = {
|
|
|
|
SK_ColorRED, SK_ColorGREEN,
|
|
|
|
};
|
2016-03-13 21:13:58 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
|
|
|
|
SkShader::kRepeat_TileMode);
|
2015-09-15 16:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class LcdBlendGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
LcdBlendGM() {
|
|
|
|
const int kPointSize = 25;
|
|
|
|
fTextHeight = SkIntToScalar(kPointSize);
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-15 16:31:40 +00:00
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
2015-09-15 20:13:50 +00:00
|
|
|
return SkString("lcdblendmodes");
|
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
2016-03-09 02:50:00 +00:00
|
|
|
fCheckerboard = sk_tool_utils::create_checkerboard_shader(SK_ColorBLACK, SK_ColorWHITE, 4);
|
2015-09-15 16:31:40 +00:00
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-15 16:31:40 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-15 16:31:40 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2015-09-15 20:13:50 +00:00
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(false);
|
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
|
|
|
p.setShader(fCheckerboard);
|
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
|
2018-09-05 18:41:40 +00:00
|
|
|
auto surface(sk_tool_utils::makeSurface(canvas, info));
|
2015-09-15 20:13:50 +00:00
|
|
|
|
|
|
|
SkCanvas* surfCanvas = surface->getCanvas();
|
|
|
|
this->drawColumn(surfCanvas, SK_ColorBLACK, SK_ColorWHITE, false);
|
|
|
|
surfCanvas->translate(SkIntToScalar(kColWidth), 0);
|
|
|
|
this->drawColumn(surfCanvas, SK_ColorWHITE, SK_ColorBLACK, false);
|
|
|
|
surfCanvas->translate(SkIntToScalar(kColWidth), 0);
|
|
|
|
this->drawColumn(surfCanvas, SK_ColorGREEN, SK_ColorMAGENTA, false);
|
|
|
|
surfCanvas->translate(SkIntToScalar(kColWidth), 0);
|
|
|
|
this->drawColumn(surfCanvas, SK_ColorCYAN, SK_ColorMAGENTA, true);
|
|
|
|
|
|
|
|
SkPaint surfPaint;
|
2016-10-06 00:33:02 +00:00
|
|
|
surfPaint.setBlendMode(SkBlendMode::kSrcOver);
|
2015-09-15 20:13:50 +00:00
|
|
|
surface->draw(canvas, 0, 0, &surfPaint);
|
2015-09-15 16:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawColumn(SkCanvas* canvas, SkColor backgroundColor, SkColor textColor, bool useGrad) {
|
2017-03-15 16:19:37 +00:00
|
|
|
const SkBlendMode gModes[] = {
|
|
|
|
SkBlendMode::kClear,
|
|
|
|
SkBlendMode::kSrc,
|
|
|
|
SkBlendMode::kDst,
|
|
|
|
SkBlendMode::kSrcOver,
|
|
|
|
SkBlendMode::kDstOver,
|
|
|
|
SkBlendMode::kSrcIn,
|
|
|
|
SkBlendMode::kDstIn,
|
|
|
|
SkBlendMode::kSrcOut,
|
|
|
|
SkBlendMode::kDstOut,
|
|
|
|
SkBlendMode::kSrcATop,
|
|
|
|
SkBlendMode::kDstATop,
|
|
|
|
SkBlendMode::kXor,
|
|
|
|
SkBlendMode::kPlus,
|
|
|
|
SkBlendMode::kModulate,
|
|
|
|
SkBlendMode::kScreen,
|
|
|
|
SkBlendMode::kOverlay,
|
|
|
|
SkBlendMode::kDarken,
|
|
|
|
SkBlendMode::kLighten,
|
|
|
|
SkBlendMode::kColorDodge,
|
|
|
|
SkBlendMode::kColorBurn,
|
|
|
|
SkBlendMode::kHardLight,
|
|
|
|
SkBlendMode::kSoftLight,
|
|
|
|
SkBlendMode::kDifference,
|
|
|
|
SkBlendMode::kExclusion,
|
|
|
|
SkBlendMode::kMultiply,
|
|
|
|
SkBlendMode::kHue,
|
|
|
|
SkBlendMode::kSaturation,
|
|
|
|
SkBlendMode::kColor,
|
|
|
|
SkBlendMode::kLuminosity,
|
2015-09-15 16:31:40 +00:00
|
|
|
};
|
|
|
|
// Draw background rect
|
|
|
|
SkPaint backgroundPaint;
|
|
|
|
backgroundPaint.setColor(backgroundColor);
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->drawRect(SkRect::MakeIWH(kColWidth, kHeight), backgroundPaint);
|
2015-09-15 16:31:40 +00:00
|
|
|
SkScalar y = fTextHeight;
|
|
|
|
for (size_t m = 0; m < SK_ARRAY_COUNT(gModes); m++) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(textColor);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setSubpixelText(true);
|
|
|
|
paint.setLCDRenderText(true);
|
|
|
|
paint.setTextSize(fTextHeight);
|
2017-03-15 16:19:37 +00:00
|
|
|
paint.setBlendMode(gModes[m]);
|
2015-09-15 16:31:40 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
|
|
|
if (useGrad) {
|
|
|
|
SkRect r;
|
|
|
|
r.setXYWH(0, y - fTextHeight, SkIntToScalar(kColWidth), fTextHeight);
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(make_shader(r));
|
2015-09-15 16:31:40 +00:00
|
|
|
}
|
2017-03-15 16:19:37 +00:00
|
|
|
SkString string(SkBlendMode_Name(gModes[m]));
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(string, 0, y, paint);
|
2015-09-15 16:31:40 +00:00
|
|
|
y+=fTextHeight;
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-15 16:31:40 +00:00
|
|
|
private:
|
|
|
|
SkScalar fTextHeight;
|
2016-03-09 02:50:00 +00:00
|
|
|
sk_sp<SkShader> fCheckerboard;
|
2015-09-15 16:31:40 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM( return new LcdBlendGM; )
|
|
|
|
}
|