dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 9 | #include "gm.h" |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 10 | #include "SkGradientShader.h" |
| 11 | #include "SkPatchUtils.h" |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 12 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 13 | static SkShader* make_shader() { |
| 14 | const SkColor colors[] = { |
| 15 | SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, SK_ColorMAGENTA, SK_ColorBLUE, |
| 16 | SK_ColorYELLOW, |
| 17 | }; |
| 18 | const SkPoint pts[] = { { 100.f / 4.f, 0.f }, { 3.f * 100.f / 4.f, 100.f } }; |
| 19 | |
| 20 | return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), |
| 21 | SkShader::kMirror_TileMode); |
| 22 | } |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 23 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 24 | static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) { |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 25 | //draw control points |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 26 | SkPaint paint; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 27 | SkPoint bottom[SkPatchUtils::kNumPtsCubic]; |
| 28 | SkPatchUtils::getBottomCubic(cubics, bottom); |
| 29 | SkPoint top[SkPatchUtils::kNumPtsCubic]; |
| 30 | SkPatchUtils::getTopCubic(cubics, top); |
| 31 | SkPoint left[SkPatchUtils::kNumPtsCubic]; |
| 32 | SkPatchUtils::getLeftCubic(cubics, left); |
| 33 | SkPoint right[SkPatchUtils::kNumPtsCubic]; |
| 34 | SkPatchUtils::getRightCubic(cubics, right); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 35 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 36 | paint.setColor(SK_ColorBLACK); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 37 | paint.setStrokeWidth(0.5f); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 38 | SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] }; |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 39 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 40 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint); |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 41 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, top, paint); |
| 42 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, left, paint); |
| 43 | canvas->drawPoints(SkCanvas::kLines_PointMode, 4, right, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 44 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 45 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top + 1, paint); |
| 46 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, left + 1, paint); |
| 47 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, right + 1, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 48 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 49 | paint.setStrokeWidth(2); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 50 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 51 | paint.setColor(SK_ColorRED); |
| 52 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 53 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 54 | paint.setColor(SK_ColorBLUE); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 55 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 56 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 57 | paint.setColor(SK_ColorCYAN); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 58 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 59 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 60 | paint.setColor(SK_ColorYELLOW); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 61 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint); |
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_ColorGREEN); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 64 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 65 | } |
| 66 | |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 67 | namespace skiagm { |
| 68 | /** |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 69 | * This GM draws a cubics coons patch using the specialized call SkCanvas::drawPatch. |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 70 | */ |
| 71 | class SkPatchGM : public GM { |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 72 | |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 73 | public: |
| 74 | SkPatchGM() { |
| 75 | this->setBGColor(0xFFFFFFFF); |
| 76 | } |
| 77 | |
| 78 | protected: |
| 79 | virtual SkString onShortName() SK_OVERRIDE { |
| 80 | return SkString("patch_primitive"); |
| 81 | } |
| 82 | |
| 83 | virtual SkISize onISize() SK_OVERRIDE { |
| 84 | return SkISize::Make(800, 800); |
| 85 | } |
| 86 | |
| 87 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 88 | return kSkipTiled_Flag; |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 89 | } |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 90 | |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 91 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
dandov | cc03adb | 2014-08-12 17:14:57 -0700 | [diff] [blame] | 92 | |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 93 | SkPaint paint; |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 94 | |
| 95 | // 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] | 96 | const SkPoint cubics[SkPatchUtils::kNumCtrlPts] = { |
| 97 | //top points |
| 98 | {100,100},{150,50},{250,150}, {300,100}, |
| 99 | //right points |
| 100 | {250, 150},{350,250}, |
| 101 | //bottom points |
| 102 | {300,300},{250,250},{150,350},{100,300}, |
| 103 | //left points |
| 104 | {50,250},{150,150} |
| 105 | }; |
| 106 | |
| 107 | const SkColor colors[SkPatchUtils::kNumCorners] = { |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 108 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN |
| 109 | }; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 110 | const SkPoint texCoords[SkPatchUtils::kNumCorners] = { |
| 111 | {0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f}} |
| 112 | ; |
| 113 | |
| 114 | const SkXfermode::Mode modes[] = { |
| 115 | SkXfermode::kSrc_Mode, |
| 116 | SkXfermode::kDst_Mode, |
| 117 | SkXfermode::kModulate_Mode, |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 118 | }; |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 119 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 120 | SkAutoTUnref<SkShader> shader(make_shader()); |
| 121 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 122 | canvas->save(); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 123 | for (int y = 0; y < 3; y++) { |
| 124 | SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(modes[y])); |
| 125 | |
| 126 | for (int x = 0; x < 4; x++) { |
| 127 | canvas->save(); |
| 128 | canvas->translate(x * 350.0f, y * 350.0f); |
| 129 | switch (x) { |
| 130 | case 0: |
| 131 | canvas->drawPatch(cubics, NULL, NULL, xfer, paint); |
| 132 | break; |
| 133 | case 1: |
| 134 | canvas->drawPatch(cubics, colors, NULL, xfer, paint); |
| 135 | break; |
| 136 | case 2: |
| 137 | paint.setShader(shader); |
| 138 | canvas->drawPatch(cubics, NULL, texCoords, xfer, paint); |
| 139 | paint.setShader(NULL); |
| 140 | break; |
| 141 | case 3: |
| 142 | paint.setShader(shader); |
| 143 | canvas->drawPatch(cubics, colors, texCoords, xfer, paint); |
| 144 | paint.setShader(NULL); |
| 145 | break; |
| 146 | default: |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | draw_control_points(canvas, cubics); |
| 151 | canvas->restore(); |
| 152 | } |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 153 | } |
| 154 | canvas->restore(); |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | private: |
| 158 | typedef GM INHERITED; |
| 159 | }; |
| 160 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 161 | DEF_GM(return SkNEW(SkPatchGM); ) |
dandov | 50d7154 | 2014-07-25 10:44:53 -0700 | [diff] [blame] | 162 | |
| 163 | } |