blob: efc66a573c21626cb8e4a8f5134d0cead3c3276f [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTraceMemoryDump.h"
Robert Phillips6d344c32020-07-06 10:56:46 -04009#include "include/gpu/GrDirectContext.h"
primiano9a5bd7e2015-08-20 08:00:32 -070010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "tests/Test.h"
primiano9a5bd7e2015-08-20 08:00:32 -070012
Adlai Hollera0693042020-10-14 11:23:11 -040013#include "src/gpu/GrDirectContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040014#include "src/gpu/GrRenderTarget.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000015#include "src/gpu/GrTexture.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080016#ifdef SK_GL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/gl/GrGLBuffer.h"
18#include "src/gpu/gl/GrGLDefines.h"
19#include "src/gpu/gl/GrGLGpu.h"
Chris Daltona655a332021-04-07 15:04:22 -060020#include "src/gpu/gl/GrGLTextureRenderTarget.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080021#endif
Eric Karlaf770022018-03-19 13:04:03 -070022
primiano9a5bd7e2015-08-20 08:00:32 -070023/*
24 * Build test for SkTraceMemoryDump.
25 */
26class TestSkTraceMemoryDump : public SkTraceMemoryDump {
27public:
Eric Karlaf770022018-03-19 13:04:03 -070028 TestSkTraceMemoryDump(bool shouldDumpWrappedObjects)
29 : fShouldDumpWrappedObjects(shouldDumpWrappedObjects) {}
primiano9a5bd7e2015-08-20 08:00:32 -070030 ~TestSkTraceMemoryDump() override { }
31
32 void dumpNumericValue(const char* dumpName, const char* valueName, const char* units,
Eric Karlaf770022018-03-19 13:04:03 -070033 uint64_t value) override {
34 // Only count "size" dumps, others are just providing metadata.
35 if (SkString("size") == SkString(valueName)) {
36 ++fNumDumpedObjects;
37 fDumpedObjectsSize += value;
38 }
39 }
primiano9a5bd7e2015-08-20 08:00:32 -070040 void setMemoryBacking(const char* dumpName, const char* backingType,
41 const char* backingObjectId) override { }
42 void setDiscardableMemoryBacking(
43 const char* dumpName,
44 const SkDiscardableMemory& discardableMemoryObject) override { }
ssidf0c98652015-09-30 04:31:23 -070045 LevelOfDetail getRequestedDetails() const override {
46 return SkTraceMemoryDump::kObjectsBreakdowns_LevelOfDetail;
47 }
Eric Karlaf770022018-03-19 13:04:03 -070048 bool shouldDumpWrappedObjects() const override { return fShouldDumpWrappedObjects; }
49
50 size_t numDumpedObjects() const { return fNumDumpedObjects; }
51 size_t dumpedObjectsSize() const { return fDumpedObjectsSize; }
52
53private:
54 bool fShouldDumpWrappedObjects;
55 size_t fNumDumpedObjects = 0;
56 size_t fDumpedObjectsSize = 0;
primiano9a5bd7e2015-08-20 08:00:32 -070057};
58
Chris Daltona655a332021-04-07 15:04:22 -060059void ValidateMemoryDumps(skiatest::Reporter* reporter, GrDirectContext* dContext,
60 size_t numDumpedObjects, size_t size, bool isOwned) {
Khushal71652e22018-10-29 13:05:36 -070061 // Note than one entry in the dumped objects is expected for the text blob cache.
Eric Karlaf770022018-03-19 13:04:03 -070062 TestSkTraceMemoryDump dump_with_wrapped(true /* shouldDumpWrappedObjects */);
Robert Phillips58adb342020-07-23 09:41:57 -040063 dContext->dumpMemoryStatistics(&dump_with_wrapped);
Chris Daltona655a332021-04-07 15:04:22 -060064 REPORTER_ASSERT(reporter, numDumpedObjects == dump_with_wrapped.numDumpedObjects());
Eric Karlaf770022018-03-19 13:04:03 -070065 REPORTER_ASSERT(reporter, size == dump_with_wrapped.dumpedObjectsSize());
66
67 TestSkTraceMemoryDump dump_no_wrapped(false /* shouldDumpWrappedObjects */);
Robert Phillips58adb342020-07-23 09:41:57 -040068 dContext->dumpMemoryStatistics(&dump_no_wrapped);
Eric Karlaf770022018-03-19 13:04:03 -070069 if (isOwned) {
Chris Daltona655a332021-04-07 15:04:22 -060070 REPORTER_ASSERT(reporter, numDumpedObjects == dump_no_wrapped.numDumpedObjects());
Eric Karlaf770022018-03-19 13:04:03 -070071 REPORTER_ASSERT(reporter, size == dump_no_wrapped.dumpedObjectsSize());
72 } else {
Khushal71652e22018-10-29 13:05:36 -070073 REPORTER_ASSERT(reporter, 1 == dump_no_wrapped.numDumpedObjects());
Eric Karlaf770022018-03-19 13:04:03 -070074 REPORTER_ASSERT(reporter, 0 == dump_no_wrapped.dumpedObjectsSize());
ssidf0c98652015-09-30 04:31:23 -070075 }
primiano9a5bd7e2015-08-20 08:00:32 -070076}
Eric Karlaf770022018-03-19 13:04:03 -070077
John Rosascoa9b348f2019-11-08 13:18:15 -080078#ifdef SK_GL
Eric Karlaf770022018-03-19 13:04:03 -070079DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLBuffer, reporter, ctxInfo) {
Robert Phillips58adb342020-07-23 09:41:57 -040080 auto dContext = ctxInfo.directContext();
81 GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -070082 const size_t kMemorySize = 1024;
Brian Salomon12d22642019-01-29 14:38:50 -050083 sk_sp<GrGLBuffer> buffer =
Brian Salomonae64c192019-02-05 09:41:37 -050084 GrGLBuffer::Make(gpu, kMemorySize, GrGpuBufferType::kVertex, kDynamic_GrAccessPattern);
Eric Karlaf770022018-03-19 13:04:03 -070085
Chris Daltona655a332021-04-07 15:04:22 -060086 ValidateMemoryDumps(reporter, dContext, 2, kMemorySize, true /* isOwned */);
Eric Karlaf770022018-03-19 13:04:03 -070087}
88
89DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLTexture, reporter, ctxInfo) {
Robert Phillips58adb342020-07-23 09:41:57 -040090 auto dContext = ctxInfo.directContext();
91 GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -070092
Brian Salomonea4ad302019-08-07 13:04:55 -040093 GrGLTexture::Desc desc;
94 desc.fTarget = GR_GL_TEXTURE_2D;
95 desc.fID = 7; // Arbitrary, we don't actually use the texture.
96 desc.fFormat = GrGLFormat::kRGBA8;
97 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomonea4ad302019-08-07 13:04:55 -040098 desc.fSize = SkISize::Make(64, 64);
Eric Karlaf770022018-03-19 13:04:03 -070099
Brian Salomonea4ad302019-08-07 13:04:55 -0400100 auto texture =
Brian Salomona6db5102020-07-21 09:56:23 -0400101 sk_make_sp<GrGLTexture>(gpu, SkBudgeted::kNo, desc, GrMipmapStatus::kNotAllocated);
Eric Karlaf770022018-03-19 13:04:03 -0700102
Chris Daltona655a332021-04-07 15:04:22 -0600103 ValidateMemoryDumps(reporter, dContext, 2, texture->gpuMemorySize(), true /* isOwned */);
Eric Karlaf770022018-03-19 13:04:03 -0700104}
105
106DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLTexture, reporter, ctxInfo) {
Robert Phillips58adb342020-07-23 09:41:57 -0400107 auto dContext = ctxInfo.directContext();
108 GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -0700109
Brian Salomonea4ad302019-08-07 13:04:55 -0400110 GrGLTexture::Desc desc;
111 desc.fTarget = GR_GL_TEXTURE_2D;
112 desc.fID = 7; // Arbitrary, we don't actually use the texture.
113 desc.fFormat = GrGLFormat::kRGBA8;
114 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomonea4ad302019-08-07 13:04:55 -0400115 desc.fSize = SkISize::Make(64, 64);
Eric Karlaf770022018-03-19 13:04:03 -0700116
Brian Salomone2826ab2019-06-04 15:58:31 -0400117 auto params = sk_make_sp<GrGLTextureParameters>();
118
119 auto texture =
Brian Salomona6db5102020-07-21 09:56:23 -0400120 GrGLTexture::MakeWrapped(gpu, GrMipmapStatus::kNotAllocated, desc, std::move(params),
Brian Salomonea4ad302019-08-07 13:04:55 -0400121 GrWrapCacheable::kNo, kRead_GrIOType);
Eric Karlaf770022018-03-19 13:04:03 -0700122
Chris Daltona655a332021-04-07 15:04:22 -0600123 ValidateMemoryDumps(reporter, dContext, 2, texture->gpuMemorySize(), false /* isOwned */);
Eric Karlaf770022018-03-19 13:04:03 -0700124}
125
126DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLRenderTarget, reporter, ctxInfo) {
Robert Phillips58adb342020-07-23 09:41:57 -0400127 auto dContext = ctxInfo.directContext();
128 GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -0700129
Brian Salomonea4ad302019-08-07 13:04:55 -0400130 static constexpr auto kSize = SkISize::Make(64, 64);
Eric Karlaf770022018-03-19 13:04:03 -0700131
Brian Salomonea4ad302019-08-07 13:04:55 -0400132 GrGLRenderTarget::IDs rtIDs;
Chris Daltona655a332021-04-07 15:04:22 -0600133 rtIDs.fMultisampleFBOID = 0;
Brian Salomonea4ad302019-08-07 13:04:55 -0400134 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
Chris Daltona655a332021-04-07 15:04:22 -0600135 rtIDs.fSingleSampleFBOID = 20;
136 rtIDs.fMSColorRenderbufferID = 0;
137 rtIDs.fTotalMemorySamplesPerPixel = 1;
Eric Karlaf770022018-03-19 13:04:03 -0700138
Brian Salomonea4ad302019-08-07 13:04:55 -0400139 sk_sp<GrGLRenderTarget> rt =
Greg Danield51fa2f2020-01-22 16:53:38 -0500140 GrGLRenderTarget::MakeWrapped(gpu, kSize, GrGLFormat::kRGBA8, 1, rtIDs, 0);
Eric Karlaf770022018-03-19 13:04:03 -0700141
Chris Daltona655a332021-04-07 15:04:22 -0600142 ValidateMemoryDumps(reporter, dContext, 2, rt->gpuMemorySize(), true /* isOwned */);
Eric Karlaf770022018-03-19 13:04:03 -0700143}
144
145DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLRenderTarget, reporter, ctxInfo) {
Robert Phillips58adb342020-07-23 09:41:57 -0400146 auto dContext = ctxInfo.directContext();
147 GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -0700148
Brian Salomonea4ad302019-08-07 13:04:55 -0400149 static constexpr auto kSize = SkISize::Make(64, 64);
Eric Karlaf770022018-03-19 13:04:03 -0700150
Brian Salomonea4ad302019-08-07 13:04:55 -0400151 GrGLRenderTarget::IDs rtIDs;
Chris Daltona655a332021-04-07 15:04:22 -0600152 rtIDs.fMultisampleFBOID = 12;
Brian Salomonea4ad302019-08-07 13:04:55 -0400153 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
Chris Daltona655a332021-04-07 15:04:22 -0600154 rtIDs.fSingleSampleFBOID = 0;
155 rtIDs.fMSColorRenderbufferID = 0;
156 rtIDs.fTotalMemorySamplesPerPixel = 4;
Eric Karlaf770022018-03-19 13:04:03 -0700157
Brian Salomonea4ad302019-08-07 13:04:55 -0400158 sk_sp<GrGLRenderTarget> rt =
Chris Daltona655a332021-04-07 15:04:22 -0600159 GrGLRenderTarget::MakeWrapped(gpu, kSize, GrGLFormat::kRGBA8, 4, rtIDs, 0);
Eric Karlaf770022018-03-19 13:04:03 -0700160
Chris Daltona655a332021-04-07 15:04:22 -0600161 ValidateMemoryDumps(reporter, dContext, 2, rt->gpuMemorySize(), false /* isOwned */);
Eric Karlaf770022018-03-19 13:04:03 -0700162}
Chris Daltona655a332021-04-07 15:04:22 -0600163
164DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLTextureRenderTarget, reporter,
165 ctxInfo) {
166 auto dContext = ctxInfo.directContext();
167 GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
168
169 static constexpr auto kSize = SkISize::Make(64, 64);
170
171 GrGLTexture::Desc texDesc;
172 texDesc.fSize = kSize;
173 texDesc.fTarget = GR_GL_TEXTURE_2D;
174 texDesc.fID = 17;
175 texDesc.fFormat = GrGLFormat::kRGBA8;
176 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
177
178 GrGLRenderTarget::IDs rtIDs;
179 rtIDs.fMultisampleFBOID = 12;
180 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
181 rtIDs.fSingleSampleFBOID = 20;
182 rtIDs.fMSColorRenderbufferID = 22;
183 rtIDs.fTotalMemorySamplesPerPixel = 9;
184
185 auto texRT = sk_make_sp<GrGLTextureRenderTarget>(gpu, SkBudgeted::kYes, 8, texDesc, rtIDs,
186 GrMipmapStatus::kNotAllocated);
187
188 ValidateMemoryDumps(reporter, dContext, 3, texRT->gpuMemorySize(), true /* isOwned */);
189}
190
John Rosascoa9b348f2019-11-08 13:18:15 -0800191#endif // SK_GL