Home | History | Annotate | Download | only in tests
      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 
     12 // These tests are currently GPU-speicifc.
     13 #if SK_SUPPORT_GPU
     14 #include "GrContextPriv.h"
     15 #include "GrRenderTarget.h"
     16 #include "GrTexture.h"
     17 #include "gl/GrGLBuffer.h"
     18 #include "gl/GrGLDefines.h"
     19 #include "gl/GrGLGpu.h"
     20 
     21 /*
     22  * Build test for SkTraceMemoryDump.
     23  */
     24 class TestSkTraceMemoryDump : public SkTraceMemoryDump {
     25 public:
     26     TestSkTraceMemoryDump(bool shouldDumpWrappedObjects)
     27             : fShouldDumpWrappedObjects(shouldDumpWrappedObjects) {}
     28     ~TestSkTraceMemoryDump() override { }
     29 
     30     void dumpNumericValue(const char* dumpName, const char* valueName, const char* units,
     31                           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     }
     38     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 { }
     43     LevelOfDetail getRequestedDetails() const override {
     44         return SkTraceMemoryDump::kObjectsBreakdowns_LevelOfDetail;
     45     }
     46     bool shouldDumpWrappedObjects() const override { return fShouldDumpWrappedObjects; }
     47 
     48     size_t numDumpedObjects() const { return fNumDumpedObjects; }
     49     size_t dumpedObjectsSize() const { return fDumpedObjectsSize; }
     50 
     51 private:
     52     bool fShouldDumpWrappedObjects;
     53     size_t fNumDumpedObjects = 0;
     54     size_t fDumpedObjectsSize = 0;
     55 };
     56 
     57 void ValidateMemoryDumps(skiatest::Reporter* reporter, GrContext* context, size_t size,
     58                          bool isOwned) {
     59     TestSkTraceMemoryDump dump_with_wrapped(true /* shouldDumpWrappedObjects */);
     60     context->dumpMemoryStatistics(&dump_with_wrapped);
     61     REPORTER_ASSERT(reporter, 1 == dump_with_wrapped.numDumpedObjects());
     62     REPORTER_ASSERT(reporter, size == dump_with_wrapped.dumpedObjectsSize());
     63 
     64     TestSkTraceMemoryDump dump_no_wrapped(false /* shouldDumpWrappedObjects */);
     65     context->dumpMemoryStatistics(&dump_no_wrapped);
     66     if (isOwned) {
     67         REPORTER_ASSERT(reporter, 1 == dump_no_wrapped.numDumpedObjects());
     68         REPORTER_ASSERT(reporter, size == dump_no_wrapped.dumpedObjectsSize());
     69     } else {
     70         REPORTER_ASSERT(reporter, 0 == dump_no_wrapped.numDumpedObjects());
     71         REPORTER_ASSERT(reporter, 0 == dump_no_wrapped.dumpedObjectsSize());
     72     }
     73 }
     74 
     75 DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLBuffer, reporter, ctxInfo) {
     76     GrContext* context = ctxInfo.grContext();
     77     GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu());
     78     const size_t kMemorySize = 1024;
     79     sk_sp<GrGLBuffer> buffer(
     80             GrGLBuffer::Create(gpu, kMemorySize, kVertex_GrBufferType, kDynamic_GrAccessPattern));
     81 
     82     ValidateMemoryDumps(reporter, context, kMemorySize, true /* isOwned */);
     83 }
     84 
     85 DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLTexture, reporter, ctxInfo) {
     86     GrContext* context = ctxInfo.grContext();
     87     GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu());
     88 
     89     GrSurfaceDesc desc;
     90     desc.fFlags = kNone_GrSurfaceFlags;
     91     desc.fWidth = 64;
     92     desc.fHeight = 64;
     93     desc.fConfig = kRGBA_8888_GrPixelConfig;
     94     desc.fSampleCnt = 1;
     95 
     96     GrGLTextureInfo glInfo;
     97     glInfo.fTarget = GR_GL_TEXTURE_2D;
     98     glInfo.fID = 7;  // Arbitrary, we don't actually use the texture.
     99     glInfo.fFormat = GR_GL_RGBA8;
    100 
    101     GrGLTexture::IDDesc idDesc;
    102     idDesc.fInfo = glInfo;
    103     idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
    104 
    105     auto texture = sk_make_sp<GrGLTexture>(gpu, SkBudgeted::kNo, desc, idDesc,
    106                                            GrMipMapsStatus::kNotAllocated);
    107 
    108     ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), true /* isOwned */);
    109 }
    110 
    111 DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLTexture, reporter, ctxInfo) {
    112     GrContext* context = ctxInfo.grContext();
    113     GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu());
    114 
    115     GrSurfaceDesc desc;
    116     desc.fFlags = kNone_GrSurfaceFlags;
    117     desc.fWidth = 64;
    118     desc.fHeight = 64;
    119     desc.fConfig = kRGBA_8888_GrPixelConfig;
    120     desc.fSampleCnt = 1;
    121 
    122     GrGLTextureInfo glInfo;
    123     glInfo.fTarget = GR_GL_TEXTURE_2D;
    124     glInfo.fID = 7;  // Arbitrary, we don't actually use the texture.
    125     glInfo.fFormat = GR_GL_RGBA8;
    126 
    127     GrGLTexture::IDDesc idDesc;
    128     idDesc.fInfo = glInfo;
    129     idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
    130 
    131     auto texture = GrGLTexture::MakeWrapped(gpu, desc, GrMipMapsStatus::kNotAllocated, idDesc);
    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 
    153     sk_sp<GrGLRenderTarget> rt = GrGLRenderTarget::MakeWrapped(gpu, sd, iddesc, 0);
    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 
    175     sk_sp<GrGLRenderTarget> rt = GrGLRenderTarget::MakeWrapped(gpu, sd, iddesc, 0);
    176 
    177     ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), false /* isOwned */);
    178 }
    179 
    180 #endif
    181