2017-04-21 19:06:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 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/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkColorSpace.h"
|
|
|
|
#include "include/core/SkImageInfo.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/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"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/third_party/skcms/skcms.h"
|
|
|
|
|
|
|
|
#include <utility>
|
2017-04-21 19:06:51 +00:00
|
|
|
|
|
|
|
class PictureShaderCacheGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
PictureShaderCacheGM(SkScalar tileSize)
|
|
|
|
: fTileSize(tileSize) {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
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 onOnceBeforeDraw() override {
|
|
|
|
SkPictureRecorder recorder;
|
2020-08-21 17:42:01 +00:00
|
|
|
SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize);
|
2017-04-21 19:06:51 +00:00
|
|
|
this->drawTile(pictureCanvas);
|
|
|
|
fPicture = recorder.finishRecordingAsPicture();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("pictureshadercache");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(100, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
SkPaint paint;
|
2019-04-03 14:27:45 +00:00
|
|
|
paint.setShader(fPicture->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat));
|
2017-04-21 19:06:51 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Render in a funny color space that converts green to yellow.
|
2019-01-04 22:03:00 +00:00
|
|
|
skcms_Matrix3x3 greenToYellow = {{
|
|
|
|
{ 1, 1, 0 },
|
|
|
|
{ 0, 1, 0 },
|
|
|
|
{ 0, 0, 1 },
|
|
|
|
}};
|
|
|
|
sk_sp<SkColorSpace> gty = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
|
2017-04-21 19:06:51 +00:00
|
|
|
greenToYellow);
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100, std::move(gty));
|
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRaster(info));
|
|
|
|
surface->getCanvas()->drawRect(SkRect::MakeWH(fTileSize, fTileSize), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// When we draw to the canvas, we should see green because we should *not* reuse the
|
|
|
|
// cached picture shader.
|
|
|
|
canvas->drawRect(SkRect::MakeWH(fTileSize, fTileSize), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkScalar fTileSize;
|
|
|
|
sk_sp<SkPicture> fPicture;
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2017-04-21 19:06:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new PictureShaderCacheGM(100);)
|