primiano | 9a5bd7e | 2015-08-20 08:00:32 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #include "SkTraceMemoryDump.h" |
| 9 | |
| 10 | #include "Test.h" |
| 11 | |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
| 13 | #include "GrRenderTarget.h" |
| 14 | #include "GrTexture.h" |
| 15 | #include "gl/GrGLBuffer.h" |
| 16 | #include "gl/GrGLDefines.h" |
| 17 | #include "gl/GrGLGpu.h" |
| 18 | |
primiano | 9a5bd7e | 2015-08-20 08:00:32 -0700 | [diff] [blame] | 19 | /* |
| 20 | * Build test for SkTraceMemoryDump. |
| 21 | */ |
| 22 | class TestSkTraceMemoryDump : public SkTraceMemoryDump { |
| 23 | public: |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 24 | TestSkTraceMemoryDump(bool shouldDumpWrappedObjects) |
| 25 | : fShouldDumpWrappedObjects(shouldDumpWrappedObjects) {} |
primiano | 9a5bd7e | 2015-08-20 08:00:32 -0700 | [diff] [blame] | 26 | ~TestSkTraceMemoryDump() override { } |
| 27 | |
| 28 | void dumpNumericValue(const char* dumpName, const char* valueName, const char* units, |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 29 | 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 | } |
primiano | 9a5bd7e | 2015-08-20 08:00:32 -0700 | [diff] [blame] | 36 | 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 { } |
ssid | f0c9865 | 2015-09-30 04:31:23 -0700 | [diff] [blame] | 41 | LevelOfDetail getRequestedDetails() const override { |
| 42 | return SkTraceMemoryDump::kObjectsBreakdowns_LevelOfDetail; |
| 43 | } |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 44 | bool shouldDumpWrappedObjects() const override { return fShouldDumpWrappedObjects; } |
| 45 | |
| 46 | size_t numDumpedObjects() const { return fNumDumpedObjects; } |
| 47 | size_t dumpedObjectsSize() const { return fDumpedObjectsSize; } |
| 48 | |
| 49 | private: |
| 50 | bool fShouldDumpWrappedObjects; |
| 51 | size_t fNumDumpedObjects = 0; |
| 52 | size_t fDumpedObjectsSize = 0; |
primiano | 9a5bd7e | 2015-08-20 08:00:32 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 55 | void ValidateMemoryDumps(skiatest::Reporter* reporter, GrContext* context, size_t size, |
| 56 | bool isOwned) { |
Khushal | 71652e2 | 2018-10-29 13:05:36 -0700 | [diff] [blame] | 57 | // Note than one entry in the dumped objects is expected for the text blob cache. |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 58 | TestSkTraceMemoryDump dump_with_wrapped(true /* shouldDumpWrappedObjects */); |
| 59 | context->dumpMemoryStatistics(&dump_with_wrapped); |
Khushal | 71652e2 | 2018-10-29 13:05:36 -0700 | [diff] [blame] | 60 | REPORTER_ASSERT(reporter, 2 == dump_with_wrapped.numDumpedObjects()); |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 61 | 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) { |
Khushal | 71652e2 | 2018-10-29 13:05:36 -0700 | [diff] [blame] | 66 | REPORTER_ASSERT(reporter, 2 == dump_no_wrapped.numDumpedObjects()); |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 67 | REPORTER_ASSERT(reporter, size == dump_no_wrapped.dumpedObjectsSize()); |
| 68 | } else { |
Khushal | 71652e2 | 2018-10-29 13:05:36 -0700 | [diff] [blame] | 69 | REPORTER_ASSERT(reporter, 1 == dump_no_wrapped.numDumpedObjects()); |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 70 | REPORTER_ASSERT(reporter, 0 == dump_no_wrapped.dumpedObjectsSize()); |
ssid | f0c9865 | 2015-09-30 04:31:23 -0700 | [diff] [blame] | 71 | } |
primiano | 9a5bd7e | 2015-08-20 08:00:32 -0700 | [diff] [blame] | 72 | } |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 73 | |
| 74 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLBuffer, reporter, ctxInfo) { |
| 75 | GrContext* context = ctxInfo.grContext(); |
| 76 | GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu()); |
| 77 | const size_t kMemorySize = 1024; |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame^] | 78 | sk_sp<GrGLBuffer> buffer = |
| 79 | GrGLBuffer::Make(gpu, kMemorySize, kVertex_GrBufferType, kDynamic_GrAccessPattern); |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 80 | |
| 81 | ValidateMemoryDumps(reporter, context, kMemorySize, true /* isOwned */); |
| 82 | } |
| 83 | |
| 84 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLTexture, reporter, ctxInfo) { |
| 85 | GrContext* context = ctxInfo.grContext(); |
| 86 | GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu()); |
| 87 | |
| 88 | GrSurfaceDesc desc; |
| 89 | desc.fFlags = kNone_GrSurfaceFlags; |
| 90 | desc.fWidth = 64; |
| 91 | desc.fHeight = 64; |
| 92 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 93 | desc.fSampleCnt = 1; |
| 94 | |
| 95 | GrGLTextureInfo glInfo; |
| 96 | glInfo.fTarget = GR_GL_TEXTURE_2D; |
| 97 | glInfo.fID = 7; // Arbitrary, we don't actually use the texture. |
| 98 | glInfo.fFormat = GR_GL_RGBA8; |
| 99 | |
| 100 | GrGLTexture::IDDesc idDesc; |
| 101 | idDesc.fInfo = glInfo; |
| 102 | idDesc.fOwnership = GrBackendObjectOwnership::kOwned; |
| 103 | |
| 104 | auto texture = sk_make_sp<GrGLTexture>(gpu, SkBudgeted::kNo, desc, idDesc, |
| 105 | GrMipMapsStatus::kNotAllocated); |
| 106 | |
| 107 | ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), true /* isOwned */); |
| 108 | } |
| 109 | |
| 110 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLTexture, reporter, ctxInfo) { |
| 111 | GrContext* context = ctxInfo.grContext(); |
| 112 | GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu()); |
| 113 | |
| 114 | GrSurfaceDesc desc; |
| 115 | desc.fFlags = kNone_GrSurfaceFlags; |
| 116 | desc.fWidth = 64; |
| 117 | desc.fHeight = 64; |
| 118 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 119 | desc.fSampleCnt = 1; |
| 120 | |
| 121 | GrGLTextureInfo glInfo; |
| 122 | glInfo.fTarget = GR_GL_TEXTURE_2D; |
| 123 | glInfo.fID = 7; // Arbitrary, we don't actually use the texture. |
| 124 | glInfo.fFormat = GR_GL_RGBA8; |
| 125 | |
| 126 | GrGLTexture::IDDesc idDesc; |
| 127 | idDesc.fInfo = glInfo; |
| 128 | idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed; |
| 129 | |
Greg Daniel | 2268ad2 | 2018-11-15 09:27:38 -0500 | [diff] [blame] | 130 | auto texture = GrGLTexture::MakeWrapped(gpu, desc, GrMipMapsStatus::kNotAllocated, idDesc, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 131 | GrWrapCacheable::kNo, kRead_GrIOType); |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 132 | |
| 133 | ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), false /* isOwned */); |
| 134 | } |
| 135 | |
| 136 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLRenderTarget, reporter, ctxInfo) { |
| 137 | GrContext* context = ctxInfo.grContext(); |
| 138 | GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu()); |
| 139 | |
| 140 | GrSurfaceDesc sd; |
| 141 | sd.fFlags = kRenderTarget_GrSurfaceFlag; |
| 142 | sd.fWidth = 64; |
| 143 | sd.fHeight = 64; |
| 144 | sd.fConfig = kRGBA_8888_GrPixelConfig; |
| 145 | |
| 146 | GrGLRenderTarget::IDDesc iddesc; |
| 147 | iddesc.fRTFBOID = 20; |
| 148 | iddesc.fRTFBOOwnership = GrBackendObjectOwnership::kOwned; |
| 149 | iddesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID; |
| 150 | iddesc.fMSColorRenderbufferID = 22; |
| 151 | iddesc.fIsMixedSampled = false; |
| 152 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 153 | sk_sp<GrGLRenderTarget> rt = GrGLRenderTarget::MakeWrapped(gpu, sd, GR_GL_RGBA8, iddesc, 0); |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 154 | |
| 155 | ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), true /* isOwned */); |
| 156 | } |
| 157 | |
| 158 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLRenderTarget, reporter, ctxInfo) { |
| 159 | GrContext* context = ctxInfo.grContext(); |
| 160 | GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu()); |
| 161 | |
| 162 | GrSurfaceDesc sd; |
| 163 | sd.fFlags = kRenderTarget_GrSurfaceFlag; |
| 164 | sd.fWidth = 64; |
| 165 | sd.fHeight = 64; |
| 166 | sd.fConfig = kRGBA_8888_GrPixelConfig; |
| 167 | |
| 168 | GrGLRenderTarget::IDDesc iddesc; |
| 169 | iddesc.fRTFBOID = 20; |
| 170 | iddesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed; |
| 171 | iddesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID; |
| 172 | iddesc.fMSColorRenderbufferID = 22; |
| 173 | iddesc.fIsMixedSampled = false; |
| 174 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 175 | sk_sp<GrGLRenderTarget> rt = GrGLRenderTarget::MakeWrapped(gpu, sd, GR_GL_RGBA8, iddesc, 0); |
Eric Karl | af77002 | 2018-03-19 13:04:03 -0700 | [diff] [blame] | 176 | |
| 177 | ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), false /* isOwned */); |
| 178 | } |