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 "include/third_party/skcms/skcms.h" 26 27 #include <utility> 28 29 class PictureShaderCacheGM : public skiagm::GM { 30 public: PictureShaderCacheGM(SkScalar tileSize)31 PictureShaderCacheGM(SkScalar tileSize) 32 : fTileSize(tileSize) { 33 } 34 35 protected: drawTile(SkCanvas * canvas)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 onOnceBeforeDraw()53 void onOnceBeforeDraw() override { 54 SkPictureRecorder recorder; 55 SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize, nullptr, 0); 56 this->drawTile(pictureCanvas); 57 fPicture = recorder.finishRecordingAsPicture(); 58 } 59 onShortName()60 SkString onShortName() override { 61 return SkString("pictureshadercache"); 62 } 63 onISize()64 SkISize onISize() override { 65 return SkISize::Make(100, 100); 66 } 67 onDraw(SkCanvas * canvas)68 void onDraw(SkCanvas* canvas) override { 69 SkPaint paint; 70 paint.setShader(fPicture->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat)); 71 72 { 73 // Render in a funny color space that converts green to yellow. 74 skcms_Matrix3x3 greenToYellow = {{ 75 { 1, 1, 0 }, 76 { 0, 1, 0 }, 77 { 0, 0, 1 }, 78 }}; 79 sk_sp<SkColorSpace> gty = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, 80 greenToYellow); 81 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100, std::move(gty)); 82 sk_sp<SkSurface> surface(SkSurface::MakeRaster(info)); 83 surface->getCanvas()->drawRect(SkRect::MakeWH(fTileSize, fTileSize), paint); 84 } 85 86 // When we draw to the canvas, we should see green because we should *not* reuse the 87 // cached picture shader. 88 canvas->drawRect(SkRect::MakeWH(fTileSize, fTileSize), paint); 89 } 90 91 private: 92 SkScalar fTileSize; 93 sk_sp<SkPicture> fPicture; 94 SkBitmap fBitmap; 95 96 typedef GM INHERITED; 97 }; 98 99 DEF_GM(return new PictureShaderCacheGM(100);) 100