blob: 16096a2098b6ac400f918b3a856a3dc27d334c79 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/gl/GrGLBuffer.h"
16#include "src/gpu/gl/GrGLDefines.h"
17#include "src/gpu/gl/GrGLGpu.h"
Eric Karlaf770022018-03-19 13:04:03 -070018
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
Brian Salomonea4ad302019-08-07 13:04:55 -040088 GrGLTexture::Desc desc;
89 desc.fTarget = GR_GL_TEXTURE_2D;
90 desc.fID = 7; // Arbitrary, we don't actually use the texture.
91 desc.fFormat = GrGLFormat::kRGBA8;
92 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Eric Karlaf770022018-03-19 13:04:03 -070093 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonea4ad302019-08-07 13:04:55 -040094 desc.fSize = SkISize::Make(64, 64);
Eric Karlaf770022018-03-19 13:04:03 -070095
Brian Salomonea4ad302019-08-07 13:04:55 -040096 auto texture =
97 sk_make_sp<GrGLTexture>(gpu, SkBudgeted::kNo, desc, GrMipMapsStatus::kNotAllocated);
Eric Karlaf770022018-03-19 13:04:03 -070098
99 ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), true /* isOwned */);
100}
101
102DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLTexture, reporter, ctxInfo) {
103 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500104 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -0700105
Brian Salomonea4ad302019-08-07 13:04:55 -0400106 GrGLTexture::Desc desc;
107 desc.fTarget = GR_GL_TEXTURE_2D;
108 desc.fID = 7; // Arbitrary, we don't actually use the texture.
109 desc.fFormat = GrGLFormat::kRGBA8;
110 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Eric Karlaf770022018-03-19 13:04:03 -0700111 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonea4ad302019-08-07 13:04:55 -0400112 desc.fSize = SkISize::Make(64, 64);
Eric Karlaf770022018-03-19 13:04:03 -0700113
Brian Salomone2826ab2019-06-04 15:58:31 -0400114 auto params = sk_make_sp<GrGLTextureParameters>();
115
116 auto texture =
Brian Salomonea4ad302019-08-07 13:04:55 -0400117 GrGLTexture::MakeWrapped(gpu, GrMipMapsStatus::kNotAllocated, desc, std::move(params),
118 GrWrapCacheable::kNo, kRead_GrIOType);
Eric Karlaf770022018-03-19 13:04:03 -0700119
120 ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), false /* isOwned */);
121}
122
123DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLRenderTarget, reporter, ctxInfo) {
124 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500125 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Eric Karlaf770022018-03-19 13:04:03 -0700126
Brian Salomonea4ad302019-08-07 13:04:55 -0400127 static constexpr auto kSize = SkISize::Make(64, 64);
128 static constexpr auto kConfig = kRGBA_8888_GrPixelConfig;
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 =
137 GrGLRenderTarget::MakeWrapped(gpu, kSize, GrGLFormat::kRGBA8, kConfig, 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);
147 static constexpr auto kConfig = kRGBA_8888_GrPixelConfig;
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 =
156 GrGLRenderTarget::MakeWrapped(gpu, kSize, GrGLFormat::kRGBA8, kConfig, 1, rtIDs, 0);
Eric Karlaf770022018-03-19 13:04:03 -0700157
158 ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), false /* isOwned */);
159}