Surface_test.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "DummyConsumer.h"
  17. #include <gtest/gtest.h>
  18. #include <binder/IMemory.h>
  19. #include <gui/ISurfaceComposer.h>
  20. #include <gui/Surface.h>
  21. #include <gui/SurfaceComposerClient.h>
  22. #include <gui/BufferItemConsumer.h>
  23. #include <ui/Rect.h>
  24. #include <utils/String8.h>
  25. #include <private/gui/ComposerService.h>
  26. #include <binder/ProcessState.h>
  27. namespace android {
  28. class SurfaceTest : public ::testing::Test {
  29. protected:
  30. SurfaceTest() {
  31. ProcessState::self()->startThreadPool();
  32. }
  33. virtual void SetUp() {
  34. mComposerClient = new SurfaceComposerClient;
  35. ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
  36. mSurfaceControl = mComposerClient->createSurface(
  37. String8("Test Surface"), 32, 32, PIXEL_FORMAT_RGBA_8888, 0);
  38. ASSERT_TRUE(mSurfaceControl != NULL);
  39. ASSERT_TRUE(mSurfaceControl->isValid());
  40. SurfaceComposerClient::openGlobalTransaction();
  41. ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7fffffff));
  42. ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
  43. SurfaceComposerClient::closeGlobalTransaction();
  44. mSurface = mSurfaceControl->getSurface();
  45. ASSERT_TRUE(mSurface != NULL);
  46. }
  47. virtual void TearDown() {
  48. mComposerClient->dispose();
  49. }
  50. sp<Surface> mSurface;
  51. sp<SurfaceComposerClient> mComposerClient;
  52. sp<SurfaceControl> mSurfaceControl;
  53. };
  54. TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenVisible) {
  55. sp<ANativeWindow> anw(mSurface);
  56. int result = -123;
  57. int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
  58. &result);
  59. EXPECT_EQ(NO_ERROR, err);
  60. EXPECT_EQ(1, result);
  61. }
  62. TEST_F(SurfaceTest, QueuesToWindowComposerIsTrueWhenPurgatorized) {
  63. mSurfaceControl.clear();
  64. sp<ANativeWindow> anw(mSurface);
  65. int result = -123;
  66. int err = anw->query(anw.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
  67. &result);
  68. EXPECT_EQ(NO_ERROR, err);
  69. EXPECT_EQ(1, result);
  70. }
  71. // This test probably doesn't belong here.
  72. TEST_F(SurfaceTest, ScreenshotsOfProtectedBuffersSucceed) {
  73. sp<ANativeWindow> anw(mSurface);
  74. // Verify the screenshot works with no protected buffers.
  75. sp<IGraphicBufferProducer> producer;
  76. sp<IGraphicBufferConsumer> consumer;
  77. BufferQueue::createBufferQueue(&producer, &consumer);
  78. sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
  79. sp<ISurfaceComposer> sf(ComposerService::getComposerService());
  80. sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
  81. ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
  82. 64, 64, 0, 0x7fffffff, false));
  83. // Set the PROTECTED usage bit and verify that the screenshot fails. Note
  84. // that we need to dequeue a buffer in order for it to actually get
  85. // allocated in SurfaceFlinger.
  86. ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(),
  87. GRALLOC_USAGE_PROTECTED));
  88. ASSERT_EQ(NO_ERROR, native_window_set_buffer_count(anw.get(), 3));
  89. ANativeWindowBuffer* buf = 0;
  90. status_t err = native_window_dequeue_buffer_and_wait(anw.get(), &buf);
  91. if (err) {
  92. // we could fail if GRALLOC_USAGE_PROTECTED is not supported.
  93. // that's okay as long as this is the reason for the failure.
  94. // try again without the GRALLOC_USAGE_PROTECTED bit.
  95. ASSERT_EQ(NO_ERROR, native_window_set_usage(anw.get(), 0));
  96. ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
  97. &buf));
  98. return;
  99. }
  100. ASSERT_EQ(NO_ERROR, anw->cancelBuffer(anw.get(), buf, -1));
  101. for (int i = 0; i < 4; i++) {
  102. // Loop to make sure SurfaceFlinger has retired a protected buffer.
  103. ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
  104. &buf));
  105. ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf, -1));
  106. }
  107. ASSERT_EQ(NO_ERROR, sf->captureScreen(display, producer, Rect(),
  108. 64, 64, 0, 0x7fffffff, false));
  109. }
  110. TEST_F(SurfaceTest, ConcreteTypeIsSurface) {
  111. sp<ANativeWindow> anw(mSurface);
  112. int result = -123;
  113. int err = anw->query(anw.get(), NATIVE_WINDOW_CONCRETE_TYPE, &result);
  114. EXPECT_EQ(NO_ERROR, err);
  115. EXPECT_EQ(NATIVE_WINDOW_SURFACE, result);
  116. }
  117. TEST_F(SurfaceTest, QueryConsumerUsage) {
  118. const int TEST_USAGE_FLAGS =
  119. GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER;
  120. sp<IGraphicBufferProducer> producer;
  121. sp<IGraphicBufferConsumer> consumer;
  122. BufferQueue::createBufferQueue(&producer, &consumer);
  123. sp<BufferItemConsumer> c = new BufferItemConsumer(consumer,
  124. TEST_USAGE_FLAGS);
  125. sp<Surface> s = new Surface(producer);
  126. sp<ANativeWindow> anw(s);
  127. int flags = -1;
  128. int err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &flags);
  129. ASSERT_EQ(NO_ERROR, err);
  130. ASSERT_EQ(TEST_USAGE_FLAGS, flags);
  131. }
  132. TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
  133. const android_dataspace TEST_DATASPACE = HAL_DATASPACE_SRGB;
  134. sp<IGraphicBufferProducer> producer;
  135. sp<IGraphicBufferConsumer> consumer;
  136. BufferQueue::createBufferQueue(&producer, &consumer);
  137. sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
  138. cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
  139. sp<Surface> s = new Surface(producer);
  140. sp<ANativeWindow> anw(s);
  141. android_dataspace dataSpace;
  142. int err = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
  143. reinterpret_cast<int*>(&dataSpace));
  144. ASSERT_EQ(NO_ERROR, err);
  145. ASSERT_EQ(TEST_DATASPACE, dataSpace);
  146. }
  147. TEST_F(SurfaceTest, SettingGenerationNumber) {
  148. sp<IGraphicBufferProducer> producer;
  149. sp<IGraphicBufferConsumer> consumer;
  150. BufferQueue::createBufferQueue(&producer, &consumer);
  151. sp<CpuConsumer> cpuConsumer = new CpuConsumer(consumer, 1);
  152. sp<Surface> surface = new Surface(producer);
  153. sp<ANativeWindow> window(surface);
  154. // Allocate a buffer with a generation number of 0
  155. ANativeWindowBuffer* buffer;
  156. int fenceFd;
  157. ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
  158. ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, fenceFd));
  159. // Detach the buffer and check its generation number
  160. sp<GraphicBuffer> graphicBuffer;
  161. sp<Fence> fence;
  162. ASSERT_EQ(NO_ERROR, surface->detachNextBuffer(&graphicBuffer, &fence));
  163. ASSERT_EQ(0U, graphicBuffer->getGenerationNumber());
  164. ASSERT_EQ(NO_ERROR, surface->setGenerationNumber(1));
  165. buffer = static_cast<ANativeWindowBuffer*>(graphicBuffer.get());
  166. // This should change the generation number of the GraphicBuffer
  167. ASSERT_EQ(NO_ERROR, surface->attachBuffer(buffer));
  168. // Check that the new generation number sticks with the buffer
  169. ASSERT_EQ(NO_ERROR, window->cancelBuffer(window.get(), buffer, -1));
  170. ASSERT_EQ(NO_ERROR, window->dequeueBuffer(window.get(), &buffer, &fenceFd));
  171. graphicBuffer = static_cast<GraphicBuffer*>(buffer);
  172. ASSERT_EQ(1U, graphicBuffer->getGenerationNumber());
  173. }
  174. TEST_F(SurfaceTest, GetConsumerName) {
  175. sp<IGraphicBufferProducer> producer;
  176. sp<IGraphicBufferConsumer> consumer;
  177. BufferQueue::createBufferQueue(&producer, &consumer);
  178. sp<DummyConsumer> dummyConsumer(new DummyConsumer);
  179. consumer->consumerConnect(dummyConsumer, false);
  180. consumer->setConsumerName(String8("TestConsumer"));
  181. sp<Surface> surface = new Surface(producer);
  182. sp<ANativeWindow> window(surface);
  183. native_window_api_connect(window.get(), NATIVE_WINDOW_API_CPU);
  184. EXPECT_STREQ("TestConsumer", surface->getConsumerName().string());
  185. }
  186. }