TraceMemoryDumpTest.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include "include/core/SkTraceMemoryDump.h"
  8. #include "tests/Test.h"
  9. #include "include/gpu/GrRenderTarget.h"
  10. #include "include/gpu/GrTexture.h"
  11. #include "src/gpu/GrContextPriv.h"
  12. #include "src/gpu/gl/GrGLBuffer.h"
  13. #include "src/gpu/gl/GrGLDefines.h"
  14. #include "src/gpu/gl/GrGLGpu.h"
  15. /*
  16. * Build test for SkTraceMemoryDump.
  17. */
  18. class TestSkTraceMemoryDump : public SkTraceMemoryDump {
  19. public:
  20. TestSkTraceMemoryDump(bool shouldDumpWrappedObjects)
  21. : fShouldDumpWrappedObjects(shouldDumpWrappedObjects) {}
  22. ~TestSkTraceMemoryDump() override { }
  23. void dumpNumericValue(const char* dumpName, const char* valueName, const char* units,
  24. uint64_t value) override {
  25. // Only count "size" dumps, others are just providing metadata.
  26. if (SkString("size") == SkString(valueName)) {
  27. ++fNumDumpedObjects;
  28. fDumpedObjectsSize += value;
  29. }
  30. }
  31. void setMemoryBacking(const char* dumpName, const char* backingType,
  32. const char* backingObjectId) override { }
  33. void setDiscardableMemoryBacking(
  34. const char* dumpName,
  35. const SkDiscardableMemory& discardableMemoryObject) override { }
  36. LevelOfDetail getRequestedDetails() const override {
  37. return SkTraceMemoryDump::kObjectsBreakdowns_LevelOfDetail;
  38. }
  39. bool shouldDumpWrappedObjects() const override { return fShouldDumpWrappedObjects; }
  40. size_t numDumpedObjects() const { return fNumDumpedObjects; }
  41. size_t dumpedObjectsSize() const { return fDumpedObjectsSize; }
  42. private:
  43. bool fShouldDumpWrappedObjects;
  44. size_t fNumDumpedObjects = 0;
  45. size_t fDumpedObjectsSize = 0;
  46. };
  47. void ValidateMemoryDumps(skiatest::Reporter* reporter, GrContext* context, size_t size,
  48. bool isOwned) {
  49. // Note than one entry in the dumped objects is expected for the text blob cache.
  50. TestSkTraceMemoryDump dump_with_wrapped(true /* shouldDumpWrappedObjects */);
  51. context->dumpMemoryStatistics(&dump_with_wrapped);
  52. REPORTER_ASSERT(reporter, 2 == dump_with_wrapped.numDumpedObjects());
  53. REPORTER_ASSERT(reporter, size == dump_with_wrapped.dumpedObjectsSize());
  54. TestSkTraceMemoryDump dump_no_wrapped(false /* shouldDumpWrappedObjects */);
  55. context->dumpMemoryStatistics(&dump_no_wrapped);
  56. if (isOwned) {
  57. REPORTER_ASSERT(reporter, 2 == dump_no_wrapped.numDumpedObjects());
  58. REPORTER_ASSERT(reporter, size == dump_no_wrapped.dumpedObjectsSize());
  59. } else {
  60. REPORTER_ASSERT(reporter, 1 == dump_no_wrapped.numDumpedObjects());
  61. REPORTER_ASSERT(reporter, 0 == dump_no_wrapped.dumpedObjectsSize());
  62. }
  63. }
  64. DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLBuffer, reporter, ctxInfo) {
  65. GrContext* context = ctxInfo.grContext();
  66. GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
  67. const size_t kMemorySize = 1024;
  68. sk_sp<GrGLBuffer> buffer =
  69. GrGLBuffer::Make(gpu, kMemorySize, GrGpuBufferType::kVertex, kDynamic_GrAccessPattern);
  70. ValidateMemoryDumps(reporter, context, kMemorySize, true /* isOwned */);
  71. }
  72. DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLTexture, reporter, ctxInfo) {
  73. GrContext* context = ctxInfo.grContext();
  74. GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
  75. GrSurfaceDesc desc;
  76. desc.fWidth = 64;
  77. desc.fHeight = 64;
  78. desc.fConfig = kRGBA_8888_GrPixelConfig;
  79. GrGLTextureInfo glInfo;
  80. glInfo.fTarget = GR_GL_TEXTURE_2D;
  81. glInfo.fID = 7; // Arbitrary, we don't actually use the texture.
  82. glInfo.fFormat = GR_GL_RGBA8;
  83. GrGLTexture::IDDesc idDesc;
  84. idDesc.fInfo = glInfo;
  85. idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
  86. auto texture = sk_make_sp<GrGLTexture>(gpu, SkBudgeted::kNo, desc, idDesc,
  87. GrMipMapsStatus::kNotAllocated);
  88. ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), true /* isOwned */);
  89. }
  90. DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLTexture, reporter, ctxInfo) {
  91. GrContext* context = ctxInfo.grContext();
  92. GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
  93. GrSurfaceDesc desc;
  94. desc.fWidth = 64;
  95. desc.fHeight = 64;
  96. desc.fConfig = kRGBA_8888_GrPixelConfig;
  97. GrGLTextureInfo glInfo;
  98. glInfo.fTarget = GR_GL_TEXTURE_2D;
  99. glInfo.fID = 7; // Arbitrary, we don't actually use the texture.
  100. glInfo.fFormat = GR_GL_RGBA8;
  101. GrGLTexture::IDDesc idDesc;
  102. idDesc.fInfo = glInfo;
  103. idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
  104. auto params = sk_make_sp<GrGLTextureParameters>();
  105. auto texture =
  106. GrGLTexture::MakeWrapped(gpu, desc, GrMipMapsStatus::kNotAllocated, idDesc,
  107. std::move(params), GrWrapCacheable::kNo, kRead_GrIOType);
  108. ValidateMemoryDumps(reporter, context, texture->gpuMemorySize(), false /* isOwned */);
  109. }
  110. DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_ownedGLRenderTarget, reporter, ctxInfo) {
  111. GrContext* context = ctxInfo.grContext();
  112. GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
  113. GrSurfaceDesc sd;
  114. sd.fWidth = 64;
  115. sd.fHeight = 64;
  116. sd.fConfig = kRGBA_8888_GrPixelConfig;
  117. GrGLRenderTarget::IDDesc iddesc;
  118. iddesc.fRTFBOID = 20;
  119. iddesc.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
  120. iddesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
  121. iddesc.fMSColorRenderbufferID = 22;
  122. sk_sp<GrGLRenderTarget> rt = GrGLRenderTarget::MakeWrapped(gpu, sd, 1, GR_GL_RGBA8, iddesc, 0);
  123. ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), true /* isOwned */);
  124. }
  125. DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkTraceMemoryDump_unownedGLRenderTarget, reporter, ctxInfo) {
  126. GrContext* context = ctxInfo.grContext();
  127. GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
  128. GrSurfaceDesc sd;
  129. sd.fWidth = 64;
  130. sd.fHeight = 64;
  131. sd.fConfig = kRGBA_8888_GrPixelConfig;
  132. GrGLRenderTarget::IDDesc iddesc;
  133. iddesc.fRTFBOID = 20;
  134. iddesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
  135. iddesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
  136. iddesc.fMSColorRenderbufferID = 22;
  137. sk_sp<GrGLRenderTarget> rt = GrGLRenderTarget::MakeWrapped(gpu, sd, 1, GR_GL_RGBA8, iddesc, 0);
  138. ValidateMemoryDumps(reporter, context, rt->gpuMemorySize(), false /* isOwned */);
  139. }