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