2014-04-08 15:19:34 +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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkBitmap.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPicture.h"
|
|
|
|
#include "include/core/SkPictureRecorder.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkShader.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2014-04-08 15:19:34 +00:00
|
|
|
|
|
|
|
static struct {
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode tmx;
|
|
|
|
SkTileMode tmy;
|
2014-04-08 15:19:34 +00:00
|
|
|
} kTileConfigs[] = {
|
2019-04-03 14:27:45 +00:00
|
|
|
{ SkTileMode::kRepeat, SkTileMode::kRepeat },
|
|
|
|
{ SkTileMode::kRepeat, SkTileMode::kClamp },
|
|
|
|
{ SkTileMode::kMirror, SkTileMode::kRepeat },
|
2014-04-08 15:19:34 +00:00
|
|
|
};
|
|
|
|
|
2014-05-02 21:23:52 +00:00
|
|
|
class PictureShaderGM : public skiagm::GM {
|
2014-04-08 15:19:34 +00:00
|
|
|
public:
|
2021-09-24 15:16:19 +00:00
|
|
|
PictureShaderGM(SkScalar tileSize, SkScalar sceneSize, bool useLocalMatrixWrapper = false,
|
|
|
|
float alpha = 1)
|
2014-04-08 15:19:34 +00:00
|
|
|
: fTileSize(tileSize)
|
2018-02-06 21:49:40 +00:00
|
|
|
, fSceneSize(sceneSize)
|
2021-09-24 15:16:19 +00:00
|
|
|
, fAlpha(alpha)
|
|
|
|
, fUseLocalMatrixWrapper(useLocalMatrixWrapper)
|
|
|
|
{}
|
2014-04-08 15:19:34 +00:00
|
|
|
|
2015-02-25 17:04:04 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2015-02-25 17:04:04 +00:00
|
|
|
// Build the picture.
|
2014-04-13 19:09:42 +00:00
|
|
|
SkPictureRecorder recorder;
|
2020-08-21 17:42:01 +00:00
|
|
|
SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize);
|
2014-04-08 15:19:34 +00:00
|
|
|
this->drawTile(pictureCanvas);
|
2016-03-18 14:25:55 +00:00
|
|
|
fPicture = recorder.finishRecordingAsPicture();
|
2014-04-08 15:19:34 +00:00
|
|
|
|
|
|
|
// Build a reference bitmap.
|
2015-02-25 17:04:04 +00:00
|
|
|
fBitmap.allocN32Pixels(SkScalarCeilToInt(fTileSize), SkScalarCeilToInt(fTileSize));
|
2014-05-02 21:23:52 +00:00
|
|
|
fBitmap.eraseColor(SK_ColorTRANSPARENT);
|
|
|
|
SkCanvas bitmapCanvas(fBitmap);
|
2014-04-08 15:19:34 +00:00
|
|
|
this->drawTile(&bitmapCanvas);
|
|
|
|
}
|
|
|
|
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2021-09-24 15:16:19 +00:00
|
|
|
return SkStringPrintf("pictureshader%s%s",
|
|
|
|
fUseLocalMatrixWrapper ? "_localwrapper" : "",
|
|
|
|
fAlpha < 1 ? "_alpha" : "");
|
2014-04-08 15:19:34 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2015-01-15 18:45:56 +00:00
|
|
|
return SkISize::Make(1400, 1450);
|
2014-04-08 15:19:34 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-04-08 15:19:34 +00:00
|
|
|
this->drawSceneColumn(canvas, SkPoint::Make(0, 0), 1, 1, 0);
|
|
|
|
this->drawSceneColumn(canvas, SkPoint::Make(0, fSceneSize * 6.4f), 1, 2, 0);
|
|
|
|
this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 2.4f, 0), 1, 1, 1);
|
|
|
|
this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 2.4f, fSceneSize * 6.4f), 1, 1, 2);
|
|
|
|
this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 4.8f, 0), 2, 1, 0);
|
|
|
|
this->drawSceneColumn(canvas, SkPoint::Make(fSceneSize * 9.6f, 0), 2, 2, 0);
|
2015-01-15 18:45:56 +00:00
|
|
|
|
|
|
|
// One last custom row to exercise negative scaling
|
|
|
|
SkMatrix ctm, localMatrix;
|
|
|
|
ctm.setTranslate(fSceneSize * 2.1f, fSceneSize * 13.8f);
|
|
|
|
ctm.preScale(-1, -1);
|
|
|
|
localMatrix.setScale(2, 2);
|
|
|
|
this->drawScene(canvas, ctm, localMatrix, 0);
|
|
|
|
|
|
|
|
ctm.setTranslate(fSceneSize * 2.4f, fSceneSize * 12.8f);
|
|
|
|
localMatrix.setScale(-1, -1);
|
|
|
|
this->drawScene(canvas, ctm, localMatrix, 0);
|
|
|
|
|
|
|
|
ctm.setTranslate(fSceneSize * 4.8f, fSceneSize * 12.3f);
|
|
|
|
ctm.preScale(2, 2);
|
|
|
|
this->drawScene(canvas, ctm, localMatrix, 0);
|
|
|
|
|
|
|
|
ctm.setTranslate(fSceneSize * 13.8f, fSceneSize * 14.3f);
|
|
|
|
ctm.preScale(-2, -2);
|
|
|
|
localMatrix.setTranslate(fTileSize / 4, fTileSize / 4);
|
|
|
|
localMatrix.preRotate(45);
|
|
|
|
localMatrix.preScale(-2, -2);
|
|
|
|
this->drawScene(canvas, ctm, localMatrix, 0);
|
2014-04-08 15:19:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void drawSceneColumn(SkCanvas* canvas, const SkPoint& pos, SkScalar scale, SkScalar localScale,
|
|
|
|
unsigned tileMode) {
|
|
|
|
SkMatrix ctm, localMatrix;
|
|
|
|
|
|
|
|
ctm.setTranslate(pos.x(), pos.y());
|
|
|
|
ctm.preScale(scale, scale);
|
|
|
|
localMatrix.setScale(localScale, localScale);
|
|
|
|
this->drawScene(canvas, ctm, localMatrix, tileMode);
|
|
|
|
|
|
|
|
ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 1.2f * scale);
|
|
|
|
ctm.preScale(scale, scale);
|
|
|
|
localMatrix.setTranslate(fTileSize / 4, fTileSize / 4);
|
|
|
|
localMatrix.preScale(localScale, localScale);
|
|
|
|
this->drawScene(canvas, ctm, localMatrix, tileMode);
|
|
|
|
|
|
|
|
ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 2.4f * scale);
|
|
|
|
ctm.preScale(scale, scale);
|
|
|
|
localMatrix.setRotate(45);
|
|
|
|
localMatrix.preScale(localScale, localScale);
|
|
|
|
this->drawScene(canvas, ctm, localMatrix, tileMode);
|
|
|
|
|
|
|
|
ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 3.6f * scale);
|
|
|
|
ctm.preScale(scale, scale);
|
|
|
|
localMatrix.setSkew(1, 0);
|
|
|
|
localMatrix.preScale(localScale, localScale);
|
|
|
|
this->drawScene(canvas, ctm, localMatrix, tileMode);
|
|
|
|
|
|
|
|
ctm.setTranslate(pos.x(), pos.y() + fSceneSize * 4.8f * scale);
|
|
|
|
ctm.preScale(scale, scale);
|
|
|
|
localMatrix.setTranslate(fTileSize / 4, fTileSize / 4);
|
|
|
|
localMatrix.preRotate(45);
|
|
|
|
localMatrix.preScale(localScale, localScale);
|
|
|
|
this->drawScene(canvas, ctm, localMatrix, tileMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawTile(SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
canvas->drawCircle(fTileSize / 4, fTileSize / 4, fTileSize / 4, paint);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(fTileSize / 2, fTileSize / 2,
|
|
|
|
fTileSize / 2, fTileSize / 2), paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawLine(fTileSize / 2, fTileSize * 1 / 3,
|
|
|
|
fTileSize / 2, fTileSize * 2 / 3, paint);
|
|
|
|
canvas->drawLine(fTileSize * 1 / 3, fTileSize / 2,
|
|
|
|
fTileSize * 2 / 3, fTileSize / 2, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawScene(SkCanvas* canvas, const SkMatrix& matrix, const SkMatrix& localMatrix,
|
|
|
|
unsigned tileMode) {
|
|
|
|
SkASSERT(tileMode < SK_ARRAY_COUNT(kTileConfigs));
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
2018-08-16 14:17:03 +00:00
|
|
|
paint.setColor(SK_ColorLTGRAY);
|
2014-04-08 15:19:34 +00:00
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(matrix);
|
|
|
|
canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(fSceneSize * 1.1f, 0, fSceneSize, fSceneSize), paint);
|
|
|
|
|
2021-09-24 15:16:19 +00:00
|
|
|
paint.setAlphaf(fAlpha);
|
|
|
|
|
2019-04-03 14:27:45 +00:00
|
|
|
auto pictureShader = fPicture->makeShader(kTileConfigs[tileMode].tmx,
|
|
|
|
kTileConfigs[tileMode].tmy,
|
2021-03-18 21:18:58 +00:00
|
|
|
SkFilterMode::kNearest,
|
2019-04-03 14:27:45 +00:00
|
|
|
fUseLocalMatrixWrapper ? nullptr : &localMatrix,
|
|
|
|
nullptr);
|
2018-02-06 21:49:40 +00:00
|
|
|
paint.setShader(fUseLocalMatrixWrapper
|
|
|
|
? pictureShader->makeWithLocalMatrix(localMatrix)
|
|
|
|
: pictureShader);
|
2014-04-08 15:19:34 +00:00
|
|
|
canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
|
|
|
|
|
|
|
|
canvas->translate(fSceneSize * 1.1f, 0);
|
|
|
|
|
2020-12-12 14:51:11 +00:00
|
|
|
auto bitmapShader = fBitmap.makeShader(kTileConfigs[tileMode].tmx,
|
|
|
|
kTileConfigs[tileMode].tmy,
|
|
|
|
SkSamplingOptions(),
|
|
|
|
fUseLocalMatrixWrapper ? nullptr : &localMatrix);
|
2018-02-06 21:49:40 +00:00
|
|
|
paint.setShader(fUseLocalMatrixWrapper
|
|
|
|
? bitmapShader->makeWithLocalMatrix(localMatrix)
|
|
|
|
: bitmapShader);
|
2014-04-08 15:19:34 +00:00
|
|
|
canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2021-09-24 15:16:19 +00:00
|
|
|
const SkScalar fTileSize;
|
|
|
|
const SkScalar fSceneSize;
|
|
|
|
const float fAlpha;
|
|
|
|
const bool fUseLocalMatrixWrapper;
|
2014-04-08 15:19:34 +00:00
|
|
|
|
2021-09-24 15:16:19 +00:00
|
|
|
sk_sp<SkPicture> fPicture;
|
|
|
|
SkBitmap fBitmap;
|
2018-02-06 21:49:40 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2014-04-08 15:19:34 +00:00
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new PictureShaderGM(50, 100);)
|
2018-02-06 21:49:40 +00:00
|
|
|
DEF_GM(return new PictureShaderGM(50, 100, true);)
|
2021-09-24 15:16:19 +00:00
|
|
|
DEF_GM(return new PictureShaderGM(50, 100, false, 0.25f);)
|
2015-02-05 18:26:27 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(tiled_picture_shader, canvas, 400, 400) {
|
|
|
|
// https://code.google.com/p/skia/issues/detail?id=3398
|
|
|
|
SkRect tile = SkRect::MakeWH(100, 100);
|
|
|
|
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkCanvas* c = recorder.beginRecording(tile);
|
|
|
|
|
|
|
|
SkRect r = tile;
|
|
|
|
r.inset(4, 4);
|
|
|
|
SkPaint p;
|
2019-03-20 16:12:10 +00:00
|
|
|
p.setColor(ToolUtils::color_to_565(0xFF303F9F)); // dark blue
|
2015-02-05 18:26:27 +00:00
|
|
|
c->drawRect(r, p);
|
2019-03-20 16:12:10 +00:00
|
|
|
p.setColor(ToolUtils::color_to_565(0xFFC5CAE9)); // light blue
|
2015-02-05 18:26:27 +00:00
|
|
|
p.setStrokeWidth(10);
|
|
|
|
c->drawLine(20, 20, 80, 80, p);
|
|
|
|
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
|
2015-02-05 18:26:27 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
p.setColor(ToolUtils::color_to_565(0xFF8BC34A)); // green
|
2015-02-05 18:26:27 +00:00
|
|
|
canvas->drawPaint(p);
|
|
|
|
|
|
|
|
canvas->clipRect(SkRect::MakeXYWH(0, 0, 400, 350));
|
2018-08-16 14:17:03 +00:00
|
|
|
p.setColor(0xFFB6B6B6); // gray
|
2015-02-05 18:26:27 +00:00
|
|
|
canvas->drawPaint(p);
|
|
|
|
|
2021-03-18 21:18:58 +00:00
|
|
|
p.setShader(picture->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
|
|
|
|
SkFilterMode::kNearest));
|
2015-02-05 18:26:27 +00:00
|
|
|
canvas->drawPaint(p);
|
|
|
|
}
|