| 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 "SkBitmapProcShader.h" |
| 11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
| 12 #include "SkComposeShader.h" | |
| 13 #include "SkGradientShader.h" | 12 #include "SkGradientShader.h" |
| 14 #include "SkGraphics.h" | 13 #include "SkGraphics.h" |
| 15 #include "SkShader.h" | 14 #include "SkShader.h" |
| 16 #include "SkString.h" | 15 #include "SkString.h" |
| 17 #include "SkXfermode.h" | 16 #include "SkXfermode.h" |
| 18 | 17 |
| 19 static SkShader* make_shader(SkXfermode::Mode mode) { | 18 static SkShader* make_shader(SkXfermode::Mode mode) { |
| 20 SkPoint pts[2]; | 19 SkPoint pts[2]; |
| 21 SkColor colors[2]; | 20 SkColor colors[2]; |
| 22 | 21 |
| 23 pts[0].set(0, 0); | 22 pts[0].set(0, 0); |
| 24 pts[1].set(SkIntToScalar(100), 0); | 23 pts[1].set(SkIntToScalar(100), 0); |
| 25 colors[0] = SK_ColorRED; | 24 colors[0] = SK_ColorRED; |
| 26 colors[1] = SK_ColorBLUE; | 25 colors[1] = SK_ColorBLUE; |
| 27 SkAutoTUnref<SkShader> shaderA(SkGradientShader::CreateLinear(pts, colors, n
ullptr, 2, | 26 SkAutoTUnref<SkShader> shaderA(SkGradientShader::CreateLinear(pts, colors, n
ullptr, 2, |
| 28 SkShader::kCla
mp_TileMode)); | 27 SkShader::kCla
mp_TileMode)); |
| 29 | 28 |
| 30 pts[0].set(0, 0); | 29 pts[0].set(0, 0); |
| 31 pts[1].set(0, SkIntToScalar(100)); | 30 pts[1].set(0, SkIntToScalar(100)); |
| 32 colors[0] = SK_ColorBLACK; | 31 colors[0] = SK_ColorBLACK; |
| 33 colors[1] = SkColorSetARGB(0x80, 0, 0, 0); | 32 colors[1] = SkColorSetARGB(0x80, 0, 0, 0); |
| 34 SkAutoTUnref<SkShader> shaderB(SkGradientShader::CreateLinear(pts, colors, n
ullptr, 2, | 33 SkAutoTUnref<SkShader> shaderB(SkGradientShader::CreateLinear(pts, colors, n
ullptr, 2, |
| 35 SkShader::kCla
mp_TileMode)); | 34 SkShader::kCla
mp_TileMode)); |
| 36 | 35 |
| 37 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(mode)); | 36 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(mode)); |
| 38 | 37 |
| 39 return new SkComposeShader(shaderA, shaderB, xfer); | 38 return SkShader::CreateComposeShader(shaderA, shaderB, xfer); |
| 40 } | 39 } |
| 41 | 40 |
| 42 class ComposeShaderGM : public skiagm::GM { | 41 class ComposeShaderGM : public skiagm::GM { |
| 43 public: | 42 public: |
| 44 ComposeShaderGM() { | 43 ComposeShaderGM() { |
| 45 fShader = make_shader(SkXfermode::kDstIn_Mode); | 44 fShader = make_shader(SkXfermode::kDstIn_Mode); |
| 46 } | 45 } |
| 47 | 46 |
| 48 virtual ~ComposeShaderGM() { | 47 virtual ~ComposeShaderGM() { |
| 49 SkSafeUnref(fShader); | 48 SkSafeUnref(fShader); |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 } | 186 } |
| 188 | 187 |
| 189 SkISize onISize() override { | 188 SkISize onISize() override { |
| 190 return SkISize::Make(7 * (squareLength + 5), 2 * (squareLength + 5)); | 189 return SkISize::Make(7 * (squareLength + 5), 2 * (squareLength + 5)); |
| 191 } | 190 } |
| 192 | 191 |
| 193 void onDraw(SkCanvas* canvas) override { | 192 void onDraw(SkCanvas* canvas) override { |
| 194 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mo
de)); | 193 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mo
de)); |
| 195 | 194 |
| 196 // gradient should appear over color bitmap | 195 // gradient should appear over color bitmap |
| 197 SkAutoTUnref<SkShader> shader0(new SkComposeShader(fLinearGradientShader
, | 196 SkAutoTUnref<SkShader> shader0(SkShader::CreateComposeShader(fLinearGrad
ientShader, |
| 198 fColorBitmapShader, | 197 fColorBitmapShader, |
| 199 xfer)); | 198 xfer)); |
| 200 // gradient should appear over alpha8 bitmap colorized by the paint colo
r | 199 // gradient should appear over alpha8 bitmap colorized by the paint colo
r |
| 201 SkAutoTUnref<SkShader> shader1(new SkComposeShader(fLinearGradientShader
, | 200 SkAutoTUnref<SkShader> shader1(SkShader::CreateComposeShader(fLinearGrad
ientShader, |
| 202 fAlpha8BitmapShader, | 201 fAlpha8BitmapShader, |
| 203 xfer)); | 202 xfer)); |
| 204 | 203 |
| 205 SkShader* shaders[] = { shader0.get(), shader1.get() }; | 204 SkShader* shaders[] = { shader0.get(), shader1.get() }; |
| 206 | 205 |
| 207 SkPaint paint; | 206 SkPaint paint; |
| 208 paint.setColor(SK_ColorYELLOW); | 207 paint.setColor(SK_ColorYELLOW); |
| 209 | 208 |
| 210 const SkRect r = SkRect::MakeXYWH(0, 0, SkIntToScalar(squareLength), | 209 const SkRect r = SkRect::MakeXYWH(0, 0, SkIntToScalar(squareLength), |
| 211 SkIntToScalar(squareLength)); | 210 SkIntToScalar(squareLength)); |
| 212 | 211 |
| 213 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) { | 212 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) { |
| 214 SkShader* shader = shaders[y]; | 213 SkShader* shader = shaders[y]; |
| 215 canvas->save(); | 214 canvas->save(); |
| 216 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) { | 215 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) { |
| 217 paint.setAlpha(alpha); | 216 paint.setAlpha(alpha); |
| 218 paint.setShader(shader); | 217 paint.setShader(shader); |
| 219 canvas->drawRect(r, paint); | 218 canvas->drawRect(r, paint); |
| 220 | 219 |
| 221 canvas->translate(r.width() + 5, 0); | 220 canvas->translate(r.width() + 5, 0); |
| 222 } | 221 } |
| 223 canvas->restore(); | 222 canvas->restore(); |
| 224 canvas->translate(0, r.height() + 5); | 223 canvas->translate(0, r.height() + 5); |
| 225 } | 224 } |
| 226 } | 225 } |
| 227 | 226 |
| 228 private: | 227 private: |
| 229 /** This determines the length and width of the bitmaps used in the SkCompos
eShaders. Values | 228 /** This determines the length and width of the bitmaps used in the ComposeS
haders. Values |
| 230 * above 20 may cause an SkASSERT to fail in SkSmallAllocator. However, lar
ger values will | 229 * above 20 may cause an SkASSERT to fail in SkSmallAllocator. However, lar
ger values will |
| 231 * work in a release build. You can change this parameter and then compile
a release build | 230 * work in a release build. You can change this parameter and then compile
a release build |
| 232 * to have this GM draw larger bitmaps for easier visual inspection. | 231 * to have this GM draw larger bitmaps for easier visual inspection. |
| 233 */ | 232 */ |
| 234 static const int squareLength = 20; | 233 static const int squareLength = 20; |
| 235 | 234 |
| 236 SkBitmap fColorBitmap; | 235 SkBitmap fColorBitmap; |
| 237 SkBitmap fAlpha8Bitmap; | 236 SkBitmap fAlpha8Bitmap; |
| 238 SkShader* fColorBitmapShader{nullptr}; | 237 SkShader* fColorBitmapShader{nullptr}; |
| 239 SkShader* fAlpha8BitmapShader{nullptr}; | 238 SkShader* fAlpha8BitmapShader{nullptr}; |
| 240 SkShader* fLinearGradientShader{nullptr}; | 239 SkShader* fLinearGradientShader{nullptr}; |
| 241 | 240 |
| 242 typedef GM INHERITED; | 241 typedef GM INHERITED; |
| 243 }; | 242 }; |
| 244 | 243 |
| 245 ////////////////////////////////////////////////////////////////////////////// | 244 ////////////////////////////////////////////////////////////////////////////// |
| 246 | 245 |
| 247 DEF_GM( return new ComposeShaderGM; ) | 246 DEF_GM( return new ComposeShaderGM; ) |
| 248 DEF_GM( return new ComposeShaderAlphaGM; ) | 247 DEF_GM( return new ComposeShaderAlphaGM; ) |
| 249 DEF_GM( return new ComposeShaderBitmapGM; ) | 248 DEF_GM( return new ComposeShaderBitmapGM; ) |
| OLD | NEW |