blob: 5cae99bca65613e479ce4595d89a92c00990d9f2 [file] [log] [blame]
primiano9a5bd7e2015-08-20 08:00:32 -07001/*
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 "SkTraceMemoryDump.h"
9
10#include "Test.h"
11
Eric Karlaf770022018-03-19 13:04:03 -070012// These tests are currently GPU-speicifc.
13#if SK_SUPPORT_GPU
14#include "GrContextPriv.h"
15#include "GrRenderTarget.h"
16#include "GrTexture.h"
17#include "gl/GrGLBuffer.h"
18#include "gl/GrGLDefines.h"
19#include "gl/GrGLGpu.h"
20
primiano9a5bd7e2015-08-20 08:00:32 -070021/*
22 * Build test for SkTraceMemoryDump.
23 */
24class TestSkTraceMemoryDump : public SkTraceMemoryDump {
25public:
Eric Karlaf770022018-03-19 13:04:03 -070026 TestSkTraceMemoryDump(bool shouldDumpWrappedObjects)
27 : fShouldDumpWrappedObjects(shouldDumpWrappedObjects) {}
primiano9a5bd7e2015-08-20 08:00:32 -070028 ~TestSkTraceMemoryDump() override { }
29
30 void dumpNumericValue(const char* dumpName, const char* valueName, const char* units,
Eric Karlaf770022018-03-19 13:04:03 -070031 uint64_t value) override {
32 // Only count "size" dumps, others are just providing metadata.
33 if (SkString("size") == SkString(valueName)) {
34 ++fNumDumpedObjects;
35 fDumpedObjectsSize += value;
36 }
37 }
primiano9a5bd7e2015-08-20 08:00:32 -070038 void setMemoryBacking(const char* dumpName, const char* backingType,
39 const char* backingObjectId) override { }
40 void setDiscardableMemoryBacking(
41 const char* dumpName,
42 const SkDiscardableMemory& discardableMemoryObject) override { }
ssidf0c98652015-09-30 04:31:23 -070043 LevelOfDetail getRequestedDetails() const override {
44 return SkTraceMemoryDump::kObjectsBreakdowns_LevelOfDetail;
45 }
Eric Karlaf770022018-03-19 13:04:03 -070046 bool shouldDumpWrappedObjects() const override { return fShouldDumpWrappedObjects; }
47
48 size_t numDumpedObjects() const { return fNumDumpedObjects; }
49 size_t dumpedObjectsSize() const { return fDumpedObjectsSize; }
50
51private:
52 bool fShouldDumpWrappedObjects;
53 size_t fNumDumpedObjects = 0;
54 size_t fDumpedObjectsSize = 0;
primiano9a5bd7e2015-08-20 08:00:32 -070055};
56
Eric Karlaf770022018-03-19 13:04:03 -070057void ValidateMemoryDumps(skiatest::Reporter* reporter, GrContext* context, size_t size,
58 bool isOwned) {
59 TestSkTraceMemoryDump dump_with_wrapped(true /* shouldDumpWrappedObjects */);
60 context->dumpMemoryStatistics(&dump_with_wrapped);
61 REPORTER_ASSERT(reporter, 1 == dump_with_wrapped.numDumpedObjects());
62 REPORTER_ASSERT(reporter, size == dump_with_wrapped.dumpedObjectsSize());
63
64 TestSkTraceMemoryDump dump_no_wrapped(false /* shouldDumpWrappedObjects */);
65 context->dumpMemoryStatistics(&dump_no_wrapped);
66 if (isOwned) {
67 REPORTER_ASSERT(reporter, 1 == dump_no_wrapped.numDumpedObjects());
68 REPORTER_ASSERT(reporter, size == dump_no_wrapped.dumpedObjectsSize());
69 } else {
70 REPORTER_ASSERT(reporter, 0 == dump_no_wrapped.numDumpedObjects());
71 REPORTER_ASSERT(reporter, 0 == dump_no_wrapped.dumpedObjectsSize());
ssidf0c98652015-09-30 04:31:23 -070072 }
primiano9a5bd7e2015-08-20 08:00:32 -070073}
Eric Karlaf770022018-03-19 13:04:03 -070074
75DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLBuffer, reporter, ctxInfo) {
76 GrContext* context = ctxInfo.grContext();
77 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu());
78 const size_t kMemorySize = 1024;
79 sk_sp<GrGLBuffer> buffer(
80 GrGLBuffer::Create(gpu, kMemorySize, kVertex_GrBufferType, kDynamic_GrAccessPattern));
81
82 ValidateMemoryDumps(reporter, context, kMemorySize, true /* isOwned */);
83}
84
85DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLTexture, reporter, ctxInfo) {
86 GrContext* context = ctxInfo.grContext();
87 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu());
88
89 GrSurfaceDesc desc;
90 desc.fFlags = kNone_GrSurfaceFlags;
91 desc.fWidth = 64;
92 desc.fHeight = 64;
93 desc.fConfig = kRGBA_8888_GrPixelConfig;
94 desc.fSampleCnt = 1;
95
96 GrGLTextureInfo glInfo;
97 glInfo.fTarget = GR_GL_TEXTURE_2D;
98 glInfo.fID = 7; // Arbitrary, we don't actually use the texture.
99 glInfo.fFormat = GR_GL_RGBA8;
100
101 GrGLTexture::IDDesc idDesc;
102 idDesc.fInfo = glInfo;
103 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
104
105 auto texture = sk_make_sp<GrGLTexture>(gpu, SkBudgeted::kNo, desc, idDesc,
106 GrMipMapsStatus::kNotAllocated);
107
108 ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), true /* isOwned */);
109}
110
111DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLTexture, reporter, ctxInfo) {
112 GrContext* context = ctxInfo.grContext();
113 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu());
114
115 GrSurfaceDesc desc;
116 desc.fFlags = kNone_GrSurfaceFlags;
117 desc.fWidth = 64;
118 desc.fHeight = 64;
119 desc.fConfig = kRGBA_8888_GrPixelConfig;
120 desc.fSampleCnt = 1;
121
122 GrGLTextureInfo glInfo;
123 glInfo.fTarget = GR_GL_TEXTURE_2D;
124 glInfo.fID = 7; // Arbitrary, we don't actually use the texture.
125 glInfo.fFormat = GR_GL_RGBA8;
126
127 GrGLTexture::IDDesc idDesc;
128 idDesc.fInfo = glInfo;
129 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
130
131 auto texture = GrGLTexture::MakeWrapped(gpu, desc, GrMipMapsStatus::kNotAllocated, idDesc);
132
133 ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), false /* isOwned */);
134}
135
136DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLRenderTarget, reporter, ctxInfo) {
137 GrContext* context = ctxInfo.grContext();
138 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu());
139
140 GrSurfaceDesc sd;
141 sd.fFlags = kRenderTarget_GrSurfaceFlag;
142 sd.fWidth = 64;
143 sd.fHeight = 64;
144 sd.fConfig = kRGBA_8888_GrPixelConfig;
145
146 GrGLRenderTarget::IDDesc iddesc;
147 iddesc.fRTFBOID = 20;
148 iddesc.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
149 iddesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
150 iddesc.fMSColorRenderbufferID = 22;
151 iddesc.fIsMixedSampled = false;
152
153 sk_sp<GrGLRenderTarget> rt = GrGLRenderTarget::MakeWrapped(gpu, sd, iddesc, 0);
154
155 ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), true /* isOwned */);
156}
157
158DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLRenderTarget, reporter, ctxInfo) {
159 GrContext* context = ctxInfo.grContext();
160 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu());
161
162 GrSurfaceDesc sd;
163 sd.fFlags = kRenderTarget_GrSurfaceFlag;
164 sd.fWidth = 64;
165 sd.fHeight = 64;
166 sd.fConfig = kRGBA_8888_GrPixelConfig;
167
168 GrGLRenderTarget::IDDesc iddesc;
169 iddesc.fRTFBOID = 20;
170 iddesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
171 iddesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
172 iddesc.fMSColorRenderbufferID = 22;
173 iddesc.fIsMixedSampled = false;
174
175 sk_sp<GrGLRenderTarget> rt = GrGLRenderTarget::MakeWrapped(gpu, sd, iddesc, 0);
176
177 ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), false /* isOwned */);
178}
179
180#endif