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 <jni.h>
18 #include <set>
19 #include <unistd.h>
20
21 #include "CameraTestHelpers.h"
22 #include "ImageReaderTestHelpers.h"
23 #include "NativeTestHelpers.h"
24 #include "VulkanTestHelpers.h"
25
26 namespace {
27
28 // Constants used to control test execution.
29 static constexpr uint32_t kTestImageWidth = 640;
30 static constexpr uint32_t kTestImageHeight = 480;
31 static constexpr uint32_t kTestImageFormat = AIMAGE_FORMAT_PRIVATE;
32 static constexpr uint64_t kTestImageUsage =
33 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
34 static constexpr uint32_t kTestImageCount = 3;
35
36 } // namespace
37
38 // A basic test which does the following:
39 // 1) Reads a Camera frame into an AHardwareBuffer.
40 // 2) Creates a VkImage from this AHardwareBuffer.
41 // 3) Renders the AHardwareBuffer to a Vulkan RGBA intermediate.
42 // 4) Reads back the intermediate into a CPU accessible VkBuffer.
43 // 5) Validates that there is noise in the buffer.
loadCameraAndVerifyFrameImport(JNIEnv * env,jclass,jobject assetMgr)44 static void loadCameraAndVerifyFrameImport(JNIEnv *env, jclass,
45 jobject assetMgr) {
46 // Initialize Vulkan.
47 VkInit init;
48 if (!init.init()) {
49 // Could not initialize Vulkan due to lack of device support, skip test.
50 return;
51 }
52 VkImageRenderer renderer(&init, kTestImageWidth, kTestImageHeight,
53 VK_FORMAT_R8G8B8A8_UNORM, 4);
54 ASSERT(renderer.init(env, assetMgr), "Could not init VkImageRenderer.");
55
56 // Initialize image reader and camera helpers.
57 ImageReaderHelper imageReader(kTestImageWidth, kTestImageHeight,
58 kTestImageFormat, kTestImageUsage,
59 kTestImageCount);
60 CameraHelper camera;
61 ASSERT(imageReader.initImageReader() >= 0,
62 "Failed to initialize image reader.");
63 ASSERT(camera.initCamera(imageReader.getNativeWindow()) >= 0,
64 "Failed to initialize camera.");
65 ASSERT(camera.isCameraReady(), "Camera is not ready.");
66
67 // Acquire an AHardwareBuffer from the camera.
68 ASSERT(camera.takePicture() >= 0, "Camera failed to take picture.");
69 AHardwareBuffer *buffer;
70 int ret = imageReader.getBufferFromCurrentImage(&buffer);
71 while (ret != 0) {
72 usleep(1000);
73 ret = imageReader.getBufferFromCurrentImage(&buffer);
74 }
75
76 // Impor the AHardwareBuffer into Vulkan.
77 VkAHardwareBufferImage vkImage(&init);
78 ASSERT(vkImage.init(buffer, true /* useExternalFormat */), "Could not initialize VkAHardwareBufferImage.");
79
80 // Render the AHardwareBuffer using Vulkan and read back the result.
81 std::vector<uint32_t> imageData;
82 ASSERT(renderer.renderImageAndReadback(
83 vkImage.image(), vkImage.sampler(), vkImage.view(),
84 vkImage.semaphore(), vkImage.isSamplerImmutable(), &imageData),
85 "Could not render/read-back Vulkan pixels.");
86
87 // TODO(b/110025779): We should find a way to validate pixels.
88 }
89
90 static JNINativeMethod gMethods[] = {
91 {"loadCameraAndVerifyFrameImport", "(Landroid/content/res/AssetManager;)V",
92 (void *)loadCameraAndVerifyFrameImport},
93 };
94
register_android_graphics_cts_CameraVulkanGpuTest(JNIEnv * env)95 int register_android_graphics_cts_CameraVulkanGpuTest(JNIEnv *env) {
96 jclass clazz = env->FindClass("android/graphics/cts/CameraVulkanGpuTest");
97 return env->RegisterNatives(clazz, gMethods,
98 sizeof(gMethods) / sizeof(JNINativeMethod));
99 }
100