reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkPaint.h" |
| 12 | #include "include/core/SkPoint.h" |
| 13 | #include "include/core/SkRect.h" |
| 14 | #include "include/core/SkShader.h" |
| 15 | #include "include/core/SkSize.h" |
| 16 | #include "include/core/SkString.h" |
| 17 | #include "include/core/SkTileMode.h" |
| 18 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/effects/SkGradientShader.h" |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 20 | |
| 21 | class AlphaGradientsGM : public skiagm::GM { |
| 22 | public: |
| 23 | AlphaGradientsGM() {} |
| 24 | |
| 25 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 26 | SkString onShortName() override { |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 27 | return SkString("alphagradients"); |
| 28 | } |
| 29 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | SkISize onISize() override { |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 31 | return SkISize::Make(640, 480); |
| 32 | } |
| 33 | |
| 34 | static void draw_grad(SkCanvas* canvas, const SkRect& r, |
| 35 | SkColor c0, SkColor c1, bool doPreMul) { |
| 36 | SkColor colors[] = { c0, c1 }; |
| 37 | SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fBottom } }; |
| 38 | SkPaint paint; |
| 39 | uint32_t flags = doPreMul ? SkGradientShader::kInterpolateColorsInPremul_Flag : 0; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 40 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 41 | SkTileMode::kClamp, flags, nullptr)); |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 42 | canvas->drawRect(r, paint); |
skia.committer@gmail.com | 454ae46 | 2013-05-25 07:01:12 +0000 | [diff] [blame] | 43 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 44 | paint.setShader(nullptr); |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 45 | paint.setStyle(SkPaint::kStroke_Style); |
| 46 | canvas->drawRect(r, paint); |
| 47 | } |
| 48 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 49 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 50 | constexpr struct { |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 51 | SkColor fColor0; |
| 52 | SkColor fColor1; |
| 53 | } gRec[] = { |
| 54 | { 0xFFFFFFFF, 0x00000000 }, |
| 55 | { 0xFFFFFFFF, 0x00FF0000 }, |
| 56 | { 0xFFFFFFFF, 0x00FFFF00 }, |
| 57 | { 0xFFFFFFFF, 0x00FFFFFF }, |
| 58 | { 0xFFFF0000, 0x00000000 }, |
| 59 | { 0xFFFF0000, 0x00FF0000 }, |
| 60 | { 0xFFFF0000, 0x00FFFF00 }, |
| 61 | { 0xFFFF0000, 0x00FFFFFF }, |
| 62 | { 0xFF0000FF, 0x00000000 }, |
| 63 | { 0xFF0000FF, 0x00FF0000 }, |
| 64 | { 0xFF0000FF, 0x00FFFF00 }, |
| 65 | { 0xFF0000FF, 0x00FFFFFF }, |
| 66 | }; |
skia.committer@gmail.com | 454ae46 | 2013-05-25 07:01:12 +0000 | [diff] [blame] | 67 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 68 | SkRect r = SkRect::MakeWH(300, 30); |
skia.committer@gmail.com | 454ae46 | 2013-05-25 07:01:12 +0000 | [diff] [blame] | 69 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 70 | canvas->translate(10, 10); |
| 71 | |
| 72 | for (int doPreMul = 0; doPreMul <= 1; ++doPreMul) { |
| 73 | canvas->save(); |
| 74 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 75 | draw_grad(canvas, r, gRec[i].fColor0, gRec[i].fColor1, SkToBool(doPreMul)); |
| 76 | canvas->translate(0, r.height() + 8); |
| 77 | } |
| 78 | canvas->restore(); |
| 79 | canvas->translate(r.width() + 10, 0); |
| 80 | } |
| 81 | } |
| 82 | |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 83 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 84 | using INHERITED = skiagm::GM; |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 87 | DEF_GM(return new AlphaGradientsGM;) |