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
9 #include "gm.h"
10 #include "SkGradientShader.h"
11 #include "SkPatchUtils.h"
12
make_shader()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, nullptr, SK_ARRAY_COUNT(colors),
21 SkShader::kMirror_TileMode);
22 }
23
draw_control_points(SkCanvas * canvas,const SkPoint cubics[12])24 static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
25 //draw control points
26 SkPaint paint;
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);
35
36 paint.setColor(SK_ColorBLACK);
37 paint.setStrokeWidth(0.5f);
38 SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
39 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
40 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint);
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);
44
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);
48
49 paint.setStrokeWidth(2);
50
51 paint.setColor(SK_ColorRED);
52 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
53
54 paint.setColor(SK_ColorBLUE);
55 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint);
56
57 paint.setColor(SK_ColorCYAN);
58 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint);
59
60 paint.setColor(SK_ColorYELLOW);
61 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint);
62
63 paint.setColor(SK_ColorGREEN);
64 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint);
65 }
66
67 DEF_SIMPLE_GM(patch_primitive, canvas, 800, 800) {
68 SkPaint paint;
69
70 // The order of the colors and points is clockwise starting at upper-left corner.
71 const SkPoint cubics[SkPatchUtils::kNumCtrlPts] = {
72 //top points
73 {100,100},{150,50},{250,150}, {300,100},
74 //right points
75 {250, 150},{350,250},
76 //bottom points
77 {300,300},{250,250},{150,350},{100,300},
78 //left points
79 {50,250},{150,150}
80 };
81
82 const SkColor colors[SkPatchUtils::kNumCorners] = {
83 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
84 };
85 const SkPoint texCoords[SkPatchUtils::kNumCorners] = {
86 {0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f}}
87 ;
88
89 const SkXfermode::Mode modes[] = {
90 SkXfermode::kSrc_Mode,
91 SkXfermode::kDst_Mode,
92 SkXfermode::kModulate_Mode,
93 };
94
95 SkAutoTUnref<SkShader> shader(make_shader());
96
97 canvas->save();
98 for (int y = 0; y < 3; y++) {
99 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(modes[y]));
100
101 for (int x = 0; x < 4; x++) {
102 canvas->save();
103 canvas->translate(x * 350.0f, y * 350.0f);
104 switch (x) {
105 case 0:
106 canvas->drawPatch(cubics, nullptr, nullptr, xfer, paint);
107 break;
108 case 1:
109 canvas->drawPatch(cubics, colors, nullptr, xfer, paint);
110 break;
111 case 2:
112 paint.setShader(shader);
113 canvas->drawPatch(cubics, nullptr, texCoords, xfer, paint);
114 paint.setShader(nullptr);
115 break;
116 case 3:
117 paint.setShader(shader);
118 canvas->drawPatch(cubics, colors, texCoords, xfer, paint);
119 paint.setShader(nullptr);
120 break;
121 default:
122 break;
123 }
124
125 draw_control_points(canvas, cubics);
126 canvas->restore();
127 }
128 }
129 canvas->restore();
130 }
131