skia2/gm/pictureshadercache.cpp
Mike Klein 4429a4f82c re-precate SkMatrix44::SkMatrix44()
It's been driving me nuts that I can't just write `SkMatrix44 m;`,
and I often don't care whether it's initialized or not.  The default
identity constructor would be nice to use, but it's deprecated.

By tagging this constructor deprecated, we're only hurting ourselves;
our big clients disable warnings about deprecated routines and use it
freely.

A quick tally in Skia shows we mostly use the uninitialized constructor,
but sometimes the identity constructor, and there is a spread of all
three in Chromium.  So I've left the two explicit calls available.

I switched a bunch of calls in Skia to use the less verbose constructor
where it was clear that it didn't matter if the matrix was initialized.
Literally zero of the kUninitialized constructor calls looked important
for performance, so the only place I've kept is its lone unit test.

A few places read clearer with an explicit "identity" to read.

Change-Id: I0573cb6201f5a36f3b43070fb111f7d9af92736f
Reviewed-on: https://skia-review.googlesource.com/c/159480
Reviewed-by: Brian Osman <brianosman@google.com>
2018-10-04 14:01:11 +00:00

86 lines
2.8 KiB
C++

/*
* Copyright 2017 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 "SkPaint.h"
#include "SkPicture.h"
#include "SkPictureRecorder.h"
#include "SkShader.h"
#include "SkSurface.h"
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;
SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize, nullptr, 0);
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;
paint.setShader(SkShader::MakePictureShader(fPicture, SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode, nullptr,
nullptr));
{
// Render in a funny color space that converts green to yellow.
SkMatrix44 greenToYellow;
greenToYellow.setFloat(0, 1, 1.0f);
sk_sp<SkColorSpace> gty = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
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;
typedef GM INHERITED;
};
DEF_GM(return new PictureShaderCacheGM(100);)