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
18 #include <android/log.h>
19 #include <jni.h>
20 #include <unistd.h>
21
22 #include "ImageReaderTestHelpers.h"
23 #include "MediaTestHelpers.h"
24 #include "NativeTestHelpers.h"
25 #include "VulkanTestHelpers.h"
26
27 namespace {
28
29 static constexpr uint32_t kTestImageWidth = 1920;
30 static constexpr uint32_t kTestImageHeight = 1080;
31 static constexpr uint32_t kTestImageFormat = AIMAGE_FORMAT_YUV_420_888;
32 static constexpr uint64_t kTestImageUsage =
33 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
34 static constexpr uint32_t kTestImageCount = 3;
35
36 // Confirms that the two values match, allowing for an error of tolerance per
37 // channel.
fuzzyMatch(uint32_t value1,uint32_t value2,int32_t tolerance)38 bool fuzzyMatch(uint32_t value1, uint32_t value2, int32_t tolerance) {
39 for (size_t i = 0; i < 4; ++i) {
40 size_t shift = 8 * i;
41 uint32_t mask = 0x000000FF << shift;
42
43 int32_t value1Masked = static_cast<int32_t>((value1 & mask) >> shift);
44 int32_t value2Masked = static_cast<int32_t>((value2 & mask) >> shift);
45
46 if (std::abs(value1Masked - value2Masked) > tolerance)
47 return false;
48 }
49
50 return true;
51 }
52
swizzleBgraToRgba(uint32_t bgra)53 uint32_t swizzleBgraToRgba(uint32_t bgra) {
54 uint32_t result = 0;
55 result |= (bgra & 0xFF000000) >> 0; // Alpha
56 result |= (bgra & 0x00FF0000) >> 16; // Red
57 result |= (bgra & 0x0000FF00) >> 0; // Green
58 result |= (bgra & 0x000000FF) << 16; // Blue
59 return result;
60 }
61
62 } // namespace
63
64 // A Vulkan media import test which does the following:
65 // 1) Reads the first frame from a video as an AHardwareBuffer.
66 // 2) Creates a VkImage from this AHardwareBuffer.
67 // 3) Renders the AHardwareBuffer to a Vulkan RGBA intermediate.
68 // 4) Reads back the intermediate into a CPU accessible VkBuffer.
69 // 5) Validates that the values are as expected.
loadMediaAndVerifyFrameImport(JNIEnv * env,jclass,jobject assetMgr,jstring jfilename,jintArray referencePixels)70 static void loadMediaAndVerifyFrameImport(JNIEnv *env, jclass, jobject assetMgr,
71 jstring jfilename,
72 jintArray referencePixels) {
73 // Set up Vulkan.
74 VkInit init;
75 if (!init.init()) {
76 // Could not initialize Vulkan due to lack of device support, skip test.
77 return;
78 }
79
80 // Set up the image reader and media helpers used to get a frames from video.
81 ImageReaderHelper imageReader(kTestImageWidth, kTestImageHeight,
82 kTestImageFormat, kTestImageUsage,
83 kTestImageCount);
84 ASSERT(imageReader.initImageReader() >= 0,
85 "Failed to initialize image reader.");
86 MediaHelper media;
87 ASSERT(media.init(env, assetMgr, jfilename, imageReader.getNativeWindow()),
88 "Failed to initialize media codec.");
89
90 // Get an AHardwareBuffer for the first frame of the video.
91 ASSERT(media.processOneFrame(),
92 "Could not get a media frame to import into Vulkan.");
93 AHardwareBuffer *buffer;
94 int ret = imageReader.getBufferFromCurrentImage(&buffer);
95 while (ret != 0) {
96 usleep(1000);
97 ret = imageReader.getBufferFromCurrentImage(&buffer);
98 }
99
100 // Read the width/height of the produced AHardwareBuffer. AImageReader may round up from our
101 // expected video size.
102 AHardwareBuffer_Desc bufferDesc;
103 AHardwareBuffer_describe(buffer, &bufferDesc);
104 // The AImageReader may round up the size of the AHardwareBuffer returned.
105 ASSERT(bufferDesc.width >= kTestImageWidth, "Unexpectedly small image width read from video.");
106 ASSERT(bufferDesc.height >= kTestImageHeight, "Unexpectedly small image height read from video.");
107
108 // Create a VkImageRenderer with the actual width/height of the AHardwareBuffer.
109 VkImageRenderer renderer(&init, bufferDesc.width, bufferDesc.height,
110 VK_FORMAT_R8G8B8A8_UNORM, 4);
111 ASSERT(renderer.init(env, assetMgr), "Could not init VkImageRenderer.");
112
113 // Import the AHardwareBuffer into Vulkan.
114 VkAHardwareBufferImage vkImage(&init);
115 ASSERT(vkImage.init(buffer, true /* useExternalFormat */),
116 "Could not init VkAHardwareBufferImage.");
117
118 // Render the AHardwareBuffer using Vulkan and read back the result.
119 std::vector<uint32_t> framePixels;
120 ASSERT(renderer.renderImageAndReadback(
121 vkImage.image(), vkImage.sampler(), vkImage.view(),
122 vkImage.semaphore(), vkImage.isSamplerImmutable(), &framePixels),
123 "Could not get frame pixels from Vulkan.");
124 ASSERT(framePixels.size() == bufferDesc.width * bufferDesc.height,
125 "Unexpected number of pixels in frame");
126
127 // Ensure that the data we read back matches our reference image.
128 size_t referenceSize =
129 static_cast<size_t>(env->GetArrayLength(referencePixels));
130 ASSERT(referenceSize == kTestImageWidth * kTestImageHeight,
131 "Unexpected number of pixels in reference image.");
132 uint32_t *referenceData = reinterpret_cast<uint32_t *>(
133 env->GetIntArrayElements(referencePixels, 0));
134 for (uint32_t x = 0; x < kTestImageWidth; ++x) {
135 for (uint32_t y = 0; y < kTestImageHeight; ++y) {
136 size_t frame_offset = y * bufferDesc.width + x;
137 size_t reference_offset = y * kTestImageWidth + x;
138 static const int32_t kTolerance = 0x30;
139 uint32_t value1 = framePixels[frame_offset];
140 // Reference data is BGRA, Vk data is BGRA.
141 uint32_t value2 = swizzleBgraToRgba(referenceData[reference_offset]);
142 ASSERT(fuzzyMatch(value1, value2, kTolerance),
143 "Expected ~0x%08X at (%i,%i), got 0x%08X", value2, x, y, value1);
144 }
145 }
146 }
147
148 static JNINativeMethod gMethods[] = {
149 {"loadMediaAndVerifyFrameImport",
150 "(Landroid/content/res/AssetManager;Ljava/lang/String;[I)V",
151 (void *)loadMediaAndVerifyFrameImport},
152 };
153
register_android_graphics_cts_MediaVulkanGpuTest(JNIEnv * env)154 int register_android_graphics_cts_MediaVulkanGpuTest(JNIEnv *env) {
155 jclass clazz = env->FindClass("android/graphics/cts/MediaVulkanGpuTest");
156 return env->RegisterNatives(clazz, gMethods,
157 sizeof(gMethods) / sizeof(JNINativeMethod));
158 }
159