• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "gm/gm.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPath.h"
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"
21 #include "include/effects/SkGradientShader.h"
22 #include "src/utils/SkPatchUtils.h"
23 #include "tools/Resources.h"
24 
make_shader()25 static sk_sp<SkShader> make_shader() {
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 } };
31 
32     return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
33                                         SkTileMode::kMirror);
34 }
35 
draw_control_points(SkCanvas * canvas,const SkPoint cubics[12])36 static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
37     //draw control points
38     SkPaint paint;
39     SkPoint bottom[SkPatchUtils::kNumPtsCubic];
40     SkPatchUtils::GetBottomCubic(cubics, bottom);
41     SkPoint top[SkPatchUtils::kNumPtsCubic];
42     SkPatchUtils::GetTopCubic(cubics, top);
43     SkPoint left[SkPatchUtils::kNumPtsCubic];
44     SkPatchUtils::GetLeftCubic(cubics, left);
45     SkPoint right[SkPatchUtils::kNumPtsCubic];
46     SkPatchUtils::GetRightCubic(cubics, right);
47 
48     paint.setColor(SK_ColorBLACK);
49     paint.setStrokeWidth(0.5f);
50     SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
51     canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
52     canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint);
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);
56 
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);
60 
61     paint.setStrokeWidth(2);
62 
63     paint.setColor(SK_ColorRED);
64     canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
65 
66     paint.setColor(SK_ColorBLUE);
67     canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint);
68 
69     paint.setColor(SK_ColorCYAN);
70     canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint);
71 
72     paint.setColor(SK_ColorYELLOW);
73     canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint);
74 
75     paint.setColor(SK_ColorGREEN);
76     canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint);
77 }
78 
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 };
90 
91 const SkPoint gTexCoords[SkPatchUtils::kNumCorners] = {
92     {0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f}
93 };
94 
95 
dopatch(SkCanvas * canvas,const SkColor colors[],sk_sp<SkImage> img,const SkMatrix * localMatrix)96 static void dopatch(SkCanvas* canvas, const SkColor colors[], sk_sp<SkImage> img,
97                     const SkMatrix* localMatrix) {
98     SkPaint paint;
99     paint.setColor(SK_ColorGREEN);
100 
101     const SkBlendMode modes[] = {
102         SkBlendMode::kSrc,
103         SkBlendMode::kDst,
104         SkBlendMode::kColorDodge,
105     };
106 
107     SkPoint texStorage[4];
108     const SkPoint* tex = gTexCoords;
109 
110     sk_sp<SkShader> shader;
111     if (img) {
112         SkScalar w = img->width();
113         SkScalar h = img->height();
114         shader = img->makeShader(SkSamplingOptions(), localMatrix);
115         texStorage[0].set(0, 0);
116         texStorage[1].set(w, 0);
117         texStorage[2].set(w, h);
118         texStorage[3].set(0, h);
119         tex = texStorage;
120     } else {
121         shader = make_shader();
122     }
123 
124     canvas->save();
125     for (int y = 0; y < 3; y++) {
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(gCubics, nullptr, nullptr, modes[y], paint);
132                     break;
133                 case 1:
134                     canvas->drawPatch(gCubics, colors, nullptr, modes[y], paint);
135                     break;
136                 case 2:
137                     paint.setShader(shader);
138                     canvas->drawPatch(gCubics, nullptr, tex, modes[y], paint);
139                     paint.setShader(nullptr);
140                     break;
141                 case 3:
142                     paint.setShader(shader);
143                     canvas->drawPatch(gCubics, colors, tex, modes[y], paint);
144                     paint.setShader(nullptr);
145                     break;
146                 default:
147                     break;
148             }
149 
150             draw_control_points(canvas, gCubics);
151             canvas->restore();
152         }
153     }
154     canvas->restore();
155 }
156 
157 DEF_SIMPLE_GM(patch_primitive, canvas, 1500, 1100) {
158     const SkColor colors[SkPatchUtils::kNumCorners] = {
159         SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
160     };
161     dopatch(canvas, colors, nullptr, nullptr);
162 }
163 DEF_SIMPLE_GM(patch_image, canvas, 1500, 1100) {
164     const SkColor colors[SkPatchUtils::kNumCorners] = {
165         SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
166     };
167     dopatch(canvas, colors, GetResourceAsImage("images/mandrill_128.png"), nullptr);
168 }
169 DEF_SIMPLE_GM(patch_image_persp, canvas, 1500, 1100) {
170     const SkColor colors[SkPatchUtils::kNumCorners] = {
171         SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
172     };
173     SkMatrix localM;
174     localM.reset();
175     localM[6] = 0.00001f;    // force perspective
176     dopatch(canvas, colors, GetResourceAsImage("images/mandrill_128.png"), &localM);
177 }
178 DEF_SIMPLE_GM(patch_alpha, canvas, 1500, 1100) {
179     const SkColor colors[SkPatchUtils::kNumCorners] = {
180         SK_ColorRED, 0x0000FF00, SK_ColorBLUE, 0x00FF00FF,
181     };
182     dopatch(canvas, colors, nullptr, nullptr);
183 }
184 
185 // These two should look the same (one patch, one simple path)
186 DEF_SIMPLE_GM(patch_alpha_test, canvas, 550, 250) {
187     canvas->translate(-75, -75);
188 
189     const SkColor colors[SkPatchUtils::kNumCorners] = {
190         0x80FF0000, 0x80FF0000, 0x80FF0000, 0x80FF0000,
191     };
192     SkPaint paint;
193     canvas->drawPatch(gCubics, colors, nullptr, SkBlendMode::kDst, paint);
194 
195     canvas->translate(300, 0);
196 
197     SkPath path;
198     path.moveTo(gCubics[0]);
199     path.cubicTo(gCubics[ 1], gCubics[ 2], gCubics[ 3]);
200     path.cubicTo(gCubics[ 4], gCubics[ 5], gCubics[ 6]);
201     path.cubicTo(gCubics[ 7], gCubics[ 8], gCubics[ 9]);
202     path.cubicTo(gCubics[10], gCubics[11], gCubics[ 0]);
203     paint.setColor(colors[0]);
204     canvas->drawPath(path, paint);
205 }
206 
207