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.
10 #include "SkGradientShader.h"
12 #include "SkSurface.h"
15 #include "SkGpuDevice.h"
18 #define W SkIntToScalar(80)
19 #define H SkIntToScalar(60)
21 typedef void (*PaintProc)(SkPaint*);
23 static void identity_paintproc(SkPaint* paint) {
24 paint->setShader(nullptr);
27 static void gradient_paintproc(SkPaint* paint) {
28 const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
29 const SkPoint pts[] = { { 0, 0 }, { W, H } };
30 paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
31 SkShader::kClamp_TileMode));
34 typedef void (*Proc)(SkCanvas*, const SkPaint&);
36 static void draw_hair(SkCanvas* canvas, const SkPaint& paint) {
39 canvas->drawLine(0, 0, W, H, p);
42 static void draw_thick(SkCanvas* canvas, const SkPaint& paint) {
44 p.setStrokeWidth(H/5);
45 canvas->drawLine(0, 0, W, H, p);
48 static void draw_rect(SkCanvas* canvas, const SkPaint& paint) {
49 canvas->drawRect(SkRect::MakeWH(W, H), paint);
52 static void draw_oval(SkCanvas* canvas, const SkPaint& paint) {
53 canvas->drawOval(SkRect::MakeWH(W, H), paint);
56 static void draw_text(SkCanvas* canvas, const SkPaint& paint) {
59 canvas->drawText("Hamburge", 8, 0, H*2/3, p);
62 class SrcModeGM : public skiagm::GM {
66 this->setBGColor(SK_ColorBLACK);
70 virtual SkString onShortName() {
71 return SkString("srcmode");
74 virtual SkISize onISize() {
75 return SkISize::Make(640, 760);
78 void drawContent(SkCanvas* canvas) {
79 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
82 sk_tool_utils::set_portable_typeface(&paint);
83 paint.setColor(0x80F60000);
85 const Proc procs[] = {
86 draw_hair, draw_thick, draw_rect, draw_oval, draw_text
89 const SkXfermode::Mode modes[] = {
90 SkXfermode::kSrcOver_Mode, SkXfermode::kSrc_Mode, SkXfermode::kClear_Mode
93 const PaintProc paintProcs[] = {
94 identity_paintproc, gradient_paintproc
97 for (int aa = 0; aa <= 1; ++aa) {
98 paint.setAntiAlias(SkToBool(aa));
100 for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) {
101 paintProcs[i](&paint);
102 for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) {
103 paint.setXfermodeMode(modes[x]);
105 for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) {
106 procs[y](canvas, paint);
107 canvas->translate(0, H * 5 / 4);
110 canvas->translate(W * 5 / 4, 0);
114 canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs));
118 static SkSurface* compat_surface(SkCanvas* canvas, const SkISize& size, bool skipGPU) {
119 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
121 bool callNewSurface = true;
123 if (canvas->getGrContext() && skipGPU) {
124 callNewSurface = false;
127 SkSurface* surface = callNewSurface ? canvas->newSurface(info) : nullptr;
128 if (nullptr == surface) {
129 // picture canvas will return null, so fall-back to raster
130 surface = SkSurface::NewRaster(info);
135 virtual void onDraw(SkCanvas* canvas) {
136 SkAutoTUnref<SkSurface> surf(compat_surface(canvas, this->getISize(),
137 this->isCanvasDeferred()));
138 surf->getCanvas()->drawColor(SK_ColorWHITE);
139 this->drawContent(surf->getCanvas());
140 surf->draw(canvas, 0, 0, nullptr);
144 typedef skiagm::GM INHERITED;
147 ///////////////////////////////////////////////////////////////////////////////
149 DEF_GM(return new SrcModeGM;)