dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlendMode.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 15 | #include "include/core/SkPoint.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkShader.h" |
| 19 | #include "include/core/SkTileMode.h" |
| 20 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "include/effects/SkGradientShader.h" |
| 22 | #include "src/utils/SkPatchUtils.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 23 | #include "tools/Resources.h" |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 24 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 25 | static sk_sp<SkShader> make_shader() { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 26 | const SkColor colors[] = { |
| 27 | SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, SK_ColorMAGENTA, SK_ColorBLUE, |
| 28 | SK_ColorYELLOW, |
| 29 | }; |
| 30 | const SkPoint pts[] = { { 100.f / 4.f, 0.f }, { 3.f * 100.f / 4.f, 100.f } }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 31 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 32 | return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 33 | SkTileMode::kMirror); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 34 | } |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 35 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 36 | static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) { |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 37 | //draw control points |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 38 | SkPaint paint; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 39 | SkPoint bottom[SkPatchUtils::kNumPtsCubic]; |
Mike Reed | 4ebb43e | 2017-04-05 11:06:15 -0400 | [diff] [blame] | 40 | SkPatchUtils::GetBottomCubic(cubics, bottom); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 41 | SkPoint top[SkPatchUtils::kNumPtsCubic]; |
Mike Reed | 4ebb43e | 2017-04-05 11:06:15 -0400 | [diff] [blame] | 42 | SkPatchUtils::GetTopCubic(cubics, top); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 43 | SkPoint left[SkPatchUtils::kNumPtsCubic]; |
Mike Reed | 4ebb43e | 2017-04-05 11:06:15 -0400 | [diff] [blame] | 44 | SkPatchUtils::GetLeftCubic(cubics, left); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 45 | SkPoint right[SkPatchUtils::kNumPtsCubic]; |
Mike Reed | 4ebb43e | 2017-04-05 11:06:15 -0400 | [diff] [blame] | 46 | SkPatchUtils::GetRightCubic(cubics, right); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 47 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 48 | paint.setColor(SK_ColorBLACK); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 49 | paint.setStrokeWidth(0.5f); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 50 | SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] }; |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 51 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 52 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint); |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 53 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, top, paint); |
| 54 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, left, paint); |
| 55 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, right, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 56 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 57 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top + 1, paint); |
| 58 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, left + 1, paint); |
| 59 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, right + 1, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 60 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 61 | paint.setStrokeWidth(2); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 62 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 63 | paint.setColor(SK_ColorRED); |
| 64 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 65 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 66 | paint.setColor(SK_ColorBLUE); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 67 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 68 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 69 | paint.setColor(SK_ColorCYAN); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 70 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 71 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 72 | paint.setColor(SK_ColorYELLOW); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 73 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 74 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 75 | paint.setColor(SK_ColorGREEN); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 76 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Mike Reed | b8abb4c | 2017-05-23 09:53:37 -0400 | [diff] [blame] | 79 | // The order of the colors and points is clockwise starting at upper-left corner. |
| 80 | const SkPoint gCubics[SkPatchUtils::kNumCtrlPts] = { |
| 81 | //top points |
| 82 | {100,100},{150,50},{250,150}, {300,100}, |
| 83 | //right points |
| 84 | {250, 150},{350,250}, |
| 85 | //bottom points |
| 86 | {300,300},{250,250},{150,350},{100,300}, |
| 87 | //left points |
| 88 | {50,250},{150,150} |
| 89 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 90 | |
Mike Reed | b8abb4c | 2017-05-23 09:53:37 -0400 | [diff] [blame] | 91 | const SkPoint gTexCoords[SkPatchUtils::kNumCorners] = { |
| 92 | {0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f} |
| 93 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 94 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 95 | |
Mike Reed | 0ae1b3d | 2020-03-04 17:47:43 -0500 | [diff] [blame] | 96 | static void dopatch(SkCanvas* canvas, const SkColor colors[], sk_sp<SkImage> img, |
| 97 | const SkMatrix* localMatrix) { |
Mike Reed | b8abb4c | 2017-05-23 09:53:37 -0400 | [diff] [blame] | 98 | SkPaint paint; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 99 | |
Mike Reed | b8abb4c | 2017-05-23 09:53:37 -0400 | [diff] [blame] | 100 | const SkBlendMode modes[] = { |
| 101 | SkBlendMode::kSrc, |
| 102 | SkBlendMode::kDst, |
| 103 | SkBlendMode::kModulate, |
| 104 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 105 | |
Mike Reed | d36968b | 2017-05-23 21:27:59 -0400 | [diff] [blame] | 106 | SkPoint texStorage[4]; |
| 107 | const SkPoint* tex = gTexCoords; |
| 108 | |
| 109 | sk_sp<SkShader> shader; |
| 110 | if (img) { |
| 111 | SkScalar w = img->width(); |
| 112 | SkScalar h = img->height(); |
Mike Reed | 0ae1b3d | 2020-03-04 17:47:43 -0500 | [diff] [blame] | 113 | shader = img->makeShader(localMatrix); |
Mike Reed | d36968b | 2017-05-23 21:27:59 -0400 | [diff] [blame] | 114 | texStorage[0].set(0, 0); |
| 115 | texStorage[1].set(w, 0); |
| 116 | texStorage[2].set(w, h); |
| 117 | texStorage[3].set(0, h); |
| 118 | tex = texStorage; |
| 119 | } else { |
| 120 | shader = make_shader(); |
| 121 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 122 | |
Mike Reed | b8abb4c | 2017-05-23 09:53:37 -0400 | [diff] [blame] | 123 | canvas->save(); |
| 124 | for (int y = 0; y < 3; y++) { |
| 125 | for (int x = 0; x < 4; x++) { |
| 126 | canvas->save(); |
| 127 | canvas->translate(x * 350.0f, y * 350.0f); |
| 128 | switch (x) { |
| 129 | case 0: |
| 130 | canvas->drawPatch(gCubics, nullptr, nullptr, modes[y], paint); |
| 131 | break; |
| 132 | case 1: |
| 133 | canvas->drawPatch(gCubics, colors, nullptr, modes[y], paint); |
| 134 | break; |
| 135 | case 2: |
| 136 | paint.setShader(shader); |
Mike Reed | d36968b | 2017-05-23 21:27:59 -0400 | [diff] [blame] | 137 | canvas->drawPatch(gCubics, nullptr, tex, modes[y], paint); |
Mike Reed | b8abb4c | 2017-05-23 09:53:37 -0400 | [diff] [blame] | 138 | paint.setShader(nullptr); |
| 139 | break; |
| 140 | case 3: |
| 141 | paint.setShader(shader); |
Mike Reed | d36968b | 2017-05-23 21:27:59 -0400 | [diff] [blame] | 142 | canvas->drawPatch(gCubics, colors, tex, modes[y], paint); |
Mike Reed | b8abb4c | 2017-05-23 09:53:37 -0400 | [diff] [blame] | 143 | paint.setShader(nullptr); |
| 144 | break; |
| 145 | default: |
| 146 | break; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 147 | } |
Mike Reed | b8abb4c | 2017-05-23 09:53:37 -0400 | [diff] [blame] | 148 | |
| 149 | draw_control_points(canvas, gCubics); |
| 150 | canvas->restore(); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 151 | } |
Mike Reed | b8abb4c | 2017-05-23 09:53:37 -0400 | [diff] [blame] | 152 | } |
| 153 | canvas->restore(); |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 154 | } |
Mike Reed | b8abb4c | 2017-05-23 09:53:37 -0400 | [diff] [blame] | 155 | |
| 156 | DEF_SIMPLE_GM(patch_primitive, canvas, 1500, 1100) { |
| 157 | const SkColor colors[SkPatchUtils::kNumCorners] = { |
| 158 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN |
| 159 | }; |
Mike Reed | 0ae1b3d | 2020-03-04 17:47:43 -0500 | [diff] [blame] | 160 | dopatch(canvas, colors, nullptr, nullptr); |
Mike Reed | b8abb4c | 2017-05-23 09:53:37 -0400 | [diff] [blame] | 161 | } |
Mike Reed | d36968b | 2017-05-23 21:27:59 -0400 | [diff] [blame] | 162 | DEF_SIMPLE_GM(patch_image, canvas, 1500, 1100) { |
| 163 | const SkColor colors[SkPatchUtils::kNumCorners] = { |
| 164 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN |
| 165 | }; |
Mike Reed | 0ae1b3d | 2020-03-04 17:47:43 -0500 | [diff] [blame] | 166 | dopatch(canvas, colors, GetResourceAsImage("images/mandrill_128.png"), nullptr); |
| 167 | } |
| 168 | DEF_SIMPLE_GM(patch_image_persp, canvas, 1500, 1100) { |
| 169 | const SkColor colors[SkPatchUtils::kNumCorners] = { |
| 170 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN |
| 171 | }; |
| 172 | SkMatrix localM; |
| 173 | localM.reset(); |
| 174 | localM[6] = 0.00001f; // force perspective |
| 175 | dopatch(canvas, colors, GetResourceAsImage("images/mandrill_128.png"), &localM); |
Mike Reed | d36968b | 2017-05-23 21:27:59 -0400 | [diff] [blame] | 176 | } |
Mike Reed | b8abb4c | 2017-05-23 09:53:37 -0400 | [diff] [blame] | 177 | DEF_SIMPLE_GM(patch_alpha, canvas, 1500, 1100) { |
| 178 | const SkColor colors[SkPatchUtils::kNumCorners] = { |
| 179 | SK_ColorRED, 0x0000FF00, SK_ColorBLUE, 0x00FF00FF, |
| 180 | }; |
Mike Reed | 0ae1b3d | 2020-03-04 17:47:43 -0500 | [diff] [blame] | 181 | dopatch(canvas, colors, nullptr, nullptr); |
Mike Reed | b8abb4c | 2017-05-23 09:53:37 -0400 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // These two should look the same (one patch, one simple path) |
| 185 | DEF_SIMPLE_GM(patch_alpha_test, canvas, 550, 250) { |
| 186 | canvas->translate(-75, -75); |
| 187 | |
| 188 | const SkColor colors[SkPatchUtils::kNumCorners] = { |
| 189 | 0x80FF0000, 0x80FF0000, 0x80FF0000, 0x80FF0000, |
| 190 | }; |
| 191 | SkPaint paint; |
| 192 | canvas->drawPatch(gCubics, colors, nullptr, SkBlendMode::kModulate, paint); |
| 193 | |
| 194 | canvas->translate(300, 0); |
| 195 | |
| 196 | SkPath path; |
| 197 | path.moveTo(gCubics[0]); |
| 198 | path.cubicTo(gCubics[ 1], gCubics[ 2], gCubics[ 3]); |
| 199 | path.cubicTo(gCubics[ 4], gCubics[ 5], gCubics[ 6]); |
| 200 | path.cubicTo(gCubics[ 7], gCubics[ 8], gCubics[ 9]); |
| 201 | path.cubicTo(gCubics[10], gCubics[11], gCubics[ 0]); |
| 202 | paint.setColor(colors[0]); |
| 203 | canvas->drawPath(path, paint); |
| 204 | } |
| 205 | |