3 * Copyright 2014 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkGradientShader.h"
11 #include "SkPatchUtils.h"
13 static SkShader* make_shader() {
14 const SkColor colors[] = {
15 SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, SK_ColorMAGENTA, SK_ColorBLUE,
18 const SkPoint pts[] = { { 100.f / 4.f, 0.f }, { 3.f * 100.f / 4.f, 100.f } };
20 return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors),
21 SkShader::kMirror_TileMode);
24 static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
27 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);
36 paint.setColor(SK_ColorBLACK);
37 paint.setStrokeWidth(0.5f);
38 SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
39 canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
40 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint);
41 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);
45 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);
49 paint.setStrokeWidth(2);
51 paint.setColor(SK_ColorRED);
52 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
54 paint.setColor(SK_ColorBLUE);
55 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint);
57 paint.setColor(SK_ColorCYAN);
58 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint);
60 paint.setColor(SK_ColorYELLOW);
61 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint);
63 paint.setColor(SK_ColorGREEN);
64 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint);
69 * This GM draws a cubics coons patch using the specialized call SkCanvas::drawPatch.
71 class SkPatchGM : public GM {
75 this->setBGColor(0xFFFFFFFF);
79 SkString onShortName() SK_OVERRIDE {
80 return SkString("patch_primitive");
83 SkISize onISize() SK_OVERRIDE {
84 return SkISize::Make(800, 800);
87 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
91 // The order of the colors and points is clockwise starting at upper-left corner.
92 const SkPoint cubics[SkPatchUtils::kNumCtrlPts] = {
94 {100,100},{150,50},{250,150}, {300,100},
98 {300,300},{250,250},{150,350},{100,300},
103 const SkColor colors[SkPatchUtils::kNumCorners] = {
104 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
106 const SkPoint texCoords[SkPatchUtils::kNumCorners] = {
107 {0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f}}
110 const SkXfermode::Mode modes[] = {
111 SkXfermode::kSrc_Mode,
112 SkXfermode::kDst_Mode,
113 SkXfermode::kModulate_Mode,
116 SkAutoTUnref<SkShader> shader(make_shader());
119 for (int y = 0; y < 3; y++) {
120 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(modes[y]));
122 for (int x = 0; x < 4; x++) {
124 canvas->translate(x * 350.0f, y * 350.0f);
127 canvas->drawPatch(cubics, NULL, NULL, xfer, paint);
130 canvas->drawPatch(cubics, colors, NULL, xfer, paint);
133 paint.setShader(shader);
134 canvas->drawPatch(cubics, NULL, texCoords, xfer, paint);
135 paint.setShader(NULL);
138 paint.setShader(shader);
139 canvas->drawPatch(cubics, colors, texCoords, xfer, paint);
140 paint.setShader(NULL);
146 draw_control_points(canvas, cubics);
154 typedef GM INHERITED;
157 DEF_GM(return SkNEW(SkPatchGM); )