1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2015 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "include/core/SkTraceMemoryDump.h"
9cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h"
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "tests/Test.h"
12cb93a386Sopenharmony_ci
13cb93a386Sopenharmony_ci#include "src/gpu/GrDirectContextPriv.h"
14cb93a386Sopenharmony_ci#include "src/gpu/GrRenderTarget.h"
15cb93a386Sopenharmony_ci#include "src/gpu/GrTexture.h"
16cb93a386Sopenharmony_ci#ifdef SK_GL
17cb93a386Sopenharmony_ci#include "src/gpu/gl/GrGLBuffer.h"
18cb93a386Sopenharmony_ci#include "src/gpu/gl/GrGLDefines.h"
19cb93a386Sopenharmony_ci#include "src/gpu/gl/GrGLGpu.h"
20cb93a386Sopenharmony_ci#include "src/gpu/gl/GrGLTextureRenderTarget.h"
21cb93a386Sopenharmony_ci#endif
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_ci/*
24cb93a386Sopenharmony_ci * Build test for SkTraceMemoryDump.
25cb93a386Sopenharmony_ci */
26cb93a386Sopenharmony_ciclass TestSkTraceMemoryDump : public SkTraceMemoryDump {
27cb93a386Sopenharmony_cipublic:
28cb93a386Sopenharmony_ci    TestSkTraceMemoryDump(bool shouldDumpWrappedObjects)
29cb93a386Sopenharmony_ci            : fShouldDumpWrappedObjects(shouldDumpWrappedObjects) {}
30cb93a386Sopenharmony_ci    ~TestSkTraceMemoryDump() override { }
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_ci    void dumpNumericValue(const char* dumpName, const char* valueName, const char* units,
33cb93a386Sopenharmony_ci                          uint64_t value) override {
34cb93a386Sopenharmony_ci        // Only count "size" dumps, others are just providing metadata.
35cb93a386Sopenharmony_ci        if (SkString("size") == SkString(valueName)) {
36cb93a386Sopenharmony_ci            ++fNumDumpedObjects;
37cb93a386Sopenharmony_ci            fDumpedObjectsSize += value;
38cb93a386Sopenharmony_ci        }
39cb93a386Sopenharmony_ci    }
40cb93a386Sopenharmony_ci    void setMemoryBacking(const char* dumpName, const char* backingType,
41cb93a386Sopenharmony_ci                          const char* backingObjectId) override { }
42cb93a386Sopenharmony_ci    void setDiscardableMemoryBacking(
43cb93a386Sopenharmony_ci        const char* dumpName,
44cb93a386Sopenharmony_ci        const SkDiscardableMemory& discardableMemoryObject) override { }
45cb93a386Sopenharmony_ci    LevelOfDetail getRequestedDetails() const override {
46cb93a386Sopenharmony_ci        return SkTraceMemoryDump::kObjectsBreakdowns_LevelOfDetail;
47cb93a386Sopenharmony_ci    }
48cb93a386Sopenharmony_ci    bool shouldDumpWrappedObjects() const override { return fShouldDumpWrappedObjects; }
49cb93a386Sopenharmony_ci
50cb93a386Sopenharmony_ci    size_t numDumpedObjects() const { return fNumDumpedObjects; }
51cb93a386Sopenharmony_ci    size_t dumpedObjectsSize() const { return fDumpedObjectsSize; }
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_ciprivate:
54cb93a386Sopenharmony_ci    bool fShouldDumpWrappedObjects;
55cb93a386Sopenharmony_ci    size_t fNumDumpedObjects = 0;
56cb93a386Sopenharmony_ci    size_t fDumpedObjectsSize = 0;
57cb93a386Sopenharmony_ci};
58cb93a386Sopenharmony_ci
59cb93a386Sopenharmony_civoid ValidateMemoryDumps(skiatest::Reporter* reporter, GrDirectContext* dContext,
60cb93a386Sopenharmony_ci                         size_t numDumpedObjects, size_t size, bool isOwned) {
61cb93a386Sopenharmony_ci    // Note than one entry in the dumped objects is expected for the text blob cache.
62cb93a386Sopenharmony_ci    TestSkTraceMemoryDump dump_with_wrapped(true /* shouldDumpWrappedObjects */);
63cb93a386Sopenharmony_ci    dContext->dumpMemoryStatistics(&dump_with_wrapped);
64cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, numDumpedObjects == dump_with_wrapped.numDumpedObjects());
65cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, size == dump_with_wrapped.dumpedObjectsSize());
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci    TestSkTraceMemoryDump dump_no_wrapped(false /* shouldDumpWrappedObjects */);
68cb93a386Sopenharmony_ci    dContext->dumpMemoryStatistics(&dump_no_wrapped);
69cb93a386Sopenharmony_ci    if (isOwned) {
70cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, numDumpedObjects == dump_no_wrapped.numDumpedObjects());
71cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, size == dump_no_wrapped.dumpedObjectsSize());
72cb93a386Sopenharmony_ci    } else {
73cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, 1 == dump_no_wrapped.numDumpedObjects());
74cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, 0 == dump_no_wrapped.dumpedObjectsSize());
75cb93a386Sopenharmony_ci    }
76cb93a386Sopenharmony_ci}
77cb93a386Sopenharmony_ci
78cb93a386Sopenharmony_ci#ifdef SK_GL
79cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLBuffer, reporter, ctxInfo) {
80cb93a386Sopenharmony_ci    auto dContext = ctxInfo.directContext();
81cb93a386Sopenharmony_ci    GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
82cb93a386Sopenharmony_ci    const size_t kMemorySize = 1024;
83cb93a386Sopenharmony_ci    sk_sp<GrGLBuffer> buffer =
84cb93a386Sopenharmony_ci            GrGLBuffer::Make(gpu, kMemorySize, GrGpuBufferType::kVertex, kDynamic_GrAccessPattern);
85cb93a386Sopenharmony_ci
86cb93a386Sopenharmony_ci    ValidateMemoryDumps(reporter, dContext, 2, kMemorySize, true /* isOwned */);
87cb93a386Sopenharmony_ci}
88cb93a386Sopenharmony_ci
89cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLTexture, reporter, ctxInfo) {
90cb93a386Sopenharmony_ci    auto dContext = ctxInfo.directContext();
91cb93a386Sopenharmony_ci    GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
92cb93a386Sopenharmony_ci
93cb93a386Sopenharmony_ci    GrGLTexture::Desc desc;
94cb93a386Sopenharmony_ci    desc.fTarget = GR_GL_TEXTURE_2D;
95cb93a386Sopenharmony_ci    desc.fID = 7;  // Arbitrary, we don't actually use the texture.
96cb93a386Sopenharmony_ci    desc.fFormat = GrGLFormat::kRGBA8;
97cb93a386Sopenharmony_ci    desc.fOwnership = GrBackendObjectOwnership::kOwned;
98cb93a386Sopenharmony_ci    desc.fSize = SkISize::Make(64, 64);
99cb93a386Sopenharmony_ci
100cb93a386Sopenharmony_ci    auto texture =
101cb93a386Sopenharmony_ci            sk_make_sp<GrGLTexture>(gpu, SkBudgeted::kNo, desc, GrMipmapStatus::kNotAllocated);
102cb93a386Sopenharmony_ci
103cb93a386Sopenharmony_ci    ValidateMemoryDumps(reporter, dContext, 2, texture->gpuMemorySize(), true /* isOwned */);
104cb93a386Sopenharmony_ci}
105cb93a386Sopenharmony_ci
106cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLTexture, reporter, ctxInfo) {
107cb93a386Sopenharmony_ci    auto dContext = ctxInfo.directContext();
108cb93a386Sopenharmony_ci    GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
109cb93a386Sopenharmony_ci
110cb93a386Sopenharmony_ci    GrGLTexture::Desc desc;
111cb93a386Sopenharmony_ci    desc.fTarget = GR_GL_TEXTURE_2D;
112cb93a386Sopenharmony_ci    desc.fID = 7;  // Arbitrary, we don't actually use the texture.
113cb93a386Sopenharmony_ci    desc.fFormat = GrGLFormat::kRGBA8;
114cb93a386Sopenharmony_ci    desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
115cb93a386Sopenharmony_ci    desc.fSize = SkISize::Make(64, 64);
116cb93a386Sopenharmony_ci
117cb93a386Sopenharmony_ci    auto params = sk_make_sp<GrGLTextureParameters>();
118cb93a386Sopenharmony_ci
119cb93a386Sopenharmony_ci    auto texture =
120cb93a386Sopenharmony_ci            GrGLTexture::MakeWrapped(gpu, GrMipmapStatus::kNotAllocated, desc, std::move(params),
121cb93a386Sopenharmony_ci                                     GrWrapCacheable::kNo, kRead_GrIOType);
122cb93a386Sopenharmony_ci
123cb93a386Sopenharmony_ci    ValidateMemoryDumps(reporter, dContext, 2, texture->gpuMemorySize(), false /* isOwned */);
124cb93a386Sopenharmony_ci}
125cb93a386Sopenharmony_ci
126cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLRenderTarget, reporter, ctxInfo) {
127cb93a386Sopenharmony_ci    auto dContext = ctxInfo.directContext();
128cb93a386Sopenharmony_ci    GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
129cb93a386Sopenharmony_ci
130cb93a386Sopenharmony_ci    static constexpr auto kSize = SkISize::Make(64, 64);
131cb93a386Sopenharmony_ci
132cb93a386Sopenharmony_ci    GrGLRenderTarget::IDs rtIDs;
133cb93a386Sopenharmony_ci    rtIDs.fMultisampleFBOID = 0;
134cb93a386Sopenharmony_ci    rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
135cb93a386Sopenharmony_ci    rtIDs.fSingleSampleFBOID = 20;
136cb93a386Sopenharmony_ci    rtIDs.fMSColorRenderbufferID = 0;
137cb93a386Sopenharmony_ci    rtIDs.fTotalMemorySamplesPerPixel = 1;
138cb93a386Sopenharmony_ci
139cb93a386Sopenharmony_ci    sk_sp<GrGLRenderTarget> rt =
140cb93a386Sopenharmony_ci            GrGLRenderTarget::MakeWrapped(gpu, kSize, GrGLFormat::kRGBA8, 1, rtIDs, 0);
141cb93a386Sopenharmony_ci
142cb93a386Sopenharmony_ci    ValidateMemoryDumps(reporter, dContext, 2, rt->gpuMemorySize(), true /* isOwned */);
143cb93a386Sopenharmony_ci}
144cb93a386Sopenharmony_ci
145cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLRenderTarget, reporter, ctxInfo) {
146cb93a386Sopenharmony_ci    auto dContext = ctxInfo.directContext();
147cb93a386Sopenharmony_ci    GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
148cb93a386Sopenharmony_ci
149cb93a386Sopenharmony_ci    static constexpr auto kSize = SkISize::Make(64, 64);
150cb93a386Sopenharmony_ci
151cb93a386Sopenharmony_ci    GrGLRenderTarget::IDs rtIDs;
152cb93a386Sopenharmony_ci    rtIDs.fMultisampleFBOID = 12;
153cb93a386Sopenharmony_ci    rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
154cb93a386Sopenharmony_ci    rtIDs.fSingleSampleFBOID = 0;
155cb93a386Sopenharmony_ci    rtIDs.fMSColorRenderbufferID = 0;
156cb93a386Sopenharmony_ci    rtIDs.fTotalMemorySamplesPerPixel = 4;
157cb93a386Sopenharmony_ci
158cb93a386Sopenharmony_ci    sk_sp<GrGLRenderTarget> rt =
159cb93a386Sopenharmony_ci            GrGLRenderTarget::MakeWrapped(gpu, kSize, GrGLFormat::kRGBA8, 4, rtIDs, 0);
160cb93a386Sopenharmony_ci
161cb93a386Sopenharmony_ci    ValidateMemoryDumps(reporter, dContext, 2, rt->gpuMemorySize(), false /* isOwned */);
162cb93a386Sopenharmony_ci}
163cb93a386Sopenharmony_ci
164cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLTextureRenderTarget, reporter,
165cb93a386Sopenharmony_ci                                      ctxInfo) {
166cb93a386Sopenharmony_ci    auto dContext = ctxInfo.directContext();
167cb93a386Sopenharmony_ci    GrGLGpu* gpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
168cb93a386Sopenharmony_ci
169cb93a386Sopenharmony_ci    static constexpr auto kSize = SkISize::Make(64, 64);
170cb93a386Sopenharmony_ci
171cb93a386Sopenharmony_ci    GrGLTexture::Desc texDesc;
172cb93a386Sopenharmony_ci    texDesc.fSize = kSize;
173cb93a386Sopenharmony_ci    texDesc.fTarget = GR_GL_TEXTURE_2D;
174cb93a386Sopenharmony_ci    texDesc.fID = 17;
175cb93a386Sopenharmony_ci    texDesc.fFormat = GrGLFormat::kRGBA8;
176cb93a386Sopenharmony_ci    texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
177cb93a386Sopenharmony_ci
178cb93a386Sopenharmony_ci    GrGLRenderTarget::IDs rtIDs;
179cb93a386Sopenharmony_ci    rtIDs.fMultisampleFBOID = 12;
180cb93a386Sopenharmony_ci    rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
181cb93a386Sopenharmony_ci    rtIDs.fSingleSampleFBOID = 20;
182cb93a386Sopenharmony_ci    rtIDs.fMSColorRenderbufferID = 22;
183cb93a386Sopenharmony_ci    rtIDs.fTotalMemorySamplesPerPixel = 9;
184cb93a386Sopenharmony_ci
185cb93a386Sopenharmony_ci    auto texRT = sk_make_sp<GrGLTextureRenderTarget>(gpu, SkBudgeted::kYes, 8, texDesc, rtIDs,
186cb93a386Sopenharmony_ci                                                     GrMipmapStatus::kNotAllocated);
187cb93a386Sopenharmony_ci
188cb93a386Sopenharmony_ci    ValidateMemoryDumps(reporter, dContext, 2, texRT->gpuMemorySize(), true /* isOwned */);
189cb93a386Sopenharmony_ci}
190cb93a386Sopenharmony_ci
191cb93a386Sopenharmony_ci#endif  // SK_GL
192