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" |
Mike Reed | 07ee548 | 2021-03-07 12:53:44 -0500 | [diff] [blame] | 13 | #include "src/core/SkPicturePriv.h" |
| 14 | #include "src/core/SkResourceCache.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/shaders/SkPictureShader.h" |
| 16 | #include "tests/Test.h" |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 17 | |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame] | 18 | // Test that the SkPictureShader cache is purged on shader deletion. |
| 19 | DEF_TEST(PictureShader_caching, reporter) { |
| 20 | auto makePicture = [] () { |
| 21 | SkPictureRecorder recorder; |
| 22 | recorder.beginRecording(100, 100)->drawColor(SK_ColorGREEN); |
| 23 | return recorder.finishRecordingAsPicture(); |
| 24 | }; |
| 25 | |
| 26 | sk_sp<SkPicture> picture = makePicture(); |
| 27 | REPORTER_ASSERT(reporter, picture->unique()); |
| 28 | |
| 29 | sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100); |
| 30 | |
| 31 | { |
| 32 | SkPaint paint; |
Mike Reed | 10a5ff2 | 2021-03-18 17:18:58 -0400 | [diff] [blame] | 33 | paint.setShader(picture->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
| 34 | SkFilterMode::kNearest)); |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame] | 35 | surface->getCanvas()->drawPaint(paint); |
| 36 | |
| 37 | // We should have about 3 refs by now: local + shader + shader cache. |
| 38 | REPORTER_ASSERT(reporter, !picture->unique()); |
| 39 | } |
| 40 | |
| 41 | // Draw another picture shader to have a chance to purge. |
| 42 | { |
| 43 | SkPaint paint; |
Mike Reed | 10a5ff2 | 2021-03-18 17:18:58 -0400 | [diff] [blame] | 44 | paint.setShader(makePicture()->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
| 45 | SkFilterMode::kNearest)); |
Florin Malita | b00a360 | 2017-07-13 22:34:04 -0400 | [diff] [blame] | 46 | surface->getCanvas()->drawPaint(paint); |
| 47 | |
| 48 | } |
| 49 | |
| 50 | // All but the local ref should be gone now. |
| 51 | REPORTER_ASSERT(reporter, picture->unique()); |
| 52 | } |
Mike Reed | ff83dda | 2021-03-04 13:40:44 -0500 | [diff] [blame] | 53 | |
Mike Reed | ff83dda | 2021-03-04 13:40:44 -0500 | [diff] [blame] | 54 | /* |
| 55 | * Check caching of picture-shaders |
| 56 | * - we do cache the underlying image (i.e. there is a cache entry) |
| 57 | * - there is only 1 entry, even with differing tile modes |
| 58 | * - after deleting the picture, the cache entry is purged |
| 59 | */ |
| 60 | DEF_TEST(PictureShader_caching2, reporter) { |
| 61 | auto picture = []() { |
| 62 | SkPictureRecorder recorder; |
| 63 | recorder.beginRecording(100, 100)->drawColor(SK_ColorGREEN); |
| 64 | return recorder.finishRecordingAsPicture(); |
| 65 | }(); |
| 66 | REPORTER_ASSERT(reporter, picture->unique()); |
| 67 | |
| 68 | struct Data { |
| 69 | uint64_t sharedID; |
| 70 | int counter; |
| 71 | } data = { |
| 72 | SkPicturePriv::MakeSharedID(picture->uniqueID()), |
| 73 | 0, |
| 74 | }; |
| 75 | |
| 76 | auto counter = [](const SkResourceCache::Rec& rec, void* dataPtr) { |
| 77 | if (rec.getKey().getSharedID() == ((Data*)dataPtr)->sharedID) { |
| 78 | ((Data*)dataPtr)->counter += 1; |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | SkResourceCache::VisitAll(counter, &data); |
| 83 | REPORTER_ASSERT(reporter, data.counter == 0); |
| 84 | |
| 85 | // Draw with a view variants of picture-shaders that all use the same picture. |
| 86 | // Only expect 1 cache entry for all (since same CTM for all). |
| 87 | sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100); |
| 88 | for (SkTileMode m : { |
| 89 | SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kRepeat, SkTileMode::kDecal |
| 90 | }) { |
| 91 | SkPaint paint; |
Mike Reed | 10a5ff2 | 2021-03-18 17:18:58 -0400 | [diff] [blame] | 92 | paint.setShader(picture->makeShader(m, m, SkFilterMode::kNearest)); |
Mike Reed | ff83dda | 2021-03-04 13:40:44 -0500 | [diff] [blame] | 93 | surface->getCanvas()->drawPaint(paint); |
| 94 | } |
| 95 | |
| 96 | // Don't expect any additional refs on the picture |
| 97 | REPORTER_ASSERT(reporter, picture->unique()); |
| 98 | |
| 99 | // Check that we did cache something, but only 1 thing |
| 100 | data.counter = 0; |
| 101 | SkResourceCache::VisitAll(counter, &data); |
| 102 | REPORTER_ASSERT(reporter, data.counter == 1); |
| 103 | |
| 104 | // Now delete the picture, and check the we purge the cache entry |
| 105 | |
| 106 | picture.reset(); |
| 107 | SkResourceCache::CheckMessages(); |
| 108 | |
| 109 | data.counter = 0; |
| 110 | SkResourceCache::VisitAll(counter, &data); |
| 111 | REPORTER_ASSERT(reporter, data.counter == 0); |
| 112 | } |