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 | |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 8 | #include "gm.h" |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 9 | #include "SkGradientShader.h" |
| 10 | #include "SkPatchUtils.h" |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 11 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 12 | static sk_sp<SkShader> make_shader() { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 13 | const SkColor colors[] = { |
| 14 | SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, SK_ColorMAGENTA, SK_ColorBLUE, |
| 15 | SK_ColorYELLOW, |
| 16 | }; |
| 17 | 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] | 18 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 19 | return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 20 | SkShader::kMirror_TileMode); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 21 | } |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 22 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 23 | static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) { |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 24 | //draw control points |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 25 | SkPaint paint; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 26 | SkPoint bottom[SkPatchUtils::kNumPtsCubic]; |
| 27 | SkPatchUtils::getBottomCubic(cubics, bottom); |
| 28 | SkPoint top[SkPatchUtils::kNumPtsCubic]; |
| 29 | SkPatchUtils::getTopCubic(cubics, top); |
| 30 | SkPoint left[SkPatchUtils::kNumPtsCubic]; |
| 31 | SkPatchUtils::getLeftCubic(cubics, left); |
| 32 | SkPoint right[SkPatchUtils::kNumPtsCubic]; |
| 33 | SkPatchUtils::getRightCubic(cubics, right); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 34 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 35 | paint.setColor(SK_ColorBLACK); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 36 | paint.setStrokeWidth(0.5f); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 37 | SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] }; |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 38 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 39 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint); |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 40 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, top, paint); |
| 41 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, left, paint); |
| 42 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, right, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 43 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 44 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top + 1, paint); |
| 45 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, left + 1, paint); |
| 46 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, right + 1, paint); |
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.setStrokeWidth(2); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 49 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 50 | paint.setColor(SK_ColorRED); |
| 51 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 52 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 53 | paint.setColor(SK_ColorBLUE); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 54 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 55 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 56 | paint.setColor(SK_ColorCYAN); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 57 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 58 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 59 | paint.setColor(SK_ColorYELLOW); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 60 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 61 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 62 | paint.setColor(SK_ColorGREEN); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 63 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 64 | } |
| 65 | |
mtklein | 11064df | 2016-04-06 15:01:57 -0700 | [diff] [blame] | 66 | DEF_SIMPLE_GM(patch_primitive, canvas, 1500, 1100) { |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 67 | SkPaint paint; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 68 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 69 | // The order of the colors and points is clockwise starting at upper-left corner. |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 70 | const SkPoint cubics[SkPatchUtils::kNumCtrlPts] = { |
| 71 | //top points |
| 72 | {100,100},{150,50},{250,150}, {300,100}, |
| 73 | //right points |
| 74 | {250, 150},{350,250}, |
| 75 | //bottom points |
| 76 | {300,300},{250,250},{150,350},{100,300}, |
| 77 | //left points |
| 78 | {50,250},{150,150} |
| 79 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 80 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 81 | const SkColor colors[SkPatchUtils::kNumCorners] = { |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 82 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN |
| 83 | }; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 84 | const SkPoint texCoords[SkPatchUtils::kNumCorners] = { |
| 85 | {0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f}} |
| 86 | ; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 87 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 88 | const SkBlendMode modes[] = { |
| 89 | SkBlendMode::kSrc, |
| 90 | SkBlendMode::kDst, |
| 91 | SkBlendMode::kModulate, |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 92 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 93 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 94 | sk_sp<SkShader> shader(make_shader()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 95 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 96 | canvas->save(); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 97 | for (int y = 0; y < 3; y++) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 98 | for (int x = 0; x < 4; x++) { |
| 99 | canvas->save(); |
| 100 | canvas->translate(x * 350.0f, y * 350.0f); |
| 101 | switch (x) { |
| 102 | case 0: |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 103 | canvas->drawPatch(cubics, nullptr, nullptr, modes[y], paint); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 104 | break; |
| 105 | case 1: |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 106 | canvas->drawPatch(cubics, colors, nullptr, modes[y], paint); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 107 | break; |
| 108 | case 2: |
| 109 | paint.setShader(shader); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 110 | canvas->drawPatch(cubics, nullptr, texCoords, modes[y], paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 111 | paint.setShader(nullptr); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 112 | break; |
| 113 | case 3: |
| 114 | paint.setShader(shader); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 115 | canvas->drawPatch(cubics, colors, texCoords, modes[y], paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 116 | paint.setShader(nullptr); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 117 | break; |
| 118 | default: |
| 119 | break; |
| 120 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 121 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 122 | draw_control_points(canvas, cubics); |
| 123 | canvas->restore(); |
| 124 | } |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 125 | } |
| 126 | canvas->restore(); |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 127 | } |