bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +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 "SkBitmapProcShader.h" |
| 10 | #include "SkColorFilter.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | |
| 13 | #define WIDTH 512 |
| 14 | #define HEIGHT 1024 |
| 15 | |
| 16 | namespace skiagm { |
| 17 | |
| 18 | // Using gradients because GPU doesn't currently have an implementation of SkColorShader (duh!) |
| 19 | static SkShader* make_color_shader(SkColor color) { |
| 20 | static const SkPoint kPts[] = {{0, 0}, {1, 1}}; |
| 21 | SkColor colors[] = {color, color}; |
| 22 | |
| 23 | return SkGradientShader::CreateLinear(kPts, colors, NULL, 2, SkShader::kClamp_TileMode); |
| 24 | } |
| 25 | |
| 26 | static SkShader* make_solid_shader() { |
| 27 | return make_color_shader(SkColorSetARGB(0xFF, 0x40, 0x80, 0x20)); |
| 28 | } |
| 29 | |
| 30 | static SkShader* make_transparent_shader() { |
| 31 | return make_color_shader(SkColorSetARGB(0x80, 0x10, 0x70, 0x20)); |
| 32 | } |
| 33 | |
| 34 | static SkShader* make_trans_black_shader() { |
| 35 | return make_color_shader(0x0); |
| 36 | } |
| 37 | |
| 38 | // draws a background behind each test rect to see transparency |
| 39 | static SkShader* make_bg_shader(int checkSize) { |
| 40 | SkBitmap bmp; |
| 41 | bmp.setConfig(SkBitmap::kARGB_8888_Config, 2 * checkSize, 2 * checkSize); |
| 42 | bmp.allocPixels(); |
| 43 | SkCanvas canvas(bmp); |
| 44 | canvas.clear(0xFF800000); |
| 45 | SkPaint paint; |
| 46 | paint.setColor(0xFF000080); |
| 47 | SkRect rect0 = SkRect::MakeXYWH(0, 0, |
| 48 | SkIntToScalar(checkSize), SkIntToScalar(checkSize)); |
| 49 | SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(checkSize), SkIntToScalar(checkSize), |
| 50 | SkIntToScalar(checkSize), SkIntToScalar(checkSize)); |
| 51 | canvas.drawRect(rect1, paint); |
| 52 | canvas.drawRect(rect0, paint); |
| 53 | return SkNEW_ARGS(SkBitmapProcShader, (bmp, SkShader::kRepeat_TileMode, |
| 54 | SkShader::kRepeat_TileMode)); |
| 55 | } |
| 56 | |
| 57 | class ModeColorFilterGM : public GM { |
| 58 | public: |
| 59 | ModeColorFilterGM() { |
| 60 | this->setBGColor(0xFF303030); |
| 61 | } |
| 62 | |
| 63 | protected: |
| 64 | virtual SkString onShortName() { |
| 65 | return SkString("modecolorfilters"); |
| 66 | } |
| 67 | |
| 68 | virtual SkISize onISize() { |
| 69 | return make_isize(WIDTH, HEIGHT); |
| 70 | } |
| 71 | |
| 72 | virtual void onDraw(SkCanvas* canvas) { |
| 73 | // size of rect for each test case |
| 74 | static const int kRectWidth = 20; |
skia.committer@gmail.com | 24c29d9 | 2012-10-20 02:01:23 +0000 | [diff] [blame] | 75 | static const int kRectHeight = 20; |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 76 | |
| 77 | static const int kCheckSize = 10; |
| 78 | |
| 79 | if (!fBmpShader) { |
| 80 | fBmpShader.reset(make_bg_shader(kCheckSize)); |
| 81 | } |
| 82 | SkPaint bgPaint; |
| 83 | bgPaint.setShader(fBmpShader); |
| 84 | bgPaint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 85 | |
| 86 | SkShader* shaders[] = { |
| 87 | NULL, // use a paint color instead of a shader |
| 88 | make_solid_shader(), |
| 89 | make_transparent_shader(), |
| 90 | make_trans_black_shader(), |
| 91 | }; |
| 92 | |
| 93 | // used without shader |
| 94 | SkColor colors[] = { |
| 95 | SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF), |
| 96 | SkColorSetARGB(0xFF, 0x00, 0x00, 0x00), |
| 97 | SkColorSetARGB(0x00, 0x00, 0x00, 0x00), |
| 98 | SkColorSetARGB(0xFF, 0x10, 0x20, 0x40), |
| 99 | SkColorSetARGB(0xA0, 0x20, 0x30, 0x90), |
| 100 | }; |
| 101 | |
| 102 | // used with shaders |
| 103 | SkColor alphas[] = {0xFFFFFFFF, 0x80808080}; |
skia.committer@gmail.com | 24c29d9 | 2012-10-20 02:01:23 +0000 | [diff] [blame] | 104 | |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 105 | SkXfermode::Mode modes[] = { // currently just doing the Modes expressible as Coeffs |
| 106 | SkXfermode::kClear_Mode, |
| 107 | SkXfermode::kSrc_Mode, |
| 108 | SkXfermode::kDst_Mode, |
| 109 | SkXfermode::kSrcOver_Mode, |
| 110 | SkXfermode::kDstOver_Mode, |
| 111 | SkXfermode::kSrcIn_Mode, |
| 112 | SkXfermode::kDstIn_Mode, |
| 113 | SkXfermode::kSrcOut_Mode, |
| 114 | SkXfermode::kDstOut_Mode, |
| 115 | SkXfermode::kSrcATop_Mode, |
| 116 | SkXfermode::kDstATop_Mode, |
| 117 | SkXfermode::kXor_Mode, |
| 118 | SkXfermode::kPlus_Mode, |
| 119 | SkXfermode::kMultiply_Mode, |
| 120 | }; |
| 121 | |
| 122 | SkPaint paint; |
| 123 | int idx = 0; |
| 124 | static const int kRectsPerRow = SkMax32(this->getISize().fWidth / kRectWidth, 1); |
| 125 | for (size_t cfm = 0; cfm < SK_ARRAY_COUNT(modes); ++cfm) { |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 126 | for (size_t cfc = 0; cfc < SK_ARRAY_COUNT(colors); ++cfc) { |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 127 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(colors[cfc], |
| 128 | modes[cfm])); |
| 129 | paint.setColorFilter(cf); |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 130 | for (size_t s = 0; s < SK_ARRAY_COUNT(shaders); ++s) { |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 131 | paint.setShader(shaders[s]); |
| 132 | bool hasShader = NULL == paint.getShader(); |
| 133 | int paintColorCnt = hasShader ? SK_ARRAY_COUNT(alphas) : SK_ARRAY_COUNT(colors); |
| 134 | SkColor* paintColors = hasShader ? alphas : colors; |
| 135 | for (int pc = 0; pc < paintColorCnt; ++pc) { |
| 136 | paint.setColor(paintColors[pc]); |
| 137 | SkScalar x = SkIntToScalar(idx % kRectsPerRow); |
| 138 | SkScalar y = SkIntToScalar(idx / kRectsPerRow); |
| 139 | SkRect rect = SkRect::MakeXYWH(x * kRectWidth, y * kRectHeight, |
skia.committer@gmail.com | 306ab9d | 2012-12-13 02:01:33 +0000 | [diff] [blame] | 140 | SkIntToScalar(kRectWidth), |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 141 | SkIntToScalar(kRectHeight)); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 142 | canvas->drawRect(rect, bgPaint); |
| 143 | canvas->drawRect(rect, paint); |
| 144 | ++idx; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 150 | for (size_t i = 0; i < SK_ARRAY_COUNT(shaders); ++i) { |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 151 | SkSafeUnref(shaders[i]); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | SkAutoTUnref<SkShader> fBmpShader; |
| 157 | typedef GM INHERITED; |
| 158 | }; |
| 159 | |
| 160 | ////////////////////////////////////////////////////////////////////////////// |
| 161 | |
| 162 | static GM* MyFactory(void*) { return new ModeColorFilterGM; } |
| 163 | static GMRegistry reg(MyFactory); |
| 164 | |
| 165 | } |