Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
PictureShaderTest.cpp
Go to the documentation of this file.
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
20#include "tests/Test.h"
21
22#include <cstdint>
23#include <initializer_list>
24
25// Test that the SkPictureShader cache is purged on shader deletion.
26DEF_TEST(PictureShader_caching, reporter) {
27 auto makePicture = [] () {
28 SkPictureRecorder recorder;
29 recorder.beginRecording(100, 100)->drawColor(SK_ColorGREEN);
30 return recorder.finishRecordingAsPicture();
31 };
32
33 sk_sp<SkPicture> picture = makePicture();
34 REPORTER_ASSERT(reporter, picture->unique());
35
37
38 {
42 surface->getCanvas()->drawPaint(paint);
43
44 // We should have about 3 refs by now: local + shader + shader cache.
45 REPORTER_ASSERT(reporter, !picture->unique());
46 }
47
48 // Draw another picture shader to have a chance to purge.
49 {
51 paint.setShader(makePicture()->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
53 surface->getCanvas()->drawPaint(paint);
54
55 }
56
57 // All but the local ref should be gone now.
58 REPORTER_ASSERT(reporter, picture->unique());
59}
60
61/*
62 * Check caching of picture-shaders
63 * - we do cache the underlying image (i.e. there is a cache entry)
64 * - there is only 1 entry, even with differing tile modes
65 * - after deleting the picture, the cache entry is purged
66 */
67DEF_TEST(PictureShader_caching2, reporter) {
68 auto picture = []() {
69 SkPictureRecorder recorder;
70 recorder.beginRecording(100, 100)->drawColor(SK_ColorGREEN);
71 return recorder.finishRecordingAsPicture();
72 }();
73 REPORTER_ASSERT(reporter, picture->unique());
74
75 struct Data {
76 uint64_t sharedID;
77 int counter;
78 } data = {
80 0,
81 };
82
83 auto counter = [](const SkResourceCache::Rec& rec, void* dataPtr) {
84 if (rec.getKey().getSharedID() == ((Data*)dataPtr)->sharedID) {
85 ((Data*)dataPtr)->counter += 1;
86 }
87 };
88
89 SkResourceCache::VisitAll(counter, &data);
90 REPORTER_ASSERT(reporter, data.counter == 0);
91
92 // Draw with a view variants of picture-shaders that all use the same picture.
93 // Only expect 1 cache entry for all (since same CTM for all).
95 for (SkTileMode m : {
97 }) {
99 paint.setShader(picture->makeShader(m, m, SkFilterMode::kNearest));
100 surface->getCanvas()->drawPaint(paint);
101 }
102
103 // Don't expect any additional refs on the picture
104 REPORTER_ASSERT(reporter, picture->unique());
105
106 // Check that we did cache something, but only 1 thing
107 data.counter = 0;
108 SkResourceCache::VisitAll(counter, &data);
109 REPORTER_ASSERT(reporter, data.counter == 1);
110
111 // Now delete the picture, and check the we purge the cache entry
112
113 picture.reset();
115
116 data.counter = 0;
117 SkResourceCache::VisitAll(counter, &data);
118 REPORTER_ASSERT(reporter, data.counter == 0);
119}
reporter
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
SkTileMode
Definition SkTileMode.h:13
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
static uint64_t MakeSharedID(uint32_t pictureID)
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, SkFilterMode mode, const SkMatrix *localMatrix, const SkRect *tileRect) const
uint32_t uniqueID() const
Definition SkPicture.h:155
bool unique() const
Definition SkRefCnt.h:50
static void CheckMessages()
static void VisitAll(Visitor, void *context)
void reset(T *ptr=nullptr)
Definition SkRefCnt.h:310
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
static SkImageInfo MakeN32Premul(int width, int height)
uint64_t getSharedID() const
virtual const Key & getKey() const =0