| 1 |
/* |
| 2 |
* Copyright 2017 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkImageInfo.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkPicture.h" |
| 16 |
#include "include/core/SkPictureRecorder.h" |
| 17 |
#include "include/core/SkRect.h" |
| 18 |
#include "include/core/SkRefCnt.h" |
| 19 |
#include "include/core/SkScalar.h" |
| 20 |
#include "include/core/SkShader.h" |
| 21 |
#include "include/core/SkSize.h" |
| 22 |
#include "include/core/SkString.h" |
| 23 |
#include "include/core/SkSurface.h" |
| 24 |
#include "include/core/SkTileMode.h" |
| 25 |
#include "modules/skcms/skcms.h" |
| 26 |
|
| 27 |
#include <utility> |
| 28 |
|
| 29 |
class PictureShaderCacheGM : public skiagm::GM { |
| 30 |
public: |
| 31 |
PictureShaderCacheGM(SkScalar tileSize) |
| 32 |
: fTileSize(tileSize) { |
| 33 |
} |
| 34 |
|
| 35 |
protected: |
| 36 |
void drawTile(SkCanvas* canvas) { |
| 37 |
SkPaint paint; |
| 38 |
paint.setColor(SK_ColorGREEN); |
| 39 |
paint.setStyle(SkPaint::kFill_Style); |
| 40 |
paint.setAntiAlias(true); |
| 41 |
|
| 42 |
canvas->drawCircle(fTileSize / 4, fTileSize / 4, fTileSize / 4, paint); |
| 43 |
canvas->drawRect(SkRect::MakeXYWH(fTileSize / 2, fTileSize / 2, |
| 44 |
fTileSize / 2, fTileSize / 2), paint); |
| 45 |
|
| 46 |
paint.setColor(SK_ColorRED); |
| 47 |
canvas->drawLine(fTileSize / 2, fTileSize * 1 / 3, |
| 48 |
fTileSize / 2, fTileSize * 2 / 3, paint); |
| 49 |
canvas->drawLine(fTileSize * 1 / 3, fTileSize / 2, |
| 50 |
fTileSize * 2 / 3, fTileSize / 2, paint); |
| 51 |
} |
| 52 |
|
| 53 |
void onOnceBeforeDraw() override { |
| 54 |
SkPictureRecorder recorder; |
| 55 |
SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize); |
| 56 |
this->drawTile(pictureCanvas); |
| 57 |
fPicture = recorder.finishRecordingAsPicture(); |
| 58 |
} |
| 59 |
|
| 60 |
SkString getName() const override { return SkString("pictureshadercache"); } |
| 61 |
|
| 62 |
SkISize getISize() override { return SkISize::Make(100, 100); } |
| 63 |
|
| 64 |
void onDraw(SkCanvas* canvas) override { |
| 65 |
SkPaint paint; |
| 66 |
paint.setShader(fPicture->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
| 67 |
SkFilterMode::kNearest)); |
| 68 |
|
| 69 |
{ |
| 70 |
// Render in a funny color space that converts green to yellow. |
| 71 |
skcms_Matrix3x3 greenToYellow = {{ |
| 72 |
{ 1, 1, 0 }, |
| 73 |
{ 0, 1, 0 }, |
| 74 |
{ 0, 0, 1 }, |
| 75 |
}}; |
| 76 |
sk_sp<SkColorSpace> gty = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, |
| 77 |
greenToYellow); |
| 78 |
SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100, std::move(gty)); |
| 79 |
sk_sp<SkSurface> surface(SkSurfaces::Raster(info)); |
| 80 |
surface->getCanvas()->drawRect(SkRect::MakeWH(fTileSize, fTileSize), paint); |
| 81 |
} |
| 82 |
|
| 83 |
// When we draw to the canvas, we should see green because we should *not* reuse the |
| 84 |
// cached picture shader. |
| 85 |
canvas->drawRect(SkRect::MakeWH(fTileSize, fTileSize), paint); |
| 86 |
} |
| 87 |
|
| 88 |
private: |
| 89 |
SkScalar fTileSize; |
| 90 |
sk_sp<SkPicture> fPicture; |
| 91 |
SkBitmap fBitmap; |
| 92 |
|
| 93 |
using INHERITED = GM; |
| 94 |
}; |
| 95 |
|
| 96 |
DEF_GM(return new PictureShaderCacheGM(100);) |