| 1 |
/* |
| 2 |
* Copyright 2023 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/SkCanvas.h" |
| 10 |
#include "include/core/SkImage.h" |
| 11 |
#include "include/core/SkMatrix.h" |
| 12 |
#include "include/core/SkPaint.h" |
| 13 |
#include "include/core/SkRect.h" |
| 14 |
#include "include/core/SkShader.h" |
| 15 |
#include "include/core/SkTileMode.h" |
| 16 |
#include "tools/DecodeUtils.h" |
| 17 |
#include "tools/Resources.h" |
| 18 |
|
| 19 |
DEF_SIMPLE_GM(coordclampshader, canvas, 1074, 795) { |
| 20 |
auto image = ToolUtils::GetResourceAsImage("images/mandrill_256.png"); |
| 21 |
if (!image) { |
| 22 |
return; |
| 23 |
} |
| 24 |
// The mandrill_512 image has a bottom row of mostly black pixels. Remove it. |
| 25 |
image = image->makeSubset(nullptr, SkIRect::MakeWH(image->width(), image->height() - 1), {}); |
| 26 |
image = image->withDefaultMipmaps(); |
| 27 |
|
| 28 |
auto imageShader = image->makeShader(SkFilterMode::kLinear); |
| 29 |
|
| 30 |
SkPaint paint; |
| 31 |
|
| 32 |
auto drawRect = SkRect::Make(image->dimensions()); |
| 33 |
|
| 34 |
auto rotate = SkMatrix::RotateDeg(45.f, drawRect.center()); |
| 35 |
|
| 36 |
auto clampRect = drawRect.makeInset(20, 40); |
| 37 |
|
| 38 |
canvas->translate(10, 10); |
| 39 |
|
| 40 |
auto shader = SkShaders::CoordClamp(imageShader, clampRect); |
| 41 |
paint.setShader(std::move(shader)); |
| 42 |
canvas->drawRect(drawRect, paint); |
| 43 |
|
| 44 |
canvas->save(); |
| 45 |
canvas->translate(image->width(), 0); |
| 46 |
shader = SkShaders::CoordClamp(imageShader->makeWithLocalMatrix(rotate), clampRect); |
| 47 |
paint.setShader(std::move(shader)); |
| 48 |
canvas->drawRect(drawRect, paint); |
| 49 |
canvas->restore(); |
| 50 |
|
| 51 |
canvas->save(); |
| 52 |
canvas->translate(0, image->height()); |
| 53 |
shader = SkShaders::CoordClamp(imageShader, clampRect)->makeWithLocalMatrix(rotate); |
| 54 |
paint.setShader(std::move(shader)); |
| 55 |
canvas->drawRect(drawRect, paint); |
| 56 |
canvas->restore(); |
| 57 |
|
| 58 |
canvas->save(); |
| 59 |
canvas->translate(image->width(), image->height()); |
| 60 |
shader = SkShaders::CoordClamp(imageShader->makeWithLocalMatrix(rotate), clampRect) |
| 61 |
->makeWithLocalMatrix(rotate); |
| 62 |
paint.setShader(std::move(shader)); |
| 63 |
canvas->drawRect(drawRect, paint); |
| 64 |
canvas->restore(); |
| 65 |
|
| 66 |
canvas->translate(0, 2 * image->height() + 10); |
| 67 |
|
| 68 |
static const SkSamplingOptions kSamplers[] = { |
| 69 |
SkSamplingOptions{SkFilterMode::kNearest}, |
| 70 |
SkSamplingOptions{SkFilterMode::kLinear}, |
| 71 |
SkSamplingOptions{SkFilterMode::kLinear, SkMipmapMode::kLinear}, |
| 72 |
SkSamplingOptions::Aniso(16) |
| 73 |
}; |
| 74 |
|
| 75 |
for (const auto& sampler : kSamplers) { |
| 76 |
imageShader = image->makeShader(SkTileMode::kMirror, |
| 77 |
SkTileMode::kMirror, |
| 78 |
sampler, |
| 79 |
SkMatrix::Scale(0.3f, 1.0)); |
| 80 |
|
| 81 |
shader = SkShaders::CoordClamp(imageShader, clampRect); |
| 82 |
paint.setShader(std::move(shader)); |
| 83 |
canvas->drawRect(drawRect, paint); |
| 84 |
|
| 85 |
canvas->translate(image->width() + 10, 0); |
| 86 |
} |
| 87 |
} |