blob: a0a5b2a768121038c3977843bdd67e77964687a3 [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"
primiano9a5bd7e2015-08-20 08:00:32 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
primiano9a5bd7e2015-08-20 08:00:32 -070011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/gpu/GrTexture.h"
13#include "src/gpu/GrContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040014#include "src/gpu/GrRenderTarget.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080015#ifdef SK_GL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/gl/GrGLBuffer.h"
17#include "src/gpu/gl/GrGLDefines.h"
18#include "src/gpu/gl/GrGLGpu.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080019#endif
Eric Karlaf770022018-03-19 13:04:03 -070020
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) {
Khushal71652e22018-10-29 13:05:36 -070059 // Note than one entry in the dumped objects is expected for the text blob cache.
Eric Karlaf770022018-03-19 13:04:03 -070060 TestSkTraceMemoryDump dump_with_wrapped(true /* shouldDumpWrappedObjects */);
61 context->dumpMemoryStatistics(&dump_with_wrapped);
Khushal71652e22018-10-29 13:05:36 -070062 REPORTER_ASSERT(reporter, 2 == dump_with_wrapped.numDumpedObjects());
Eric Karlaf770022018-03-19 13:04:03 -070063 REPORTER_ASSERT(reporter, size == dump_with_wrapped.dumpedObjectsSize());
64
65 TestSkTraceMemoryDump dump_no_wrapped(false /* shouldDumpWrappedObjects */);
66 context->dumpMemoryStatistics(&dump_no_wrapped);
67 if (isOwned) {
Khushal71652e22018-10-29 13:05:36 -070068 REPORTER_ASSERT(reporter, 2 == dump_no_wrapped.numDumpedObjects());
Eric Karlaf770022018-03-19 13:04:03 -070069 REPORTER_ASSERT(reporter, size == dump_no_wrapped.dumpedObjectsSize());
70 } else {
Khushal71652e22018-10-29 13:05:36 -070071 REPORTER_ASSERT(reporter, 1 == dump_no_wrapped.numDumpedObjects());
Eric Karlaf770022018-03-19 13:04:03 -070072 REPORTER_ASSERT(reporter, 0 == dump_no_wrapped.dumpedObjectsSize());
ssidf0c98652015-09-30 04:31:23 -070073 }
primiano9a5bd7e2015-08-20 08:00:32 -070074}
Eric Karlaf770022018-03-19 13:04:03 -070075
John Rosascoa9b348f2019-11-08 13:18:15 -080076#ifdef SK_GL
Eric Karlaf770022018-03-19 13:04:03 -070077DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLBuffer, reporter, ctxInfo) {
78 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050079 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -070080 const size_t kMemorySize = 1024;
Brian Salomon12d22642019-01-29 14:38:50 -050081 sk_sp<GrGLBuffer> buffer =
Brian Salomonae64c192019-02-05 09:41:37 -050082 GrGLBuffer::Make(gpu, kMemorySize, GrGpuBufferType::kVertex, kDynamic_GrAccessPattern);
Eric Karlaf770022018-03-19 13:04:03 -070083
84 ValidateMemoryDumps(reporter, context, kMemorySize, true /* isOwned */);
85}
86
87DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLTexture, reporter, ctxInfo) {
88 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050089 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -070090
Brian Salomonea4ad302019-08-07 13:04:55 -040091 GrGLTexture::Desc desc;
92 desc.fTarget = GR_GL_TEXTURE_2D;
93 desc.fID = 7; // Arbitrary, we don't actually use the texture.
94 desc.fFormat = GrGLFormat::kRGBA8;
95 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomonea4ad302019-08-07 13:04:55 -040096 desc.fSize = SkISize::Make(64, 64);
Eric Karlaf770022018-03-19 13:04:03 -070097
Brian Salomonea4ad302019-08-07 13:04:55 -040098 auto texture =
99 sk_make_sp<GrGLTexture>(gpu, SkBudgeted::kNo, desc, GrMipMapsStatus::kNotAllocated);
Eric Karlaf770022018-03-19 13:04:03 -0700100
101 ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), true /* isOwned */);
102}
103
104DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLTexture, reporter, ctxInfo) {
105 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500106 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -0700107
Brian Salomonea4ad302019-08-07 13:04:55 -0400108 GrGLTexture::Desc desc;
109 desc.fTarget = GR_GL_TEXTURE_2D;
110 desc.fID = 7; // Arbitrary, we don't actually use the texture.
111 desc.fFormat = GrGLFormat::kRGBA8;
112 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomonea4ad302019-08-07 13:04:55 -0400113 desc.fSize = SkISize::Make(64, 64);
Eric Karlaf770022018-03-19 13:04:03 -0700114
Brian Salomone2826ab2019-06-04 15:58:31 -0400115 auto params = sk_make_sp<GrGLTextureParameters>();
116
117 auto texture =
Brian Salomonea4ad302019-08-07 13:04:55 -0400118 GrGLTexture::MakeWrapped(gpu, GrMipMapsStatus::kNotAllocated, desc, std::move(params),
119 GrWrapCacheable::kNo, kRead_GrIOType);
Eric Karlaf770022018-03-19 13:04:03 -0700120
121 ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), false /* isOwned */);
122}
123
124DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLRenderTarget, reporter, ctxInfo) {
125 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500126 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -0700127
Brian Salomonea4ad302019-08-07 13:04:55 -0400128 static constexpr auto kSize = SkISize::Make(64, 64);
Eric Karlaf770022018-03-19 13:04:03 -0700129
Brian Salomonea4ad302019-08-07 13:04:55 -0400130 GrGLRenderTarget::IDs rtIDs;
131 rtIDs.fRTFBOID = 20;
132 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
133 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
134 rtIDs.fMSColorRenderbufferID = 22;
Eric Karlaf770022018-03-19 13:04:03 -0700135
Brian Salomonea4ad302019-08-07 13:04:55 -0400136 sk_sp<GrGLRenderTarget> rt =
Greg Danield51fa2f2020-01-22 16:53:38 -0500137 GrGLRenderTarget::MakeWrapped(gpu, kSize, GrGLFormat::kRGBA8, 1, rtIDs, 0);
Eric Karlaf770022018-03-19 13:04:03 -0700138
139 ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), true /* isOwned */);
140}
141
142DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLRenderTarget, reporter, ctxInfo) {
143 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500144 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -0700145
Brian Salomonea4ad302019-08-07 13:04:55 -0400146 static constexpr auto kSize = SkISize::Make(64, 64);
Eric Karlaf770022018-03-19 13:04:03 -0700147
Brian Salomonea4ad302019-08-07 13:04:55 -0400148 GrGLRenderTarget::IDs rtIDs;
149 rtIDs.fRTFBOID = 20;
150 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
151 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
152 rtIDs.fMSColorRenderbufferID = 22;
Eric Karlaf770022018-03-19 13:04:03 -0700153
Brian Salomonea4ad302019-08-07 13:04:55 -0400154 sk_sp<GrGLRenderTarget> rt =
Greg Danield51fa2f2020-01-22 16:53:38 -0500155 GrGLRenderTarget::MakeWrapped(gpu, kSize, GrGLFormat::kRGBA8, 1, rtIDs, 0);
Eric Karlaf770022018-03-19 13:04:03 -0700156
157 ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), false /* isOwned */);
158}
John Rosascoa9b348f2019-11-08 13:18:15 -0800159#endif // SK_GL