gl_set_aggressively_free_resources_unittest.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <GLES2/gl2.h>
  5. #include <GLES2/gl2ext.h>
  6. #include <GLES2/gl2extchromium.h>
  7. #include <stdint.h>
  8. #include "gpu/command_buffer/client/gles2_implementation.h"
  9. #include "gpu/command_buffer/tests/gl_manager.h"
  10. #include "gpu/command_buffer/tests/gl_test_utils.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace gpu {
  13. class SetAggressivelyFreeResourcesTest : public testing::Test {
  14. protected:
  15. void SetUp() override {
  16. GLManager::Options options;
  17. options.context_type = CONTEXT_TYPE_OPENGLES3;
  18. gl_.Initialize(options);
  19. if (!gl_.IsInitialized()) {
  20. options.context_type = CONTEXT_TYPE_OPENGLES2;
  21. gl_.Initialize(options);
  22. }
  23. context_type_ = options.context_type;
  24. // Make sure we start with a clean slate.
  25. gl_.gles2_implementation()->FreeEverything();
  26. EXPECT_EQ(0u, gl_.GetSharedMemoryBytesAllocated());
  27. }
  28. void TearDown() override { gl_.Destroy(); }
  29. GLManager gl_;
  30. ContextType context_type_ = CONTEXT_TYPE_OPENGLES3;
  31. };
  32. // Tests that SetAggressivelyFreeResources releases command buffer memory.
  33. TEST_F(SetAggressivelyFreeResourcesTest, FreeAllMemory_CommandBuffer) {
  34. GLuint texture = 0;
  35. EXPECT_EQ(0u, gl_.GetSharedMemoryBytesAllocated());
  36. // Basic command that just uses command buffer.
  37. glGenTextures(1, &texture);
  38. EXPECT_LT(0u, gl_.GetSharedMemoryBytesAllocated());
  39. gl_.gles2_implementation()->SetAggressivelyFreeResources(true);
  40. EXPECT_EQ(0u, gl_.GetSharedMemoryBytesAllocated());
  41. }
  42. // Tests that SetAggressivelyFreeResources releases transfer buffer memory.
  43. TEST_F(SetAggressivelyFreeResourcesTest, FreeAllMemory_TransferBuffer) {
  44. GLuint texture = 0;
  45. glGenTextures(1, &texture);
  46. glBindTexture(GL_TEXTURE_2D, texture);
  47. const char kPixels[4 * 4 * 4] = {0};
  48. // Allocates transfer buffer space for the pixels.
  49. size_t old_size = gl_.GetSharedMemoryBytesAllocated();
  50. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  51. kPixels);
  52. EXPECT_LT(old_size, gl_.GetSharedMemoryBytesAllocated());
  53. gl_.gles2_implementation()->SetAggressivelyFreeResources(true);
  54. EXPECT_EQ(0u, gl_.GetSharedMemoryBytesAllocated());
  55. }
  56. // Tests that SetAggressivelyFreeResources releases mapped memory.
  57. TEST_F(SetAggressivelyFreeResourcesTest, FreeAllMemory_MappedMemory) {
  58. GLuint buffer = 0;
  59. glGenBuffers(1, &buffer);
  60. glBindBuffer(GL_ARRAY_BUFFER, buffer);
  61. const char kData[256] = {0};
  62. glBufferData(GL_ARRAY_BUFFER, sizeof(kData), kData, GL_STATIC_DRAW);
  63. size_t old_size = gl_.GetSharedMemoryBytesAllocated();
  64. // Allocates mapped memory for data.
  65. void* data = glMapBufferSubDataCHROMIUM(GL_ARRAY_BUFFER, 0, sizeof(kData),
  66. GL_WRITE_ONLY);
  67. ASSERT_TRUE(data);
  68. memcpy(data, kData, sizeof(kData));
  69. glUnmapBufferSubDataCHROMIUM(data);
  70. EXPECT_LT(old_size, gl_.GetSharedMemoryBytesAllocated());
  71. gl_.gles2_implementation()->SetAggressivelyFreeResources(true);
  72. EXPECT_EQ(0u, gl_.GetSharedMemoryBytesAllocated());
  73. }
  74. // Tests that SetAggressivelyFreeResources releases QuerySyncs.
  75. TEST_F(SetAggressivelyFreeResourcesTest, FreeAllMemory_Queries) {
  76. GLuint query = 0;
  77. glGenQueriesEXT(1, &query);
  78. size_t old_size = gl_.GetSharedMemoryBytesAllocated();
  79. // Allocates a QuerySync.
  80. glBeginQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM, query);
  81. glEndQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM);
  82. glDeleteQueriesEXT(1, &query);
  83. EXPECT_LT(old_size, gl_.GetSharedMemoryBytesAllocated());
  84. gl_.gles2_implementation()->SetAggressivelyFreeResources(true);
  85. EXPECT_EQ(0u, gl_.GetSharedMemoryBytesAllocated());
  86. }
  87. // Tests that SetAggressivelyFreeResources releases all types of shared memory.
  88. TEST_F(SetAggressivelyFreeResourcesTest, FreeAllMemory) {
  89. GLuint query = 0;
  90. glGenQueriesEXT(1, &query);
  91. // Allocates a QuerySync.
  92. glBeginQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM, query);
  93. GLuint texture = 0;
  94. glGenTextures(1, &texture);
  95. glBindTexture(GL_TEXTURE_2D, texture);
  96. const char kPixels[4 * 4 * 4] = {0};
  97. // Allocates transfer buffer space for the pixels.
  98. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  99. kPixels);
  100. GLuint buffer = 0;
  101. glGenBuffers(1, &buffer);
  102. glBindBuffer(GL_ARRAY_BUFFER, buffer);
  103. const char kData[256] = {0};
  104. // Allocates transfer buffer space for kData.
  105. glBufferData(GL_ARRAY_BUFFER, sizeof(kData), kData, GL_STATIC_DRAW);
  106. // Allocates mapped memory for data.
  107. void* data = glMapBufferSubDataCHROMIUM(GL_ARRAY_BUFFER, 0, sizeof(kData),
  108. GL_WRITE_ONLY);
  109. ASSERT_TRUE(data);
  110. memcpy(data, kData, sizeof(kData));
  111. glUnmapBufferSubDataCHROMIUM(data);
  112. glEndQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM);
  113. glDeleteQueriesEXT(1, &query);
  114. EXPECT_LT(0u, gl_.GetSharedMemoryBytesAllocated());
  115. gl_.gles2_implementation()->SetAggressivelyFreeResources(true);
  116. EXPECT_EQ(0u, gl_.GetSharedMemoryBytesAllocated());
  117. }
  118. } // namespace gpu