reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 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.h" |
| 9 | #include "SkCanvas.h" |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 10 | #include "SkGradientShader.h" |
reed@google.com | 86eca47 | 2012-11-15 17:24:23 +0000 | [diff] [blame] | 11 | #include "SkSurface.h" |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 12 | |
mike@reedtribe.org | 1588974 | 2012-11-16 02:31:03 +0000 | [diff] [blame] | 13 | #if SK_SUPPORT_GPU |
| 14 | #include "SkGpuDevice.h" |
| 15 | #endif |
mike@reedtribe.org | 92aa756 | 2012-11-16 02:23:10 +0000 | [diff] [blame] | 16 | |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 17 | #define W SkIntToScalar(80) |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 18 | #define H SkIntToScalar(60) |
| 19 | |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 20 | typedef void (*PaintProc)(SkPaint*); |
| 21 | |
mike@reedtribe.org | 92aa756 | 2012-11-16 02:23:10 +0000 | [diff] [blame] | 22 | static void identity_paintproc(SkPaint* paint) { |
| 23 | paint->setShader(NULL); |
| 24 | } |
| 25 | |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 26 | static void gradient_paintproc(SkPaint* paint) { |
| 27 | const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE }; |
| 28 | const SkPoint pts[] = { { 0, 0 }, { W, H } }; |
| 29 | SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, |
| 30 | SK_ARRAY_COUNT(colors), |
| 31 | SkShader::kClamp_TileMode); |
| 32 | paint->setShader(s)->unref(); |
| 33 | } |
| 34 | |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 35 | typedef void (*Proc)(SkCanvas*, const SkPaint&); |
| 36 | |
| 37 | static void draw_hair(SkCanvas* canvas, const SkPaint& paint) { |
| 38 | SkPaint p(paint); |
| 39 | p.setStrokeWidth(0); |
| 40 | canvas->drawLine(0, 0, W, H, p); |
| 41 | } |
| 42 | |
| 43 | static void draw_thick(SkCanvas* canvas, const SkPaint& paint) { |
| 44 | SkPaint p(paint); |
| 45 | p.setStrokeWidth(H/5); |
| 46 | canvas->drawLine(0, 0, W, H, p); |
| 47 | } |
| 48 | |
| 49 | static void draw_rect(SkCanvas* canvas, const SkPaint& paint) { |
| 50 | canvas->drawRect(SkRect::MakeWH(W, H), paint); |
| 51 | } |
| 52 | |
| 53 | static void draw_oval(SkCanvas* canvas, const SkPaint& paint) { |
| 54 | canvas->drawOval(SkRect::MakeWH(W, H), paint); |
| 55 | } |
| 56 | |
| 57 | static void draw_text(SkCanvas* canvas, const SkPaint& paint) { |
| 58 | SkPaint p(paint); |
| 59 | p.setTextSize(H/4); |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 60 | canvas->drawText("Hamburge", 8, 0, H*2/3, p); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | class SrcModeGM : public skiagm::GM { |
| 64 | SkPath fPath; |
| 65 | public: |
| 66 | SrcModeGM() { |
reed@google.com | 86eca47 | 2012-11-15 17:24:23 +0000 | [diff] [blame] | 67 | this->setBGColor(SK_ColorBLACK); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | protected: |
| 71 | virtual SkString onShortName() { |
| 72 | return SkString("srcmode"); |
| 73 | } |
| 74 | |
| 75 | virtual SkISize onISize() { |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 76 | return SkISize::Make(640, 760); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 77 | } |
| 78 | |
reed@google.com | 86eca47 | 2012-11-15 17:24:23 +0000 | [diff] [blame] | 79 | void drawContent(SkCanvas* canvas) { |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 80 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 81 | |
| 82 | SkPaint paint; |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 83 | paint.setColor(0x80FF0000); |
| 84 | |
| 85 | const Proc procs[] = { |
| 86 | draw_hair, draw_thick, draw_rect, draw_oval, draw_text |
| 87 | }; |
| 88 | |
| 89 | const SkXfermode::Mode modes[] = { |
| 90 | SkXfermode::kSrcOver_Mode, SkXfermode::kSrc_Mode, SkXfermode::kClear_Mode |
| 91 | }; |
| 92 | |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 93 | const PaintProc paintProcs[] = { |
| 94 | identity_paintproc, gradient_paintproc |
| 95 | }; |
| 96 | |
| 97 | for (int aa = 0; aa <= 1; ++aa) { |
| 98 | paint.setAntiAlias(SkToBool(aa)); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 99 | canvas->save(); |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 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]); |
| 104 | canvas->save(); |
| 105 | for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) { |
| 106 | procs[y](canvas, paint); |
| 107 | canvas->translate(0, H * 5 / 4); |
| 108 | } |
| 109 | canvas->restore(); |
| 110 | canvas->translate(W * 5 / 4, 0); |
| 111 | } |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 112 | } |
| 113 | canvas->restore(); |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 114 | canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs)); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
reed@google.com | aef7361 | 2012-11-16 13:41:45 +0000 | [diff] [blame] | 118 | static SkSurface* compat_surface(SkCanvas* canvas, const SkISize& size, |
| 119 | bool skipGPU) { |
reed@google.com | 86eca47 | 2012-11-15 17:24:23 +0000 | [diff] [blame] | 120 | SkImage::Info info = { |
| 121 | size.width(), |
| 122 | size.height(), |
| 123 | SkImage::kPMColor_ColorType, |
| 124 | SkImage::kPremul_AlphaType |
| 125 | }; |
mike@reedtribe.org | 1588974 | 2012-11-16 02:31:03 +0000 | [diff] [blame] | 126 | #if SK_SUPPORT_GPU |
robertphillips@google.com | 9b051a3 | 2013-08-20 20:06:40 +0000 | [diff] [blame^] | 127 | SkDevice* dev = canvas->getDevice(); |
reed@google.com | aef7361 | 2012-11-16 13:41:45 +0000 | [diff] [blame] | 128 | if (!skipGPU && dev->accessRenderTarget()) { |
mike@reedtribe.org | 92aa756 | 2012-11-16 02:23:10 +0000 | [diff] [blame] | 129 | SkGpuDevice* gd = (SkGpuDevice*)dev; |
| 130 | GrContext* ctx = gd->context(); |
| 131 | return SkSurface::NewRenderTarget(ctx, info, 0); |
| 132 | } |
mike@reedtribe.org | 1588974 | 2012-11-16 02:31:03 +0000 | [diff] [blame] | 133 | #endif |
reed@google.com | 86eca47 | 2012-11-15 17:24:23 +0000 | [diff] [blame] | 134 | return SkSurface::NewRaster(info); |
| 135 | } |
| 136 | |
| 137 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | aef7361 | 2012-11-16 13:41:45 +0000 | [diff] [blame] | 138 | SkAutoTUnref<SkSurface> surf(compat_surface(canvas, this->getISize(), |
| 139 | this->isCanvasDeferred())); |
reed@google.com | 86eca47 | 2012-11-15 17:24:23 +0000 | [diff] [blame] | 140 | surf->getCanvas()->drawColor(SK_ColorWHITE); |
| 141 | this->drawContent(surf->getCanvas()); |
| 142 | surf->draw(canvas, 0, 0, NULL); |
reed@google.com | 86eca47 | 2012-11-15 17:24:23 +0000 | [diff] [blame] | 143 | } |
| 144 | |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 145 | private: |
| 146 | typedef skiagm::GM INHERITED; |
| 147 | }; |
| 148 | |
| 149 | /////////////////////////////////////////////////////////////////////////////// |
| 150 | |
| 151 | DEF_GM(return new SrcModeGM;) |