blob: a00a1b86af0d72358d60559e038c174e76da4099 [file] [log] [blame]
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +01001/*
2 * Copyright (C) 2023 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_NDEBUG 0
18#define LOG_TAG "TestPatternHelper"
19
20#include "TestPatternHelper.h"
21
22#include <complex>
23#include <cstdint>
24
25#include "log/log.h"
26#include "utils/Errors.h"
27
28namespace android {
29namespace companion {
30namespace virtualcamera {
31
32namespace {
33
34uint8_t julia(const std::complex<float> n, const std::complex<float> c) {
35 std::complex<float> z = n;
36 for (int i = 0; i < 64; i++) {
37 z = z * z + c;
38 if (std::abs(z) > 2.0) return i * 4;
39 }
40 return 0xff;
41}
42
43uint8_t pixelToFractal(const int x, const int y, const std::complex<float> c) {
44 std::complex<float> n(float(x) / 640.0f - 0.5, float(y) / 480.0f - 0.5);
45 return julia(n * 5.f, c);
46}
47
48void renderTestPatternYcbCr420(uint8_t* data_ptr, const int width,
49 const int height, const int frameNumber) {
50 float time = float(frameNumber) / 120.0f;
51 const std::complex<float> c(std::sin(time), std::cos(time));
52
53 uint8_t* y_data = data_ptr;
54 uint8_t* uv_data = static_cast<uint8_t*>(y_data + width * height);
55
56 for (int i = 0; i < width; ++i) {
57 for (int j = 0; j < height; ++j) {
58 y_data[j * width + i] = pixelToFractal(i, j, c * 0.78f);
59 if ((i & 1) && (j & 1)) {
60 uv_data[((j / 2) * (width / 2) + i / 2) * 2] =
61 static_cast<uint8_t>((float(i) / float(width)) * 255.f);
62 uv_data[((j / 2) * (width / 2) + i / 2) * 2 + 1] =
63 static_cast<uint8_t>((float(j) / float(height)) * 255.f);
64 }
65 }
66 }
67}
68
69} // namespace
70
71// This is just to see some meaningfull image in the buffer for testing, only
72// works with YcbCr420.
73void renderTestPatternYCbCr420(const std::shared_ptr<AHardwareBuffer> buffer,
74 const int frameNumber, const int fence) {
75 AHardwareBuffer_Planes planes_info;
76
77 AHardwareBuffer_Desc hwBufferDesc;
78 AHardwareBuffer_describe(buffer.get(), &hwBufferDesc);
79
80 const int width = hwBufferDesc.width;
81 const int height = hwBufferDesc.height;
82
83 int result = AHardwareBuffer_lockPlanes(buffer.get(),
84 AHARDWAREBUFFER_USAGE_CPU_READ_RARELY,
85 fence, nullptr, &planes_info);
86 if (result != OK) {
87 ALOGE("%s: Failed to lock planes: %d", __func__, result);
88 return;
89 }
90
91 renderTestPatternYcbCr420(
92 reinterpret_cast<uint8_t*>(planes_info.planes[0].data), width, height,
93 frameNumber);
94
95 AHardwareBuffer_unlock(buffer.get(), nullptr);
96}
97
98void renderTestPatternYCbCr420(sp<Surface> surface, int frameNumber) {
99 ANativeWindow_Buffer buffer;
100 surface->lock(&buffer, nullptr);
101
102 ALOGV("buffer: %dx%d stride %d, pixfmt %d", buffer.width, buffer.height,
103 buffer.stride, buffer.format);
104
105 renderTestPatternYcbCr420(reinterpret_cast<uint8_t*>(buffer.bits),
106 buffer.width, buffer.height, frameNumber);
107
108 surface->unlockAndPost();
109}
110
111} // namespace virtualcamera
112} // namespace companion
113} // namespace android