2012-11-15 13:46:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 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/SkFont.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPath.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#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"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#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 "tools/ToolUtils.h"
|
2012-11-15 13:46:47 +00:00
|
|
|
|
2012-11-15 15:56:38 +00:00
|
|
|
#define W SkIntToScalar(80)
|
2012-11-15 13:46:47 +00:00
|
|
|
#define H SkIntToScalar(60)
|
|
|
|
|
2012-11-15 15:56:38 +00:00
|
|
|
typedef void (*PaintProc)(SkPaint*);
|
|
|
|
|
2012-11-16 02:23:10 +00:00
|
|
|
static void identity_paintproc(SkPaint* paint) {
|
2015-08-27 14:41:13 +00:00
|
|
|
paint->setShader(nullptr);
|
2012-11-16 02:23:10 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 15:56:38 +00:00
|
|
|
static void gradient_paintproc(SkPaint* paint) {
|
|
|
|
const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
|
|
|
|
const SkPoint pts[] = { { 0, 0 }, { W, H } };
|
2016-03-13 21:13:58 +00:00
|
|
|
paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp));
|
2012-11-15 15:56:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 14:38:02 +00:00
|
|
|
typedef void (*Proc)(SkCanvas*, const SkPaint&, const SkFont&);
|
2012-11-15 13:46:47 +00:00
|
|
|
|
2019-01-08 14:38:02 +00:00
|
|
|
static void draw_hair(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
|
2012-11-15 13:46:47 +00:00
|
|
|
SkPaint p(paint);
|
|
|
|
p.setStrokeWidth(0);
|
|
|
|
canvas->drawLine(0, 0, W, H, p);
|
|
|
|
}
|
|
|
|
|
2019-01-08 14:38:02 +00:00
|
|
|
static void draw_thick(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
|
2012-11-15 13:46:47 +00:00
|
|
|
SkPaint p(paint);
|
|
|
|
p.setStrokeWidth(H/5);
|
|
|
|
canvas->drawLine(0, 0, W, H, p);
|
|
|
|
}
|
|
|
|
|
2019-01-08 14:38:02 +00:00
|
|
|
static void draw_rect(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
|
2012-11-15 13:46:47 +00:00
|
|
|
canvas->drawRect(SkRect::MakeWH(W, H), paint);
|
|
|
|
}
|
|
|
|
|
2019-01-08 14:38:02 +00:00
|
|
|
static void draw_oval(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
|
2012-11-15 13:46:47 +00:00
|
|
|
canvas->drawOval(SkRect::MakeWH(W, H), paint);
|
|
|
|
}
|
|
|
|
|
2019-01-08 14:38:02 +00:00
|
|
|
static void draw_text(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) {
|
|
|
|
canvas->drawString("Hamburge", 0, H*2/3, font, paint);
|
2012-11-15 13:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class SrcModeGM : public skiagm::GM {
|
|
|
|
SkPath fPath;
|
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
void onOnceBeforeDraw() override { this->setBGColor(SK_ColorBLACK); }
|
2012-11-15 13:46:47 +00:00
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
SkString onShortName() override { return SkString("srcmode"); }
|
|
|
|
|
|
|
|
SkISize onISize() override { return {640, 760}; }
|
2012-11-15 13:46:47 +00:00
|
|
|
|
2012-11-15 17:24:23 +00:00
|
|
|
void drawContent(SkCanvas* canvas) {
|
2012-11-15 13:46:47 +00:00
|
|
|
canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
|
|
|
|
|
|
|
|
SkPaint paint;
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), H / 4);
|
2015-07-17 20:20:48 +00:00
|
|
|
paint.setColor(0x80F60000);
|
2012-11-15 13:46:47 +00:00
|
|
|
|
|
|
|
const Proc procs[] = {
|
|
|
|
draw_hair, draw_thick, draw_rect, draw_oval, draw_text
|
|
|
|
};
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
const SkBlendMode modes[] = {
|
|
|
|
SkBlendMode::kSrcOver, SkBlendMode::kSrc, SkBlendMode::kClear
|
2012-11-15 13:46:47 +00:00
|
|
|
};
|
|
|
|
|
2012-11-15 15:56:38 +00:00
|
|
|
const PaintProc paintProcs[] = {
|
|
|
|
identity_paintproc, gradient_paintproc
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int aa = 0; aa <= 1; ++aa) {
|
|
|
|
paint.setAntiAlias(SkToBool(aa));
|
2019-01-08 14:38:02 +00:00
|
|
|
font.setEdging(SkToBool(aa) ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
|
2012-11-15 13:46:47 +00:00
|
|
|
canvas->save();
|
2012-11-15 15:56:38 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) {
|
|
|
|
paintProcs[i](&paint);
|
|
|
|
for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) {
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(modes[x]);
|
2012-11-15 15:56:38 +00:00
|
|
|
canvas->save();
|
|
|
|
for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) {
|
2019-01-08 14:38:02 +00:00
|
|
|
procs[y](canvas, paint, font);
|
2012-11-15 15:56:38 +00:00
|
|
|
canvas->translate(0, H * 5 / 4);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(W * 5 / 4, 0);
|
|
|
|
}
|
2012-11-15 13:46:47 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
2012-11-15 15:56:38 +00:00
|
|
|
canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs));
|
2012-11-15 13:46:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 15:43:25 +00:00
|
|
|
static sk_sp<SkSurface> compat_surface(SkCanvas* canvas, const SkISize& size) {
|
2014-01-15 02:38:22 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(size);
|
2019-09-10 15:43:25 +00:00
|
|
|
sk_sp<SkSurface> surface = canvas->makeSurface(info);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == surface) {
|
2018-09-05 18:41:40 +00:00
|
|
|
// picture canvas will return null, so fall-back to raster
|
2016-03-24 01:59:25 +00:00
|
|
|
surface = SkSurface::MakeRaster(info);
|
2014-06-30 16:05:34 +00:00
|
|
|
}
|
|
|
|
return surface;
|
2012-11-15 17:24:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2019-09-10 15:43:25 +00:00
|
|
|
auto surf(compat_surface(canvas, this->getISize()));
|
2012-11-15 17:24:23 +00:00
|
|
|
surf->getCanvas()->drawColor(SK_ColorWHITE);
|
|
|
|
this->drawContent(surf->getCanvas());
|
2021-01-06 13:43:51 +00:00
|
|
|
surf->draw(canvas, 0, 0);
|
2012-11-15 17:24:23 +00:00
|
|
|
}
|
2012-11-15 13:46:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM(return new SrcModeGM;)
|