Flutter Engine
The Flutter Engine
third_party
skia
tests
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
8
#include "
include/core/SkCanvas.h
"
9
#include "
include/core/SkColor.h
"
10
#include "
include/core/SkImageInfo.h
"
11
#include "
include/core/SkPaint.h
"
12
#include "
include/core/SkPicture.h
"
13
#include "
include/core/SkPictureRecorder.h
"
14
#include "
include/core/SkRefCnt.h
"
15
#include "
include/core/SkSamplingOptions.h
"
16
#include "
include/core/SkSurface.h
"
17
#include "
include/core/SkTileMode.h
"
18
#include "
src/core/SkPicturePriv.h
"
19
#include "
src/core/SkResourceCache.h
"
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.
26
DEF_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
36
sk_sp<SkSurface>
surface
=
SkSurfaces::Raster
(
SkImageInfo::MakeN32Premul
(100, 100));
37
38
{
39
SkPaint
paint
;
40
paint
.setShader(
picture
->
makeShader
(
SkTileMode::kRepeat
,
SkTileMode::kRepeat
,
41
SkFilterMode::kNearest
));
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
{
50
SkPaint
paint
;
51
paint
.setShader(makePicture()->makeShader(
SkTileMode::kRepeat
,
SkTileMode::kRepeat
,
52
SkFilterMode::kNearest
));
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
*/
67
DEF_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
= {
79
SkPicturePriv::MakeSharedID
(
picture
->
uniqueID
()),
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).
94
sk_sp<SkSurface>
surface
=
SkSurfaces::Raster
(
SkImageInfo::MakeN32Premul
(100, 100));
95
for
(
SkTileMode
m
: {
96
SkTileMode::kClamp
,
SkTileMode::kRepeat
,
SkTileMode::kRepeat
,
SkTileMode::kDecal
97
}) {
98
SkPaint
paint
;
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
();
114
SkResourceCache::CheckMessages
();
115
116
data
.counter = 0;
117
SkResourceCache::VisitAll
(counter, &
data
);
118
REPORTER_ASSERT
(
reporter
,
data
.counter == 0);
119
}
reporter
reporter
Definition:
FontMgrTest.cpp:39
DEF_TEST
DEF_TEST(PictureShader_caching, reporter)
Definition:
PictureShaderTest.cpp:26
SkCanvas.h
SkColor.h
SK_ColorGREEN
constexpr SkColor SK_ColorGREEN
Definition:
SkColor.h:131
SkImageInfo.h
SkPaint.h
SkPicturePriv.h
SkPictureRecorder.h
SkPicture.h
SkRefCnt.h
SkResourceCache.h
SkSamplingOptions.h
SkFilterMode::kNearest
@ kNearest
SkSurface.h
SkTileMode.h
SkTileMode
SkTileMode
Definition:
SkTileMode.h:13
SkTileMode::kDecal
@ kDecal
SkTileMode::kRepeat
@ kRepeat
SkTileMode::kClamp
@ kClamp
Test.h
REPORTER_ASSERT
#define REPORTER_ASSERT(r, cond,...)
Definition:
Test.h:286
SkCanvas::drawColor
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition:
SkCanvas.h:1182
SkPaint
Definition:
SkPaint.h:44
SkPicturePriv::MakeSharedID
static uint64_t MakeSharedID(uint32_t pictureID)
Definition:
SkPicturePriv.h:40
SkPictureRecorder
Definition:
SkPictureRecorder.h:32
SkPictureRecorder::beginRecording
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
Definition:
SkPictureRecorder.cpp:35
SkPictureRecorder::finishRecordingAsPicture
sk_sp< SkPicture > finishRecordingAsPicture()
Definition:
SkPictureRecorder.cpp:67
SkPicture::makeShader
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, SkFilterMode mode, const SkMatrix *localMatrix, const SkRect *tileRect) const
Definition:
SkPictureShader.cpp:39
SkPicture::uniqueID
uint32_t uniqueID() const
Definition:
SkPicture.h:155
SkRefCntBase::unique
bool unique() const
Definition:
SkRefCnt.h:50
SkResourceCache::CheckMessages
static void CheckMessages()
Definition:
SkResourceCache.cpp:536
SkResourceCache::VisitAll
static void VisitAll(Visitor, void *context)
Definition:
SkResourceCache.cpp:551
sk_sp< SkPicture >
sk_sp::reset
void reset(T *ptr=nullptr)
Definition:
SkRefCnt.h:310
paint
const Paint & paint
Definition:
color_source.cc:38
surface
VkSurfaceKHR surface
Definition:
main.cc:49
SkRecords::picture
sk_sp< const SkPicture > picture
Definition:
SkRecords.h:299
SkSurfaces::Raster
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
Definition:
SkSurface_Raster.cpp:186
dart_profiler_symbols.m
m
Definition:
dart_profiler_symbols.py:64
flutter::Data
struct PathData * Data(SkPath *path)
Definition:
path_ops.cc:52
SkImageInfo::MakeN32Premul
static SkImageInfo MakeN32Premul(int width, int height)
Definition:
SkImageInfo.cpp:157
SkResourceCache::Key::getSharedID
uint64_t getSharedID() const
Definition:
SkResourceCache.h:48
SkResourceCache::Rec
Definition:
SkResourceCache.h:76
SkResourceCache::Rec::getKey
virtual const Key & getKey() const =0
data
std::shared_ptr< const fml::Mapping > data
Definition:
texture_gles.cc:63
Generated on Sun Jun 23 2024 21:56:46 for Flutter Engine by
1.9.4