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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPicture.h"
|
2014-04-18 18:04:41 +00:00
|
|
|
#include "SkPictureRecorder.h"
|
2014-04-08 15:19:34 +00:00
|
|
|
#include "SkShader.h"
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
SkShader::TileMode tmx;
|
|
|
|
SkShader::TileMode tmy;
|
|
|
|
} kTileConfigs[] = {
|
|
|
|
{ SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode },
|
|
|
|
{ SkShader::kRepeat_TileMode, SkShader::kClamp_TileMode },
|
|
|
|
{ SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode },
|
|
|
|
};
|
|
|
|
|
2014-05-02 21:23:52 +00:00
|
|
|
class PictureShaderGM : public skiagm::GM {
|
2014-04-08 15:19:34 +00:00
|
|
|
public:
|
|
|
|
PictureShaderGM(SkScalar tileSize, SkScalar sceneSize)
|
|
|
|
: fTileSize(tileSize)
|
|
|
|
, fSceneSize(sceneSize) {
|
|
|
|
|
|
|
|
// Build the picture.
|
2014-04-13 19:09:42 +00:00
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkCanvas* pictureCanvas = recorder.beginRecording(SkScalarRoundToInt(tileSize),
|
2014-04-17 23:35:06 +00:00
|
|
|
SkScalarRoundToInt(tileSize),
|
|
|
|
NULL, 0);
|
2014-04-08 15:19:34 +00:00
|
|
|
this->drawTile(pictureCanvas);
|
2014-05-02 21:23:52 +00:00
|
|
|
fPicture.reset(recorder.endRecording());
|
2014-04-08 15:19:34 +00:00
|
|
|
|
|
|
|
// Build a reference bitmap.
|
2014-05-02 21:23:52 +00:00
|
|
|
fBitmap.allocN32Pixels(SkScalarRoundToInt(tileSize), SkScalarRoundToInt(tileSize));
|
|
|
|
fBitmap.eraseColor(SK_ColorTRANSPARENT);
|
|
|
|
SkCanvas bitmapCanvas(fBitmap);
|
2014-04-08 15:19:34 +00:00
|
|
|
this->drawTile(&bitmapCanvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
virtual uint32_t onGetFlags() const SK_OVERRIDE {
|
|
|
|
return kSkipTiled_Flag;
|
|
|
|
}
|
|
|
|
|
2014-04-08 15:19:34 +00:00
|
|
|
virtual SkString onShortName() SK_OVERRIDE {
|
|
|
|
return SkString("pictureshader");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() SK_OVERRIDE {
|
|
|
|
return SkISize::Make(1400, 1250);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
paint.setColor(SK_ColorLTGRAY);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(matrix);
|
|
|
|
canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(fSceneSize * 1.1f, 0, fSceneSize, fSceneSize), paint);
|
|
|
|
|
2014-05-02 21:23:52 +00:00
|
|
|
SkAutoTUnref<SkShader> pictureShader(SkShader::CreatePictureShader(
|
|
|
|
fPicture,
|
|
|
|
kTileConfigs[tileMode].tmx,
|
|
|
|
kTileConfigs[tileMode].tmy,
|
|
|
|
&localMatrix));
|
|
|
|
paint.setShader(pictureShader.get());
|
2014-04-08 15:19:34 +00:00
|
|
|
canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
|
|
|
|
|
|
|
|
canvas->translate(fSceneSize * 1.1f, 0);
|
|
|
|
|
2014-05-02 21:23:52 +00:00
|
|
|
SkAutoTUnref<SkShader> bitmapShader(SkShader::CreateBitmapShader(
|
|
|
|
fBitmap,
|
|
|
|
kTileConfigs[tileMode].tmx,
|
|
|
|
kTileConfigs[tileMode].tmy,
|
|
|
|
&localMatrix));
|
|
|
|
paint.setShader(bitmapShader.get());
|
2014-04-08 15:19:34 +00:00
|
|
|
canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkScalar fTileSize;
|
|
|
|
SkScalar fSceneSize;
|
|
|
|
|
2014-05-02 21:23:52 +00:00
|
|
|
SkAutoTUnref<SkPicture> fPicture;
|
|
|
|
SkBitmap fBitmap;
|
2014-04-08 15:19:34 +00:00
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM( return SkNEW_ARGS(PictureShaderGM, (50, 100)); )
|