| 1 |
/* |
| 2 |
* Copyright 2012 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/SkPath.h" |
| 16 |
#include "include/core/SkPoint.h" |
| 17 |
#include "include/core/SkRect.h" |
| 18 |
#include "include/core/SkRefCnt.h" |
| 19 |
#include "include/core/SkScalar.h" |
| 20 |
#include "include/core/SkShader.h" |
| 21 |
#include "include/core/SkSize.h" |
| 22 |
#include "include/core/SkString.h" |
| 23 |
#include "include/core/SkSurface.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 "tools/ToolUtils.h" |
| 29 |
#include "tools/fonts/FontToolUtils.h" |
| 30 |
|
| 31 |
#define W SkIntToScalar(80) |
| 32 |
#define H SkIntToScalar(60) |
| 33 |
|
| 34 |
typedef void (*PaintProc)(SkPaint*); |
| 35 |
|
| 36 |
static void identity_paintproc(SkPaint* paint) { |
| 37 |
paint->setShader(nullptr); |
| 38 |
} |
| 39 |
|
| 40 |
static void gradient_paintproc(SkPaint* paint) { |
| 41 |
const SkColor4f colors[] = { SkColors::kGreen, SkColors::kBlue }; |
| 42 |
const SkPoint pts[] = { { 0, 0 }, { W, H } }; |
| 43 |
paint->setShader(SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}})); |
| 44 |
} |
| 45 |
|
| 46 |
typedef void (*Proc)(SkCanvas*, const SkPaint&, const SkFont&); |
| 47 |
|
| 48 |
static void draw_hair(SkCanvas* canvas, const SkPaint& paint, const SkFont&) { |
| 49 |
SkPaint p(paint); |
| 50 |
p.setStrokeWidth(0); |
| 51 |
canvas->drawLine(0, 0, W, H, p); |
| 52 |
} |
| 53 |
|
| 54 |
static void draw_thick(SkCanvas* canvas, const SkPaint& paint, const SkFont&) { |
| 55 |
SkPaint p(paint); |
| 56 |
p.setStrokeWidth(H/5); |
| 57 |
canvas->drawLine(0, 0, W, H, p); |
| 58 |
} |
| 59 |
|
| 60 |
static void draw_rect(SkCanvas* canvas, const SkPaint& paint, const SkFont&) { |
| 61 |
canvas->drawRect(SkRect::MakeWH(W, H), paint); |
| 62 |
} |
| 63 |
|
| 64 |
static void draw_oval(SkCanvas* canvas, const SkPaint& paint, const SkFont&) { |
| 65 |
canvas->drawOval(SkRect::MakeWH(W, H), paint); |
| 66 |
} |
| 67 |
|
| 68 |
static void draw_text(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) { |
| 69 |
canvas->drawString("Hamburge", 0, H*2/3, font, paint); |
| 70 |
} |
| 71 |
|
| 72 |
class SrcModeGM : public skiagm::GM { |
| 73 |
SkPath fPath; |
| 74 |
|
| 75 |
void onOnceBeforeDraw() override { this->setBGColor(SK_ColorBLACK); } |
| 76 |
|
| 77 |
SkString getName() const override { return SkString("srcmode"); } |
| 78 |
|
| 79 |
SkISize getISize() override { return {640, 760}; } |
| 80 |
|
| 81 |
void drawContent(SkCanvas* canvas) { |
| 82 |
canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 83 |
|
| 84 |
SkPaint paint; |
| 85 |
SkFont font(ToolUtils::DefaultPortableTypeface(), H / 4); |
| 86 |
paint.setColor(0x80F60000); |
| 87 |
|
| 88 |
const Proc procs[] = { |
| 89 |
draw_hair, draw_thick, draw_rect, draw_oval, draw_text |
| 90 |
}; |
| 91 |
|
| 92 |
const SkBlendMode modes[] = { |
| 93 |
SkBlendMode::kSrcOver, SkBlendMode::kSrc, SkBlendMode::kClear |
| 94 |
}; |
| 95 |
|
| 96 |
const PaintProc paintProcs[] = { |
| 97 |
identity_paintproc, gradient_paintproc |
| 98 |
}; |
| 99 |
|
| 100 |
for (int aa = 0; aa <= 1; ++aa) { |
| 101 |
paint.setAntiAlias(SkToBool(aa)); |
| 102 |
font.setEdging(SkToBool(aa) ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias); |
| 103 |
canvas->save(); |
| 104 |
for (size_t i = 0; i < std::size(paintProcs); ++i) { |
| 105 |
paintProcs[i](&paint); |
| 106 |
for (size_t x = 0; x < std::size(modes); ++x) { |
| 107 |
paint.setBlendMode(modes[x]); |
| 108 |
canvas->save(); |
| 109 |
for (size_t y = 0; y < std::size(procs); ++y) { |
| 110 |
procs[y](canvas, paint, font); |
| 111 |
canvas->translate(0, H * 5 / 4); |
| 112 |
} |
| 113 |
canvas->restore(); |
| 114 |
canvas->translate(W * 5 / 4, 0); |
| 115 |
} |
| 116 |
} |
| 117 |
canvas->restore(); |
| 118 |
canvas->translate(0, (H * 5 / 4) * std::size(procs)); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
static sk_sp<SkSurface> compat_surface(SkCanvas* canvas, const SkISize& size) { |
| 123 |
SkImageInfo info = SkImageInfo::MakeN32Premul(size); |
| 124 |
sk_sp<SkSurface> surface = canvas->makeSurface(info); |
| 125 |
if (nullptr == surface) { |
| 126 |
// picture canvas will return null, so fall-back to raster |
| 127 |
surface = SkSurfaces::Raster(info); |
| 128 |
} |
| 129 |
return surface; |
| 130 |
} |
| 131 |
|
| 132 |
void onDraw(SkCanvas* canvas) override { |
| 133 |
auto surf(compat_surface(canvas, this->getISize())); |
| 134 |
surf->getCanvas()->drawColor(SK_ColorWHITE); |
| 135 |
this->drawContent(surf->getCanvas()); |
| 136 |
surf->draw(canvas, 0, 0); |
| 137 |
} |
| 138 |
}; |
| 139 |
|
| 140 |
/////////////////////////////////////////////////////////////////////////////// |
| 141 |
|
| 142 |
DEF_GM(return new SrcModeGM;) |