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"
|
2017-05-24 01:27:59 +00:00
|
|
|
#include "SkImage.h"
|
2014-08-12 15:34:29 +00:00
|
|
|
#include "SkPatchUtils.h"
|
2017-05-23 13:53:37 +00:00
|
|
|
#include "SkPath.h"
|
2014-08-12 15:34:29 +00:00
|
|
|
|
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),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kMirror);
|
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];
|
2017-04-05 15:06:15 +00:00
|
|
|
SkPatchUtils::GetBottomCubic(cubics, bottom);
|
2014-08-12 15:34:29 +00:00
|
|
|
SkPoint top[SkPatchUtils::kNumPtsCubic];
|
2017-04-05 15:06:15 +00:00
|
|
|
SkPatchUtils::GetTopCubic(cubics, top);
|
2014-08-12 15:34:29 +00:00
|
|
|
SkPoint left[SkPatchUtils::kNumPtsCubic];
|
2017-04-05 15:06:15 +00:00
|
|
|
SkPatchUtils::GetLeftCubic(cubics, left);
|
2014-08-12 15:34:29 +00:00
|
|
|
SkPoint right[SkPatchUtils::kNumPtsCubic];
|
2017-04-05 15:06:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-05-23 13:53:37 +00:00
|
|
|
// The order of the colors and points is clockwise starting at upper-left corner.
|
|
|
|
const SkPoint gCubics[SkPatchUtils::kNumCtrlPts] = {
|
|
|
|
//top points
|
|
|
|
{100,100},{150,50},{250,150}, {300,100},
|
|
|
|
//right points
|
|
|
|
{250, 150},{350,250},
|
|
|
|
//bottom points
|
|
|
|
{300,300},{250,250},{150,350},{100,300},
|
|
|
|
//left points
|
|
|
|
{50,250},{150,150}
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkPoint gTexCoords[SkPatchUtils::kNumCorners] = {
|
|
|
|
{0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-24 01:27:59 +00:00
|
|
|
static void dopatch(SkCanvas* canvas, const SkColor colors[], sk_sp<SkImage> img = nullptr) {
|
2017-05-23 13:53:37 +00:00
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
const SkBlendMode modes[] = {
|
|
|
|
SkBlendMode::kSrc,
|
|
|
|
SkBlendMode::kDst,
|
|
|
|
SkBlendMode::kModulate,
|
|
|
|
};
|
|
|
|
|
2017-05-24 01:27:59 +00:00
|
|
|
SkPoint texStorage[4];
|
|
|
|
const SkPoint* tex = gTexCoords;
|
|
|
|
|
|
|
|
sk_sp<SkShader> shader;
|
|
|
|
if (img) {
|
|
|
|
SkScalar w = img->width();
|
|
|
|
SkScalar h = img->height();
|
2019-04-03 14:27:45 +00:00
|
|
|
shader = img->makeShader();
|
2017-05-24 01:27:59 +00:00
|
|
|
texStorage[0].set(0, 0);
|
|
|
|
texStorage[1].set(w, 0);
|
|
|
|
texStorage[2].set(w, h);
|
|
|
|
texStorage[3].set(0, h);
|
|
|
|
tex = texStorage;
|
|
|
|
} else {
|
|
|
|
shader = make_shader();
|
|
|
|
}
|
2017-05-23 13:53:37 +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:
|
|
|
|
canvas->drawPatch(gCubics, nullptr, nullptr, modes[y], paint);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
canvas->drawPatch(gCubics, colors, nullptr, modes[y], paint);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
paint.setShader(shader);
|
2017-05-24 01:27:59 +00:00
|
|
|
canvas->drawPatch(gCubics, nullptr, tex, modes[y], paint);
|
2017-05-23 13:53:37 +00:00
|
|
|
paint.setShader(nullptr);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
paint.setShader(shader);
|
2017-05-24 01:27:59 +00:00
|
|
|
canvas->drawPatch(gCubics, colors, tex, modes[y], paint);
|
2017-05-23 13:53:37 +00:00
|
|
|
paint.setShader(nullptr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-08-12 15:34:29 +00:00
|
|
|
}
|
2017-05-23 13:53:37 +00:00
|
|
|
|
|
|
|
draw_control_points(canvas, gCubics);
|
|
|
|
canvas->restore();
|
2014-08-04 17:02:00 +00:00
|
|
|
}
|
2017-05-23 13:53:37 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
2014-07-25 17:44:53 +00:00
|
|
|
}
|
2017-05-23 13:53:37 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(patch_primitive, canvas, 1500, 1100) {
|
|
|
|
const SkColor colors[SkPatchUtils::kNumCorners] = {
|
|
|
|
SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
|
|
|
|
};
|
|
|
|
dopatch(canvas, colors);
|
|
|
|
}
|
2017-05-24 01:27:59 +00:00
|
|
|
#include "Resources.h"
|
|
|
|
DEF_SIMPLE_GM(patch_image, canvas, 1500, 1100) {
|
|
|
|
const SkColor colors[SkPatchUtils::kNumCorners] = {
|
|
|
|
SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
|
|
|
|
};
|
2017-12-08 15:21:31 +00:00
|
|
|
dopatch(canvas, colors, GetResourceAsImage("images/mandrill_128.png"));
|
2017-05-24 01:27:59 +00:00
|
|
|
}
|
2017-05-23 13:53:37 +00:00
|
|
|
DEF_SIMPLE_GM(patch_alpha, canvas, 1500, 1100) {
|
|
|
|
const SkColor colors[SkPatchUtils::kNumCorners] = {
|
|
|
|
SK_ColorRED, 0x0000FF00, SK_ColorBLUE, 0x00FF00FF,
|
|
|
|
};
|
|
|
|
dopatch(canvas, colors);
|
|
|
|
}
|
|
|
|
|
|
|
|
// These two should look the same (one patch, one simple path)
|
|
|
|
DEF_SIMPLE_GM(patch_alpha_test, canvas, 550, 250) {
|
|
|
|
canvas->translate(-75, -75);
|
|
|
|
|
|
|
|
const SkColor colors[SkPatchUtils::kNumCorners] = {
|
|
|
|
0x80FF0000, 0x80FF0000, 0x80FF0000, 0x80FF0000,
|
|
|
|
};
|
|
|
|
SkPaint paint;
|
|
|
|
canvas->drawPatch(gCubics, colors, nullptr, SkBlendMode::kModulate, paint);
|
|
|
|
|
|
|
|
canvas->translate(300, 0);
|
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(gCubics[0]);
|
|
|
|
path.cubicTo(gCubics[ 1], gCubics[ 2], gCubics[ 3]);
|
|
|
|
path.cubicTo(gCubics[ 4], gCubics[ 5], gCubics[ 6]);
|
|
|
|
path.cubicTo(gCubics[ 7], gCubics[ 8], gCubics[ 9]);
|
|
|
|
path.cubicTo(gCubics[10], gCubics[11], gCubics[ 0]);
|
|
|
|
paint.setColor(colors[0]);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|