Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | namespace android { |
| 29 | namespace companion { |
| 30 | namespace virtualcamera { |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | uint8_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 | |
| 43 | uint8_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 | |
| 48 | void 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. |
| 73 | void 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 | |
| 98 | void 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 |