blob: b758752a0aaefb414392fde61f5c2a0551be1453 [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"
Kevin Lubick8b741882023-10-06 11:41:38 -040023#include "tools/DecodeUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "tools/Resources.h"
dandov50d71542014-07-25 10:44:53 -070025
reed1a9b9642016-03-13 14:13:58 -070026static sk_sp<SkShader> make_shader() {
dandovb3c9d1c2014-08-12 08:34:29 -070027 const SkColor colors[] = {
28 SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, SK_ColorMAGENTA, SK_ColorBLUE,
29 SK_ColorYELLOW,
30 };
31 const SkPoint pts[] = { { 100.f / 4.f, 0.f }, { 3.f * 100.f / 4.f, 100.f } };
halcanary9d524f22016-03-29 09:03:52 -070032
Herb Derbyc37b3862022-06-21 09:49:17 -040033 return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040034 SkTileMode::kMirror);
dandovb3c9d1c2014-08-12 08:34:29 -070035}
dandov50d71542014-07-25 10:44:53 -070036
dandovb3c9d1c2014-08-12 08:34:29 -070037static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
dandovecfff212014-08-04 10:02:00 -070038 //draw control points
dandov963137b2014-08-07 07:49:53 -070039 SkPaint paint;
dandovb3c9d1c2014-08-12 08:34:29 -070040 SkPoint bottom[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040041 SkPatchUtils::GetBottomCubic(cubics, bottom);
dandovb3c9d1c2014-08-12 08:34:29 -070042 SkPoint top[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040043 SkPatchUtils::GetTopCubic(cubics, top);
dandovb3c9d1c2014-08-12 08:34:29 -070044 SkPoint left[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040045 SkPatchUtils::GetLeftCubic(cubics, left);
dandovb3c9d1c2014-08-12 08:34:29 -070046 SkPoint right[SkPatchUtils::kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -040047 SkPatchUtils::GetRightCubic(cubics, right);
dandovecfff212014-08-04 10:02:00 -070048
dandov963137b2014-08-07 07:49:53 -070049 paint.setColor(SK_ColorBLACK);
dandovb3c9d1c2014-08-12 08:34:29 -070050 paint.setStrokeWidth(0.5f);
dandovecfff212014-08-04 10:02:00 -070051 SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
dandov963137b2014-08-07 07:49:53 -070052 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
dandovb3c9d1c2014-08-12 08:34:29 -070053 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint);
dandov963137b2014-08-07 07:49:53 -070054 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, top, paint);
55 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, left, paint);
56 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, right, paint);
dandovecfff212014-08-04 10:02:00 -070057
dandovb3c9d1c2014-08-12 08:34:29 -070058 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top + 1, paint);
59 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, left + 1, paint);
60 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, right + 1, paint);
dandovecfff212014-08-04 10:02:00 -070061
dandov963137b2014-08-07 07:49:53 -070062 paint.setStrokeWidth(2);
dandovecfff212014-08-04 10:02:00 -070063
dandov963137b2014-08-07 07:49:53 -070064 paint.setColor(SK_ColorRED);
65 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
dandovecfff212014-08-04 10:02:00 -070066
dandov963137b2014-08-07 07:49:53 -070067 paint.setColor(SK_ColorBLUE);
dandovb3c9d1c2014-08-12 08:34:29 -070068 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint);
dandovecfff212014-08-04 10:02:00 -070069
dandov963137b2014-08-07 07:49:53 -070070 paint.setColor(SK_ColorCYAN);
dandovb3c9d1c2014-08-12 08:34:29 -070071 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint);
dandovecfff212014-08-04 10:02:00 -070072
dandov963137b2014-08-07 07:49:53 -070073 paint.setColor(SK_ColorYELLOW);
dandovb3c9d1c2014-08-12 08:34:29 -070074 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint);
dandovecfff212014-08-04 10:02:00 -070075
dandov963137b2014-08-07 07:49:53 -070076 paint.setColor(SK_ColorGREEN);
dandovb3c9d1c2014-08-12 08:34:29 -070077 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint);
dandovecfff212014-08-04 10:02:00 -070078}
79
Mike Reedb8abb4c2017-05-23 09:53:37 -040080// The order of the colors and points is clockwise starting at upper-left corner.
81const SkPoint gCubics[SkPatchUtils::kNumCtrlPts] = {
82 //top points
83 {100,100},{150,50},{250,150}, {300,100},
84 //right points
85 {250, 150},{350,250},
86 //bottom points
87 {300,300},{250,250},{150,350},{100,300},
88 //left points
89 {50,250},{150,150}
90};
halcanary9d524f22016-03-29 09:03:52 -070091
Mike Reedb8abb4c2017-05-23 09:53:37 -040092const SkPoint gTexCoords[SkPatchUtils::kNumCorners] = {
93 {0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f}
94};
halcanary9d524f22016-03-29 09:03:52 -070095
halcanary9d524f22016-03-29 09:03:52 -070096
Mike Reed0ae1b3d2020-03-04 17:47:43 -050097static void dopatch(SkCanvas* canvas, const SkColor colors[], sk_sp<SkImage> img,
98 const SkMatrix* localMatrix) {
Mike Reedb8abb4c2017-05-23 09:53:37 -040099 SkPaint paint;
Brian Salomone00afb02021-11-22 12:22:16 -0500100 paint.setColor(SK_ColorGREEN);
halcanary9d524f22016-03-29 09:03:52 -0700101
Mike Reedb8abb4c2017-05-23 09:53:37 -0400102 const SkBlendMode modes[] = {
103 SkBlendMode::kSrc,
104 SkBlendMode::kDst,
Brian Salomone00afb02021-11-22 12:22:16 -0500105 SkBlendMode::kColorDodge,
Mike Reedb8abb4c2017-05-23 09:53:37 -0400106 };
halcanary9d524f22016-03-29 09:03:52 -0700107
Mike Reedd36968b2017-05-23 21:27:59 -0400108 SkPoint texStorage[4];
109 const SkPoint* tex = gTexCoords;
110
111 sk_sp<SkShader> shader;
112 if (img) {
113 SkScalar w = img->width();
114 SkScalar h = img->height();
Mike Reedb612b6c2020-12-08 21:58:35 -0500115 shader = img->makeShader(SkSamplingOptions(), localMatrix);
Mike Reedd36968b2017-05-23 21:27:59 -0400116 texStorage[0].set(0, 0);
117 texStorage[1].set(w, 0);
118 texStorage[2].set(w, h);
119 texStorage[3].set(0, h);
120 tex = texStorage;
121 } else {
122 shader = make_shader();
123 }
halcanary9d524f22016-03-29 09:03:52 -0700124
Mike Reedb8abb4c2017-05-23 09:53:37 -0400125 canvas->save();
126 for (int y = 0; y < 3; y++) {
127 for (int x = 0; x < 4; x++) {
128 canvas->save();
129 canvas->translate(x * 350.0f, y * 350.0f);
130 switch (x) {
131 case 0:
132 canvas->drawPatch(gCubics, nullptr, nullptr, modes[y], paint);
133 break;
134 case 1:
135 canvas->drawPatch(gCubics, colors, nullptr, modes[y], paint);
136 break;
137 case 2:
138 paint.setShader(shader);
Mike Reedd36968b2017-05-23 21:27:59 -0400139 canvas->drawPatch(gCubics, nullptr, tex, modes[y], paint);
Mike Reedb8abb4c2017-05-23 09:53:37 -0400140 paint.setShader(nullptr);
141 break;
142 case 3:
143 paint.setShader(shader);
Mike Reedd36968b2017-05-23 21:27:59 -0400144 canvas->drawPatch(gCubics, colors, tex, modes[y], paint);
Mike Reedb8abb4c2017-05-23 09:53:37 -0400145 paint.setShader(nullptr);
146 break;
147 default:
148 break;
dandovb3c9d1c2014-08-12 08:34:29 -0700149 }
Mike Reedb8abb4c2017-05-23 09:53:37 -0400150
151 draw_control_points(canvas, gCubics);
152 canvas->restore();
dandovecfff212014-08-04 10:02:00 -0700153 }
Mike Reedb8abb4c2017-05-23 09:53:37 -0400154 }
155 canvas->restore();
dandov50d71542014-07-25 10:44:53 -0700156}
Mike Reedb8abb4c2017-05-23 09:53:37 -0400157
158DEF_SIMPLE_GM(patch_primitive, canvas, 1500, 1100) {
159 const SkColor colors[SkPatchUtils::kNumCorners] = {
160 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
161 };
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500162 dopatch(canvas, colors, nullptr, nullptr);
Mike Reedb8abb4c2017-05-23 09:53:37 -0400163}
Mike Reedd36968b2017-05-23 21:27:59 -0400164DEF_SIMPLE_GM(patch_image, canvas, 1500, 1100) {
165 const SkColor colors[SkPatchUtils::kNumCorners] = {
166 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
167 };
Kevin Lubick8b741882023-10-06 11:41:38 -0400168 dopatch(canvas, colors, ToolUtils::GetResourceAsImage("images/mandrill_128.png"), nullptr);
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500169}
170DEF_SIMPLE_GM(patch_image_persp, canvas, 1500, 1100) {
171 const SkColor colors[SkPatchUtils::kNumCorners] = {
172 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
173 };
174 SkMatrix localM;
175 localM.reset();
176 localM[6] = 0.00001f; // force perspective
Kevin Lubick8b741882023-10-06 11:41:38 -0400177 dopatch(canvas, colors, ToolUtils::GetResourceAsImage("images/mandrill_128.png"), &localM);
Mike Reedd36968b2017-05-23 21:27:59 -0400178}
Mike Reedb8abb4c2017-05-23 09:53:37 -0400179DEF_SIMPLE_GM(patch_alpha, canvas, 1500, 1100) {
180 const SkColor colors[SkPatchUtils::kNumCorners] = {
181 SK_ColorRED, 0x0000FF00, SK_ColorBLUE, 0x00FF00FF,
182 };
Mike Reed0ae1b3d2020-03-04 17:47:43 -0500183 dopatch(canvas, colors, nullptr, nullptr);
Mike Reedb8abb4c2017-05-23 09:53:37 -0400184}
185
186// These two should look the same (one patch, one simple path)
187DEF_SIMPLE_GM(patch_alpha_test, canvas, 550, 250) {
188 canvas->translate(-75, -75);
189
190 const SkColor colors[SkPatchUtils::kNumCorners] = {
191 0x80FF0000, 0x80FF0000, 0x80FF0000, 0x80FF0000,
192 };
193 SkPaint paint;
Brian Salomone00afb02021-11-22 12:22:16 -0500194 canvas->drawPatch(gCubics, colors, nullptr, SkBlendMode::kDst, paint);
Mike Reedb8abb4c2017-05-23 09:53:37 -0400195
196 canvas->translate(300, 0);
197
198 SkPath path;
199 path.moveTo(gCubics[0]);
200 path.cubicTo(gCubics[ 1], gCubics[ 2], gCubics[ 3]);
201 path.cubicTo(gCubics[ 4], gCubics[ 5], gCubics[ 6]);
202 path.cubicTo(gCubics[ 7], gCubics[ 8], gCubics[ 9]);
203 path.cubicTo(gCubics[10], gCubics[11], gCubics[ 0]);
204 paint.setColor(colors[0]);
205 canvas->drawPath(path, paint);
206}
207