xref: /aosp_15_r20/external/skia/tests/TraceMemoryDumpTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2015 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/SkTypes.h"
9 #if defined(SK_GL)
10 #include "include/core/SkRefCnt.h"
11 #include "include/core/SkSize.h"
12 #include "include/core/SkString.h"
13 #include "include/core/SkTraceMemoryDump.h"
14 #include "include/gpu/GpuTypes.h"
15 #include "include/gpu/ganesh/GrDirectContext.h"
16 #include "include/gpu/ganesh/gl/GrGLTypes.h"
17 #include "include/private/gpu/ganesh/GrTypesPriv.h"
18 #include "src/gpu/ganesh/GrDirectContextPriv.h"
19 #include "src/gpu/ganesh/gl/GrGLBuffer.h"
20 #include "src/gpu/ganesh/gl/GrGLDefines.h"
21 #include "src/gpu/ganesh/gl/GrGLGpu.h"
22 #include "src/gpu/ganesh/gl/GrGLRenderTarget.h"
23 #include "src/gpu/ganesh/gl/GrGLTexture.h"
24 #include "src/gpu/ganesh/gl/GrGLTextureRenderTarget.h"
25 #include "src/gpu/ganesh/gl/GrGLTypesPriv.h"
26 #include "tests/CtsEnforcement.h"
27 #include "tests/Test.h"
28 
29 #include <cstddef>
30 #include <cstdint>
31 #include <utility>
32 
33 class SkDiscardableMemory;
34 struct GrContextOptions;
35 
36 /*
37  * Build test for SkTraceMemoryDump.
38  */
39 class TestSkTraceMemoryDump : public SkTraceMemoryDump {
40 public:
TestSkTraceMemoryDump(bool shouldDumpWrappedObjects)41     TestSkTraceMemoryDump(bool shouldDumpWrappedObjects)
42             : fShouldDumpWrappedObjects(shouldDumpWrappedObjects) {}
~TestSkTraceMemoryDump()43     ~TestSkTraceMemoryDump() override { }
44 
dumpNumericValue(const char * dumpName,const char * valueName,const char * units,uint64_t value)45     void dumpNumericValue(const char* dumpName, const char* valueName, const char* units,
46                           uint64_t value) override {
47         // Only count "size" dumps, others are just providing metadata.
48         if (SkString("size") == SkString(valueName)) {
49             ++fNumDumpedObjects;
50             fDumpedObjectsSize += value;
51         }
52     }
setMemoryBacking(const char * dumpName,const char * backingType,const char * backingObjectId)53     void setMemoryBacking(const char* dumpName, const char* backingType,
54                           const char* backingObjectId) override { }
setDiscardableMemoryBacking(const char * dumpName,const SkDiscardableMemory & discardableMemoryObject)55     void setDiscardableMemoryBacking(
56         const char* dumpName,
57         const SkDiscardableMemory& discardableMemoryObject) override { }
getRequestedDetails() const58     LevelOfDetail getRequestedDetails() const override {
59         return SkTraceMemoryDump::kObjectsBreakdowns_LevelOfDetail;
60     }
shouldDumpWrappedObjects() const61     bool shouldDumpWrappedObjects() const override { return fShouldDumpWrappedObjects; }
62 
numDumpedObjects() const63     size_t numDumpedObjects() const { return fNumDumpedObjects; }
dumpedObjectsSize() const64     size_t dumpedObjectsSize() const { return fDumpedObjectsSize; }
65 
66 private:
67     bool fShouldDumpWrappedObjects;
68     size_t fNumDumpedObjects = 0;
69     size_t fDumpedObjectsSize = 0;
70 };
71 
ValidateMemoryDumps(skiatest::Reporter * reporter,GrDirectContext * dContext,size_t numDumpedObjects,size_t size,bool isOwned)72 void ValidateMemoryDumps(skiatest::Reporter* reporter, GrDirectContext* dContext,
73                          size_t numDumpedObjects, size_t size, bool isOwned) {
74     // Note than one entry in the dumped objects is expected for the text blob cache.
75     TestSkTraceMemoryDump dump_with_wrapped(true /* shouldDumpWrappedObjects */);
76     dContext->dumpMemoryStatistics(&dump_with_wrapped);
77     REPORTER_ASSERT(reporter, numDumpedObjects == dump_with_wrapped.numDumpedObjects());
78     REPORTER_ASSERT(reporter, size == dump_with_wrapped.dumpedObjectsSize());
79 
80     TestSkTraceMemoryDump dump_no_wrapped(false /* shouldDumpWrappedObjects */);
81     dContext->dumpMemoryStatistics(&dump_no_wrapped);
82     if (isOwned) {
83         REPORTER_ASSERT(reporter, numDumpedObjects == dump_no_wrapped.numDumpedObjects());
84         REPORTER_ASSERT(reporter, size == dump_no_wrapped.dumpedObjectsSize());
85     } else {
86         REPORTER_ASSERT(reporter, 1 == dump_no_wrapped.numDumpedObjects());
87         REPORTER_ASSERT(reporter, 0 == dump_no_wrapped.dumpedObjectsSize());
88     }
89 }
90 
DEF_GANESH_TEST_FOR_GL_CONTEXT(SkTraceMemoryDump_ownedGLBuffer,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)91 DEF_GANESH_TEST_FOR_GL_CONTEXT(SkTraceMemoryDump_ownedGLBuffer,
92                                reporter,
93                                ctxInfo,
94                                CtsEnforcement::kApiLevel_T) {
95     auto dContext = ctxInfo.directContext();
96     GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
97     const size_t kMemorySize = 1024;
98     sk_sp<GrGLBuffer> buffer =
99             GrGLBuffer::Make(gpu, kMemorySize, GrGpuBufferType::kVertex, kDynamic_GrAccessPattern);
100 
101     ValidateMemoryDumps(reporter, dContext, 2, kMemorySize, true /* isOwned */);
102 }
103 
DEF_GANESH_TEST_FOR_GL_CONTEXT(SkTraceMemoryDump_ownedGLTexture,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)104 DEF_GANESH_TEST_FOR_GL_CONTEXT(SkTraceMemoryDump_ownedGLTexture,
105                                reporter,
106                                ctxInfo,
107                                CtsEnforcement::kApiLevel_T) {
108     auto dContext = ctxInfo.directContext();
109     GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
110 
111     GrGLTexture::Desc desc;
112     desc.fTarget = GR_GL_TEXTURE_2D;
113     desc.fID = 7;  // Arbitrary, we don't actually use the texture.
114     desc.fFormat = GrGLFormat::kRGBA8;
115     desc.fOwnership = GrBackendObjectOwnership::kOwned;
116     desc.fIsProtected = skgpu::Protected::kNo;
117     desc.fSize = SkISize::Make(64, 64);
118 
119     auto texture = sk_make_sp<GrGLTexture>(gpu,
120                                            skgpu::Budgeted::kNo,
121                                            desc,
122                                            GrMipmapStatus::kNotAllocated,
123                                            /*label=*/"SkTraceMemoryDump_ownedGLTexture");
124 
125     ValidateMemoryDumps(reporter, dContext, 2, texture->gpuMemorySize(), true /* isOwned */);
126 }
127 
DEF_GANESH_TEST_FOR_GL_CONTEXT(SkTraceMemoryDump_unownedGLTexture,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)128 DEF_GANESH_TEST_FOR_GL_CONTEXT(SkTraceMemoryDump_unownedGLTexture,
129                                reporter,
130                                ctxInfo,
131                                CtsEnforcement::kApiLevel_T) {
132     auto dContext = ctxInfo.directContext();
133     GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
134 
135     GrGLTexture::Desc desc;
136     desc.fTarget = GR_GL_TEXTURE_2D;
137     desc.fID = 7;  // Arbitrary, we don't actually use the texture.
138     desc.fFormat = GrGLFormat::kRGBA8;
139     desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
140     desc.fSize = SkISize::Make(64, 64);
141     desc.fIsProtected = skgpu::Protected::kNo;
142 
143     auto params = sk_make_sp<GrGLTextureParameters>();
144 
145     auto texture = GrGLTexture::MakeWrapped(gpu,
146                                             GrMipmapStatus::kNotAllocated,
147                                             desc,
148                                             std::move(params),
149                                             GrWrapCacheable::kNo,
150                                             kRead_GrIOType,
151                                             /*label=*/{});
152 
153     ValidateMemoryDumps(reporter, dContext, 2, texture->gpuMemorySize(), false /* isOwned */);
154 }
155 
DEF_GANESH_TEST_FOR_GL_CONTEXT(SkTraceMemoryDump_ownedGLRenderTarget,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)156 DEF_GANESH_TEST_FOR_GL_CONTEXT(SkTraceMemoryDump_ownedGLRenderTarget,
157                                reporter,
158                                ctxInfo,
159                                CtsEnforcement::kApiLevel_T) {
160     auto dContext = ctxInfo.directContext();
161     GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
162 
163     static constexpr auto kSize = SkISize::Make(64, 64);
164 
165     GrGLRenderTarget::IDs rtIDs;
166     rtIDs.fMultisampleFBOID = 0;
167     rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
168     rtIDs.fSingleSampleFBOID = 20;
169     rtIDs.fMSColorRenderbufferID = 0;
170     rtIDs.fTotalMemorySamplesPerPixel = 1;
171 
172     sk_sp<GrGLRenderTarget> rt = GrGLRenderTarget::MakeWrapped(gpu,
173                                                                kSize,
174                                                                GrGLFormat::kRGBA8,
175                                                                1,
176                                                                rtIDs,
177                                                                0,
178                                                                skgpu::Protected::kNo,
179                                                                /*label=*/{});
180 
181     ValidateMemoryDumps(reporter, dContext, 2, rt->gpuMemorySize(), true /* isOwned */);
182 }
183 
DEF_GANESH_TEST_FOR_GL_CONTEXT(SkTraceMemoryDump_unownedGLRenderTarget,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)184 DEF_GANESH_TEST_FOR_GL_CONTEXT(SkTraceMemoryDump_unownedGLRenderTarget,
185                                reporter,
186                                ctxInfo,
187                                CtsEnforcement::kApiLevel_T) {
188     auto dContext = ctxInfo.directContext();
189     GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
190 
191     static constexpr auto kSize = SkISize::Make(64, 64);
192 
193     GrGLRenderTarget::IDs rtIDs;
194     rtIDs.fMultisampleFBOID = 12;
195     rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
196     rtIDs.fSingleSampleFBOID = 0;
197     rtIDs.fMSColorRenderbufferID = 0;
198     rtIDs.fTotalMemorySamplesPerPixel = 4;
199 
200     sk_sp<GrGLRenderTarget> rt = GrGLRenderTarget::MakeWrapped(gpu,
201                                                                kSize,
202                                                                GrGLFormat::kRGBA8,
203                                                                4,
204                                                                rtIDs,
205                                                                0,
206                                                                skgpu::Protected::kNo,
207                                                                /*label=*/{});
208 
209     ValidateMemoryDumps(reporter, dContext, 2, rt->gpuMemorySize(), false /* isOwned */);
210 }
211 
DEF_GANESH_TEST_FOR_GL_CONTEXT(SkTraceMemoryDump_ownedGLTextureRenderTarget,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)212 DEF_GANESH_TEST_FOR_GL_CONTEXT(SkTraceMemoryDump_ownedGLTextureRenderTarget,
213                                reporter,
214                                ctxInfo,
215                                CtsEnforcement::kApiLevel_T) {
216     auto dContext = ctxInfo.directContext();
217     GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
218 
219     static constexpr auto kSize = SkISize::Make(64, 64);
220 
221     GrGLTexture::Desc texDesc;
222     texDesc.fSize = kSize;
223     texDesc.fTarget = GR_GL_TEXTURE_2D;
224     texDesc.fID = 17;
225     texDesc.fFormat = GrGLFormat::kRGBA8;
226     texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
227     texDesc.fIsProtected = skgpu::Protected::kNo;
228 
229     GrGLRenderTarget::IDs rtIDs;
230     rtIDs.fMultisampleFBOID = 12;
231     rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
232     rtIDs.fSingleSampleFBOID = 20;
233     rtIDs.fMSColorRenderbufferID = 22;
234     rtIDs.fTotalMemorySamplesPerPixel = 9;
235 
236     auto texRT = sk_make_sp<GrGLTextureRenderTarget>(
237             gpu,
238             skgpu::Budgeted::kYes,
239             8,
240             texDesc,
241             rtIDs,
242             GrMipmapStatus::kNotAllocated,
243             /*label=*/"SkTraceMemoryDump_ownedGLTextureRenderTarget");
244 
245     ValidateMemoryDumps(reporter, dContext, 3, texRT->gpuMemorySize(), true /* isOwned */);
246 }
247 
248 #endif  // defined(SK_GL)
249