commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 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 | |
mtklein | cfa90a1 | 2015-05-18 13:44:35 -0700 | [diff] [blame] | 8 | #include "SkCanvas.h" |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 9 | #include "SkPicture.h" |
| 10 | #include "SkPictureRecorder.h" |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame^] | 11 | #include "SkPictureShader.h" |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 12 | #include "SkShader.h" |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame^] | 13 | #include "SkSurface.h" |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 14 | #include "Test.h" |
| 15 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 16 | // Test that attempting to create a picture shader with a nullptr picture or |
mtklein | cfa90a1 | 2015-05-18 13:44:35 -0700 | [diff] [blame] | 17 | // empty picture returns a shader that draws nothing. |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 18 | DEF_TEST(PictureShader_empty, reporter) { |
mtklein | cfa90a1 | 2015-05-18 13:44:35 -0700 | [diff] [blame] | 19 | SkPaint paint; |
| 20 | |
| 21 | SkBitmap bitmap; |
| 22 | bitmap.allocN32Pixels(1,1); |
| 23 | |
| 24 | SkCanvas canvas(bitmap); |
| 25 | canvas.clear(SK_ColorGREEN); |
| 26 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 27 | paint.setShader(SkShader::MakePictureShader( |
| 28 | nullptr, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, nullptr, nullptr)); |
mtklein | cfa90a1 | 2015-05-18 13:44:35 -0700 | [diff] [blame] | 29 | |
| 30 | canvas.drawRect(SkRect::MakeWH(1,1), paint); |
| 31 | REPORTER_ASSERT(reporter, *bitmap.getAddr32(0,0) == SK_ColorGREEN); |
| 32 | |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 33 | |
| 34 | SkPictureRecorder factory; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | factory.beginRecording(0, 0, nullptr, 0); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 36 | paint.setShader(SkShader::MakePictureShader(factory.finishRecordingAsPicture(), |
| 37 | SkShader::kClamp_TileMode, |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 38 | SkShader::kClamp_TileMode, nullptr, nullptr)); |
mtklein | cfa90a1 | 2015-05-18 13:44:35 -0700 | [diff] [blame] | 39 | |
| 40 | canvas.drawRect(SkRect::MakeWH(1,1), paint); |
| 41 | REPORTER_ASSERT(reporter, *bitmap.getAddr32(0,0) == SK_ColorGREEN); |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 42 | } |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame^] | 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 | } |