blob: 5cfc8b5d68c07488c0d562cce51a06e00e52bb80 [file] [log] [blame]
dandov50d71542014-07-25 10:44:53 -07001/*
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
dandov50d71542014-07-25 10:44:53 -07008#include "gm.h"
dandovb3c9d1c2014-08-12 08:34:29 -07009#include "SkGradientShader.h"
Mike Reedd36968b2017-05-23 21:27:59 -040010#include "SkImage.h"
dandovb3c9d1c2014-08-12 08:34:29 -070011#include "SkPatchUtils.h"
Mike Reedb8abb4c2017-05-23 09:53:37 -040012#include "SkPath.h"
dandov50d71542014-07-25 10:44:53 -070013
reed1a9b9642016-03-13 14:13:58 -070014static sk_sp<SkShader> make_shader() {
dandovb3c9d1c2014-08-12 08:34:29 -070015 const SkColor colors[] = {
16 SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, SK_ColorMAGENTA, SK_ColorBLUE,
17 SK_ColorYELLOW,
18 };
19 const SkPoint pts[] = { { 100.f / 4.f, 0.f }, { 3.f * 100.f / 4.f, 100.f } };
halcanary9d524f22016-03-29 09:03:52 -070020
reed1a9b9642016-03-13 14:13:58 -070021 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
22 SkShader::kMirror_TileMode);
dandovb3c9d1c2014-08-12 08:34:29 -070023}
dandov50d71542014-07-25 10:44:53 -070024
dandovb3c9d1c2014-08-12 08:34:29 -070025static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
dandovecfff212014-08-04 10:02:00 -070026 //draw control points
dandov963137b2014-08-07 07:49:53 -070027 SkPaint paint;
dandovb3c9d1c2014-08-12 08:34:29 -070028 SkPoint bottom[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040029 SkPatchUtils::GetBottomCubic(cubics, bottom);
dandovb3c9d1c2014-08-12 08:34:29 -070030 SkPoint top[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040031 SkPatchUtils::GetTopCubic(cubics, top);
dandovb3c9d1c2014-08-12 08:34:29 -070032 SkPoint left[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040033 SkPatchUtils::GetLeftCubic(cubics, left);
dandovb3c9d1c2014-08-12 08:34:29 -070034 SkPoint right[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040035 SkPatchUtils::GetRightCubic(cubics, right);
dandovecfff212014-08-04 10:02:00 -070036
dandov963137b2014-08-07 07:49:53 -070037 paint.setColor(SK_ColorBLACK);
dandovb3c9d1c2014-08-12 08:34:29 -070038 paint.setStrokeWidth(0.5f);
dandovecfff212014-08-04 10:02:00 -070039 SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
dandov963137b2014-08-07 07:49:53 -070040 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
dandovb3c9d1c2014-08-12 08:34:29 -070041 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint);
dandov963137b2014-08-07 07:49:53 -070042 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, top, paint);
43 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, left, paint);
44 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, right, paint);
dandovecfff212014-08-04 10:02:00 -070045
dandovb3c9d1c2014-08-12 08:34:29 -070046 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top + 1, paint);
47 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, left + 1, paint);
48 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, right + 1, paint);
dandovecfff212014-08-04 10:02:00 -070049
dandov963137b2014-08-07 07:49:53 -070050 paint.setStrokeWidth(2);
dandovecfff212014-08-04 10:02:00 -070051
dandov963137b2014-08-07 07:49:53 -070052 paint.setColor(SK_ColorRED);
53 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
dandovecfff212014-08-04 10:02:00 -070054
dandov963137b2014-08-07 07:49:53 -070055 paint.setColor(SK_ColorBLUE);
dandovb3c9d1c2014-08-12 08:34:29 -070056 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint);
dandovecfff212014-08-04 10:02:00 -070057
dandov963137b2014-08-07 07:49:53 -070058 paint.setColor(SK_ColorCYAN);
dandovb3c9d1c2014-08-12 08:34:29 -070059 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint);
dandovecfff212014-08-04 10:02:00 -070060
dandov963137b2014-08-07 07:49:53 -070061 paint.setColor(SK_ColorYELLOW);
dandovb3c9d1c2014-08-12 08:34:29 -070062 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint);
dandovecfff212014-08-04 10:02:00 -070063
dandov963137b2014-08-07 07:49:53 -070064 paint.setColor(SK_ColorGREEN);
dandovb3c9d1c2014-08-12 08:34:29 -070065 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint);
dandovecfff212014-08-04 10:02:00 -070066}
67
Mike Reedb8abb4c2017-05-23 09:53:37 -040068// The order of the colors and points is clockwise starting at upper-left corner.
69const SkPoint gCubics[SkPatchUtils::kNumCtrlPts] = {
70 //top points
71 {100,100},{150,50},{250,150}, {300,100},
72 //right points
73 {250, 150},{350,250},
74 //bottom points
75 {300,300},{250,250},{150,350},{100,300},
76 //left points
77 {50,250},{150,150}
78};
halcanary9d524f22016-03-29 09:03:52 -070079
Mike Reedb8abb4c2017-05-23 09:53:37 -040080const SkPoint gTexCoords[SkPatchUtils::kNumCorners] = {
81 {0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f}
82};
halcanary9d524f22016-03-29 09:03:52 -070083
halcanary9d524f22016-03-29 09:03:52 -070084
Mike Reedd36968b2017-05-23 21:27:59 -040085static void dopatch(SkCanvas* canvas, const SkColor colors[], sk_sp<SkImage> img = nullptr) {
Mike Reedb8abb4c2017-05-23 09:53:37 -040086 SkPaint paint;
halcanary9d524f22016-03-29 09:03:52 -070087
Mike Reedb8abb4c2017-05-23 09:53:37 -040088 const SkBlendMode modes[] = {
89 SkBlendMode::kSrc,
90 SkBlendMode::kDst,
91 SkBlendMode::kModulate,
92 };
halcanary9d524f22016-03-29 09:03:52 -070093
Mike Reedd36968b2017-05-23 21:27:59 -040094 SkPoint texStorage[4];
95 const SkPoint* tex = gTexCoords;
96
97 sk_sp<SkShader> shader;
98 if (img) {
99 SkScalar w = img->width();
100 SkScalar h = img->height();
101 shader = img->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
102 texStorage[0].set(0, 0);
103 texStorage[1].set(w, 0);
104 texStorage[2].set(w, h);
105 texStorage[3].set(0, h);
106 tex = texStorage;
107 } else {
108 shader = make_shader();
109 }
halcanary9d524f22016-03-29 09:03:52 -0700110
Mike Reedb8abb4c2017-05-23 09:53:37 -0400111 canvas->save();
112 for (int y = 0; y < 3; y++) {
113 for (int x = 0; x < 4; x++) {
114 canvas->save();
115 canvas->translate(x * 350.0f, y * 350.0f);
116 switch (x) {
117 case 0:
118 canvas->drawPatch(gCubics, nullptr, nullptr, modes[y], paint);
119 break;
120 case 1:
121 canvas->drawPatch(gCubics, colors, nullptr, modes[y], paint);
122 break;
123 case 2:
124 paint.setShader(shader);
Mike Reedd36968b2017-05-23 21:27:59 -0400125 canvas->drawPatch(gCubics, nullptr, tex, modes[y], paint);
Mike Reedb8abb4c2017-05-23 09:53:37 -0400126 paint.setShader(nullptr);
127 break;
128 case 3:
129 paint.setShader(shader);
Mike Reedd36968b2017-05-23 21:27:59 -0400130 canvas->drawPatch(gCubics, colors, tex, modes[y], paint);
Mike Reedb8abb4c2017-05-23 09:53:37 -0400131 paint.setShader(nullptr);
132 break;
133 default:
134 break;
dandovb3c9d1c2014-08-12 08:34:29 -0700135 }
Mike Reedb8abb4c2017-05-23 09:53:37 -0400136
137 draw_control_points(canvas, gCubics);
138 canvas->restore();
dandovecfff212014-08-04 10:02:00 -0700139 }
Mike Reedb8abb4c2017-05-23 09:53:37 -0400140 }
141 canvas->restore();
dandov50d71542014-07-25 10:44:53 -0700142}
Mike Reedb8abb4c2017-05-23 09:53:37 -0400143
144DEF_SIMPLE_GM(patch_primitive, canvas, 1500, 1100) {
145 const SkColor colors[SkPatchUtils::kNumCorners] = {
146 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
147 };
148 dopatch(canvas, colors);
149}
Mike Reedd36968b2017-05-23 21:27:59 -0400150#include "Resources.h"
151DEF_SIMPLE_GM(patch_image, canvas, 1500, 1100) {
152 const SkColor colors[SkPatchUtils::kNumCorners] = {
153 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
154 };
Hal Canaryc465d132017-12-08 10:21:31 -0500155 dopatch(canvas, colors, GetResourceAsImage("images/mandrill_128.png"));
Mike Reedd36968b2017-05-23 21:27:59 -0400156}
Mike Reedb8abb4c2017-05-23 09:53:37 -0400157DEF_SIMPLE_GM(patch_alpha, canvas, 1500, 1100) {
158 const SkColor colors[SkPatchUtils::kNumCorners] = {
159 SK_ColorRED, 0x0000FF00, SK_ColorBLUE, 0x00FF00FF,
160 };
161 dopatch(canvas, colors);
162}
163
164// These two should look the same (one patch, one simple path)
165DEF_SIMPLE_GM(patch_alpha_test, canvas, 550, 250) {
166 canvas->translate(-75, -75);
167
168 const SkColor colors[SkPatchUtils::kNumCorners] = {
169 0x80FF0000, 0x80FF0000, 0x80FF0000, 0x80FF0000,
170 };
171 SkPaint paint;
172 canvas->drawPatch(gCubics, colors, nullptr, SkBlendMode::kModulate, paint);
173
174 canvas->translate(300, 0);
175
176 SkPath path;
177 path.moveTo(gCubics[0]);
178 path.cubicTo(gCubics[ 1], gCubics[ 2], gCubics[ 3]);
179 path.cubicTo(gCubics[ 4], gCubics[ 5], gCubics[ 6]);
180 path.cubicTo(gCubics[ 7], gCubics[ 8], gCubics[ 9]);
181 path.cubicTo(gCubics[10], gCubics[11], gCubics[ 0]);
182 paint.setColor(colors[0]);
183 canvas->drawPath(path, paint);
184}
185