skia2/gm/pictureshadercache.cpp
Brian Osman 82ebe04caf Reland "Add SkColorSpace factory from 3x3 row-major gamut and transfer function"
Moved named common transfer functions and gamuts to constexpr values in
SkColorSpace.h, in SkNamedTransferFn and SkNamedGamut namespaces.

Converted nearly all SkColorSpace::MakeRGB calls within Skia to use the
new factory with the named values. Multiple clients want a way to
extract named transfer function and gamut - this still doesn't provide
that, but this may be a better path forward for honestly advertising how
SkColorSpace works internally.

Originally landed as:
https://skia.googlesource.com/skia/+/a9549ab31630fc244094e6f1692371cbaf87f666

Re-landing with a new serialization format, but maintaining ability to
load old serialized color spaces, for SKP compatibility.

Bug: skia:
Change-Id: Ib84a6e1cd5d7d9816175773fdbaff2ca32658667
Reviewed-on: https://skia-review.googlesource.com/c/181176
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2019-01-05 20:06:51 +00:00

89 lines
2.9 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.
skcms_Matrix3x3 greenToYellow = {{
{ 1, 1, 0 },
{ 0, 1, 0 },
{ 0, 0, 1 },
}};
sk_sp<SkColorSpace> gty = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
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);)