2014-07-25 17:44:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2014-08-12 15:34:29 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkPatchUtils.h"
|
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> make_shader() {
|
2014-08-12 15:34:29 +00:00
|
|
|
const SkColor colors[] = {
|
|
|
|
SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, SK_ColorMAGENTA, SK_ColorBLUE,
|
|
|
|
SK_ColorYELLOW,
|
|
|
|
};
|
|
|
|
const SkPoint pts[] = { { 100.f / 4.f, 0.f }, { 3.f * 100.f / 4.f, 100.f } };
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
|
|
|
|
SkShader::kMirror_TileMode);
|
2014-08-12 15:34:29 +00:00
|
|
|
}
|
2014-07-25 17:44:53 +00:00
|
|
|
|
2014-08-12 15:34:29 +00:00
|
|
|
static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
|
2014-08-04 17:02:00 +00:00
|
|
|
//draw control points
|
2014-08-07 14:49:53 +00:00
|
|
|
SkPaint paint;
|
2014-08-12 15:34:29 +00:00
|
|
|
SkPoint bottom[SkPatchUtils::kNumPtsCubic];
|
|
|
|
SkPatchUtils::getBottomCubic(cubics, bottom);
|
|
|
|
SkPoint top[SkPatchUtils::kNumPtsCubic];
|
|
|
|
SkPatchUtils::getTopCubic(cubics, top);
|
|
|
|
SkPoint left[SkPatchUtils::kNumPtsCubic];
|
|
|
|
SkPatchUtils::getLeftCubic(cubics, left);
|
|
|
|
SkPoint right[SkPatchUtils::kNumPtsCubic];
|
|
|
|
SkPatchUtils::getRightCubic(cubics, right);
|
2014-08-04 17:02:00 +00:00
|
|
|
|
2014-08-07 14:49:53 +00:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
2014-08-12 15:34:29 +00:00
|
|
|
paint.setStrokeWidth(0.5f);
|
2014-08-04 17:02:00 +00:00
|
|
|
SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
|
2014-08-07 14:49:53 +00:00
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
|
2014-08-12 15:34:29 +00:00
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint);
|
2014-08-07 14:49:53 +00:00
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 4, top, paint);
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 4, left, paint);
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 4, right, paint);
|
2014-08-04 17:02:00 +00:00
|
|
|
|
2014-08-12 15:34:29 +00:00
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top + 1, paint);
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, left + 1, paint);
|
|
|
|
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, right + 1, paint);
|
2014-08-04 17:02:00 +00:00
|
|
|
|
2014-08-07 14:49:53 +00:00
|
|
|
paint.setStrokeWidth(2);
|
2014-08-04 17:02:00 +00:00
|
|
|
|
2014-08-07 14:49:53 +00:00
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
|
2014-08-04 17:02:00 +00:00
|
|
|
|
2014-08-07 14:49:53 +00:00
|
|
|
paint.setColor(SK_ColorBLUE);
|
2014-08-12 15:34:29 +00:00
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint);
|
2014-08-04 17:02:00 +00:00
|
|
|
|
2014-08-07 14:49:53 +00:00
|
|
|
paint.setColor(SK_ColorCYAN);
|
2014-08-12 15:34:29 +00:00
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint);
|
2014-08-04 17:02:00 +00:00
|
|
|
|
2014-08-07 14:49:53 +00:00
|
|
|
paint.setColor(SK_ColorYELLOW);
|
2014-08-12 15:34:29 +00:00
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint);
|
2014-08-04 17:02:00 +00:00
|
|
|
|
2014-08-07 14:49:53 +00:00
|
|
|
paint.setColor(SK_ColorGREEN);
|
2014-08-12 15:34:29 +00:00
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint);
|
2014-08-04 17:02:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 22:01:57 +00:00
|
|
|
DEF_SIMPLE_GM(patch_primitive, canvas, 1500, 1100) {
|
2014-07-25 17:44:53 +00:00
|
|
|
SkPaint paint;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-07 14:49:53 +00:00
|
|
|
// The order of the colors and points is clockwise starting at upper-left corner.
|
2014-08-12 15:34:29 +00:00
|
|
|
const SkPoint cubics[SkPatchUtils::kNumCtrlPts] = {
|
2014-08-07 14:49:53 +00:00
|
|
|
//top points
|
2014-08-12 15:34:29 +00:00
|
|
|
{100,100},{150,50},{250,150}, {300,100},
|
2014-08-07 14:49:53 +00:00
|
|
|
//right points
|
2014-08-12 15:34:29 +00:00
|
|
|
{250, 150},{350,250},
|
2014-08-07 14:49:53 +00:00
|
|
|
//bottom points
|
2014-08-12 15:34:29 +00:00
|
|
|
{300,300},{250,250},{150,350},{100,300},
|
2014-08-07 14:49:53 +00:00
|
|
|
//left points
|
2014-08-12 15:34:29 +00:00
|
|
|
{50,250},{150,150}
|
2014-07-25 17:44:53 +00:00
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-12 15:34:29 +00:00
|
|
|
const SkColor colors[SkPatchUtils::kNumCorners] = {
|
|
|
|
SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
|
|
|
|
};
|
|
|
|
const SkPoint texCoords[SkPatchUtils::kNumCorners] = {
|
|
|
|
{0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f}}
|
|
|
|
;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2016-10-28 19:42:34 +00:00
|
|
|
const SkBlendMode modes[] = {
|
|
|
|
SkBlendMode::kSrc,
|
|
|
|
SkBlendMode::kDst,
|
|
|
|
SkBlendMode::kModulate,
|
2014-08-12 15:34:29 +00:00
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
sk_sp<SkShader> shader(make_shader());
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-12 15:34:29 +00:00
|
|
|
canvas->save();
|
|
|
|
for (int y = 0; y < 3; y++) {
|
|
|
|
for (int x = 0; x < 4; x++) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x * 350.0f, y * 350.0f);
|
|
|
|
switch (x) {
|
|
|
|
case 0:
|
2016-10-28 19:42:34 +00:00
|
|
|
canvas->drawPatch(cubics, nullptr, nullptr, modes[y], paint);
|
2014-08-12 15:34:29 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2016-10-28 19:42:34 +00:00
|
|
|
canvas->drawPatch(cubics, colors, nullptr, modes[y], paint);
|
2014-08-12 15:34:29 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
paint.setShader(shader);
|
2016-10-28 19:42:34 +00:00
|
|
|
canvas->drawPatch(cubics, nullptr, texCoords, modes[y], paint);
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setShader(nullptr);
|
2014-08-12 15:34:29 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
paint.setShader(shader);
|
2016-10-28 19:42:34 +00:00
|
|
|
canvas->drawPatch(cubics, colors, texCoords, modes[y], paint);
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setShader(nullptr);
|
2014-08-12 15:34:29 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-12 15:34:29 +00:00
|
|
|
draw_control_points(canvas, cubics);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2014-08-04 17:02:00 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
2014-07-25 17:44:53 +00:00
|
|
|
}
|