• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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 "include/gpu/graphite/Recording.h"
13 #include "src/gpu/graphite/Buffer.h"
14 #include "src/gpu/graphite/RecorderPriv.h"
15 #include "src/gpu/graphite/UploadBufferManager.h"
16 
17 namespace skgpu::graphite {
18 
DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(UploadBufferManagerTest,reporter,context)19 DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(UploadBufferManagerTest, reporter, context) {
20     std::unique_ptr<Recorder> recorder = context->makeRecorder();
21     UploadBufferManager* bufferManager = recorder->priv().uploadBufferManager();
22 
23     // The test source data.
24     char src[8] = {
25             1, 2, 3, 4,
26             5, 6, 7, 8,
27     };
28 
29     // Test multiple small writes to a reused buffer.
30     auto [smWriter0, smBufferInfo0] = bufferManager->getUploadWriter(10, 1);
31     smWriter0.write(/*offset=*/0, src, /*srcRowBytes=*/4, /*dstRowBytes=*/3, /*trimRowBytes=*/3,
32                     /*rowCount=*/2);
33     smWriter0.write(/*offset=*/6, src, /*srcRowBytes=*/4, /*dstRowBytes=*/2, /*trimRowBytes=*/2,
34                     /*rowCount=*/2);
35 
36     auto [smWriter1, smBufferInfo1] = bufferManager->getUploadWriter(4, 1);
37     smWriter1.write(/*offset=*/0, src, /*srcRowBytes=*/4, /*dstRowBytes=*/2, /*trimRowBytes=*/2,
38                     /*rowCount=*/2);
39 
40     REPORTER_ASSERT(reporter, smBufferInfo0.fBuffer == smBufferInfo1.fBuffer);
41     REPORTER_ASSERT(reporter, smBufferInfo0.fOffset == 0);
42     REPORTER_ASSERT(reporter, smBufferInfo1.fOffset >= 10);
43 
44     // Test a large write, which should get its own dedicated buffer.
45     auto [lgWriter, lgBufferInfo] = bufferManager->getUploadWriter((64 << 10) + 1, 1);
46     lgWriter.write(/*offset=*/0, src, /*srcRowBytes=*/4, /*dstRowBytes=*/2, /*trimRowBytes=*/2,
47                    /*rowCount=*/2);
48 
49     REPORTER_ASSERT(reporter, lgBufferInfo.fBuffer != smBufferInfo0.fBuffer);
50     REPORTER_ASSERT(reporter, lgBufferInfo.fOffset == 0);
51     REPORTER_ASSERT(reporter, lgBufferInfo.fBuffer->isMapped());
52     const void* lgBufferMap = const_cast<Buffer*>(lgBufferInfo.fBuffer)->map();
53     const char expectedLgBufferMap[4] = {
54             1, 2,
55             5, 6,
56     };
57     REPORTER_ASSERT(reporter,
58                     memcmp(lgBufferMap, expectedLgBufferMap, sizeof(expectedLgBufferMap)) == 0);
59 
60     // Test another small write after the large write.
61     auto [smWriter2, smBufferInfo2] = bufferManager->getUploadWriter(2, 1);
62     smWriter2.write(/*offset=*/0, src, /*srcRowBytes=*/4, /*dstRowBytes=*/2, /*trimRowBytes=*/2,
63                     /*rowCount=*/1);
64 
65     REPORTER_ASSERT(reporter, smBufferInfo2.fBuffer == smBufferInfo0.fBuffer);
66     REPORTER_ASSERT(reporter, smBufferInfo2.fOffset >= 4 + smBufferInfo1.fOffset);
67 
68     REPORTER_ASSERT(reporter, smBufferInfo0.fBuffer->isMapped());
69     const char* smBufferMap =
70                 reinterpret_cast<const char*>(const_cast<Buffer*>(smBufferInfo0.fBuffer)->map());
71     // Each section of written data could be offset and aligned by GPU-required rules, so we can't
72     // easily validate the contents of the buffer in one go, and instead test at each of the three
73     // reported offsets.
74     const char expectedSmBuffer0[10] = { 1, 2, 3, 5, 6, 7, 1, 2, 5, 6 };
75     const char expectedSmBuffer1[4] = { 1, 2, 5, 6 };
76     const char expectedSmBuffer2[2] = { 1, 2};
77     REPORTER_ASSERT(reporter, memcmp(smBufferMap + smBufferInfo0.fOffset,
78                                      expectedSmBuffer0,
79                                      sizeof(expectedSmBuffer0)) == 0);
80     REPORTER_ASSERT(reporter, memcmp(smBufferMap + smBufferInfo1.fOffset,
81                                      expectedSmBuffer1,
82                                      sizeof(expectedSmBuffer1)) == 0);
83     REPORTER_ASSERT(reporter, memcmp(smBufferMap + smBufferInfo2.fOffset,
84                                      expectedSmBuffer1,
85                                      sizeof(expectedSmBuffer2)) == 0);
86 
87     // Snap a Recording from the Recorder. This will transfer resources from the UploadBufferManager
88     // to the Recording.
89     auto recording = recorder->snap();
90 
91     // Test writes with a required alignment.
92     auto [alWriter0, alBufferInfo0] = bufferManager->getUploadWriter(6, 4);
93     alWriter0.write(/*offset=*/0, src, /*srcRowBytes=*/4, /*dstRowBytes=*/3, /*trimRowBytes=*/3,
94                     /*rowCount=*/2);
95 
96     auto [alWriter1, alBufferInfo1] = bufferManager->getUploadWriter(2, 4);
97     alWriter1.write(/*offset=*/0, src, /*srcRowBytes=*/4, /*dstRowBytes=*/2, /*trimRowBytes=*/2,
98                     /*rowCount=*/1);
99 
100     // Should not share a buffer with earlier small writes, since we've transferred previously-
101     // allocated resources to the command buffer.
102     REPORTER_ASSERT(reporter, alBufferInfo0.fBuffer != smBufferInfo0.fBuffer);
103     REPORTER_ASSERT(reporter, alBufferInfo0.fBuffer == alBufferInfo1.fBuffer);
104     REPORTER_ASSERT(reporter, alBufferInfo0.fOffset == 0);
105     REPORTER_ASSERT(reporter, alBufferInfo1.fOffset == 8);
106 
107     // From alWriter0.
108     const char expectedAlBufferMap0[6] = {
109             1, 2, 3,
110             5, 6, 7,
111     };
112     // From alWriter1.
113     const char expectedAlBufferMap1[2] = {
114             1, 2,
115     };
116 
117     REPORTER_ASSERT(reporter, alBufferInfo0.fBuffer->isMapped());
118     const void* alBufferMap = const_cast<Buffer*>(alBufferInfo0.fBuffer)->map();
119     REPORTER_ASSERT(reporter,
120                     memcmp(alBufferMap, expectedAlBufferMap0, sizeof(expectedAlBufferMap0)) == 0);
121 
122     alBufferMap = SkTAddOffset<const void>(alBufferMap, 8);
123     REPORTER_ASSERT(reporter,
124                     memcmp(alBufferMap, expectedAlBufferMap1, sizeof(expectedAlBufferMap1)) == 0);
125 }
126 
127 }  // namespace skgpu::graphite
128