blob: c9831c40e773a469c5d22b1dd617d8366926d0f9 [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"
10#include "SkPatchUtils.h"
dandov50d71542014-07-25 10:44:53 -070011
reed1a9b9642016-03-13 14:13:58 -070012static sk_sp<SkShader> make_shader() {
dandovb3c9d1c2014-08-12 08:34:29 -070013 const SkColor colors[] = {
14 SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, SK_ColorMAGENTA, SK_ColorBLUE,
15 SK_ColorYELLOW,
16 };
17 const SkPoint pts[] = { { 100.f / 4.f, 0.f }, { 3.f * 100.f / 4.f, 100.f } };
halcanary9d524f22016-03-29 09:03:52 -070018
reed1a9b9642016-03-13 14:13:58 -070019 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
20 SkShader::kMirror_TileMode);
dandovb3c9d1c2014-08-12 08:34:29 -070021}
dandov50d71542014-07-25 10:44:53 -070022
dandovb3c9d1c2014-08-12 08:34:29 -070023static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
dandovecfff212014-08-04 10:02:00 -070024 //draw control points
dandov963137b2014-08-07 07:49:53 -070025 SkPaint paint;
dandovb3c9d1c2014-08-12 08:34:29 -070026 SkPoint bottom[SkPatchUtils::kNumPtsCubic];
27 SkPatchUtils::getBottomCubic(cubics, bottom);
28 SkPoint top[SkPatchUtils::kNumPtsCubic];
29 SkPatchUtils::getTopCubic(cubics, top);
30 SkPoint left[SkPatchUtils::kNumPtsCubic];
31 SkPatchUtils::getLeftCubic(cubics, left);
32 SkPoint right[SkPatchUtils::kNumPtsCubic];
33 SkPatchUtils::getRightCubic(cubics, right);
dandovecfff212014-08-04 10:02:00 -070034
dandov963137b2014-08-07 07:49:53 -070035 paint.setColor(SK_ColorBLACK);
dandovb3c9d1c2014-08-12 08:34:29 -070036 paint.setStrokeWidth(0.5f);
dandovecfff212014-08-04 10:02:00 -070037 SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
dandov963137b2014-08-07 07:49:53 -070038 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
dandovb3c9d1c2014-08-12 08:34:29 -070039 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint);
dandov963137b2014-08-07 07:49:53 -070040 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, top, paint);
41 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, left, paint);
42 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, right, paint);
dandovecfff212014-08-04 10:02:00 -070043
dandovb3c9d1c2014-08-12 08:34:29 -070044 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top + 1, paint);
45 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, left + 1, paint);
46 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, right + 1, paint);
dandovecfff212014-08-04 10:02:00 -070047
dandov963137b2014-08-07 07:49:53 -070048 paint.setStrokeWidth(2);
dandovecfff212014-08-04 10:02:00 -070049
dandov963137b2014-08-07 07:49:53 -070050 paint.setColor(SK_ColorRED);
51 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
dandovecfff212014-08-04 10:02:00 -070052
dandov963137b2014-08-07 07:49:53 -070053 paint.setColor(SK_ColorBLUE);
dandovb3c9d1c2014-08-12 08:34:29 -070054 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint);
dandovecfff212014-08-04 10:02:00 -070055
dandov963137b2014-08-07 07:49:53 -070056 paint.setColor(SK_ColorCYAN);
dandovb3c9d1c2014-08-12 08:34:29 -070057 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint);
dandovecfff212014-08-04 10:02:00 -070058
dandov963137b2014-08-07 07:49:53 -070059 paint.setColor(SK_ColorYELLOW);
dandovb3c9d1c2014-08-12 08:34:29 -070060 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint);
dandovecfff212014-08-04 10:02:00 -070061
dandov963137b2014-08-07 07:49:53 -070062 paint.setColor(SK_ColorGREEN);
dandovb3c9d1c2014-08-12 08:34:29 -070063 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint);
dandovecfff212014-08-04 10:02:00 -070064}
65
mtklein11064df2016-04-06 15:01:57 -070066DEF_SIMPLE_GM(patch_primitive, canvas, 1500, 1100) {
dandov50d71542014-07-25 10:44:53 -070067 SkPaint paint;
halcanary9d524f22016-03-29 09:03:52 -070068
dandov963137b2014-08-07 07:49:53 -070069 // The order of the colors and points is clockwise starting at upper-left corner.
dandovb3c9d1c2014-08-12 08:34:29 -070070 const SkPoint cubics[SkPatchUtils::kNumCtrlPts] = {
71 //top points
72 {100,100},{150,50},{250,150}, {300,100},
73 //right points
74 {250, 150},{350,250},
75 //bottom points
76 {300,300},{250,250},{150,350},{100,300},
77 //left points
78 {50,250},{150,150}
79 };
halcanary9d524f22016-03-29 09:03:52 -070080
dandovb3c9d1c2014-08-12 08:34:29 -070081 const SkColor colors[SkPatchUtils::kNumCorners] = {
dandov50d71542014-07-25 10:44:53 -070082 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
83 };
dandovb3c9d1c2014-08-12 08:34:29 -070084 const SkPoint texCoords[SkPatchUtils::kNumCorners] = {
85 {0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f}}
86 ;
halcanary9d524f22016-03-29 09:03:52 -070087
Mike Reed7d954ad2016-10-28 15:42:34 -040088 const SkBlendMode modes[] = {
89 SkBlendMode::kSrc,
90 SkBlendMode::kDst,
91 SkBlendMode::kModulate,
dandov50d71542014-07-25 10:44:53 -070092 };
halcanary9d524f22016-03-29 09:03:52 -070093
reed1a9b9642016-03-13 14:13:58 -070094 sk_sp<SkShader> shader(make_shader());
halcanary9d524f22016-03-29 09:03:52 -070095
dandovecfff212014-08-04 10:02:00 -070096 canvas->save();
dandovb3c9d1c2014-08-12 08:34:29 -070097 for (int y = 0; y < 3; y++) {
dandovb3c9d1c2014-08-12 08:34:29 -070098 for (int x = 0; x < 4; x++) {
99 canvas->save();
100 canvas->translate(x * 350.0f, y * 350.0f);
101 switch (x) {
102 case 0:
Mike Reed7d954ad2016-10-28 15:42:34 -0400103 canvas->drawPatch(cubics, nullptr, nullptr, modes[y], paint);
dandovb3c9d1c2014-08-12 08:34:29 -0700104 break;
105 case 1:
Mike Reed7d954ad2016-10-28 15:42:34 -0400106 canvas->drawPatch(cubics, colors, nullptr, modes[y], paint);
dandovb3c9d1c2014-08-12 08:34:29 -0700107 break;
108 case 2:
109 paint.setShader(shader);
Mike Reed7d954ad2016-10-28 15:42:34 -0400110 canvas->drawPatch(cubics, nullptr, texCoords, modes[y], paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700111 paint.setShader(nullptr);
dandovb3c9d1c2014-08-12 08:34:29 -0700112 break;
113 case 3:
114 paint.setShader(shader);
Mike Reed7d954ad2016-10-28 15:42:34 -0400115 canvas->drawPatch(cubics, colors, texCoords, modes[y], paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700116 paint.setShader(nullptr);
dandovb3c9d1c2014-08-12 08:34:29 -0700117 break;
118 default:
119 break;
120 }
halcanary9d524f22016-03-29 09:03:52 -0700121
dandovb3c9d1c2014-08-12 08:34:29 -0700122 draw_control_points(canvas, cubics);
123 canvas->restore();
124 }
dandovecfff212014-08-04 10:02:00 -0700125 }
126 canvas->restore();
dandov50d71542014-07-25 10:44:53 -0700127}