blob: d5a9bcb2846f5c70a6c353a266af69ffc0707ecc [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) {
Khushal71652e22018-10-29 13:05:36 -070057 // Note than one entry in the dumped objects is expected for the text blob cache.
Eric Karlaf770022018-03-19 13:04:03 -070058 TestSkTraceMemoryDump dump_with_wrapped(true /* shouldDumpWrappedObjects */);
59 context->dumpMemoryStatistics(&dump_with_wrapped);
Khushal71652e22018-10-29 13:05:36 -070060 REPORTER_ASSERT(reporter, 2 == dump_with_wrapped.numDumpedObjects());
Eric Karlaf770022018-03-19 13:04:03 -070061 REPORTER_ASSERT(reporter, size == dump_with_wrapped.dumpedObjectsSize());
62
63 TestSkTraceMemoryDump dump_no_wrapped(false /* shouldDumpWrappedObjects */);
64 context->dumpMemoryStatistics(&dump_no_wrapped);
65 if (isOwned) {
Khushal71652e22018-10-29 13:05:36 -070066 REPORTER_ASSERT(reporter, 2 == dump_no_wrapped.numDumpedObjects());
Eric Karlaf770022018-03-19 13:04:03 -070067 REPORTER_ASSERT(reporter, size == dump_no_wrapped.dumpedObjectsSize());
68 } else {
Khushal71652e22018-10-29 13:05:36 -070069 REPORTER_ASSERT(reporter, 1 == dump_no_wrapped.numDumpedObjects());
Eric Karlaf770022018-03-19 13:04:03 -070070 REPORTER_ASSERT(reporter, 0 == dump_no_wrapped.dumpedObjectsSize());
ssidf0c98652015-09-30 04:31:23 -070071 }
primiano9a5bd7e2015-08-20 08:00:32 -070072}
Eric Karlaf770022018-03-19 13:04:03 -070073
74DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLBuffer, reporter, ctxInfo) {
75 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050076 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -070077 const size_t kMemorySize = 1024;
Brian Salomon12d22642019-01-29 14:38:50 -050078 sk_sp<GrGLBuffer> buffer =
Brian Salomonae64c192019-02-05 09:41:37 -050079 GrGLBuffer::Make(gpu, kMemorySize, GrGpuBufferType::kVertex, kDynamic_GrAccessPattern);
Eric Karlaf770022018-03-19 13:04:03 -070080
81 ValidateMemoryDumps(reporter, context, kMemorySize, true /* isOwned */);
82}
83
84DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLTexture, reporter, ctxInfo) {
85 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050086 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -070087
88 GrSurfaceDesc desc;
89 desc.fFlags = kNone_GrSurfaceFlags;
90 desc.fWidth = 64;
91 desc.fHeight = 64;
92 desc.fConfig = kRGBA_8888_GrPixelConfig;
93 desc.fSampleCnt = 1;
94
95 GrGLTextureInfo glInfo;
96 glInfo.fTarget = GR_GL_TEXTURE_2D;
97 glInfo.fID = 7; // Arbitrary, we don't actually use the texture.
98 glInfo.fFormat = GR_GL_RGBA8;
99
100 GrGLTexture::IDDesc idDesc;
101 idDesc.fInfo = glInfo;
102 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
103
104 auto texture = sk_make_sp<GrGLTexture>(gpu, SkBudgeted::kNo, desc, idDesc,
105 GrMipMapsStatus::kNotAllocated);
106
107 ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), true /* isOwned */);
108}
109
110DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLTexture, reporter, ctxInfo) {
111 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500112 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -0700113
114 GrSurfaceDesc desc;
115 desc.fFlags = kNone_GrSurfaceFlags;
116 desc.fWidth = 64;
117 desc.fHeight = 64;
118 desc.fConfig = kRGBA_8888_GrPixelConfig;
119 desc.fSampleCnt = 1;
120
121 GrGLTextureInfo glInfo;
122 glInfo.fTarget = GR_GL_TEXTURE_2D;
123 glInfo.fID = 7; // Arbitrary, we don't actually use the texture.
124 glInfo.fFormat = GR_GL_RGBA8;
125
126 GrGLTexture::IDDesc idDesc;
127 idDesc.fInfo = glInfo;
128 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
129
Greg Daniel2268ad22018-11-15 09:27:38 -0500130 auto texture = GrGLTexture::MakeWrapped(gpu, desc, GrMipMapsStatus::kNotAllocated, idDesc,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500131 GrWrapCacheable::kNo, kRead_GrIOType);
Eric Karlaf770022018-03-19 13:04:03 -0700132
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();
Robert Phillips9da87e02019-02-04 13:26:26 -0500138 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -0700139
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
Greg Daniel4065d452018-11-16 15:43:41 -0500153 sk_sp<GrGLRenderTarget> rt = GrGLRenderTarget::MakeWrapped(gpu, sd, GR_GL_RGBA8, iddesc, 0);
Eric Karlaf770022018-03-19 13:04:03 -0700154
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();
Robert Phillips9da87e02019-02-04 13:26:26 -0500160 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -0700161
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
Greg Daniel4065d452018-11-16 15:43:41 -0500175 sk_sp<GrGLRenderTarget> rt = GrGLRenderTarget::MakeWrapped(gpu, sd, GR_GL_RGBA8, iddesc, 0);
Eric Karlaf770022018-03-19 13:04:03 -0700176
177 ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), false /* isOwned */);
178}