2016-07-13 20:37:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPath.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2022-04-20 18:57:03 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2016-07-13 20:37:08 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
constexpr SkRect kSrcImageClip{75, 75, 275, 275};
|
|
|
|
|
2022-04-20 18:57:03 +00:00
|
|
|
static sk_sp<SkImage> create_image(SkCanvas* destCanvas) {
|
|
|
|
sk_sp<SkSurface> srcSurface = SkSurface::MakeRasterN32Premul(500, 500);
|
|
|
|
SkCanvas* srcCanvas = srcSurface->getCanvas();
|
|
|
|
|
|
|
|
srcCanvas->clear(SK_ColorRED);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0xff00ff00);
|
|
|
|
srcCanvas->drawRect(kSrcImageClip, paint);
|
|
|
|
|
|
|
|
constexpr SkScalar kStrokeWidth = 10;
|
|
|
|
SkPaint stroke;
|
|
|
|
stroke.setStyle(SkPaint::kStroke_Style);
|
|
|
|
stroke.setStrokeWidth(kStrokeWidth);
|
|
|
|
stroke.setColor(0xff008800);
|
|
|
|
srcCanvas->drawRect(kSrcImageClip.makeInset(kStrokeWidth / 2, kStrokeWidth / 2), stroke);
|
|
|
|
|
|
|
|
return ToolUtils::MakeTextureImage(destCanvas, srcSurface->makeImageSnapshot());
|
|
|
|
}
|
|
|
|
|
2016-07-13 20:37:08 +00:00
|
|
|
/*
|
2021-07-28 19:13:20 +00:00
|
|
|
* The purpose of this test is to exercise all three codepaths in skgpu::v1::SurfaceDrawContext
|
2016-10-27 18:47:55 +00:00
|
|
|
* (drawFilledRect, fillRectToRect, fillRectWithLocalMatrix) that pre-crop filled rects based on the
|
|
|
|
* clip.
|
2016-07-13 20:37:08 +00:00
|
|
|
*
|
|
|
|
* The test creates an image of a green square surrounded by red background, then draws this image
|
|
|
|
* in various ways with the red clipped out. The test is successful if there is no visible red
|
2016-12-21 16:14:46 +00:00
|
|
|
* background, scissor is never used, and ideally, all the rectangles draw in one GrDrawOp.
|
2016-07-13 20:37:08 +00:00
|
|
|
*/
|
|
|
|
class CroppedRectsGM : public GM {
|
|
|
|
private:
|
2020-08-14 02:58:04 +00:00
|
|
|
SkString onShortName() final { return SkString("croppedrects"); }
|
2016-07-13 20:37:08 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(500, 500); }
|
|
|
|
|
2022-04-19 20:55:02 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2022-04-20 18:57:03 +00:00
|
|
|
if (!fSrcImage) {
|
|
|
|
fSrcImage = create_image(canvas);
|
|
|
|
fSrcImageShader = fSrcImage->makeShader(SkSamplingOptions());
|
|
|
|
}
|
|
|
|
|
2016-07-13 20:37:08 +00:00
|
|
|
canvas->clear(SK_ColorWHITE);
|
|
|
|
|
|
|
|
{
|
2021-07-28 19:13:20 +00:00
|
|
|
// skgpu::v1::SurfaceDrawContext::drawFilledRect.
|
2016-07-13 20:37:08 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(fSrcImageShader);
|
|
|
|
canvas->clipRect(kSrcImageClip);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-28 19:13:20 +00:00
|
|
|
// skgpu::v1::SurfaceDrawContext::fillRectToRect.
|
2016-07-13 20:37:08 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
SkRect drawRect = SkRect::MakeXYWH(350, 100, 100, 300);
|
|
|
|
canvas->clipRect(drawRect);
|
|
|
|
canvas->drawImageRect(fSrcImage.get(),
|
|
|
|
kSrcImageClip.makeOutset(0.5f * kSrcImageClip.width(),
|
|
|
|
kSrcImageClip.height()),
|
|
|
|
drawRect.makeOutset(0.5f * drawRect.width(), drawRect.height()),
|
2021-01-23 17:23:23 +00:00
|
|
|
SkSamplingOptions(), nullptr,
|
|
|
|
SkCanvas::kStrict_SrcRectConstraint);
|
2016-07-13 20:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-28 19:13:20 +00:00
|
|
|
// skgpu::v1::SurfaceDrawContext::fillRectWithLocalMatrix.
|
2016-07-13 20:37:08 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
2020-08-25 15:48:41 +00:00
|
|
|
SkPath path = SkPath::Line(
|
|
|
|
{kSrcImageClip.fLeft - kSrcImageClip.width(), kSrcImageClip.centerY()},
|
|
|
|
{kSrcImageClip.fRight + 3 * kSrcImageClip.width(), kSrcImageClip.centerY()});
|
2016-07-13 20:37:08 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(2 * kSrcImageClip.height());
|
|
|
|
paint.setShader(fSrcImageShader);
|
2017-06-06 23:20:43 +00:00
|
|
|
canvas->translate(23, 301);
|
2016-07-13 20:37:08 +00:00
|
|
|
canvas->scale(300 / kSrcImageClip.width(), 100 / kSrcImageClip.height());
|
2017-06-06 23:20:43 +00:00
|
|
|
canvas->translate(-kSrcImageClip.left(), -kSrcImageClip.top());
|
2016-07-13 20:37:08 +00:00
|
|
|
canvas->clipRect(kSrcImageClip);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
2016-12-21 16:14:46 +00:00
|
|
|
// TODO: assert the draw target only has one op in the post-MDB world.
|
2016-07-13 20:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkImage> fSrcImage;
|
|
|
|
sk_sp<SkShader> fSrcImageShader;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2016-07-13 20:37:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM( return new CroppedRectsGM(); )
|
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|