1 /*
2 * Copyright (C) 2019 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 #define LOG_TAG "GrallocBufferAllocatorTests"
18 #include <log/log.h>
19
20 #include <gralloc_buffer_allocator.h>
21 #include <gtest/gtest.h>
22
23 namespace android {
24 namespace google_camera_hal {
25
26 static const uint32_t kBufferWidth = 4032;
27 static const uint32_t kBufferHeight = 3024;
28 static const uint32_t kMaxBufferDepth = 10;
29
30 // Test GrallocBufferAllocator Create.
TEST(GrallocBufferAllocatorTests,Create)31 TEST(GrallocBufferAllocatorTests, Create) {
32 auto allocator = GrallocBufferAllocator::Create();
33 ASSERT_NE(allocator, nullptr) << "Create GrallocBufferAllocator failed.";
34 }
35
36 // Test GrallocBufferAllocator AllocateFreeBuffers.
TEST(GrallocBufferAllocatorTests,AllocateFreeBuffers)37 TEST(GrallocBufferAllocatorTests, AllocateFreeBuffers) {
38 auto allocator = GrallocBufferAllocator::Create();
39 ASSERT_NE(allocator, nullptr) << "Create GrallocBufferAllocator failed.";
40
41 HalBufferDescriptor buffer_descriptor = {};
42 buffer_descriptor.width = kBufferWidth;
43 buffer_descriptor.height = kBufferHeight;
44 buffer_descriptor.format = HAL_PIXEL_FORMAT_RAW10;
45 buffer_descriptor.producer_flags = GRALLOC1_PRODUCER_USAGE_CAMERA;
46 buffer_descriptor.consumer_flags = GRALLOC1_CONSUMER_USAGE_CAMERA;
47 buffer_descriptor.immediate_num_buffers = kMaxBufferDepth;
48 buffer_descriptor.max_num_buffers = kMaxBufferDepth;
49
50 std::vector<buffer_handle_t> buffers_;
51 status_t res = allocator->AllocateBuffers(buffer_descriptor, &buffers_);
52 ASSERT_EQ(res, OK) << "AllocateBuffers failed: " << strerror(res);
53 ASSERT_EQ(buffers_.size(), (uint32_t)kMaxBufferDepth)
54 << "AllocateBuffers failed with wrong buffer number " << buffers_.size();
55
56 allocator->FreeBuffers(&buffers_);
57 ASSERT_EQ(buffers_.size(), (uint32_t)0)
58 << "AllocateBuffers failed with wrong buffer number " << buffers_.size();
59 }
60
61 // Test GrallocBufferAllocator MultipleAllocateBuffers.
TEST(GrallocBufferAllocatorTests,MultipleAllocateBuffers)62 TEST(GrallocBufferAllocatorTests, MultipleAllocateBuffers) {
63 auto allocator = GrallocBufferAllocator::Create();
64 ASSERT_NE(allocator, nullptr) << "Create GrallocBufferAllocator failed.";
65
66 HalBufferDescriptor buffer_descriptor = {};
67 buffer_descriptor.width = kBufferWidth;
68 buffer_descriptor.height = kBufferHeight;
69 buffer_descriptor.format = HAL_PIXEL_FORMAT_RAW10;
70 buffer_descriptor.producer_flags = GRALLOC1_PRODUCER_USAGE_CAMERA;
71 buffer_descriptor.consumer_flags = GRALLOC1_CONSUMER_USAGE_CAMERA;
72 buffer_descriptor.immediate_num_buffers = kMaxBufferDepth;
73 buffer_descriptor.max_num_buffers = kMaxBufferDepth;
74
75 std::vector<buffer_handle_t> buffers_;
76 status_t res = allocator->AllocateBuffers(buffer_descriptor, &buffers_);
77 ASSERT_EQ(res, OK) << "AllocateBuffers failed: " << strerror(res);
78 ASSERT_EQ(buffers_.size(), (uint32_t)kMaxBufferDepth)
79 << "AllocateBuffers failed with wrong buffer number " << buffers_.size();
80
81 res = allocator->AllocateBuffers(buffer_descriptor, &buffers_);
82 ASSERT_EQ(res, OK) << "Second AllocateBuffers failed: " << strerror(res);
83 ASSERT_EQ(buffers_.size(), (uint32_t)(kMaxBufferDepth + kMaxBufferDepth))
84 << "Second AllocateBuffers failed with wrong buffer number "
85 << buffers_.size();
86
87 allocator->FreeBuffers(&buffers_);
88 ASSERT_EQ(buffers_.size(), (uint32_t)0)
89 << "AllocateBuffers failed with wrong buffer number " << buffers_.size();
90 }
91
92 } // namespace google_camera_hal
93 } // namespace android
94