1 /*
2 * Copyright 2018 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
17 #include <android/hidl/token/1.0/ITokenManager.h>
18 #include <android/hidl/manager/1.2/IServiceManager.h>
19 #include <gtest/gtest.h>
20 #include <hidl/ServiceManagement.h>
21 #include <media/NdkImageReader.h>
22 #include <media/NdkImage.h>
23 #include <mediautils/AImageReaderUtils.h>
24 #include <gui/IGraphicBufferProducer.h>
25 #include <gui/Surface.h>
26 #include <gui/bufferqueue/1.0/H2BGraphicBufferProducer.h>
27 #include <NdkImagePriv.h>
28 #include <NdkImageReaderPriv.h>
29 #include <vndk/hardware_buffer.h>
30 #include <memory>
31
32 namespace android {
33
34 using HGraphicBufferProducer = hardware::graphics::bufferqueue::V1_0::
35 IGraphicBufferProducer;
36 using hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer;
37 using hidl::manager::V1_2::IServiceManager;
38 using hidl::token::V1_0::ITokenManager;
39 using aimg::AImageReader_getHGBPFromHandle;
40
41 typedef IGraphicBufferProducer::QueueBufferInput QueueBufferInput;
42 typedef IGraphicBufferProducer::QueueBufferOutput QueueBufferOutput;
43
44 static constexpr uint64_t kImageBufferUsage =
45 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
46 static constexpr int kImageWidth = 640;
47 static constexpr int kImageHeight = 480;
48 static constexpr int kImageFormat = AIMAGE_FORMAT_RGBA_8888;
49 static constexpr int kMaxImages = 1;
50
51 static constexpr int64_t kQueueBufferInputTimeStamp = 1384888611;
52 static constexpr bool kQueueBufferInputIsAutoTimeStamp = false;
53 static constexpr android_dataspace kQueueBufferInputDataspace = HAL_DATASPACE_UNKNOWN;
54 static const Rect kQueueBufferInputRect = Rect(kImageWidth, kImageHeight);
55 static constexpr int kQueueBufferInputScalingMode = 0;
56 static constexpr int kQueueBufferInputTransform = 0;
57 static const sp<Fence> kQueueBufferInputFence = Fence::NO_FENCE;
58
59 static constexpr int kOnImageAvailableWaitUs = 100 * 1000;
60
61 class AImageReaderWindowTest : public ::testing::Test {
62 public:
SetUp()63 void SetUp() override {
64 AImageReader_newWithUsage(kImageWidth, kImageHeight, kImageFormat,
65 kImageBufferUsage , kMaxImages, &imageReader_);
66 media_status_t ret = AMEDIA_ERROR_UNKNOWN;
67 ASSERT_NE(imageReader_, nullptr);
68 ret = AImageReader_setImageListener(imageReader_,
69 &imageReaderAvailableCb_);
70 ASSERT_EQ(ret, AMEDIA_OK);
71 ret = AImageReader_setBufferRemovedListener(imageReader_,
72 &imageReaderDetachedCb_);
73 ASSERT_EQ(ret, AMEDIA_OK);
74 }
TearDown()75 void TearDown() override {
76 if (imageReader_) {
77 AImageReader_delete(imageReader_);
78 }
79 }
80
HandleImageAvailable()81 void HandleImageAvailable() {
82 AImage *outImage = nullptr;
83 media_status_t ret = AMEDIA_OK;
84 auto imageDeleter = [](AImage *img) { AImage_delete(img); };
85 std::unique_ptr<AImage, decltype(imageDeleter)> img(nullptr, imageDeleter);
86
87 // Test that the image can be acquired.
88 ret = AImageReader_acquireNextImage(imageReader_, &outImage);
89 ASSERT_EQ(ret, AMEDIA_OK);
90 img.reset(outImage);
91 ASSERT_NE(img, nullptr);
92
93 // Test that we can get a handle to the image's hardware buffer and a
94 // native handle to it.
95 AHardwareBuffer *hardwareBuffer = nullptr;
96 ret = AImage_getHardwareBuffer(img.get(), &hardwareBuffer);
97 ASSERT_EQ(ret, AMEDIA_OK);
98 ASSERT_NE(hardwareBuffer, nullptr);
99 const native_handle_t *nh = AHardwareBuffer_getNativeHandle(hardwareBuffer);
100 ASSERT_NE(nh, nullptr);
101 std::unique_lock<std::mutex> lock(imageAvailableMutex_);
102 imageAvailable_ = true;
103 imageCondVar_.notify_one();
104 }
105
onImageAvailable(void * context,AImageReader * reader)106 static void onImageAvailable(void *context, AImageReader *reader) {
107 (void)reader;
108 AImageReaderWindowTest *thisContext =
109 reinterpret_cast<AImageReaderWindowTest *>(context);
110 thisContext->HandleImageAvailable();
111 }
112
onBufferRemoved(void *,AImageReader *,AHardwareBuffer *)113 static void onBufferRemoved(void *, AImageReader *, AHardwareBuffer *) {
114 }
115
fillRGBA8Buffer(uint8_t * buf,int w,int h,int stride)116 static void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride) {
117 const size_t PIXEL_SIZE = 4;
118 for (int x = 0; x < w; x++) {
119 for (int y = 0; y < h; y++) {
120 off_t offset = (y * stride + x) * PIXEL_SIZE;
121 for (int c = 0; c < 4; c++) {
122 int parityX = (x / (1 << (c+2))) & 1;
123 int parityY = (y / (1 << (c+2))) & 1;
124 buf[offset + c] = (parityX ^ parityY) ? 231 : 35;
125 }
126 }
127 }
128 }
129
validateIGBP(sp<IGraphicBufferProducer> & igbp)130 void validateIGBP(sp<IGraphicBufferProducer>& igbp) {
131 int dequeuedSlot = -1;
132 sp<Fence> dequeuedFence;
133 IGraphicBufferProducer::QueueBufferOutput output;
134 ASSERT_EQ(OK, igbp->connect(nullptr, NATIVE_WINDOW_API_CPU, false, &output));
135
136 // Test that we can dequeue a buffer.
137 ASSERT_EQ(OK,
138 ~IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION &
139 (igbp->dequeueBuffer(&dequeuedSlot, &dequeuedFence,
140 kImageWidth, kImageHeight,
141 kImageFormat, kImageBufferUsage,
142 nullptr, nullptr)));
143 EXPECT_LE(0, dequeuedSlot);
144 EXPECT_GT(BufferQueue::NUM_BUFFER_SLOTS, dequeuedSlot);
145
146 sp<GraphicBuffer> dequeuedBuffer;
147 igbp->requestBuffer(dequeuedSlot, &dequeuedBuffer);
148 uint8_t* img = nullptr;
149 ASSERT_EQ(NO_ERROR, dequeuedBuffer->lock(kImageBufferUsage, (void**)(&img)));
150
151 // Write in some placeholder image data.
152 fillRGBA8Buffer(img, dequeuedBuffer->getWidth(), dequeuedBuffer->getHeight(),
153 dequeuedBuffer->getStride());
154 ASSERT_EQ(NO_ERROR, dequeuedBuffer->unlock());
155 QueueBufferInput queueBufferInput(kQueueBufferInputTimeStamp,
156 kQueueBufferInputIsAutoTimeStamp,
157 kQueueBufferInputDataspace,
158 kQueueBufferInputRect,
159 kQueueBufferInputScalingMode,
160 kQueueBufferInputTransform,
161 kQueueBufferInputFence);
162 QueueBufferOutput queueBufferOutput;
163 ASSERT_EQ(OK, igbp->queueBuffer(dequeuedSlot, queueBufferInput,
164 &queueBufferOutput));
165 // wait until the onImageAvailable callback is called, or timeout completes.
166 std::unique_lock<std::mutex> lock(imageAvailableMutex_);
167 imageCondVar_.wait_for(lock, std::chrono::microseconds(kOnImageAvailableWaitUs),
168 [this]{ return this->imageAvailable_;});
169 EXPECT_TRUE(imageAvailable_) << "Timed out waiting for image data to be handled!\n";
170 }
171
172 AImageReader *imageReader_ = nullptr;
173 AImageReader_ImageListener imageReaderAvailableCb_{this, onImageAvailable};
174 AImageReader_BufferRemovedListener imageReaderDetachedCb_{this, onBufferRemoved};
175 std::mutex imageAvailableMutex_;
176 std::condition_variable imageCondVar_;
177 bool imageAvailable_ = false;
178 };
179
180
TEST_F(AImageReaderWindowTest,CreateWindowNativeHandle)181 TEST_F(AImageReaderWindowTest, CreateWindowNativeHandle) {
182 // Check that we can create a native_handle_t corresponding to the
183 // AImageReader.
184 native_handle_t *nh = nullptr;
185 media_status_t status = AImageReader_getWindowNativeHandle(imageReader_, &nh);
186
187 // On newer devices without the HIDL TokenManager service this API is
188 // deprecated and will return an error.
189 if (IServiceManager::Transport::EMPTY ==
190 hardware::defaultServiceManager1_2()->getTransport(ITokenManager::descriptor, "default")) {
191 EXPECT_EQ(status, AMEDIA_ERROR_UNKNOWN);
192 return;
193 }
194 ASSERT_NE(nh, nullptr);
195
196 // Check that there are only ints in the handle.
197 ASSERT_EQ(nh->numFds, 0);
198 ASSERT_NE(nh->numInts, 0);
199
200 // Check that the HGBP can be retrieved from the handle.
201 sp<HGraphicBufferProducer> hgbp = AImageReader_getHGBPFromHandle(nh);
202 ASSERT_NE(hgbp, nullptr);
203 sp<IGraphicBufferProducer> igbp = new H2BGraphicBufferProducer(hgbp);
204
205 validateIGBP(igbp);
206 }
207
TEST_F(AImageReaderWindowTest,CreateWindow)208 TEST_F(AImageReaderWindowTest, CreateWindow) {
209 ANativeWindow* window = nullptr;
210 media_status_t status = AImageReader_getWindow(imageReader_, &window);
211
212 ASSERT_NE(window, nullptr);
213
214 sp<IGraphicBufferProducer> igbp = Surface::getIGraphicBufferProducer(window);
215
216 validateIGBP(igbp);
217 }
218
219 class AImageReaderPrivateFormatTest : public ::testing::Test {
220 public:
SetUp()221 void SetUp() override {
222 auto status = AImageReader_new(kImageWidth, kImageHeight, AIMAGE_FORMAT_RAW_DEPTH,
223 kMaxImages, &imgReader);
224 EXPECT_TRUE(status == AMEDIA_OK);
225 }
226
TearDown()227 void TearDown() override {
228 if (imgReader) {
229 AImageReader_delete(imgReader);
230 }
231 }
232 AImageReader *imgReader = nullptr;
233 };
234
TEST_F(AImageReaderPrivateFormatTest,CreateTest)235 TEST_F(AImageReaderPrivateFormatTest, CreateTest) {
236 EXPECT_TRUE(imgReader != nullptr);
237 }
238
239
240 } // namespace android
241