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.h" 9 10 #include "SkPaint.h" 11 #include "SkPicture.h" 12 #include "SkPictureRecorder.h" 13 #include "SkShader.h" 14 #include "SkSurface.h" 15 16 class PictureShaderCacheGM : public skiagm::GM { 17 public: 18 PictureShaderCacheGM(SkScalar tileSize) 19 : fTileSize(tileSize) { 20 } 21 22 protected: 23 void drawTile(SkCanvas* canvas) { 24 SkPaint paint; 25 paint.setColor(SK_ColorGREEN); 26 paint.setStyle(SkPaint::kFill_Style); 27 paint.setAntiAlias(true); 28 29 canvas->drawCircle(fTileSize / 4, fTileSize / 4, fTileSize / 4, paint); 30 canvas->drawRect(SkRect::MakeXYWH(fTileSize / 2, fTileSize / 2, 31 fTileSize / 2, fTileSize / 2), paint); 32 33 paint.setColor(SK_ColorRED); 34 canvas->drawLine(fTileSize / 2, fTileSize * 1 / 3, 35 fTileSize / 2, fTileSize * 2 / 3, paint); 36 canvas->drawLine(fTileSize * 1 / 3, fTileSize / 2, 37 fTileSize * 2 / 3, fTileSize / 2, paint); 38 } 39 40 void onOnceBeforeDraw() override { 41 SkPictureRecorder recorder; 42 SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize, nullptr, 0); 43 this->drawTile(pictureCanvas); 44 fPicture = recorder.finishRecordingAsPicture(); 45 } 46 47 SkString onShortName() override { 48 return SkString("pictureshadercache"); 49 } 50 51 SkISize onISize() override { 52 return SkISize::Make(100, 100); 53 } 54 55 void onDraw(SkCanvas* canvas) override { 56 SkPaint paint; 57 paint.setShader(SkShader::MakePictureShader(fPicture, SkShader::kRepeat_TileMode, 58 SkShader::kRepeat_TileMode, nullptr, 59 nullptr)); 60 61 { 62 // Render in a funny color space that converts green to yellow. 63 skcms_Matrix3x3 greenToYellow = {{ 64 { 1, 1, 0 }, 65 { 0, 1, 0 }, 66 { 0, 0, 1 }, 67 }}; 68 sk_sp<SkColorSpace> gty = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, 69 greenToYellow); 70 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100, std::move(gty)); 71 sk_sp<SkSurface> surface(SkSurface::MakeRaster(info)); 72 surface->getCanvas()->drawRect(SkRect::MakeWH(fTileSize, fTileSize), paint); 73 } 74 75 // When we draw to the canvas, we should see green because we should *not* reuse the 76 // cached picture shader. 77 canvas->drawRect(SkRect::MakeWH(fTileSize, fTileSize), paint); 78 } 79 80 private: 81 SkScalar fTileSize; 82 sk_sp<SkPicture> fPicture; 83 SkBitmap fBitmap; 84 85 typedef GM INHERITED; 86 }; 87 88 DEF_GM(return new PictureShaderCacheGM(100);) 89