OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 | 9 |
| 10 #include "SkBitmapProcShader.h" |
10 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
11 #include "SkComposeShader.h" | 12 #include "SkComposeShader.h" |
12 #include "SkGradientShader.h" | 13 #include "SkGradientShader.h" |
13 #include "SkGraphics.h" | 14 #include "SkGraphics.h" |
14 #include "SkShader.h" | 15 #include "SkShader.h" |
15 #include "SkString.h" | 16 #include "SkString.h" |
16 #include "SkXfermode.h" | 17 #include "SkXfermode.h" |
17 | 18 |
18 static SkShader* make_shader(SkXfermode::Mode mode) { | 19 static SkShader* make_shader(SkXfermode::Mode mode) { |
19 SkPoint pts[2]; | 20 SkPoint pts[2]; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 class ComposeShaderAlphaGM : public skiagm::GM { | 78 class ComposeShaderAlphaGM : public skiagm::GM { |
78 public: | 79 public: |
79 ComposeShaderAlphaGM() {} | 80 ComposeShaderAlphaGM() {} |
80 | 81 |
81 protected: | 82 protected: |
82 SkString onShortName() override { | 83 SkString onShortName() override { |
83 return SkString("composeshader_alpha"); | 84 return SkString("composeshader_alpha"); |
84 } | 85 } |
85 | 86 |
86 SkISize onISize() override { | 87 SkISize onISize() override { |
87 return SkISize::Make(220, 750); | 88 return SkISize::Make(750, 220); |
88 } | 89 } |
89 | 90 |
90 void onDraw(SkCanvas* canvas) override { | 91 void onDraw(SkCanvas* canvas) override { |
91 SkAutoTUnref<SkShader> shader0(make_shader(SkXfermode::kDstIn_Mode)); | 92 SkAutoTUnref<SkShader> shader0(make_shader(SkXfermode::kDstIn_Mode)); |
92 SkAutoTUnref<SkShader> shader1(make_shader(SkXfermode::kSrcOver_Mode)); | 93 SkAutoTUnref<SkShader> shader1(make_shader(SkXfermode::kSrcOver_Mode)); |
93 SkShader* shaders[] = { shader0.get(), shader1.get() }; | 94 SkShader* shaders[] = { shader0.get(), shader1.get() }; |
94 | 95 |
95 SkPaint paint; | 96 SkPaint paint; |
96 paint.setColor(SK_ColorGREEN); | 97 paint.setColor(SK_ColorGREEN); |
97 | 98 |
(...skipping 10 matching lines...) Expand all Loading... |
108 paint.setAlpha(alpha); | 109 paint.setAlpha(alpha); |
109 paint.setShader(shader); | 110 paint.setShader(shader); |
110 canvas->drawRect(r, paint); | 111 canvas->drawRect(r, paint); |
111 | 112 |
112 canvas->translate(r.width() + 5, 0); | 113 canvas->translate(r.width() + 5, 0); |
113 } | 114 } |
114 canvas->restore(); | 115 canvas->restore(); |
115 canvas->translate(0, r.height() + 5); | 116 canvas->translate(0, r.height() + 5); |
116 } | 117 } |
117 } | 118 } |
| 119 |
| 120 private: |
| 121 typedef GM INHERITED ; |
| 122 }; |
| 123 |
| 124 |
| 125 // creates a square bitmap with red background and a green circle in the center |
| 126 static void draw_color_bm(SkBitmap* bm, int length) { |
| 127 SkPaint paint; |
| 128 paint.setColor(SK_ColorGREEN); |
| 129 |
| 130 bm->allocN32Pixels(length, length); |
| 131 bm->eraseColor(SK_ColorRED); |
| 132 |
| 133 SkCanvas canvas(*bm); |
| 134 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToS
calar(length/2), |
| 135 paint); |
| 136 } |
| 137 |
| 138 // creates a square alpha8 bitmap with transparent background and an opaque circ
le in the center |
| 139 static void draw_alpha8_bm(SkBitmap* bm, int length) { |
| 140 SkPaint circlePaint; |
| 141 circlePaint.setColor(SK_ColorBLACK); |
| 142 |
| 143 bm->allocPixels(SkImageInfo::MakeA8(length, length)); |
| 144 bm->eraseColor(SK_ColorTRANSPARENT); |
| 145 |
| 146 SkCanvas canvas(*bm); |
| 147 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToS
calar(length/4), |
| 148 circlePaint); |
| 149 } |
| 150 |
| 151 // creates a linear gradient shader |
| 152 static SkShader* make_linear_gradient_shader(int length) { |
| 153 SkPoint pts[2]; |
| 154 SkColor colors[2]; |
| 155 pts[0].set(0, 0); |
| 156 pts[1].set(SkIntToScalar(length), 0); |
| 157 colors[0] = SK_ColorBLUE; |
| 158 colors[1] = SkColorSetARGB(0, 0, 0, 0xFF); |
| 159 return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kCl
amp_TileMode); |
| 160 } |
| 161 |
| 162 |
| 163 class ComposeShaderBitmapGM : public skiagm::GM { |
| 164 public: |
| 165 ComposeShaderBitmapGM() { |
| 166 draw_color_bm(&fColorBitmap, squareLength); |
| 167 draw_alpha8_bm(&fAlpha8Bitmap, squareLength); |
| 168 SkMatrix s; |
| 169 s.reset(); |
| 170 fColorBitmapShader = new SkBitmapProcShader(fColorBitmap, SkShader::kRep
eat_TileMode, |
| 171 SkShader::kRepeat_TileMode,
&s); |
| 172 fAlpha8BitmapShader = new SkBitmapProcShader(fAlpha8Bitmap, SkShader::kR
epeat_TileMode, |
| 173 SkShader::kRepeat_TileMode,
&s); |
| 174 fLinearGradientShader = make_linear_gradient_shader(squareLength); |
| 175 } |
| 176 ~ComposeShaderBitmapGM() { |
| 177 SkSafeUnref(fColorBitmapShader); |
| 178 SkSafeUnref(fAlpha8BitmapShader); |
| 179 SkSafeUnref(fLinearGradientShader); |
| 180 } |
| 181 |
| 182 protected: |
| 183 SkString onShortName() override { |
| 184 return SkString("composeshader_bitmap"); |
| 185 } |
| 186 |
| 187 SkISize onISize() override { |
| 188 return SkISize::Make(7 * (squareLength + 5), 2 * (squareLength + 5)); |
| 189 } |
| 190 |
| 191 void onDraw(SkCanvas* canvas) override { |
| 192 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mo
de)); |
| 193 |
| 194 // gradient should appear over color bitmap |
| 195 SkAutoTUnref<SkShader> shader0(new SkComposeShader(fLinearGradientShader
, |
| 196 fColorBitmapShader, |
| 197 xfer)); |
| 198 // gradient should appear over alpha8 bitmap colorized by the paint colo
r |
| 199 SkAutoTUnref<SkShader> shader1(new SkComposeShader(fLinearGradientShader
, |
| 200 fAlpha8BitmapShader, |
| 201 xfer)); |
| 202 |
| 203 SkShader* shaders[] = { shader0.get(), shader1.get() }; |
| 204 |
| 205 SkPaint paint; |
| 206 paint.setColor(SK_ColorYELLOW); |
| 207 |
| 208 const SkRect r = SkRect::MakeXYWH(0, 0, SkIntToScalar(squareLength), |
| 209 SkIntToScalar(squareLength)); |
| 210 |
| 211 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) { |
| 212 SkShader* shader = shaders[y]; |
| 213 canvas->save(); |
| 214 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) { |
| 215 paint.setAlpha(alpha); |
| 216 paint.setShader(shader); |
| 217 canvas->drawRect(r, paint); |
| 218 |
| 219 canvas->translate(r.width() + 5, 0); |
| 220 } |
| 221 canvas->restore(); |
| 222 canvas->translate(0, r.height() + 5); |
| 223 } |
| 224 } |
| 225 private: |
| 226 /** This determines the length and width of the bitmaps used in the SkCompos
eShaders. Values |
| 227 * above 20 may cause an SkASSERT to fail in SkSmallAllocator. However, lar
ger values will |
| 228 * work in a release build. You can change this parameter and then compile
a release build |
| 229 * to have this GM draw larger bitmaps for easier visual inspection. |
| 230 */ |
| 231 static const int squareLength = 20; |
| 232 |
| 233 SkBitmap fColorBitmap; |
| 234 SkBitmap fAlpha8Bitmap; |
| 235 SkShader* fColorBitmapShader; |
| 236 SkShader* fAlpha8BitmapShader; |
| 237 SkShader* fLinearGradientShader; |
| 238 |
| 239 typedef GM INHERITED; |
118 }; | 240 }; |
119 | 241 |
120 ////////////////////////////////////////////////////////////////////////////// | 242 ////////////////////////////////////////////////////////////////////////////// |
121 | 243 |
122 DEF_GM( return new ComposeShaderGM; ) | 244 DEF_GM( return new ComposeShaderGM; ) |
123 DEF_GM( return new ComposeShaderAlphaGM; ) | 245 DEF_GM( return new ComposeShaderAlphaGM; ) |
| 246 DEF_GM( return new ComposeShaderBitmapGM; ) |
OLD | NEW |