• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/GrDirectContext.h"
16 #include "include/gpu/gl/GrGLTypes.h"
17 #include "include/private/gpu/ganesh/GrGLTypesPriv.h"
18 #include "include/private/gpu/ganesh/GrTypesPriv.h"
19 #include "src/gpu/ganesh/GrDirectContextPriv.h"
20 #include "src/gpu/ganesh/gl/GrGLBuffer.h"
21 #include "src/gpu/ganesh/gl/GrGLDefines_impl.h"
22 #include "src/gpu/ganesh/gl/GrGLGpu.h"
23 #include "src/gpu/ganesh/gl/GrGLRenderTarget.h"
24 #include "src/gpu/ganesh/gl/GrGLTexture.h"
25 #include "src/gpu/ganesh/gl/GrGLTextureRenderTarget.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_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLBuffer,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)91 DEF_GANESH_TEST_FOR_GL_RENDERING_CONTEXTS(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_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLTexture,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)104 DEF_GANESH_TEST_FOR_GL_RENDERING_CONTEXTS(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.fSize = SkISize::Make(64, 64);
117 
118     auto texture = sk_make_sp<GrGLTexture>(gpu,
119                                            skgpu::Budgeted::kNo,
120                                            desc,
121                                            GrMipmapStatus::kNotAllocated,
122                                            /*label=*/"SkTraceMemoryDump_ownedGLTexture");
123 
124     ValidateMemoryDumps(reporter, dContext, 2, texture->gpuMemorySize(), true /* isOwned */);
125 }
126 
DEF_GANESH_TEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLTexture,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)127 DEF_GANESH_TEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLTexture,
128                                           reporter,
129                                           ctxInfo,
130                                           CtsEnforcement::kApiLevel_T) {
131     auto dContext = ctxInfo.directContext();
132     GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
133 
134     GrGLTexture::Desc desc;
135     desc.fTarget = GR_GL_TEXTURE_2D;
136     desc.fID = 7;  // Arbitrary, we don't actually use the texture.
137     desc.fFormat = GrGLFormat::kRGBA8;
138     desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
139     desc.fSize = SkISize::Make(64, 64);
140 
141     auto params = sk_make_sp<GrGLTextureParameters>();
142 
143     auto texture = GrGLTexture::MakeWrapped(gpu,
144                                             GrMipmapStatus::kNotAllocated,
145                                             desc,
146                                             std::move(params),
147                                             GrWrapCacheable::kNo,
148                                             kRead_GrIOType,
149                                             /*label=*/{});
150 
151     ValidateMemoryDumps(reporter, dContext, 2, texture->gpuMemorySize(), false /* isOwned */);
152 }
153 
DEF_GANESH_TEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLRenderTarget,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)154 DEF_GANESH_TEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLRenderTarget,
155                                           reporter,
156                                           ctxInfo,
157                                           CtsEnforcement::kApiLevel_T) {
158     auto dContext = ctxInfo.directContext();
159     GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
160 
161     static constexpr auto kSize = SkISize::Make(64, 64);
162 
163     GrGLRenderTarget::IDs rtIDs;
164     rtIDs.fMultisampleFBOID = 0;
165     rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
166     rtIDs.fSingleSampleFBOID = 20;
167     rtIDs.fMSColorRenderbufferID = 0;
168     rtIDs.fTotalMemorySamplesPerPixel = 1;
169 
170     sk_sp<GrGLRenderTarget> rt = GrGLRenderTarget::MakeWrapped(gpu,
171                                                                kSize,
172                                                                GrGLFormat::kRGBA8,
173                                                                1,
174                                                                rtIDs,
175                                                                0,
176                                                                /*label=*/{});
177 
178     ValidateMemoryDumps(reporter, dContext, 2, rt->gpuMemorySize(), true /* isOwned */);
179 }
180 
DEF_GANESH_TEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLRenderTarget,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)181 DEF_GANESH_TEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLRenderTarget,
182                                           reporter,
183                                           ctxInfo,
184                                           CtsEnforcement::kApiLevel_T) {
185     auto dContext = ctxInfo.directContext();
186     GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
187 
188     static constexpr auto kSize = SkISize::Make(64, 64);
189 
190     GrGLRenderTarget::IDs rtIDs;
191     rtIDs.fMultisampleFBOID = 12;
192     rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
193     rtIDs.fSingleSampleFBOID = 0;
194     rtIDs.fMSColorRenderbufferID = 0;
195     rtIDs.fTotalMemorySamplesPerPixel = 4;
196 
197     sk_sp<GrGLRenderTarget> rt = GrGLRenderTarget::MakeWrapped(gpu,
198                                                                kSize,
199                                                                GrGLFormat::kRGBA8,
200                                                                4,
201                                                                rtIDs,
202                                                                0,
203                                                                /*label=*/{});
204 
205     ValidateMemoryDumps(reporter, dContext, 2, rt->gpuMemorySize(), false /* isOwned */);
206 }
207 
DEF_GANESH_TEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLTextureRenderTarget,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)208 DEF_GANESH_TEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLTextureRenderTarget,
209                                           reporter,
210                                           ctxInfo,
211                                           CtsEnforcement::kApiLevel_T) {
212     auto dContext = ctxInfo.directContext();
213     GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
214 
215     static constexpr auto kSize = SkISize::Make(64, 64);
216 
217     GrGLTexture::Desc texDesc;
218     texDesc.fSize = kSize;
219     texDesc.fTarget = GR_GL_TEXTURE_2D;
220     texDesc.fID = 17;
221     texDesc.fFormat = GrGLFormat::kRGBA8;
222     texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
223 
224     GrGLRenderTarget::IDs rtIDs;
225     rtIDs.fMultisampleFBOID = 12;
226     rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
227     rtIDs.fSingleSampleFBOID = 20;
228     rtIDs.fMSColorRenderbufferID = 22;
229     rtIDs.fTotalMemorySamplesPerPixel = 9;
230 
231     auto texRT = sk_make_sp<GrGLTextureRenderTarget>(
232             gpu,
233             skgpu::Budgeted::kYes,
234             8,
235             texDesc,
236             rtIDs,
237             GrMipmapStatus::kNotAllocated,
238             /*label=*/"SkTraceMemoryDump_ownedGLTextureRenderTarget");
239 
240     ValidateMemoryDumps(reporter, dContext, 3, texRT->gpuMemorySize(), true /* isOwned */);
241 }
242 
243 #endif  // defined(SK_GL)
244