blob: 78a3071812a75bf0974f5198136b3d99f9026855 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/effects/SkGradientShader.h"
22#include "src/utils/SkPatchUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "tools/Resources.h"
dandov50d71542014-07-25 10:44:53 -070024
reed1a9b9642016-03-13 14:13:58 -070025static sk_sp<SkShader> make_shader() {
dandovb3c9d1c2014-08-12 08:34:29 -070026 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 } };
halcanary9d524f22016-03-29 09:03:52 -070031
reed1a9b9642016-03-13 14:13:58 -070032 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040033 SkTileMode::kMirror);
dandovb3c9d1c2014-08-12 08:34:29 -070034}
dandov50d71542014-07-25 10:44:53 -070035
dandovb3c9d1c2014-08-12 08:34:29 -070036static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
dandovecfff212014-08-04 10:02:00 -070037 //draw control points
dandov963137b2014-08-07 07:49:53 -070038 SkPaint paint;
dandovb3c9d1c2014-08-12 08:34:29 -070039 SkPoint bottom[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040040 SkPatchUtils::GetBottomCubic(cubics, bottom);
dandovb3c9d1c2014-08-12 08:34:29 -070041 SkPoint top[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040042 SkPatchUtils::GetTopCubic(cubics, top);
dandovb3c9d1c2014-08-12 08:34:29 -070043 SkPoint left[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040044 SkPatchUtils::GetLeftCubic(cubics, left);
dandovb3c9d1c2014-08-12 08:34:29 -070045 SkPoint right[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040046 SkPatchUtils::GetRightCubic(cubics, right);
dandovecfff212014-08-04 10:02:00 -070047
dandov963137b2014-08-07 07:49:53 -070048 paint.setColor(SK_ColorBLACK);
dandovb3c9d1c2014-08-12 08:34:29 -070049 paint.setStrokeWidth(0.5f);
dandovecfff212014-08-04 10:02:00 -070050 SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
dandov963137b2014-08-07 07:49:53 -070051 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
dandovb3c9d1c2014-08-12 08:34:29 -070052 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint);
dandov963137b2014-08-07 07:49:53 -070053 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);
dandovecfff212014-08-04 10:02:00 -070056
dandovb3c9d1c2014-08-12 08:34:29 -070057 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);
dandovecfff212014-08-04 10:02:00 -070060
dandov963137b2014-08-07 07:49:53 -070061 paint.setStrokeWidth(2);
dandovecfff212014-08-04 10:02:00 -070062
dandov963137b2014-08-07 07:49:53 -070063 paint.setColor(SK_ColorRED);
64 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
dandovecfff212014-08-04 10:02:00 -070065
dandov963137b2014-08-07 07:49:53 -070066 paint.setColor(SK_ColorBLUE);
dandovb3c9d1c2014-08-12 08:34:29 -070067 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint);
dandovecfff212014-08-04 10:02:00 -070068
dandov963137b2014-08-07 07:49:53 -070069 paint.setColor(SK_ColorCYAN);
dandovb3c9d1c2014-08-12 08:34:29 -070070 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint);
dandovecfff212014-08-04 10:02:00 -070071
dandov963137b2014-08-07 07:49:53 -070072 paint.setColor(SK_ColorYELLOW);
dandovb3c9d1c2014-08-12 08:34:29 -070073 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint);
dandovecfff212014-08-04 10:02:00 -070074
dandov963137b2014-08-07 07:49:53 -070075 paint.setColor(SK_ColorGREEN);
dandovb3c9d1c2014-08-12 08:34:29 -070076 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint);
dandovecfff212014-08-04 10:02:00 -070077}
78
Mike Reedb8abb4c2017-05-23 09:53:37 -040079// The order of the colors and points is clockwise starting at upper-left corner.
80const 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};
halcanary9d524f22016-03-29 09:03:52 -070090
Mike Reedb8abb4c2017-05-23 09:53:37 -040091const SkPoint gTexCoords[SkPatchUtils::kNumCorners] = {
92 {0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f}
93};
halcanary9d524f22016-03-29 09:03:52 -070094
halcanary9d524f22016-03-29 09:03:52 -070095
Mike Reed0ae1b3d2020-03-04 17:47:43 -050096static void dopatch(SkCanvas* canvas, const SkColor colors[], sk_sp<SkImage> img,
97 const SkMatrix* localMatrix) {
Mike Reedb8abb4c2017-05-23 09:53:37 -040098 SkPaint paint;
halcanary9d524f22016-03-29 09:03:52 -070099
Mike Reedb8abb4c2017-05-23 09:53:37 -0400100 const SkBlendMode modes[] = {
101 SkBlendMode::kSrc,
102 SkBlendMode::kDst,
103 SkBlendMode::kModulate,
104 };
halcanary9d524f22016-03-29 09:03:52 -0700105
Mike Reedd36968b2017-05-23 21:27:59 -0400106 SkPoint texStorage[4];
107 const SkPoint* tex = gTexCoords;
108
109 sk_sp<SkShader> shader;
110 if (img) {
111 SkScalar w = img->width();
112 SkScalar h = img->height();
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500113 shader = img->makeShader(localMatrix);
Mike Reedd36968b2017-05-23 21:27:59 -0400114 texStorage[0].set(0, 0);
115 texStorage[1].set(w, 0);
116 texStorage[2].set(w, h);
117 texStorage[3].set(0, h);
118 tex = texStorage;
119 } else {
120 shader = make_shader();
121 }
halcanary9d524f22016-03-29 09:03:52 -0700122
Mike Reedb8abb4c2017-05-23 09:53:37 -0400123 canvas->save();
124 for (int y = 0; y < 3; y++) {
125 for (int x = 0; x < 4; x++) {
126 canvas->save();
127 canvas->translate(x * 350.0f, y * 350.0f);
128 switch (x) {
129 case 0:
130 canvas->drawPatch(gCubics, nullptr, nullptr, modes[y], paint);
131 break;
132 case 1:
133 canvas->drawPatch(gCubics, colors, nullptr, modes[y], paint);
134 break;
135 case 2:
136 paint.setShader(shader);
Mike Reedd36968b2017-05-23 21:27:59 -0400137 canvas->drawPatch(gCubics, nullptr, tex, modes[y], paint);
Mike Reedb8abb4c2017-05-23 09:53:37 -0400138 paint.setShader(nullptr);
139 break;
140 case 3:
141 paint.setShader(shader);
Mike Reedd36968b2017-05-23 21:27:59 -0400142 canvas->drawPatch(gCubics, colors, tex, modes[y], paint);
Mike Reedb8abb4c2017-05-23 09:53:37 -0400143 paint.setShader(nullptr);
144 break;
145 default:
146 break;
dandovb3c9d1c2014-08-12 08:34:29 -0700147 }
Mike Reedb8abb4c2017-05-23 09:53:37 -0400148
149 draw_control_points(canvas, gCubics);
150 canvas->restore();
dandovecfff212014-08-04 10:02:00 -0700151 }
Mike Reedb8abb4c2017-05-23 09:53:37 -0400152 }
153 canvas->restore();
dandov50d71542014-07-25 10:44:53 -0700154}
Mike Reedb8abb4c2017-05-23 09:53:37 -0400155
156DEF_SIMPLE_GM(patch_primitive, canvas, 1500, 1100) {
157 const SkColor colors[SkPatchUtils::kNumCorners] = {
158 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
159 };
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500160 dopatch(canvas, colors, nullptr, nullptr);
Mike Reedb8abb4c2017-05-23 09:53:37 -0400161}
Mike Reedd36968b2017-05-23 21:27:59 -0400162DEF_SIMPLE_GM(patch_image, canvas, 1500, 1100) {
163 const SkColor colors[SkPatchUtils::kNumCorners] = {
164 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
165 };
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500166 dopatch(canvas, colors, GetResourceAsImage("images/mandrill_128.png"), nullptr);
167}
168DEF_SIMPLE_GM(patch_image_persp, canvas, 1500, 1100) {
169 const SkColor colors[SkPatchUtils::kNumCorners] = {
170 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
171 };
172 SkMatrix localM;
173 localM.reset();
174 localM[6] = 0.00001f; // force perspective
175 dopatch(canvas, colors, GetResourceAsImage("images/mandrill_128.png"), &localM);
Mike Reedd36968b2017-05-23 21:27:59 -0400176}
Mike Reedb8abb4c2017-05-23 09:53:37 -0400177DEF_SIMPLE_GM(patch_alpha, canvas, 1500, 1100) {
178 const SkColor colors[SkPatchUtils::kNumCorners] = {
179 SK_ColorRED, 0x0000FF00, SK_ColorBLUE, 0x00FF00FF,
180 };
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500181 dopatch(canvas, colors, nullptr, nullptr);
Mike Reedb8abb4c2017-05-23 09:53:37 -0400182}
183
184// These two should look the same (one patch, one simple path)
185DEF_SIMPLE_GM(patch_alpha_test, canvas, 550, 250) {
186 canvas->translate(-75, -75);
187
188 const SkColor colors[SkPatchUtils::kNumCorners] = {
189 0x80FF0000, 0x80FF0000, 0x80FF0000, 0x80FF0000,
190 };
191 SkPaint paint;
192 canvas->drawPatch(gCubics, colors, nullptr, SkBlendMode::kModulate, paint);
193
194 canvas->translate(300, 0);
195
196 SkPath path;
197 path.moveTo(gCubics[0]);
198 path.cubicTo(gCubics[ 1], gCubics[ 2], gCubics[ 3]);
199 path.cubicTo(gCubics[ 4], gCubics[ 5], gCubics[ 6]);
200 path.cubicTo(gCubics[ 7], gCubics[ 8], gCubics[ 9]);
201 path.cubicTo(gCubics[10], gCubics[11], gCubics[ 0]);
202 paint.setColor(colors[0]);
203 canvas->drawPath(path, paint);
204}
205