blob: 42e2c47f17f4be80086ff6c4fb4ec883bbad0d12 [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;
Eric Karlaf770022018-03-19 13:04:03 -070096 desc.fConfig = kRGBA_8888_GrPixelConfig;
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) {
106 GrContext* context = ctxInfo.grContext();
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;
Eric Karlaf770022018-03-19 13:04:03 -0700114 desc.fConfig = kRGBA_8888_GrPixelConfig;
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 Salomonea4ad302019-08-07 13:04:55 -0400120 GrGLTexture::MakeWrapped(gpu, GrMipMapsStatus::kNotAllocated, desc, std::move(params),
121 GrWrapCacheable::kNo, kRead_GrIOType);
Eric Karlaf770022018-03-19 13:04:03 -0700122
123 ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), false /* isOwned */);
124}
125
126DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLRenderTarget, reporter, ctxInfo) {
127 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500128 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->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);
131 static constexpr auto kConfig = kRGBA_8888_GrPixelConfig;
Eric Karlaf770022018-03-19 13:04:03 -0700132
Brian Salomonea4ad302019-08-07 13:04:55 -0400133 GrGLRenderTarget::IDs rtIDs;
134 rtIDs.fRTFBOID = 20;
135 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
136 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
137 rtIDs.fMSColorRenderbufferID = 22;
Eric Karlaf770022018-03-19 13:04:03 -0700138
Brian Salomonea4ad302019-08-07 13:04:55 -0400139 sk_sp<GrGLRenderTarget> rt =
140 GrGLRenderTarget::MakeWrapped(gpu, kSize, GrGLFormat::kRGBA8, kConfig, 1, rtIDs, 0);
Eric Karlaf770022018-03-19 13:04:03 -0700141
142 ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), true /* isOwned */);
143}
144
145DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLRenderTarget, reporter, ctxInfo) {
146 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500147 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->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);
150 static constexpr auto kConfig = kRGBA_8888_GrPixelConfig;
Eric Karlaf770022018-03-19 13:04:03 -0700151
Brian Salomonea4ad302019-08-07 13:04:55 -0400152 GrGLRenderTarget::IDs rtIDs;
153 rtIDs.fRTFBOID = 20;
154 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
155 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
156 rtIDs.fMSColorRenderbufferID = 22;
Eric Karlaf770022018-03-19 13:04:03 -0700157
Brian Salomonea4ad302019-08-07 13:04:55 -0400158 sk_sp<GrGLRenderTarget> rt =
159 GrGLRenderTarget::MakeWrapped(gpu, kSize, GrGLFormat::kRGBA8, kConfig, 1, rtIDs, 0);
Eric Karlaf770022018-03-19 13:04:03 -0700160
161 ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), false /* isOwned */);
162}
John Rosascoa9b348f2019-11-08 13:18:15 -0800163#endif // SK_GL