| 1 |
/* |
| 2 |
* Copyright 2015 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 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkImageInfo.h" |
| 14 |
#include "include/core/SkMatrix.h" |
| 15 |
#include "include/core/SkPaint.h" |
| 16 |
#include "include/core/SkPath.h" |
| 17 |
#include "include/core/SkPathBuilder.h" |
| 18 |
#include "include/core/SkPoint.h" |
| 19 |
#include "include/core/SkRect.h" |
| 20 |
#include "include/core/SkRefCnt.h" |
| 21 |
#include "include/core/SkScalar.h" |
| 22 |
#include "include/core/SkShader.h" |
| 23 |
#include "include/core/SkSize.h" |
| 24 |
#include "include/core/SkString.h" |
| 25 |
#include "include/core/SkSurface.h" |
| 26 |
#include "include/core/SkTileMode.h" |
| 27 |
#include "include/core/SkTypes.h" |
| 28 |
#include "include/effects/SkGradient.h" |
| 29 |
#include "tools/DecodeUtils.h" |
| 30 |
#include "tools/ToolUtils.h" |
| 31 |
|
| 32 |
static sk_sp<SkImage> make_image(SkCanvas* origCanvas, int w, int h) { |
| 33 |
SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); |
| 34 |
auto surface(ToolUtils::makeSurface(origCanvas, info)); |
| 35 |
SkCanvas* canvas = surface->getCanvas(); |
| 36 |
|
| 37 |
ToolUtils::draw_checkerboard(canvas, SK_ColorRED, SK_ColorGREEN, w / 10); |
| 38 |
return surface->makeImageSnapshot(); |
| 39 |
} |
| 40 |
|
| 41 |
namespace skiagm { |
| 42 |
|
| 43 |
class PerspShadersGM : public GM { |
| 44 |
public: |
| 45 |
PerspShadersGM(bool doAA) : fDoAA(doAA) { } |
| 46 |
|
| 47 |
protected: |
| 48 |
SkString getName() const override { |
| 49 |
SkString name; |
| 50 |
name.printf("persp_shaders_%s", |
| 51 |
fDoAA ? "aa" : "bw"); |
| 52 |
return name; |
| 53 |
} |
| 54 |
|
| 55 |
SkISize getISize() override { |
| 56 |
return SkISize::Make(kCellSize*kNumCols, kCellSize*kNumRows); |
| 57 |
} |
| 58 |
|
| 59 |
void onOnceBeforeDraw() override { |
| 60 |
fBitmapImage = ToolUtils::create_checkerboard_image( |
| 61 |
kCellSize, kCellSize, SK_ColorBLUE, SK_ColorYELLOW, kCellSize / 10); |
| 62 |
|
| 63 |
SkPoint pts1[] = { |
| 64 |
{ 0, 0 }, |
| 65 |
{ SkIntToScalar(kCellSize), SkIntToScalar(kCellSize) } |
| 66 |
}; |
| 67 |
SkPoint pts2[] = { |
| 68 |
{ 0, 0 }, |
| 69 |
{ 0, SkIntToScalar(kCellSize) } |
| 70 |
}; |
| 71 |
constexpr SkColor4f colors[] = { |
| 72 |
SkColors::kRed, SkColors::kGreen, SkColors::kRed, SkColors::kGreen, SkColors::kRed |
| 73 |
}; |
| 74 |
constexpr SkScalar pos[] = { 0, 0.25f, 0.5f, 0.75f, SK_Scalar1 }; |
| 75 |
|
| 76 |
fLinearGrad1 = SkShaders::LinearGradient(pts1, {{colors, pos, SkTileMode::kClamp}, {}}); |
| 77 |
fLinearGrad2 = SkShaders::LinearGradient(pts2, {{colors, pos, SkTileMode::kClamp}, {}}); |
| 78 |
|
| 79 |
fPerspMatrix.reset(); |
| 80 |
fPerspMatrix.setPerspY(SK_Scalar1 / 50); |
| 81 |
|
| 82 |
fPath = SkPathBuilder() |
| 83 |
.moveTo(0, 0) |
| 84 |
.lineTo(0, SkIntToScalar(kCellSize)) |
| 85 |
.lineTo(kCellSize/2.0f, kCellSize/2.0f) |
| 86 |
.lineTo(SkIntToScalar(kCellSize), SkIntToScalar(kCellSize)) |
| 87 |
.lineTo(SkIntToScalar(kCellSize), 0) |
| 88 |
.close() |
| 89 |
.detach(); |
| 90 |
} |
| 91 |
|
| 92 |
void drawRow(SkCanvas* canvas, const SkSamplingOptions& sampling) { |
| 93 |
SkPaint filterPaint; |
| 94 |
filterPaint.setAntiAlias(fDoAA); |
| 95 |
|
| 96 |
SkPaint pathPaint; |
| 97 |
pathPaint.setShader(fBitmapImage->makeShader(sampling)); |
| 98 |
pathPaint.setAntiAlias(fDoAA); |
| 99 |
|
| 100 |
SkPaint gradPaint1; |
| 101 |
gradPaint1.setShader(fLinearGrad1); |
| 102 |
gradPaint1.setAntiAlias(fDoAA); |
| 103 |
SkPaint gradPaint2; |
| 104 |
gradPaint2.setShader(fLinearGrad2); |
| 105 |
gradPaint2.setAntiAlias(fDoAA); |
| 106 |
|
| 107 |
SkRect r = SkRect::MakeWH(SkIntToScalar(kCellSize), SkIntToScalar(kCellSize)); |
| 108 |
|
| 109 |
canvas->save(); |
| 110 |
|
| 111 |
canvas->save(); |
| 112 |
canvas->concat(fPerspMatrix); |
| 113 |
canvas->drawImageRect(fBitmapImage, r, sampling, &filterPaint); |
| 114 |
canvas->restore(); |
| 115 |
|
| 116 |
canvas->translate(SkIntToScalar(kCellSize), 0); |
| 117 |
canvas->save(); |
| 118 |
canvas->concat(fPerspMatrix); |
| 119 |
canvas->drawImage(fImage.get(), 0, 0, sampling, &filterPaint); |
| 120 |
canvas->restore(); |
| 121 |
|
| 122 |
canvas->translate(SkIntToScalar(kCellSize), 0); |
| 123 |
canvas->save(); |
| 124 |
canvas->concat(fPerspMatrix); |
| 125 |
canvas->drawRect(r, pathPaint); |
| 126 |
canvas->restore(); |
| 127 |
|
| 128 |
canvas->translate(SkIntToScalar(kCellSize), 0); |
| 129 |
canvas->save(); |
| 130 |
canvas->concat(fPerspMatrix); |
| 131 |
canvas->drawPath(fPath, pathPaint); |
| 132 |
canvas->restore(); |
| 133 |
|
| 134 |
canvas->translate(SkIntToScalar(kCellSize), 0); |
| 135 |
canvas->save(); |
| 136 |
canvas->concat(fPerspMatrix); |
| 137 |
canvas->drawRect(r, gradPaint1); |
| 138 |
canvas->restore(); |
| 139 |
|
| 140 |
canvas->translate(SkIntToScalar(kCellSize), 0); |
| 141 |
canvas->save(); |
| 142 |
canvas->concat(fPerspMatrix); |
| 143 |
canvas->drawPath(fPath, gradPaint2); |
| 144 |
canvas->restore(); |
| 145 |
|
| 146 |
canvas->restore(); |
| 147 |
} |
| 148 |
|
| 149 |
void onDraw(SkCanvas* canvas) override { |
| 150 |
if (!fImage || !fImage->isValid(canvas->baseRecorder())) { |
| 151 |
fImage = make_image(canvas, kCellSize, kCellSize); |
| 152 |
} |
| 153 |
|
| 154 |
this->drawRow(canvas, SkSamplingOptions(SkFilterMode::kNearest)); |
| 155 |
canvas->translate(0, SkIntToScalar(kCellSize)); |
| 156 |
this->drawRow(canvas, SkSamplingOptions(SkFilterMode::kLinear)); |
| 157 |
canvas->translate(0, SkIntToScalar(kCellSize)); |
| 158 |
this->drawRow(canvas, SkSamplingOptions(SkFilterMode::kLinear, |
| 159 |
SkMipmapMode::kNearest)); |
| 160 |
canvas->translate(0, SkIntToScalar(kCellSize)); |
| 161 |
this->drawRow(canvas, SkSamplingOptions(SkCubicResampler::Mitchell())); |
| 162 |
canvas->translate(0, SkIntToScalar(kCellSize)); |
| 163 |
this->drawRow(canvas, SkSamplingOptions::Aniso(16)); |
| 164 |
canvas->translate(0, SkIntToScalar(kCellSize)); |
| 165 |
} |
| 166 |
private: |
| 167 |
inline static constexpr int kCellSize = 50; |
| 168 |
inline static constexpr int kNumRows = 5; |
| 169 |
inline static constexpr int kNumCols = 6; |
| 170 |
|
| 171 |
bool fDoAA; |
| 172 |
SkPath fPath; |
| 173 |
sk_sp<SkShader> fLinearGrad1; |
| 174 |
sk_sp<SkShader> fLinearGrad2; |
| 175 |
SkMatrix fPerspMatrix; |
| 176 |
sk_sp<SkImage> fImage; |
| 177 |
sk_sp<SkImage> fBitmapImage; |
| 178 |
|
| 179 |
using INHERITED = GM; |
| 180 |
}; |
| 181 |
DEF_GM(return new PerspShadersGM(true);) |
| 182 |
DEF_GM(return new PerspShadersGM(false);) |
| 183 |
} // namespace skiagm |
| 184 |
|
| 185 |
////////////////////////////////////////////////////////////////////////////// |
| 186 |
|
| 187 |
#include "tools/Resources.h" |
| 188 |
|
| 189 |
static SkPath make_path() { |
| 190 |
SkRandom rand; |
| 191 |
auto rand_pt = [&rand]() { |
| 192 |
auto x = rand.nextF(); |
| 193 |
auto y = rand.nextF(); |
| 194 |
return SkPoint{x * 400, y * 400}; |
| 195 |
}; |
| 196 |
|
| 197 |
SkPathBuilder builder; |
| 198 |
for (int i = 0; i < 4; ++i) { |
| 199 |
SkPoint pts[6]; |
| 200 |
for (auto& p : pts) { |
| 201 |
p = rand_pt(); |
| 202 |
} |
| 203 |
builder.moveTo(pts[0]) |
| 204 |
.quadTo(pts[1], pts[2]) |
| 205 |
.quadTo(pts[3], pts[4]).lineTo(pts[5]); |
| 206 |
} |
| 207 |
return builder.detach(); |
| 208 |
} |
| 209 |
|
| 210 |
DEF_SIMPLE_GM(perspective_clip, canvas, 800, 800) { |
| 211 |
SkPath path = make_path(); |
| 212 |
auto shader = ToolUtils::GetResourceAsImage("images/mandrill_128.png") |
| 213 |
->makeShader(SkSamplingOptions(), SkMatrix::Scale(3, 3)); |
| 214 |
|
| 215 |
SkPaint paint; |
| 216 |
paint.setColor({0.75, 0.75, 0.75, 1}); |
| 217 |
canvas->drawPath(path, paint); |
| 218 |
|
| 219 |
// This is a crazy perspective matrix, derived from halfplanes3, to draw a shape where |
| 220 |
// part of it is "behind" the viewer, hence showing the need for "half-plane" clipping |
| 221 |
// when in perspective. |
| 222 |
SkMatrix mx; |
| 223 |
const SkScalar array[] = { |
| 224 |
-1.7866f, 1.3357f, 273.0295f, |
| 225 |
-1.0820f, 1.3186f, 135.5196f, |
| 226 |
-0.0047f, -0.0015f, 2.1485f, |
| 227 |
}; |
| 228 |
mx.set9(array); |
| 229 |
|
| 230 |
paint.setShader(shader); |
| 231 |
canvas->concat(mx); |
| 232 |
canvas->drawPath(path, paint); |
| 233 |
} |