1 /* 2 * Copyright 2014 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 "SkCanvas.h" 9 #include "SkPicture.h" 10 #include "SkPictureRecorder.h" 11 #include "SkPictureShader.h" 12 #include "SkShader.h" 13 #include "SkSurface.h" 14 #include "Test.h" 15 16 // Test that attempting to create a picture shader with a nullptr picture or 17 // empty picture returns a shader that draws nothing. 18 DEF_TEST(PictureShader_empty, reporter) { 19 SkPaint paint; 20 21 SkBitmap bitmap; 22 bitmap.allocN32Pixels(1,1); 23 24 SkCanvas canvas(bitmap); 25 canvas.clear(SK_ColorGREEN); 26 27 paint.setShader(SkShader::MakePictureShader( 28 nullptr, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, nullptr, nullptr)); 29 30 canvas.drawRect(SkRect::MakeWH(1,1), paint); 31 REPORTER_ASSERT(reporter, *bitmap.getAddr32(0,0) == SK_ColorGREEN); 32 33 34 SkPictureRecorder factory; 35 factory.beginRecording(0, 0, nullptr, 0); 36 paint.setShader(SkShader::MakePictureShader(factory.finishRecordingAsPicture(), 37 SkShader::kClamp_TileMode, 38 SkShader::kClamp_TileMode, nullptr, nullptr)); 39 40 canvas.drawRect(SkRect::MakeWH(1,1), paint); 41 REPORTER_ASSERT(reporter, *bitmap.getAddr32(0,0) == SK_ColorGREEN); 42 } 43 44 // Test that the SkPictureShader cache is purged on shader deletion. 45 DEF_TEST(PictureShader_caching, reporter) { 46 auto makePicture = [] () { 47 SkPictureRecorder recorder; 48 recorder.beginRecording(100, 100)->drawColor(SK_ColorGREEN); 49 return recorder.finishRecordingAsPicture(); 50 }; 51 52 sk_sp<SkPicture> picture = makePicture(); 53 REPORTER_ASSERT(reporter, picture->unique()); 54 55 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100); 56 57 { 58 SkPaint paint; 59 paint.setShader(SkPictureShader::Make(picture, 60 SkShader::kRepeat_TileMode, 61 SkShader::kRepeat_TileMode, nullptr, nullptr)); 62 surface->getCanvas()->drawPaint(paint); 63 64 // We should have about 3 refs by now: local + shader + shader cache. 65 REPORTER_ASSERT(reporter, !picture->unique()); 66 } 67 68 // Draw another picture shader to have a chance to purge. 69 { 70 SkPaint paint; 71 paint.setShader(SkPictureShader::Make(makePicture(), 72 SkShader::kRepeat_TileMode, 73 SkShader::kRepeat_TileMode, nullptr, nullptr)); 74 surface->getCanvas()->drawPaint(paint); 75 76 } 77 78 // All but the local ref should be gone now. 79 REPORTER_ASSERT(reporter, picture->unique()); 80 } 81