blob: d22c20041e0cc224cfd0064cf5f4a5b11680f21d [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrContextPriv.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"
John Rosascoa9b348f2019-11-08 13:18:15 -080020#endif
Eric Karlaf770022018-03-19 13:04:03 -070021
primiano9a5bd7e2015-08-20 08:00:32 -070022/*
23 * Build test for SkTraceMemoryDump.
24 */
25class TestSkTraceMemoryDump : public SkTraceMemoryDump {
26public:
Eric Karlaf770022018-03-19 13:04:03 -070027 TestSkTraceMemoryDump(bool shouldDumpWrappedObjects)
28 : fShouldDumpWrappedObjects(shouldDumpWrappedObjects) {}
primiano9a5bd7e2015-08-20 08:00:32 -070029 ~TestSkTraceMemoryDump() override { }
30
31 void dumpNumericValue(const char* dumpName, const char* valueName, const char* units,
Eric Karlaf770022018-03-19 13:04:03 -070032 uint64_t value) override {
33 // Only count "size" dumps, others are just providing metadata.
34 if (SkString("size") == SkString(valueName)) {
35 ++fNumDumpedObjects;
36 fDumpedObjectsSize += value;
37 }
38 }
primiano9a5bd7e2015-08-20 08:00:32 -070039 void setMemoryBacking(const char* dumpName, const char* backingType,
40 const char* backingObjectId) override { }
41 void setDiscardableMemoryBacking(
42 const char* dumpName,
43 const SkDiscardableMemory& discardableMemoryObject) override { }
ssidf0c98652015-09-30 04:31:23 -070044 LevelOfDetail getRequestedDetails() const override {
45 return SkTraceMemoryDump::kObjectsBreakdowns_LevelOfDetail;
46 }
Eric Karlaf770022018-03-19 13:04:03 -070047 bool shouldDumpWrappedObjects() const override { return fShouldDumpWrappedObjects; }
48
49 size_t numDumpedObjects() const { return fNumDumpedObjects; }
50 size_t dumpedObjectsSize() const { return fDumpedObjectsSize; }
51
52private:
53 bool fShouldDumpWrappedObjects;
54 size_t fNumDumpedObjects = 0;
55 size_t fDumpedObjectsSize = 0;
primiano9a5bd7e2015-08-20 08:00:32 -070056};
57
Eric Karlaf770022018-03-19 13:04:03 -070058void ValidateMemoryDumps(skiatest::Reporter* reporter, GrContext* context, size_t size,
59 bool isOwned) {
Khushal71652e22018-10-29 13:05:36 -070060 // Note than one entry in the dumped objects is expected for the text blob cache.
Eric Karlaf770022018-03-19 13:04:03 -070061 TestSkTraceMemoryDump dump_with_wrapped(true /* shouldDumpWrappedObjects */);
62 context->dumpMemoryStatistics(&dump_with_wrapped);
Khushal71652e22018-10-29 13:05:36 -070063 REPORTER_ASSERT(reporter, 2 == dump_with_wrapped.numDumpedObjects());
Eric Karlaf770022018-03-19 13:04:03 -070064 REPORTER_ASSERT(reporter, size == dump_with_wrapped.dumpedObjectsSize());
65
66 TestSkTraceMemoryDump dump_no_wrapped(false /* shouldDumpWrappedObjects */);
67 context->dumpMemoryStatistics(&dump_no_wrapped);
68 if (isOwned) {
Khushal71652e22018-10-29 13:05:36 -070069 REPORTER_ASSERT(reporter, 2 == dump_no_wrapped.numDumpedObjects());
Eric Karlaf770022018-03-19 13:04:03 -070070 REPORTER_ASSERT(reporter, size == dump_no_wrapped.dumpedObjectsSize());
71 } else {
Khushal71652e22018-10-29 13:05:36 -070072 REPORTER_ASSERT(reporter, 1 == dump_no_wrapped.numDumpedObjects());
Eric Karlaf770022018-03-19 13:04:03 -070073 REPORTER_ASSERT(reporter, 0 == dump_no_wrapped.dumpedObjectsSize());
ssidf0c98652015-09-30 04:31:23 -070074 }
primiano9a5bd7e2015-08-20 08:00:32 -070075}
Eric Karlaf770022018-03-19 13:04:03 -070076
John Rosascoa9b348f2019-11-08 13:18:15 -080077#ifdef SK_GL
Eric Karlaf770022018-03-19 13:04:03 -070078DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLBuffer, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040079 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050080 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -070081 const size_t kMemorySize = 1024;
Brian Salomon12d22642019-01-29 14:38:50 -050082 sk_sp<GrGLBuffer> buffer =
Brian Salomonae64c192019-02-05 09:41:37 -050083 GrGLBuffer::Make(gpu, kMemorySize, GrGpuBufferType::kVertex, kDynamic_GrAccessPattern);
Eric Karlaf770022018-03-19 13:04:03 -070084
85 ValidateMemoryDumps(reporter, context, kMemorySize, true /* isOwned */);
86}
87
88DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLTexture, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040089 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050090 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -070091
Brian Salomonea4ad302019-08-07 13:04:55 -040092 GrGLTexture::Desc desc;
93 desc.fTarget = GR_GL_TEXTURE_2D;
94 desc.fID = 7; // Arbitrary, we don't actually use the texture.
95 desc.fFormat = GrGLFormat::kRGBA8;
96 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomonea4ad302019-08-07 13:04:55 -040097 desc.fSize = SkISize::Make(64, 64);
Eric Karlaf770022018-03-19 13:04:03 -070098
Brian Salomonea4ad302019-08-07 13:04:55 -040099 auto texture =
100 sk_make_sp<GrGLTexture>(gpu, SkBudgeted::kNo, desc, GrMipMapsStatus::kNotAllocated);
Eric Karlaf770022018-03-19 13:04:03 -0700101
102 ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), true /* isOwned */);
103}
104
105DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLTexture, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400106 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500107 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -0700108
Brian Salomonea4ad302019-08-07 13:04:55 -0400109 GrGLTexture::Desc desc;
110 desc.fTarget = GR_GL_TEXTURE_2D;
111 desc.fID = 7; // Arbitrary, we don't actually use the texture.
112 desc.fFormat = GrGLFormat::kRGBA8;
113 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomonea4ad302019-08-07 13:04:55 -0400114 desc.fSize = SkISize::Make(64, 64);
Eric Karlaf770022018-03-19 13:04:03 -0700115
Brian Salomone2826ab2019-06-04 15:58:31 -0400116 auto params = sk_make_sp<GrGLTextureParameters>();
117
118 auto texture =
Brian Salomonea4ad302019-08-07 13:04:55 -0400119 GrGLTexture::MakeWrapped(gpu, GrMipMapsStatus::kNotAllocated, desc, std::move(params),
120 GrWrapCacheable::kNo, kRead_GrIOType);
Eric Karlaf770022018-03-19 13:04:03 -0700121
122 ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), false /* isOwned */);
123}
124
125DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLRenderTarget, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400126 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500127 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -0700128
Brian Salomonea4ad302019-08-07 13:04:55 -0400129 static constexpr auto kSize = SkISize::Make(64, 64);
Eric Karlaf770022018-03-19 13:04:03 -0700130
Brian Salomonea4ad302019-08-07 13:04:55 -0400131 GrGLRenderTarget::IDs rtIDs;
132 rtIDs.fRTFBOID = 20;
133 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
134 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
135 rtIDs.fMSColorRenderbufferID = 22;
Eric Karlaf770022018-03-19 13:04:03 -0700136
Brian Salomonea4ad302019-08-07 13:04:55 -0400137 sk_sp<GrGLRenderTarget> rt =
Greg Danield51fa2f2020-01-22 16:53:38 -0500138 GrGLRenderTarget::MakeWrapped(gpu, kSize, GrGLFormat::kRGBA8, 1, rtIDs, 0);
Eric Karlaf770022018-03-19 13:04:03 -0700139
140 ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), true /* isOwned */);
141}
142
143DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLRenderTarget, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400144 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500145 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -0700146
Brian Salomonea4ad302019-08-07 13:04:55 -0400147 static constexpr auto kSize = SkISize::Make(64, 64);
Eric Karlaf770022018-03-19 13:04:03 -0700148
Brian Salomonea4ad302019-08-07 13:04:55 -0400149 GrGLRenderTarget::IDs rtIDs;
150 rtIDs.fRTFBOID = 20;
151 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
152 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
153 rtIDs.fMSColorRenderbufferID = 22;
Eric Karlaf770022018-03-19 13:04:03 -0700154
Brian Salomonea4ad302019-08-07 13:04:55 -0400155 sk_sp<GrGLRenderTarget> rt =
Greg Danield51fa2f2020-01-22 16:53:38 -0500156 GrGLRenderTarget::MakeWrapped(gpu, kSize, GrGLFormat::kRGBA8, 1, rtIDs, 0);
Eric Karlaf770022018-03-19 13:04:03 -0700157
158 ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), false /* isOwned */);
159}
John Rosascoa9b348f2019-11-08 13:18:15 -0800160#endif // SK_GL