blob: ea7954c636f2374b9cf4c80d5e1ef28e1a9f3782 [file] [log] [blame]
dandov50d71542014-07-25 10:44:53 -07001
2/*
3 * Copyright 2014 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
dandov50d71542014-07-25 10:44:53 -07009#include "gm.h"
dandovb3c9d1c2014-08-12 08:34:29 -070010#include "SkGradientShader.h"
11#include "SkPatchUtils.h"
dandov50d71542014-07-25 10:44:53 -070012
dandovb3c9d1c2014-08-12 08:34:29 -070013static SkShader* make_shader() {
14 const SkColor colors[] = {
15 SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, SK_ColorMAGENTA, SK_ColorBLUE,
16 SK_ColorYELLOW,
17 };
18 const SkPoint pts[] = { { 100.f / 4.f, 0.f }, { 3.f * 100.f / 4.f, 100.f } };
19
20 return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors),
21 SkShader::kMirror_TileMode);
22}
dandov50d71542014-07-25 10:44:53 -070023
dandovb3c9d1c2014-08-12 08:34:29 -070024static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
dandovecfff212014-08-04 10:02:00 -070025 //draw control points
dandov963137b2014-08-07 07:49:53 -070026 SkPaint paint;
dandovb3c9d1c2014-08-12 08:34:29 -070027 SkPoint bottom[SkPatchUtils::kNumPtsCubic];
28 SkPatchUtils::getBottomCubic(cubics, bottom);
29 SkPoint top[SkPatchUtils::kNumPtsCubic];
30 SkPatchUtils::getTopCubic(cubics, top);
31 SkPoint left[SkPatchUtils::kNumPtsCubic];
32 SkPatchUtils::getLeftCubic(cubics, left);
33 SkPoint right[SkPatchUtils::kNumPtsCubic];
34 SkPatchUtils::getRightCubic(cubics, right);
dandovecfff212014-08-04 10:02:00 -070035
dandov963137b2014-08-07 07:49:53 -070036 paint.setColor(SK_ColorBLACK);
dandovb3c9d1c2014-08-12 08:34:29 -070037 paint.setStrokeWidth(0.5f);
dandovecfff212014-08-04 10:02:00 -070038 SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
dandov963137b2014-08-07 07:49:53 -070039 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
dandovb3c9d1c2014-08-12 08:34:29 -070040 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint);
dandov963137b2014-08-07 07:49:53 -070041 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, top, paint);
42 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, left, paint);
43 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, right, paint);
dandovecfff212014-08-04 10:02:00 -070044
dandovb3c9d1c2014-08-12 08:34:29 -070045 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top + 1, paint);
46 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, left + 1, paint);
47 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, right + 1, paint);
dandovecfff212014-08-04 10:02:00 -070048
dandov963137b2014-08-07 07:49:53 -070049 paint.setStrokeWidth(2);
dandovecfff212014-08-04 10:02:00 -070050
dandov963137b2014-08-07 07:49:53 -070051 paint.setColor(SK_ColorRED);
52 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
dandovecfff212014-08-04 10:02:00 -070053
dandov963137b2014-08-07 07:49:53 -070054 paint.setColor(SK_ColorBLUE);
dandovb3c9d1c2014-08-12 08:34:29 -070055 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint);
dandovecfff212014-08-04 10:02:00 -070056
dandov963137b2014-08-07 07:49:53 -070057 paint.setColor(SK_ColorCYAN);
dandovb3c9d1c2014-08-12 08:34:29 -070058 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint);
dandovecfff212014-08-04 10:02:00 -070059
dandov963137b2014-08-07 07:49:53 -070060 paint.setColor(SK_ColorYELLOW);
dandovb3c9d1c2014-08-12 08:34:29 -070061 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint);
dandovecfff212014-08-04 10:02:00 -070062
dandov963137b2014-08-07 07:49:53 -070063 paint.setColor(SK_ColorGREEN);
dandovb3c9d1c2014-08-12 08:34:29 -070064 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint);
dandovecfff212014-08-04 10:02:00 -070065}
66
dandov50d71542014-07-25 10:44:53 -070067namespace skiagm {
68/**
dandovb3c9d1c2014-08-12 08:34:29 -070069 * This GM draws a cubics coons patch using the specialized call SkCanvas::drawPatch.
dandov50d71542014-07-25 10:44:53 -070070 */
71class SkPatchGM : public GM {
dandovecfff212014-08-04 10:02:00 -070072
dandov50d71542014-07-25 10:44:53 -070073public:
74 SkPatchGM() {
75 this->setBGColor(0xFFFFFFFF);
76 }
77
78protected:
mtklein36352bf2015-03-25 18:17:31 -070079 SkString onShortName() override {
dandov50d71542014-07-25 10:44:53 -070080 return SkString("patch_primitive");
81 }
82
mtklein36352bf2015-03-25 18:17:31 -070083 SkISize onISize() override {
dandov50d71542014-07-25 10:44:53 -070084 return SkISize::Make(800, 800);
85 }
86
mtklein36352bf2015-03-25 18:17:31 -070087 void onDraw(SkCanvas* canvas) override {
dandovcc03adb2014-08-12 17:14:57 -070088
dandov50d71542014-07-25 10:44:53 -070089 SkPaint paint;
dandov963137b2014-08-07 07:49:53 -070090
91 // The order of the colors and points is clockwise starting at upper-left corner.
dandovb3c9d1c2014-08-12 08:34:29 -070092 const SkPoint cubics[SkPatchUtils::kNumCtrlPts] = {
93 //top points
94 {100,100},{150,50},{250,150}, {300,100},
95 //right points
96 {250, 150},{350,250},
97 //bottom points
98 {300,300},{250,250},{150,350},{100,300},
99 //left points
100 {50,250},{150,150}
101 };
102
103 const SkColor colors[SkPatchUtils::kNumCorners] = {
dandov50d71542014-07-25 10:44:53 -0700104 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
105 };
dandovb3c9d1c2014-08-12 08:34:29 -0700106 const SkPoint texCoords[SkPatchUtils::kNumCorners] = {
107 {0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f}}
108 ;
109
110 const SkXfermode::Mode modes[] = {
111 SkXfermode::kSrc_Mode,
112 SkXfermode::kDst_Mode,
113 SkXfermode::kModulate_Mode,
dandov50d71542014-07-25 10:44:53 -0700114 };
dandov50d71542014-07-25 10:44:53 -0700115
dandovb3c9d1c2014-08-12 08:34:29 -0700116 SkAutoTUnref<SkShader> shader(make_shader());
117
dandovecfff212014-08-04 10:02:00 -0700118 canvas->save();
dandovb3c9d1c2014-08-12 08:34:29 -0700119 for (int y = 0; y < 3; y++) {
120 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(modes[y]));
121
122 for (int x = 0; x < 4; x++) {
123 canvas->save();
124 canvas->translate(x * 350.0f, y * 350.0f);
125 switch (x) {
126 case 0:
127 canvas->drawPatch(cubics, NULL, NULL, xfer, paint);
128 break;
129 case 1:
130 canvas->drawPatch(cubics, colors, NULL, xfer, paint);
131 break;
132 case 2:
133 paint.setShader(shader);
134 canvas->drawPatch(cubics, NULL, texCoords, xfer, paint);
135 paint.setShader(NULL);
136 break;
137 case 3:
138 paint.setShader(shader);
139 canvas->drawPatch(cubics, colors, texCoords, xfer, paint);
140 paint.setShader(NULL);
141 break;
142 default:
143 break;
144 }
145
146 draw_control_points(canvas, cubics);
147 canvas->restore();
148 }
dandovecfff212014-08-04 10:02:00 -0700149 }
150 canvas->restore();
dandov50d71542014-07-25 10:44:53 -0700151 }
152
153private:
154 typedef GM INHERITED;
155};
156
dandovecfff212014-08-04 10:02:00 -0700157DEF_GM(return SkNEW(SkPatchGM); )
dandov50d71542014-07-25 10:44:53 -0700158
159}