blob: 3b5e3585b980c02273c61a305d9c4c2ccc0ccacb [file] [log] [blame]
Jayant Chowdhary249e1f22018-09-24 15:07:45 -07001/*
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 <gtest/gtest.h>
18#include <media/NdkImageReader.h>
19#include <media/NdkImage.h>
20#include <gui/IGraphicBufferProducer.h>
21#include <gui/bufferqueue/1.0/H2BGraphicBufferProducer.h>
22#include <NdkImageReaderPriv.h>
23#include <vndk/hardware_buffer.h>
24#include <memory>
25
26namespace android {
27
28using hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer;
29typedef IGraphicBufferProducer::QueueBufferInput QueueBufferInput;
30typedef IGraphicBufferProducer::QueueBufferOutput QueueBufferOutput;
31
32static constexpr uint64_t kImageBufferUsage =
33 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
34static constexpr int kImageWidth = 640;
35static constexpr int kImageHeight = 480;
36static constexpr int kImageFormat = AIMAGE_FORMAT_RGBA_8888;
37static constexpr int kMaxImages = 1;
38
39static constexpr int64_t kQueueBufferInputTimeStamp = 1384888611;
40static constexpr bool kQueueBufferInputIsAutoTimeStamp = false;
41static constexpr android_dataspace kQueueBufferInputDataspace = HAL_DATASPACE_UNKNOWN;
42static const Rect kQueueBufferInputRect = Rect(kImageWidth, kImageHeight);
43static constexpr int kQueueBufferInputScalingMode = 0;
44static constexpr int kQueueBufferInputTransform = 0;
45static const sp<Fence> kQueueBufferInputFence = Fence::NO_FENCE;
46
47static constexpr int kOnImageAvailableWaitUs = 100 * 1000;
48
49class AImageReaderWindowHandleTest : public ::testing::Test {
50 public:
51 void SetUp() override {
52 AImageReader_newWithUsage(kImageWidth, kImageHeight, kImageFormat,
53 kImageBufferUsage , kMaxImages, &imageReader_);
54 media_status_t ret = AMEDIA_ERROR_UNKNOWN;
55 ASSERT_NE(imageReader_, nullptr);
56 ret = AImageReader_setImageListener(imageReader_,
57 &imageReaderAvailableCb_);
58 ASSERT_EQ(ret, AMEDIA_OK);
59 ret = AImageReader_setBufferRemovedListener(imageReader_,
60 &imageReaderDetachedCb_);
61 ASSERT_EQ(ret, AMEDIA_OK);
62 }
63 void TearDown() override {
64 if (imageReader_) {
65 AImageReader_delete(imageReader_);
66 }
67 }
68
69 void HandleImageAvailable() {
70 AImage *outImage = nullptr;
71 media_status_t ret = AMEDIA_OK;
72 auto imageDeleter = [](AImage *img) { AImage_delete(img); };
73 std::unique_ptr<AImage, decltype(imageDeleter)> img(nullptr, imageDeleter);
74
75 // Test that the image can be acquired.
76 ret = AImageReader_acquireNextImage(imageReader_, &outImage);
77 ASSERT_EQ(ret, AMEDIA_OK);
78 img.reset(outImage);
79 ASSERT_NE(img, nullptr);
80
81 // Test that we can get a handle to the image's hardware buffer and a
82 // native handle to it.
83 AHardwareBuffer *hardwareBuffer = nullptr;
84 ret = AImage_getHardwareBuffer(img.get(), &hardwareBuffer);
85 ASSERT_EQ(ret, AMEDIA_OK);
86 ASSERT_NE(hardwareBuffer, nullptr);
87 const native_handle_t *nh = AHardwareBuffer_getNativeHandle(hardwareBuffer);
88 ASSERT_NE(nh, nullptr);
89 std::unique_lock<std::mutex> lock(imageAvailableMutex_);
90 imageAvailable_ = true;
91 imageCondVar_.notify_one();
92 }
93
94 static void onImageAvailable(void *context, AImageReader *reader) {
95 (void)reader;
96 AImageReaderWindowHandleTest *thisContext =
97 reinterpret_cast<AImageReaderWindowHandleTest *>(context);
98 thisContext->HandleImageAvailable();
99 }
100
101 static void onBufferRemoved(void *, AImageReader *, AHardwareBuffer *) {
102 }
103
104 AImageReader *imageReader_ = nullptr;
105 AImageReader_ImageListener imageReaderAvailableCb_{this, onImageAvailable};
106 AImageReader_BufferRemovedListener imageReaderDetachedCb_{this, onBufferRemoved};
107 std::mutex imageAvailableMutex_;
108 std::condition_variable imageCondVar_;
109 bool imageAvailable_ = false;
110};
111
112static void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride) {
113 const size_t PIXEL_SIZE = 4;
114 for (int x = 0; x < w; x++) {
115 for (int y = 0; y < h; y++) {
116 off_t offset = (y * stride + x) * PIXEL_SIZE;
117 for (int c = 0; c < 4; c++) {
118 int parityX = (x / (1 << (c+2))) & 1;
119 int parityY = (y / (1 << (c+2))) & 1;
120 buf[offset + c] = (parityX ^ parityY) ? 231 : 35;
121 }
122 }
123 }
124}
125
126TEST_F(AImageReaderWindowHandleTest, CreateWindowNativeHandle) {
127 // Check that we can create a native_handle_t corresponding to the
128 // AImageReader.
129 native_handle_t *nh = nullptr;
130 AImageReader_getWindowNativeHandle(imageReader_, &nh);
131 ASSERT_NE(nh, nullptr);
132
133 // Check that there are only ints in the handle.
134 ASSERT_EQ(nh->numFds, 0);
135 ASSERT_NE(nh->numInts, 0);
136
137 // Check that the HGBP can be retrieved from the handle.
138 sp<HGraphicBufferProducer> hgbp = AImageReader_getHGBPFromHandle(nh);
139 ASSERT_NE(hgbp, nullptr);
140 sp<IGraphicBufferProducer> igbp = new H2BGraphicBufferProducer(hgbp);
141 int dequeuedSlot = -1;
142 sp<Fence> dequeuedFence;
143 IGraphicBufferProducer::QueueBufferOutput output;
144 ASSERT_EQ(OK, igbp->connect(nullptr, NATIVE_WINDOW_API_CPU, false, &output));
145
146 // Test that we can dequeue a buffer.
147 ASSERT_EQ(OK,
148 ~IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION &
149 (igbp->dequeueBuffer(&dequeuedSlot, &dequeuedFence,
150 kImageWidth, kImageHeight,
151 kImageFormat, kImageBufferUsage,
152 nullptr, nullptr)));
153 EXPECT_LE(0, dequeuedSlot);
154 EXPECT_GT(BufferQueue::NUM_BUFFER_SLOTS, dequeuedSlot);
155
156 sp<GraphicBuffer> dequeuedBuffer;
157 igbp->requestBuffer(dequeuedSlot, &dequeuedBuffer);
158 uint8_t* img = nullptr;
159 ASSERT_EQ(NO_ERROR, dequeuedBuffer->lock(kImageBufferUsage, (void**)(&img)));
160
161 // Write in some dummy image data.
162 fillRGBA8Buffer(img, dequeuedBuffer->getWidth(), dequeuedBuffer->getHeight(),
163 dequeuedBuffer->getStride());
164 ASSERT_EQ(NO_ERROR, dequeuedBuffer->unlock());
165 QueueBufferInput queueBufferInput(kQueueBufferInputTimeStamp,
166 kQueueBufferInputIsAutoTimeStamp,
167 kQueueBufferInputDataspace,
168 kQueueBufferInputRect,
169 kQueueBufferInputScalingMode,
170 kQueueBufferInputTransform,
171 kQueueBufferInputFence);
172 QueueBufferOutput queueBufferOutput;
173 ASSERT_EQ(OK, igbp->queueBuffer(dequeuedSlot, queueBufferInput,
174 &queueBufferOutput));
175 // wait until the onImageAvailable callback is called, or timeout completes.
176 std::unique_lock<std::mutex> lock(imageAvailableMutex_);
177 imageCondVar_.wait_for(lock, std::chrono::microseconds(kOnImageAvailableWaitUs),
178 [this]{ return this->imageAvailable_;});
179 EXPECT_TRUE(imageAvailable_) << "Timed out waiting for image data to be handled!\n";
180}
181
182} // namespace android