| 1 |
/* |
| 2 |
* Copyright 2016 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/SkColor.h" |
| 11 |
#include "include/core/SkImage.h" |
| 12 |
#include "include/core/SkPaint.h" |
| 13 |
#include "include/core/SkPath.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkScalar.h" |
| 17 |
#include "include/core/SkShader.h" |
| 18 |
#include "include/core/SkSize.h" |
| 19 |
#include "include/core/SkString.h" |
| 20 |
#include "include/core/SkSurface.h" |
| 21 |
#include "tools/GpuToolUtils.h" |
| 22 |
#include "tools/ToolUtils.h" |
| 23 |
|
| 24 |
namespace skiagm { |
| 25 |
|
| 26 |
constexpr SkRect kSrcImageClip{75, 75, 275, 275}; |
| 27 |
|
| 28 |
static sk_sp<SkImage> create_image(SkCanvas* destCanvas) { |
| 29 |
sk_sp<SkSurface> srcSurface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(500, 500)); |
| 30 |
SkCanvas* srcCanvas = srcSurface->getCanvas(); |
| 31 |
|
| 32 |
srcCanvas->clear(SK_ColorRED); |
| 33 |
|
| 34 |
SkPaint paint; |
| 35 |
paint.setColor(0xff00ff00); |
| 36 |
srcCanvas->drawRect(kSrcImageClip, paint); |
| 37 |
|
| 38 |
constexpr SkScalar kStrokeWidth = 10; |
| 39 |
SkPaint stroke; |
| 40 |
stroke.setStyle(SkPaint::kStroke_Style); |
| 41 |
stroke.setStrokeWidth(kStrokeWidth); |
| 42 |
stroke.setColor(0xff008800); |
| 43 |
srcCanvas->drawRect(kSrcImageClip.makeInset(kStrokeWidth / 2, kStrokeWidth / 2), stroke); |
| 44 |
|
| 45 |
return ToolUtils::MakeTextureImage(destCanvas, srcSurface->makeImageSnapshot()); |
| 46 |
} |
| 47 |
|
| 48 |
/* |
| 49 |
* The purpose of this test is to exercise all three codepaths in skgpu::ganesh::SurfaceDrawContext |
| 50 |
* (drawFilledRect, fillRectToRect, fillRectWithLocalMatrix) that pre-crop filled rects based on the |
| 51 |
* clip. |
| 52 |
* |
| 53 |
* The test creates an image of a green square surrounded by red background, then draws this image |
| 54 |
* in various ways with the red clipped out. The test is successful if there is no visible red |
| 55 |
* background, scissor is never used, and ideally, all the rectangles draw in one GrDrawOp. |
| 56 |
*/ |
| 57 |
class CroppedRectsGM : public GM { |
| 58 |
private: |
| 59 |
SkString getName() const override { return SkString("croppedrects"); } |
| 60 |
SkISize getISize() override { return SkISize::Make(500, 500); } |
| 61 |
|
| 62 |
void onDraw(SkCanvas* canvas) override { |
| 63 |
if (!fSrcImage) { |
| 64 |
fSrcImage = create_image(canvas); |
| 65 |
if (fSrcImage) { |
| 66 |
fSrcImageShader = fSrcImage->makeShader(SkSamplingOptions()); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
canvas->clear(SK_ColorWHITE); |
| 71 |
|
| 72 |
{ |
| 73 |
// skgpu::ganesh::SurfaceDrawContext::drawFilledRect. |
| 74 |
SkAutoCanvasRestore acr(canvas, true); |
| 75 |
SkPaint paint; |
| 76 |
paint.setShader(fSrcImageShader); |
| 77 |
canvas->clipRect(kSrcImageClip); |
| 78 |
canvas->drawPaint(paint); |
| 79 |
} |
| 80 |
|
| 81 |
{ |
| 82 |
// skgpu::ganesh::SurfaceDrawContext::fillRectToRect. |
| 83 |
SkAutoCanvasRestore acr(canvas, true); |
| 84 |
SkRect drawRect = SkRect::MakeXYWH(350, 100, 100, 300); |
| 85 |
canvas->clipRect(drawRect); |
| 86 |
canvas->drawImageRect(fSrcImage.get(), |
| 87 |
kSrcImageClip.makeOutset(0.5f * kSrcImageClip.width(), |
| 88 |
kSrcImageClip.height()), |
| 89 |
drawRect.makeOutset(0.5f * drawRect.width(), drawRect.height()), |
| 90 |
SkSamplingOptions(), nullptr, |
| 91 |
SkCanvas::kStrict_SrcRectConstraint); |
| 92 |
} |
| 93 |
|
| 94 |
{ |
| 95 |
// skgpu::ganesh::SurfaceDrawContext::fillRectWithLocalMatrix. |
| 96 |
SkAutoCanvasRestore acr(canvas, true); |
| 97 |
SkPath path = SkPath::Line( |
| 98 |
{kSrcImageClip.fLeft - kSrcImageClip.width(), kSrcImageClip.centerY()}, |
| 99 |
{kSrcImageClip.fRight + 3 * kSrcImageClip.width(), kSrcImageClip.centerY()}); |
| 100 |
SkPaint paint; |
| 101 |
paint.setStyle(SkPaint::kStroke_Style); |
| 102 |
paint.setStrokeWidth(2 * kSrcImageClip.height()); |
| 103 |
paint.setShader(fSrcImageShader); |
| 104 |
canvas->translate(23, 301); |
| 105 |
canvas->scale(300 / kSrcImageClip.width(), 100 / kSrcImageClip.height()); |
| 106 |
canvas->translate(-kSrcImageClip.left(), -kSrcImageClip.top()); |
| 107 |
canvas->clipRect(kSrcImageClip); |
| 108 |
canvas->drawPath(path, paint); |
| 109 |
} |
| 110 |
|
| 111 |
// TODO: assert the draw target only has one op in the post-MDB world. |
| 112 |
} |
| 113 |
|
| 114 |
sk_sp<SkImage> fSrcImage; |
| 115 |
sk_sp<SkShader> fSrcImageShader; |
| 116 |
|
| 117 |
using INHERITED = GM; |
| 118 |
}; |
| 119 |
|
| 120 |
DEF_GM( return new CroppedRectsGM(); ) |
| 121 |
|
| 122 |
} // namespace skiagm |