Flutter Engine
The Flutter Engine
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();
35
37
38 {
42 surface->getCanvas()->drawPaint(paint);
43
44 // We should have about 3 refs by now: local + shader + shader cache.
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.
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 }();
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
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 }) {
100 surface->getCanvas()->drawPaint(paint);
101 }
102
103 // Don't expect any additional refs on the picture
105
106 // Check that we did cache something, but only 1 thing
107 data.counter = 0;
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;
118 REPORTER_ASSERT(reporter, data.counter == 0);
119}
reporter
Definition: FontMgrTest.cpp:39
DEF_TEST(PictureShader_caching, reporter)
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
SkTileMode
Definition: SkTileMode.h:13
#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)
Definition: SkPicturePriv.h:40
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
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
sk_sp< const SkPicture > picture
Definition: SkRecords.h:299
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
struct PathData * Data(SkPath *path)
Definition: path_ops.cc:52
static SkImageInfo MakeN32Premul(int width, int height)
uint64_t getSharedID() const
virtual const Key & getKey() const =0
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63