xref: /aosp_15_r20/external/skia/tests/graphite/PipelineDataCacheTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 Google LLC
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 "tests/Test.h"
9 
10 #include "include/gpu/graphite/Context.h"
11 #include "include/gpu/graphite/Recorder.h"
12 #include "src/gpu/graphite/PipelineData.h"
13 #include "src/gpu/graphite/RecorderPriv.h"
14 #include "src/gpu/graphite/Uniform.h"
15 
16 using namespace skgpu::graphite;
17 
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(PipelineDataCacheTest,reporter,context,CtsEnforcement::kApiLevel_V)18 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(PipelineDataCacheTest, reporter, context,
19                                    CtsEnforcement::kApiLevel_V) {
20     UniformDataCache cache;
21 
22     REPORTER_ASSERT(reporter, cache.count() == 0);
23 
24     // UniformDataBlocks can only be created via a PipelineDataGatherer, but for this test the
25     // layout and contents don't matter other than their bits being the same or different.
26     [[maybe_unused]] static constexpr Uniform kUniforms[] = {{"data", SkSLType::kFloat4}};
27 
28     // Add a new unique UDB
29     UniformDataCache::Index id1;
30     UniformDataBlock cachedUdb1;
31     {
32         PipelineDataGatherer gatherer{Layout::kStd430};
33         SkDEBUGCODE(UniformExpectationsValidator uev(&gatherer, kUniforms);)
34 
35         gatherer.write(SkV4{7.f, 8.f, 9.f, 10.f});
36         UniformDataBlock udb1 = gatherer.finishUniformDataBlock();
37 
38         id1 = cache.insert(udb1);
39         REPORTER_ASSERT(reporter, id1 != UniformDataCache::kInvalidIndex);
40 
41         cachedUdb1 = cache.lookup(id1).fCpuData;
42         REPORTER_ASSERT(reporter, cachedUdb1.data() != udb1.data());  // must be a separate address
43         REPORTER_ASSERT(reporter, cachedUdb1 == udb1);                // but equal contents
44 
45         REPORTER_ASSERT(reporter, cache.count() == 1);
46     }
47 
48     // Try to add a duplicate UDB
49     {
50         PipelineDataGatherer gatherer{Layout::kStd430};
51         SkDEBUGCODE(UniformExpectationsValidator uev(&gatherer, kUniforms);)
52 
53         gatherer.write(SkV4{7.f, 8.f, 9.f, 10.f});
54         UniformDataBlock udb2 = gatherer.finishUniformDataBlock();
55         REPORTER_ASSERT(reporter, udb2 == cachedUdb1);  // contents are in fact duplicated
56 
57         UniformDataCache::Index id2 = cache.insert(udb2);
58         REPORTER_ASSERT(reporter, id2 == id1);  // original clone's index
59 
60         UniformDataBlock cachedUdb2 = cache.lookup(id2).fCpuData;
61         REPORTER_ASSERT(reporter, cachedUdb2.data() == cachedUdb1.data());  // original address
62 
63         REPORTER_ASSERT(reporter, cache.count() == 1);
64     }
65 
66     // Add a second new unique UDB
67     {
68         PipelineDataGatherer gatherer{Layout::kStd430};
69         SkDEBUGCODE(UniformExpectationsValidator uev(&gatherer, kUniforms);)
70 
71         gatherer.write(SkV4{11.f, 12.f, 13.f, 14.f});
72         UniformDataBlock udb3 = gatherer.finishUniformDataBlock();
73 
74         UniformDataCache::Index id3 = cache.insert(udb3);
75         REPORTER_ASSERT(reporter, id3 != UniformDataCache::kInvalidIndex);
76         REPORTER_ASSERT(reporter, id3 != id1);
77 
78         UniformDataBlock cachedUdb3 = cache.lookup(id3).fCpuData;
79         REPORTER_ASSERT(reporter, cachedUdb3 == udb3);
80 
81         REPORTER_ASSERT(reporter, cache.count() == 2);
82     }
83 
84     // TODO(robertphillips): expand this test to exercise all the UDB comparison failure modes
85 }
86