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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkPicture.h" |
| 10 | #include "include/core/SkPictureRecorder.h" |
| 11 | #include "include/core/SkShader.h" |
| 12 | #include "include/core/SkSurface.h" |
| 13 | #include "src/shaders/SkPictureShader.h" |
| 14 | #include "tests/Test.h" |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 15 | |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame] | 16 | // Test that the SkPictureShader cache is purged on shader deletion. |
| 17 | DEF_TEST(PictureShader_caching, reporter) { |
| 18 | auto makePicture = [] () { |
| 19 | SkPictureRecorder recorder; |
| 20 | recorder.beginRecording(100, 100)->drawColor(SK_ColorGREEN); |
| 21 | return recorder.finishRecordingAsPicture(); |
| 22 | }; |
| 23 | |
| 24 | sk_sp<SkPicture> picture = makePicture(); |
| 25 | REPORTER_ASSERT(reporter, picture->unique()); |
| 26 | |
| 27 | sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100); |
| 28 | |
| 29 | { |
| 30 | SkPaint paint; |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 31 | paint.setShader(picture->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat)); |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame] | 32 | surface->getCanvas()->drawPaint(paint); |
| 33 | |
| 34 | // We should have about 3 refs by now: local + shader + shader cache. |
| 35 | REPORTER_ASSERT(reporter, !picture->unique()); |
| 36 | } |
| 37 | |
| 38 | // Draw another picture shader to have a chance to purge. |
| 39 | { |
| 40 | SkPaint paint; |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 41 | paint.setShader(makePicture()->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat)); |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame] | 42 | surface->getCanvas()->drawPaint(paint); |
| 43 | |
| 44 | } |
| 45 | |
| 46 | // All but the local ref should be gone now. |
| 47 | REPORTER_ASSERT(reporter, picture->unique()); |
| 48 | } |