2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
11 #include "SkGradientShader.h"
13 #include "SkSurface.h"
15 #define W SkIntToScalar(80)
16 #define H SkIntToScalar(60)
18 typedef void (*PaintProc)(SkPaint*);
20 static void identity_paintproc(SkPaint* paint) {
21 paint->setShader(nullptr);
24 static void gradient_paintproc(SkPaint* paint) {
25 const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
26 const SkPoint pts[] = { { 0, 0 }, { W, H } };
27 paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
28 SkShader::kClamp_TileMode));
31 typedef void (*Proc)(SkCanvas*, const SkPaint&);
33 static void draw_hair(SkCanvas* canvas, const SkPaint& paint) {
36 canvas->drawLine(0, 0, W, H, p);
39 static void draw_thick(SkCanvas* canvas, const SkPaint& paint) {
41 p.setStrokeWidth(H/5);
42 canvas->drawLine(0, 0, W, H, p);
45 static void draw_rect(SkCanvas* canvas, const SkPaint& paint) {
46 canvas->drawRect(SkRect::MakeWH(W, H), paint);
49 static void draw_oval(SkCanvas* canvas, const SkPaint& paint) {
50 canvas->drawOval(SkRect::MakeWH(W, H), paint);
53 static void draw_text(SkCanvas* canvas, const SkPaint& paint) {
56 canvas->drawString("Hamburge", 0, H*2/3, p);
59 class SrcModeGM : public skiagm::GM {
63 this->setBGColor(SK_ColorBLACK);
67 virtual SkString onShortName() {
68 return SkString("srcmode");
71 virtual SkISize onISize() {
72 return SkISize::Make(640, 760);
75 void drawContent(SkCanvas* canvas) {
76 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
79 sk_tool_utils::set_portable_typeface(&paint);
80 paint.setColor(0x80F60000);
82 const Proc procs[] = {
83 draw_hair, draw_thick, draw_rect, draw_oval, draw_text
86 const SkBlendMode modes[] = {
87 SkBlendMode::kSrcOver, SkBlendMode::kSrc, SkBlendMode::kClear
90 const PaintProc paintProcs[] = {
91 identity_paintproc, gradient_paintproc
94 for (int aa = 0; aa <= 1; ++aa) {
95 paint.setAntiAlias(SkToBool(aa));
97 for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) {
98 paintProcs[i](&paint);
99 for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) {
100 paint.setBlendMode(modes[x]);
102 for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) {
103 procs[y](canvas, paint);
104 canvas->translate(0, H * 5 / 4);
107 canvas->translate(W * 5 / 4, 0);
111 canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs));
115 static sk_sp<SkSurface> compat_surface(SkCanvas* canvas, const SkISize& size, bool skipGPU) {
116 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
118 bool callNewSurface = true;
120 if (canvas->getGrContext() && skipGPU) {
121 callNewSurface = false;
124 sk_sp<SkSurface> surface = callNewSurface ? canvas->makeSurface(info) : nullptr;
125 if (nullptr == surface) {
126 // picture canvas will return null, so fall-back to raster
127 surface = SkSurface::MakeRaster(info);
132 virtual void onDraw(SkCanvas* canvas) {
133 auto surf(compat_surface(canvas, this->getISize(), this->isCanvasDeferred()));
134 surf->getCanvas()->drawColor(SK_ColorWHITE);
135 this->drawContent(surf->getCanvas());
136 surf->draw(canvas, 0, 0, nullptr);
140 typedef skiagm::GM INHERITED;
143 ///////////////////////////////////////////////////////////////////////////////
145 DEF_GM(return new SrcModeGM;)