From patchwork Sat Sep 23 16:23:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mattijs Korpershoek X-Patchwork-Id: 19080 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 72E65C326B for ; Sat, 23 Sep 2023 16:23:43 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1B61462949; Sat, 23 Sep 2023 18:23:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1695486222; bh=V8H6oWQC035fz59EKF4waa9Kvjxmd+PEIjSMxtRGSmg=; h=Date:References:In-Reply-To:To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=zuXFkOws1hdQ6ZxwLwwBA21GW518N2m+ZkTeE13ZVFwn9EGRTGPe/h/jQsXiP+D8O u5knN5biyrgpQAUgpO9ip4v6jEyhKg8Ytc7nXU942uQt9PFnBVGOiyl7mTYRq4SdLK uFa6X84fuqtI19ku8FjjMudkUNcuJJruyOMIwglx0wyi4NP4mkPRg7CrwTHUoDgbqr dNq9sE5su9YXwrjl9A0xv0NFOBFRREOWv0v3rXhbwBXpCe+dcmwTlRMYvwNFKLtJjl vsTZQlK17XlfipKzArkfse9uTka4rV0CuL8cqUbLLQoUNJzSW8V+ChT3q/+dhLTv9o i5eMmxiTl1GvA== Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D680562936 for ; Sat, 23 Sep 2023 18:23:39 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="wbp3OwIq"; dkim-atps=neutral Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-405505b07dfso9129925e9.0 for ; Sat, 23 Sep 2023 09:23:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1695486219; x=1696091019; darn=lists.libcamera.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=InTK3cpHjAowfn+YPF+0J0VMRNJykgYZo8jT8zXv8lo=; b=wbp3OwIq8OhjDUGEDekB3QrWDe8m8zG7XrsOHuPLphTDwGDryZA1Mhgs7Cd85/eFTi HPN5jg4nz4rHTxinCrgPzm2keMjJiwmPj96EmATIDe1hBGdRZrTItm4Eo+uiO6qaCmLy B/zg4Hu8nOH23ow9kCtLfBq7FgEA6s93FKKruTlvXi5IAgC2bGwa4t7jpkytybJY2k3X DpG4zMawbzBWDZFUnWk2/ly42Md2kMTXEjK0hIA2GkvPZKbOQXw9P7a/hdXKEJUInztC NVmsS+i0HO0hMd/2f6vhBYmH2tXJ28H1oMVQAeeyLejWSLYRPAt05FuSqiurLOuDg9Fb Doyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695486219; x=1696091019; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=InTK3cpHjAowfn+YPF+0J0VMRNJykgYZo8jT8zXv8lo=; b=uzzWREBZuXcznJDv8OWyYJ/qYYxGgDYTdEZVeyFDz+GyU+GD1QqXQvYeip2WchIxQ8 qUglknUmXNQTs9TNAYUu4B0h2u3vmVjRkut0wzswMGk3kZdW5GbZeEiXaaj3cO33zZdo JQuc1JKAJVwHkz19pqUxU7ftN08bE8CIlYqYqNdATdpuMGwutwOFV3kXDItVjiQxFTDj EGehFpPT+Le4zLbJasTifRHlzT4JZk9GgEaljySJUsEzU1Q7vTCPb9N+DXQxzX3Lw7JJ guzPcLbyAZxFlnxGMQJBqJH2RNslFb54dMOqBz0eb3XSkSeIzNj1wrTKg+f19mWJId8O Ce+g== X-Gm-Message-State: AOJu0YxsCD0s2reOdR4hGE9adrr8VF0G7nNbfgYEX2xOYn+LQHu75MO+ GN/H0FVkpU+NtjzKHxAImUi0L4y41PeXhC1kwSo= X-Google-Smtp-Source: AGHT+IGJJMx3712CUkS2iJaICOajmX9lbqV6bFSXyYuw+xfYea4IFEMUN/bRccFCNWVr8QL2U8vYKQ== X-Received: by 2002:adf:d0d1:0:b0:31f:f95c:dd7f with SMTP id z17-20020adfd0d1000000b0031ff95cdd7fmr4392078wrh.12.1695486219318; Sat, 23 Sep 2023 09:23:39 -0700 (PDT) Received: from [192.168.0.39] ([2a01:e0a:324:38a0:25d4:3d10:65f9:654e]) by smtp.gmail.com with ESMTPSA id y7-20020adfd087000000b00317f70240afsm7200595wrh.27.2023.09.23.09.23.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 09:23:38 -0700 (PDT) Date: Sat, 23 Sep 2023 18:23:31 +0200 MIME-Version: 1.0 Message-Id: <20230923-gralloc-api-v4-v3-1-9a9e039284ba@baylibre.com> References: <20230923-gralloc-api-v4-v3-0-9a9e039284ba@baylibre.com> In-Reply-To: <20230923-gralloc-api-v4-v3-0-9a9e039284ba@baylibre.com> To: libcamera-devel@lists.libcamera.org X-Mailer: b4 0.12.4-dev-6aa5d Subject: [libcamera-devel] [PATCH v3 1/4] android: Import libutils/libui headers from vndk v33 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Mattijs Korpershoek via libcamera-devel From: Mattijs Korpershoek Reply-To: Mattijs Korpershoek Cc: Jacopo Mondi , Guillaume La Roque Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" In order to switch to the more modern GraphicBufferAllocator class, we need additional compile-time dependencies. Import all the needed headers from the Android Vendor Native Development Kit (vndk) and add them to the android_includes. These have been imported from: URL: https://android.googlesource.com/platform/prebuilts/vndk/v33/ rev: 390028a9da88 ("Update VNDK snapshot v33 to build 9948968. am: 3042508aee am: 7092391a46") No changes have been made to the files, besides adding the SPDX comment. Signed-off-by: Mattijs Korpershoek --- .../native/libs/ui/include/ui/PixelFormat.h | 75 +++++++ include/android/meson.build | 2 + .../system/core/libutils/include/utils/Compat.h | 94 +++++++++ .../system/core/libutils/include/utils/Errors.h | 78 ++++++++ .../system/core/libutils/include/utils/Mutex.h | 219 +++++++++++++++++++++ .../system/core/libutils/include/utils/Singleton.h | 102 ++++++++++ .../system/core/libutils/include/utils/Timers.h | 103 ++++++++++ 7 files changed, 673 insertions(+) diff --git a/include/android/frameworks/native/libs/ui/include/ui/PixelFormat.h b/include/android/frameworks/native/libs/ui/include/ui/PixelFormat.h new file mode 100644 index 000000000000..2add09462218 --- /dev/null +++ b/include/android/frameworks/native/libs/ui/include/ui/PixelFormat.h @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright (C) 2005 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// + +// Pixel formats used across the system. +// These formats might not all be supported by all renderers, for instance +// skia or SurfaceFlinger are not required to support all of these formats +// (either as source or destination) + + +#ifndef UI_PIXELFORMAT_H +#define UI_PIXELFORMAT_H + +#include + +namespace android { + +enum { + // + // these constants need to match those + // in graphics/PixelFormat.java & pixelflinger/format.h + // + PIXEL_FORMAT_UNKNOWN = 0, + PIXEL_FORMAT_NONE = 0, + + // logical pixel formats used by the SurfaceFlinger ----------------------- + PIXEL_FORMAT_CUSTOM = -4, + // Custom pixel-format described by a PixelFormatInfo structure + + PIXEL_FORMAT_TRANSLUCENT = -3, + // System chooses a format that supports translucency (many alpha bits) + + PIXEL_FORMAT_TRANSPARENT = -2, + // System chooses a format that supports transparency + // (at least 1 alpha bit) + + PIXEL_FORMAT_OPAQUE = -1, + // System chooses an opaque format (no alpha bits required) + + // real pixel formats supported for rendering ----------------------------- + + PIXEL_FORMAT_RGBA_8888 = HAL_PIXEL_FORMAT_RGBA_8888, // 4x8-bit RGBA + PIXEL_FORMAT_RGBX_8888 = HAL_PIXEL_FORMAT_RGBX_8888, // 4x8-bit RGB0 + PIXEL_FORMAT_RGB_888 = HAL_PIXEL_FORMAT_RGB_888, // 3x8-bit RGB + PIXEL_FORMAT_RGB_565 = HAL_PIXEL_FORMAT_RGB_565, // 16-bit RGB + PIXEL_FORMAT_BGRA_8888 = HAL_PIXEL_FORMAT_BGRA_8888, // 4x8-bit BGRA + PIXEL_FORMAT_RGBA_5551 = 6, // 16-bit ARGB + PIXEL_FORMAT_RGBA_4444 = 7, // 16-bit ARGB + PIXEL_FORMAT_RGBA_FP16 = HAL_PIXEL_FORMAT_RGBA_FP16, // 64-bit RGBA + PIXEL_FORMAT_RGBA_1010102 = HAL_PIXEL_FORMAT_RGBA_1010102, // 32-bit RGBA + PIXEL_FORMAT_R_8 = 0x38, +}; + +typedef int32_t PixelFormat; + +uint32_t bytesPerPixel(PixelFormat format); + +}; // namespace android + +#endif // UI_PIXELFORMAT_H diff --git a/include/android/meson.build b/include/android/meson.build index da2504f2e493..59a4f36b523b 100644 --- a/include/android/meson.build +++ b/include/android/meson.build @@ -1,7 +1,9 @@ # SPDX-License-Identifier: CC0-1.0 android_includes = ([ + include_directories('frameworks/native/libs/ui/include/'), include_directories('hardware/libhardware/include/'), include_directories('metadata/'), include_directories('system/core/include'), + include_directories('system/core/libutils/include/'), ]) diff --git a/include/android/system/core/libutils/include/utils/Compat.h b/include/android/system/core/libutils/include/utils/Compat.h new file mode 100644 index 000000000000..bba568bd3226 --- /dev/null +++ b/include/android/system/core/libutils/include/utils/Compat.h @@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __LIB_UTILS_COMPAT_H +#define __LIB_UTILS_COMPAT_H + +#include + +#if !defined(__MINGW32__) +#include +#endif + +#if defined(__APPLE__) + +/* Mac OS has always had a 64-bit off_t, so it doesn't have off64_t. */ +static_assert(sizeof(off_t) >= 8, "This code requires that Mac OS have at least a 64-bit off_t."); +typedef off_t off64_t; + +static inline void* mmap64(void* addr, size_t length, int prot, int flags, int fd, off64_t offset) { + return mmap(addr, length, prot, flags, fd, offset); +} + +static inline off64_t lseek64(int fd, off64_t offset, int whence) { + return lseek(fd, offset, whence); +} + +static inline ssize_t pread64(int fd, void* buf, size_t nbytes, off64_t offset) { + return pread(fd, buf, nbytes, offset); +} + +static inline ssize_t pwrite64(int fd, const void* buf, size_t nbytes, off64_t offset) { + return pwrite(fd, buf, nbytes, offset); +} + +static inline int ftruncate64(int fd, off64_t length) { + return ftruncate(fd, length); +} + +#endif /* __APPLE__ */ + +#if defined(_WIN32) +#define O_CLOEXEC O_NOINHERIT +#define O_NOFOLLOW 0 +#define DEFFILEMODE 0666 +#endif /* _WIN32 */ + +#define ZD "%zd" +#define ZD_TYPE ssize_t + +/* + * Needed for cases where something should be constexpr if possible, but not + * being constexpr is fine if in pre-C++11 code (such as a const static float + * member variable). + */ +#if __cplusplus >= 201103L +#define CONSTEXPR constexpr +#else +#define CONSTEXPR +#endif + +/* TEMP_FAILURE_RETRY is not available on macOS, but still useful there. */ +#ifndef TEMP_FAILURE_RETRY +/* Used to retry syscalls that can return EINTR. */ +#define TEMP_FAILURE_RETRY(exp) \ + ({ \ + __typeof__(exp) _rc; \ + do { \ + _rc = (exp); \ + } while (_rc == -1 && errno == EINTR); \ + _rc; \ + }) +#endif + +#if defined(_WIN32) +#define OS_PATH_SEPARATOR '\\' +#else +#define OS_PATH_SEPARATOR '/' +#endif + +#endif /* __LIB_UTILS_COMPAT_H */ diff --git a/include/android/system/core/libutils/include/utils/Errors.h b/include/android/system/core/libutils/include/utils/Errors.h new file mode 100644 index 000000000000..f812844eacc4 --- /dev/null +++ b/include/android/system/core/libutils/include/utils/Errors.h @@ -0,0 +1,78 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include +#include +#include + +namespace android { + +/** + * The type used to return success/failure from frameworks APIs. + * See the anonymous enum below for valid values. + */ +typedef int32_t status_t; + +/* + * Error codes. + * All error codes are negative values. + */ + +enum { + OK = 0, // Preferred constant for checking success. +#ifndef NO_ERROR + // Win32 #defines NO_ERROR as well. It has the same value, so there's no + // real conflict, though it's a bit awkward. + NO_ERROR = OK, // Deprecated synonym for `OK`. Prefer `OK` because it doesn't conflict with Windows. +#endif + + UNKNOWN_ERROR = (-2147483647-1), // INT32_MIN value + + NO_MEMORY = -ENOMEM, + INVALID_OPERATION = -ENOSYS, + BAD_VALUE = -EINVAL, + BAD_TYPE = (UNKNOWN_ERROR + 1), + NAME_NOT_FOUND = -ENOENT, + PERMISSION_DENIED = -EPERM, + NO_INIT = -ENODEV, + ALREADY_EXISTS = -EEXIST, + DEAD_OBJECT = -EPIPE, + FAILED_TRANSACTION = (UNKNOWN_ERROR + 2), +#if !defined(_WIN32) + BAD_INDEX = -EOVERFLOW, + NOT_ENOUGH_DATA = -ENODATA, + WOULD_BLOCK = -EWOULDBLOCK, + TIMED_OUT = -ETIMEDOUT, + UNKNOWN_TRANSACTION = -EBADMSG, +#else + BAD_INDEX = -E2BIG, + NOT_ENOUGH_DATA = (UNKNOWN_ERROR + 3), + WOULD_BLOCK = (UNKNOWN_ERROR + 4), + TIMED_OUT = (UNKNOWN_ERROR + 5), + UNKNOWN_TRANSACTION = (UNKNOWN_ERROR + 6), +#endif + FDS_NOT_ALLOWED = (UNKNOWN_ERROR + 7), + UNEXPECTED_NULL = (UNKNOWN_ERROR + 8), +}; + +// Human readable name of error +std::string statusToString(status_t status); + +} // namespace android diff --git a/include/android/system/core/libutils/include/utils/Mutex.h b/include/android/system/core/libutils/include/utils/Mutex.h new file mode 100644 index 000000000000..72d20bdca888 --- /dev/null +++ b/include/android/system/core/libutils/include/utils/Mutex.h @@ -0,0 +1,219 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _LIBS_UTILS_MUTEX_H +#define _LIBS_UTILS_MUTEX_H + +#include +#include +#include + +#if !defined(_WIN32) +# include +#endif + +#include +#include + +// Enable thread safety attributes only with clang. +// The attributes can be safely erased when compiling with other compilers. +#if defined(__clang__) && (!defined(SWIG)) +#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) +#else +#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op +#endif + +#define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x)) + +#define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) + +#define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) + +#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) + +#define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) + +#define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) + +#define REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__)) + +#define REQUIRES_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__)) + +#define ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__)) + +#define ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__)) + +#define RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__)) + +#define RELEASE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__)) + +#define TRY_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__)) + +#define TRY_ACQUIRE_SHARED(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__)) + +#define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) + +#define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x)) + +#define ASSERT_SHARED_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x)) + +#define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) + +#define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) + +// --------------------------------------------------------------------------- +namespace android { +// --------------------------------------------------------------------------- + +class Condition; + +/* + * NOTE: This class is for code that builds on Win32. Its usage is + * deprecated for code which doesn't build for Win32. New code which + * doesn't build for Win32 should use std::mutex and std::lock_guard instead. + * + * Simple mutex class. The implementation is system-dependent. + * + * The mutex must be unlocked by the thread that locked it. They are not + * recursive, i.e. the same thread can't lock it multiple times. + */ +class CAPABILITY("mutex") Mutex { + public: + enum { + PRIVATE = 0, + SHARED = 1 + }; + + Mutex(); + explicit Mutex(const char* name); + explicit Mutex(int type, const char* name = nullptr); + ~Mutex(); + + // lock or unlock the mutex + status_t lock() ACQUIRE(); + void unlock() RELEASE(); + + // lock if possible; returns 0 on success, error otherwise + status_t tryLock() TRY_ACQUIRE(0); + +#if defined(__ANDROID__) + // Lock the mutex, but don't wait longer than timeoutNs (relative time). + // Returns 0 on success, TIMED_OUT for failure due to timeout expiration. + // + // OSX doesn't have pthread_mutex_timedlock() or equivalent. To keep + // capabilities consistent across host OSes, this method is only available + // when building Android binaries. + // + // FIXME?: pthread_mutex_timedlock is based on CLOCK_REALTIME, + // which is subject to NTP adjustments, and includes time during suspend, + // so a timeout may occur even though no processes could run. + // Not holding a partial wakelock may lead to a system suspend. + status_t timedLock(nsecs_t timeoutNs) TRY_ACQUIRE(0); +#endif + + // Manages the mutex automatically. It'll be locked when Autolock is + // constructed and released when Autolock goes out of scope. + class SCOPED_CAPABILITY Autolock { + public: + inline explicit Autolock(Mutex& mutex) ACQUIRE(mutex) : mLock(mutex) { mLock.lock(); } + inline explicit Autolock(Mutex* mutex) ACQUIRE(mutex) : mLock(*mutex) { mLock.lock(); } + inline ~Autolock() RELEASE() { mLock.unlock(); } + + private: + Mutex& mLock; + // Cannot be copied or moved - declarations only + Autolock(const Autolock&); + Autolock& operator=(const Autolock&); + }; + + private: + friend class Condition; + + // A mutex cannot be copied + Mutex(const Mutex&); + Mutex& operator=(const Mutex&); + +#if !defined(_WIN32) + pthread_mutex_t mMutex; +#else + void _init(); + void* mState; +#endif +}; + +// --------------------------------------------------------------------------- + +#if !defined(_WIN32) + +inline Mutex::Mutex() { + pthread_mutex_init(&mMutex, nullptr); +} +inline Mutex::Mutex(__attribute__((unused)) const char* name) { + pthread_mutex_init(&mMutex, nullptr); +} +inline Mutex::Mutex(int type, __attribute__((unused)) const char* name) { + if (type == SHARED) { + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); + pthread_mutex_init(&mMutex, &attr); + pthread_mutexattr_destroy(&attr); + } else { + pthread_mutex_init(&mMutex, nullptr); + } +} +inline Mutex::~Mutex() { + pthread_mutex_destroy(&mMutex); +} +inline status_t Mutex::lock() { + return -pthread_mutex_lock(&mMutex); +} +inline void Mutex::unlock() { + pthread_mutex_unlock(&mMutex); +} +inline status_t Mutex::tryLock() { + return -pthread_mutex_trylock(&mMutex); +} +#if defined(__ANDROID__) +inline status_t Mutex::timedLock(nsecs_t timeoutNs) { + timeoutNs += systemTime(SYSTEM_TIME_REALTIME); + const struct timespec ts = { + /* .tv_sec = */ static_cast(timeoutNs / 1000000000), + /* .tv_nsec = */ static_cast(timeoutNs % 1000000000), + }; + return -pthread_mutex_timedlock(&mMutex, &ts); +} +#endif + +#endif // !defined(_WIN32) + +// --------------------------------------------------------------------------- + +/* + * Automatic mutex. Declare one of these at the top of a function. + * When the function returns, it will go out of scope, and release the + * mutex. + */ + +typedef Mutex::Autolock AutoMutex; + +// --------------------------------------------------------------------------- +} // namespace android +// --------------------------------------------------------------------------- + +#endif // _LIBS_UTILS_MUTEX_H diff --git a/include/android/system/core/libutils/include/utils/Singleton.h b/include/android/system/core/libutils/include/utils/Singleton.h new file mode 100644 index 000000000000..154aed8f28e5 --- /dev/null +++ b/include/android/system/core/libutils/include/utils/Singleton.h @@ -0,0 +1,102 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_UTILS_SINGLETON_H +#define ANDROID_UTILS_SINGLETON_H + +#include + +// some vendor code assumes they have atoi() after including this file. +#include + +#include +#include +#include + +namespace android { +// --------------------------------------------------------------------------- + +// Singleton may be used in multiple libraries, only one of which should +// define the static member variables using ANDROID_SINGLETON_STATIC_INSTANCE. +// Turn off -Wundefined-var-template so other users don't get: +// instantiation of variable 'android::Singleton::sLock' required here, +// but no definition is available +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wundefined-var-template" +#endif + +// DO NOT USE: Please use scoped static initialization. For instance: +// MyClass& getInstance() { +// static MyClass gInstance(...); +// return gInstance; +// } +template +class ANDROID_API Singleton +{ +public: + static TYPE& getInstance() { + Mutex::Autolock _l(sLock); + TYPE* instance = sInstance; + if (instance == nullptr) { + instance = new TYPE(); + sInstance = instance; + } + return *instance; + } + + static bool hasInstance() { + Mutex::Autolock _l(sLock); + return sInstance != nullptr; + } + +protected: + ~Singleton() { } + Singleton() { } + +private: + Singleton(const Singleton&); + Singleton& operator = (const Singleton&); + static Mutex sLock; + static TYPE* sInstance; +}; + +#if defined(__clang__) +#pragma clang diagnostic pop +#endif + +/* + * use ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) in your implementation file + * (eg: .cpp) to create the static instance of Singleton<>'s attributes, + * and avoid to have a copy of them in each compilation units Singleton + * is used. + * NOTE: we use a version of Mutex ctor that takes a parameter, because + * for some unknown reason using the default ctor doesn't emit the variable! + */ + +#define ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) \ + template<> ::android::Mutex \ + (::android::Singleton< TYPE >::sLock)(::android::Mutex::PRIVATE); \ + template<> TYPE* ::android::Singleton< TYPE >::sInstance(nullptr); /* NOLINT */ \ + template class ::android::Singleton< TYPE >; + + +// --------------------------------------------------------------------------- +} // namespace android + +#endif // ANDROID_UTILS_SINGLETON_H + diff --git a/include/android/system/core/libutils/include/utils/Timers.h b/include/android/system/core/libutils/include/utils/Timers.h new file mode 100644 index 000000000000..40acafabc099 --- /dev/null +++ b/include/android/system/core/libutils/include/utils/Timers.h @@ -0,0 +1,103 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright (C) 2005 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include +#include + +#include + +// ------------------------------------------------------------------ +// C API + +#ifdef __cplusplus +extern "C" { +#endif + +typedef int64_t nsecs_t; // nano-seconds + +static CONSTEXPR inline nsecs_t seconds_to_nanoseconds(nsecs_t secs) +{ + return secs*1000000000; +} + +static CONSTEXPR inline nsecs_t milliseconds_to_nanoseconds(nsecs_t secs) +{ + return secs*1000000; +} + +static CONSTEXPR inline nsecs_t microseconds_to_nanoseconds(nsecs_t secs) +{ + return secs*1000; +} + +static CONSTEXPR inline nsecs_t nanoseconds_to_seconds(nsecs_t secs) +{ + return secs/1000000000; +} + +static CONSTEXPR inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs) +{ + return secs/1000000; +} + +static CONSTEXPR inline nsecs_t nanoseconds_to_microseconds(nsecs_t secs) +{ + return secs/1000; +} + +static CONSTEXPR inline nsecs_t s2ns(nsecs_t v) {return seconds_to_nanoseconds(v);} +static CONSTEXPR inline nsecs_t ms2ns(nsecs_t v) {return milliseconds_to_nanoseconds(v);} +static CONSTEXPR inline nsecs_t us2ns(nsecs_t v) {return microseconds_to_nanoseconds(v);} +static CONSTEXPR inline nsecs_t ns2s(nsecs_t v) {return nanoseconds_to_seconds(v);} +static CONSTEXPR inline nsecs_t ns2ms(nsecs_t v) {return nanoseconds_to_milliseconds(v);} +static CONSTEXPR inline nsecs_t ns2us(nsecs_t v) {return nanoseconds_to_microseconds(v);} + +static CONSTEXPR inline nsecs_t seconds(nsecs_t v) { return s2ns(v); } +static CONSTEXPR inline nsecs_t milliseconds(nsecs_t v) { return ms2ns(v); } +static CONSTEXPR inline nsecs_t microseconds(nsecs_t v) { return us2ns(v); } + +enum { + SYSTEM_TIME_REALTIME = 0, // system-wide realtime clock + SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point + SYSTEM_TIME_PROCESS = 2, // high-resolution per-process clock + SYSTEM_TIME_THREAD = 3, // high-resolution per-thread clock + SYSTEM_TIME_BOOTTIME = 4, // same as SYSTEM_TIME_MONOTONIC, but including CPU suspend time +}; + +// return the system-time according to the specified clock +#ifdef __cplusplus +nsecs_t systemTime(int clock = SYSTEM_TIME_MONOTONIC); +#else +nsecs_t systemTime(int clock); +#endif // def __cplusplus + +/** + * Returns the number of milliseconds to wait between the reference time and the timeout time. + * If the timeout is in the past relative to the reference time, returns 0. + * If the timeout is more than INT_MAX milliseconds in the future relative to the reference time, + * such as when timeoutTime == LLONG_MAX, returns -1 to indicate an infinite timeout delay. + * Otherwise, returns the difference between the reference time and timeout time + * rounded up to the next millisecond. + */ +int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime); + +#ifdef __cplusplus +} // extern "C" +#endif From patchwork Sat Sep 23 16:23:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mattijs Korpershoek X-Patchwork-Id: 19081 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 7DFC3C326C for ; Sat, 23 Sep 2023 16:23:44 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 88D356294B; Sat, 23 Sep 2023 18:23:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1695486223; bh=lgj0ZB2d6fBtKECTnBueFfk0BmVgG8zjpZqDhXO0D7M=; h=Date:References:In-Reply-To:To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=uvLhJCF3dgR5LtUEgYuLIkSzItArVhATrFlb+hCQY3gSWK8ha+hlrjncVrMHirfxk eglPqM3o2R2vKPRykDfZ2N+stA8VIC4tr8z5I31ofrN+0p2gQVjfGY0BD9coP1u1nJ +nKEmP29RPFGunCxj2xKEGTIOxU+82xNWIto6Vl1itXYNVCT+KpnPXP+wq5LJ9Ae+v y0t05Nht2mb1mAvUpyEK6KSQXh/pxAJm1DkudAiJ8FdcpasG0qYuaa/CxnCU7MCkKx y3npj7jDXyHNkT972O9BHrwg0M5tMgOVz/u7+zGYuCF3hhchCp9AGfLBegDPEyE0Eb V9mblu2MVxdEg== Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 460CF61DE7 for ; Sat, 23 Sep 2023 18:23:40 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="MblEazqf"; dkim-atps=neutral Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-31fa15f4cc6so3696097f8f.2 for ; Sat, 23 Sep 2023 09:23:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1695486220; x=1696091020; darn=lists.libcamera.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=OqTOWyJSnZVOAfjh8r0tYOaae7jupjSbWMDA90wU7i8=; b=MblEazqfnwhljlLNteurkg9y1wawC/1nLic1EBI5byLGZk4DiUnpGtnXpnFK4CGCOm Pt2kSA592+jpjSYk4jM2YYYvS37Lb2ZPL935f+a1nTB26ttByLwSnt89qKzI0Vnt0aG5 p2H3Gid8yNS/A8/OeRRGnt21R/+QV2ZOTz3J+S7lBFkU/R4pgLjYLXLLbNU0zxh9u0uL m//3eCUzPc+3/xwai+ae8JoyTgR9Rv6aUXel3rHXIw9ZeagQ8cLXgY9vSAm62BWWIeKv iBxXXtcfthH3jgXuTEFc7bVVZrvizTnUtxlP0ezMO2bMQ4QVrSglJ8VGGgNK7nhrIQcn hIJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695486220; x=1696091020; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OqTOWyJSnZVOAfjh8r0tYOaae7jupjSbWMDA90wU7i8=; b=uj2uA5C9BWUw074mKvO5/x6t3lr8rlGCVGphDIWjZe6oZmAUd7IrtUMla+egVlW9cJ JFV8StVymuGXliyvqFRt4kYX8bFqruBSUggB6gtChIup07ZctREr2NFUvd+fASccPpJ1 mOJQjpNU69vRw8acXIquVFnrdW29ssATuNxnwsG39Yh38ufFys7kIl6+VL+NuGV2oY1+ omlhgBzSmDcHtcz4pheFukJnTSf36kv7QimY2sHSdPNe0e03BQ53/8WSv3W4CDYJAvG1 YoGlG+PZvocyIzDtWD3VuWyJ6wwDazpGHVqRx6Wm/0IFYreGR3St0nC6UHsiduqaAXTy qQuA== X-Gm-Message-State: AOJu0YwvNTui6zJYqHEoTp2YcsacUyvSiCKqcHU396GKmBgX2sZY6Shy xs8IYcCeSc3rD7TyWyWnZ9n4pw== X-Google-Smtp-Source: AGHT+IGhn9dywGJNkcd6Twijd5731w0O0PYaRhh9OdRZVT6lWOvpMw2b3w9jH0VZc1SYpxcFYLf2sg== X-Received: by 2002:adf:fe85:0:b0:319:8a28:4001 with SMTP id l5-20020adffe85000000b003198a284001mr2069800wrr.41.1695486219995; Sat, 23 Sep 2023 09:23:39 -0700 (PDT) Received: from [192.168.0.39] ([2a01:e0a:324:38a0:25d4:3d10:65f9:654e]) by smtp.gmail.com with ESMTPSA id y7-20020adfd087000000b00317f70240afsm7200595wrh.27.2023.09.23.09.23.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 09:23:39 -0700 (PDT) Date: Sat, 23 Sep 2023 18:23:32 +0200 MIME-Version: 1.0 Message-Id: <20230923-gralloc-api-v4-v3-2-9a9e039284ba@baylibre.com> References: <20230923-gralloc-api-v4-v3-0-9a9e039284ba@baylibre.com> In-Reply-To: <20230923-gralloc-api-v4-v3-0-9a9e039284ba@baylibre.com> To: libcamera-devel@lists.libcamera.org X-Mailer: b4 0.12.4-dev-6aa5d Subject: [libcamera-devel] [PATCH v3 2/4] android: Import GraphicBufferAllocator header from vndk v33 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Mattijs Korpershoek via libcamera-devel From: Mattijs Korpershoek Reply-To: Mattijs Korpershoek Cc: Jacopo Mondi , Guillaume La Roque Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The GraphicBufferAllocator class (from libui) provides a more modern interface to Android buffer allocation. Moreover, the gralloc.h API has been deprecated since Android P (9). Import the GraphicBufferAllocator.h from the VNDK. These have been imported from: URL: https://android.googlesource.com/platform/prebuilts/vndk/v33/ rev: 390028a9da88 ("Update VNDK snapshot v33 to build 9948968. am: 3042508aee am: 7092391a46") No changes have been made to the files, besides adding the SPDX comment. Note: because GraphicBufferAllocator has lots of Android-specific dependencies, a future patch is needed to stub/simplify this include. Signed-off-by: Mattijs Korpershoek --- .../libs/ui/include/ui/GraphicBufferAllocator.h | 111 +++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/include/android/frameworks/native/libs/ui/include/ui/GraphicBufferAllocator.h b/include/android/frameworks/native/libs/ui/include/ui/GraphicBufferAllocator.h new file mode 100644 index 000000000000..e4674d746e37 --- /dev/null +++ b/include/android/frameworks/native/libs/ui/include/ui/GraphicBufferAllocator.h @@ -0,0 +1,111 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * + * Copyright 2009, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_BUFFER_ALLOCATOR_H +#define ANDROID_BUFFER_ALLOCATOR_H + +#include + +#include +#include + +#include + +#include + +#include +#include +#include +#include + +namespace android { + +class GrallocAllocator; +class GraphicBufferMapper; + +class GraphicBufferAllocator : public Singleton +{ +public: + static inline GraphicBufferAllocator& get() { return getInstance(); } + + /** + * Allocates and imports a gralloc buffer. + * + * The handle must be freed with GraphicBufferAllocator::free() when no longer needed. + */ + status_t allocate(uint32_t w, uint32_t h, PixelFormat format, uint32_t layerCount, + uint64_t usage, buffer_handle_t* handle, uint32_t* stride, + std::string requestorName); + + /** + * Allocates and does NOT import a gralloc buffer. Buffers cannot be used until they have + * been imported. This function is for advanced use cases only. + * + * The raw native handle must be freed by calling native_handle_close() followed by + * native_handle_delete(). + */ + status_t allocateRawHandle(uint32_t w, uint32_t h, PixelFormat format, uint32_t layerCount, + uint64_t usage, buffer_handle_t* handle, uint32_t* stride, + std::string requestorName); + + /** + * DEPRECATED: GraphicBufferAllocator does not use the graphicBufferId. + */ + status_t allocate(uint32_t w, uint32_t h, PixelFormat format, + uint32_t layerCount, uint64_t usage, + buffer_handle_t* handle, uint32_t* stride, uint64_t graphicBufferId, + std::string requestorName); + + status_t free(buffer_handle_t handle); + + uint64_t getTotalSize() const; + + void dump(std::string& res, bool less = true) const; + static void dumpToSystemLog(bool less = true); + +protected: + struct alloc_rec_t { + uint32_t width; + uint32_t height; + uint32_t stride; + PixelFormat format; + uint32_t layerCount; + uint64_t usage; + size_t size; + std::string requestorName; + }; + + status_t allocateHelper(uint32_t w, uint32_t h, PixelFormat format, uint32_t layerCount, + uint64_t usage, buffer_handle_t* handle, uint32_t* stride, + std::string requestorName, bool importBuffer); + + static Mutex sLock; + static KeyedVector sAllocList; + + friend class Singleton; + GraphicBufferAllocator(); + ~GraphicBufferAllocator(); + + GraphicBufferMapper& mMapper; + std::unique_ptr mAllocator; +}; + +// --------------------------------------------------------------------------- +}; // namespace android + +#endif // ANDROID_BUFFER_ALLOCATOR_H From patchwork Sat Sep 23 16:23:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mattijs Korpershoek X-Patchwork-Id: 19082 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 4377FC326B for ; Sat, 23 Sep 2023 16:23:45 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 14DE46294D; Sat, 23 Sep 2023 18:23:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1695486225; bh=7OX63jm5dduEX5c+tg950hHKvEZVt60LzRJGFDLdW30=; h=Date:References:In-Reply-To:To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=hbRgnpFTxd+bm61BZl0JZv2dBFupkL42sR+glbBJE5JfI9aJeZj8xpSt5K6rHnM/p DIwfd+XwNn60E7uswUUfhrkKaBh1HcEstlK0/jslavpF5rbbJf6lkfDiDvJg9nQpKt T4c/OBToP607f+tiWWqBE4augxA4jvohO08nXnrIA7eKu2/elZjTQYPw2xSzHK8sFb TJym90uyUkjRHNkWso/0D0We14yrt3fU1EKMnrUNPOwkPaxyRyFm4khTnNmbVdWIbr wcSfJ3qmX8oz8oDYSbAnd/05sc9kq3PnCGmHHcHTyFIQxNB1FVUlD9rrxHiCa66lRS V+h6zpikUEx2Q== Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 04E1C62936 for ; Sat, 23 Sep 2023 18:23:41 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="0zQQEyJR"; dkim-atps=neutral Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-4051fea48a8so43645385e9.2 for ; Sat, 23 Sep 2023 09:23:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1695486221; x=1696091021; darn=lists.libcamera.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=syzQyd55bfiNG/ZNBdoFgZkiR6y0jT/ireZI6tbrAfk=; b=0zQQEyJRlYEaiNH0YN4lx2zqeweTAFa0hSkyf7M3ToC+o1tzBsRN3lOJMPMLBGPlaF dZmtXIxmTJfT2mPds2DWz4uYOYJXX+uHv7k9c/K8bWieeKn4fTc29H33tMRPXRXLc5aT Dn7YL9Qlsrru2UunQPErT3AJb0eOCLezX9hYtE9aJUqO02Qt3w0E11Qq8f34gtq3wKyU DTnYKP0ZQI0F4K0tFDFPXxURTFY7XnjoyciwmPKniU82Bt6GiekFGfyvkR37mVE9rPDO kDeSBmgyrOKowA4AwsXOpqGYmbDGHwRQwL4lndoRQXvKoXmV/2hJZNcDI6R0yuM/s0b7 7jCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695486221; x=1696091021; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=syzQyd55bfiNG/ZNBdoFgZkiR6y0jT/ireZI6tbrAfk=; b=DBlfj2van4SHaiqdu9ooej2hAxW3at8RE0lr3/qssmEf/Ye73jPqEgQJmQa0KnjP+5 t0j4KqAAGlGc77Hm8DDNZCVmaMTmvv/FXA/x+vnGJZkYiaTK+chzLPsyCgM5ih7KtKhR 7L5+IC/PSw/XuWVtxHpDZLUjRQW4WgBMY/zT4NX9jIHCVCXpW8aGlI72SZhnFDgm8pbI 7+AeQRSjO/wJHPPllk3x5lnJEkOetYYLpgu+yFLSD+w5pJqyOc8lgApyP8NGH0cP7HGV DziTD8I4/HIffhC/VcNCY5wPW4eEv5FuDuiK7M2K7E4j2zHyvXEn6TZuzv9JyPnz/4HN Nsbw== X-Gm-Message-State: AOJu0YwLC626IKU0vXoNkztCCnjH/NSXM+KkxIZU66WwGq8LvKHglYSD kftf28QR30dDD4aZwd5mDzEKLg== X-Google-Smtp-Source: AGHT+IEwbKeibtjzBSmf+u9g5ZW1F0I5qpeXrRur9brQXpqrHF4x2DPRHv+qE4TUjaCMHA6Tv8D3hg== X-Received: by 2002:adf:f68a:0:b0:320:447:3bcc with SMTP id v10-20020adff68a000000b0032004473bccmr2293444wrp.51.1695486220946; Sat, 23 Sep 2023 09:23:40 -0700 (PDT) Received: from [192.168.0.39] ([2a01:e0a:324:38a0:25d4:3d10:65f9:654e]) by smtp.gmail.com with ESMTPSA id y7-20020adfd087000000b00317f70240afsm7200595wrh.27.2023.09.23.09.23.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 09:23:40 -0700 (PDT) Date: Sat, 23 Sep 2023 18:23:33 +0200 MIME-Version: 1.0 Message-Id: <20230923-gralloc-api-v4-v3-3-9a9e039284ba@baylibre.com> References: <20230923-gralloc-api-v4-v3-0-9a9e039284ba@baylibre.com> In-Reply-To: <20230923-gralloc-api-v4-v3-0-9a9e039284ba@baylibre.com> To: libcamera-devel@lists.libcamera.org X-Mailer: b4 0.12.4-dev-6aa5d Subject: [libcamera-devel] [PATCH v3 3/4] android: Stub GraphicBufferAllocator for build tests X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Mattijs Korpershoek via libcamera-devel From: Mattijs Korpershoek Reply-To: Mattijs Korpershoek Cc: Jacopo Mondi , Guillaume La Roque Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" If we want to keep building libcamera on traditional Linux systems with: -Dandroid=enabled -Dandroid_platform=generic We should stub GraphicBufferAllocator, which is not available. It's only available when building with the VNDK or when building within the AOSP tree. Also remove some deprecated methods and inclusions which are not needed for the stub class. Note: the imported headers from Android generate the -Wextra-semi warning. To avoid patching the files, a pragma has been added before inclusion. Signed-off-by: Mattijs Korpershoek Reviewed-by: Jacopo Mondi --- .../libs/ui/include/ui/GraphicBufferAllocator.h | 30 ------------ src/android/mm/graphic_buffer_allocator_stub.cpp | 54 ++++++++++++++++++++++ src/android/mm/meson.build | 7 +++ 3 files changed, 61 insertions(+), 30 deletions(-) diff --git a/include/android/frameworks/native/libs/ui/include/ui/GraphicBufferAllocator.h b/include/android/frameworks/native/libs/ui/include/ui/GraphicBufferAllocator.h index e4674d746e37..9eac5bbe8324 100644 --- a/include/android/frameworks/native/libs/ui/include/ui/GraphicBufferAllocator.h +++ b/include/android/frameworks/native/libs/ui/include/ui/GraphicBufferAllocator.h @@ -29,15 +29,10 @@ #include #include -#include -#include #include namespace android { -class GrallocAllocator; -class GraphicBufferMapper; - class GraphicBufferAllocator : public Singleton { public: @@ -52,25 +47,6 @@ public: uint64_t usage, buffer_handle_t* handle, uint32_t* stride, std::string requestorName); - /** - * Allocates and does NOT import a gralloc buffer. Buffers cannot be used until they have - * been imported. This function is for advanced use cases only. - * - * The raw native handle must be freed by calling native_handle_close() followed by - * native_handle_delete(). - */ - status_t allocateRawHandle(uint32_t w, uint32_t h, PixelFormat format, uint32_t layerCount, - uint64_t usage, buffer_handle_t* handle, uint32_t* stride, - std::string requestorName); - - /** - * DEPRECATED: GraphicBufferAllocator does not use the graphicBufferId. - */ - status_t allocate(uint32_t w, uint32_t h, PixelFormat format, - uint32_t layerCount, uint64_t usage, - buffer_handle_t* handle, uint32_t* stride, uint64_t graphicBufferId, - std::string requestorName); - status_t free(buffer_handle_t handle); uint64_t getTotalSize() const; @@ -94,15 +70,9 @@ protected: uint64_t usage, buffer_handle_t* handle, uint32_t* stride, std::string requestorName, bool importBuffer); - static Mutex sLock; - static KeyedVector sAllocList; - friend class Singleton; GraphicBufferAllocator(); ~GraphicBufferAllocator(); - - GraphicBufferMapper& mMapper; - std::unique_ptr mAllocator; }; // --------------------------------------------------------------------------- diff --git a/src/android/mm/graphic_buffer_allocator_stub.cpp b/src/android/mm/graphic_buffer_allocator_stub.cpp new file mode 100644 index 000000000000..7e412c956887 --- /dev/null +++ b/src/android/mm/graphic_buffer_allocator_stub.cpp @@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright (C) 2023, Ideas on Board + * Copyright (C) 2023, BayLibre + * + * graphic_buffer_allocator_stub.cpp - Android GraphicBufferAllocator + * stub for compile-testing + */ + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wextra-semi" +#include +#pragma GCC diagnostic pop + +namespace android { + +ANDROID_SINGLETON_STATIC_INSTANCE(GraphicBufferAllocator) + +GraphicBufferAllocator::GraphicBufferAllocator() +{ +} + +GraphicBufferAllocator::~GraphicBufferAllocator() +{ +} + +uint64_t GraphicBufferAllocator::getTotalSize() const +{ + return 0; +} + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +status_t GraphicBufferAllocator::allocate(uint32_t width, + uint32_t height, + PixelFormat format, + uint32_t layerCount, + uint64_t usage, + buffer_handle_t *handle, + uint32_t *stride, + std::string requestorName) +{ + *handle = nullptr; + *stride = 0; + return INVALID_OPERATION; +} + +status_t GraphicBufferAllocator::free(buffer_handle_t handle) +{ + return INVALID_OPERATION; +} +#pragma GCC diagnostic pop + +} // namespace android diff --git a/src/android/mm/meson.build b/src/android/mm/meson.build index e3e0484c3720..4d1fb718e94e 100644 --- a/src/android/mm/meson.build +++ b/src/android/mm/meson.build @@ -12,6 +12,13 @@ if platform == 'generic' else android_hal_sources += files(['libhardware_stub.c']) endif + + libui = dependency('libui', required : false) + if libui.found() + android_deps += [libui] + else + android_hal_sources += files(['graphic_buffer_allocator_stub.cpp']) + endif elif platform == 'cros' android_hal_sources += files(['cros_camera_buffer.cpp', 'cros_frame_buffer_allocator.cpp']) From patchwork Sat Sep 23 16:23:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mattijs Korpershoek X-Patchwork-Id: 19083 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 2F6F4C326C for ; Sat, 23 Sep 2023 16:23:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id BF3776294B; Sat, 23 Sep 2023 18:23:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1695486226; bh=ucAA6j7B+oiN09RDQkUor/fxhuupMT6VWM5gL1TMQf8=; h=Date:References:In-Reply-To:To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=1jxP5KnKZr92YDwt8VqLZSRXZ1NAP61JSonq+574OuVGdhSLyY4iryXtXAayUDAgZ KriyDbZpNGHIAej8hlaQG85WEC0iZt7Agp+St/bUcHwvVQY8y0iv2LBWMrJI/OR94I 1jQzXhQSFC/fgU/mNW2GbpqSoeWMIW1L6408gb+9bHmNOrDdJq1/+Zbb5BzTCpg/TX zAc853jjXhhdRgyQemgN8QMA6TcQV/k8jD3F7PoVOnvxXLiSB4qQhfaAfbCNnxxW5v k26DQ8LEQVKYl83SjEfu7r897M6h6Zk8fUUP+5enTgXmB+1evVMKzoGNMyRMmSwE65 r4PLVdjKV7djQ== Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D6C1162946 for ; Sat, 23 Sep 2023 18:23:42 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="b+mPU0vS"; dkim-atps=neutral Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-4053cf48670so25297985e9.0 for ; Sat, 23 Sep 2023 09:23:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1695486222; x=1696091022; darn=lists.libcamera.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=1uGenlFyLv/JhRh2NyVmUvgesNJ0eIJ6c6DG1nsxTNA=; b=b+mPU0vS7egdwwUNYtwexn0mqU+kn+mWeR2WgXfLF4oyT+e/Mz9RmQXo+Jw08psP3Z yj5VnEeJTmtyt/HNJ3wO2qOlOCWCbmgJIwdMTzodjiunvR9UOszJ7FSZ55xG50t0jY5p DnpZmaDcSxKtWOGaMuAuQvkxkVilEPmGei8UMlx4Y0NFf0Xd1zzfvHHEZfbMkuqojW/O dwnKMGQ9dmTJx7LHXr0o9jDPfFECQv0/GLZ3AnSKMNlXwJ4/l/2oyli16jSUAGvv5RIF 9zdg/uzKjWVbGpagrwPintYKpRy5R4d5P+JqU4lOs7m5gtMoGEW57GQJzDAVt2XFnSvp xfdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695486222; x=1696091022; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1uGenlFyLv/JhRh2NyVmUvgesNJ0eIJ6c6DG1nsxTNA=; b=gKzrqAnmAkpdQroWbKzpDIwLj+S0Kx2C7rJShfXWRdkCKLkAMP/QPWq/QSZmft1v0A sakez9n6hnM67+hEx4zhse5Il3DmqD2WJt411iNlv5NuUuTOeqmbKMIqRI9CYVaXB9Ke aWr7r14Y7UMwHx82ttlOUTTP1mbI9ouu5fA5ltu7TiHNPJj8Pm5Yb+FqTafOJ9a+0N6s 0vm1tgvHjvQoC+DhRmlt072PV7tfDW0rBqPCTCjazPnZxDNBirONxKHj0iZ9s1yyCEte vs0Gb52bVRsfeMErob+rE0n7qHENGqWzIrEkAgX4BAJqmMoig9LHxJE3Pdc1YUEp7zzD uAXw== X-Gm-Message-State: AOJu0YwbEnGg0TAomgpkpRnr7F9qW0UG7RamKFVRoVMxy47KAllGFnw7 KivNMv3LpkAXZqNH1Q1To+Ck4g== X-Google-Smtp-Source: AGHT+IHIH3O9gg1eL/Cw9eTyJe7B17uPW8KKwbhOCiR3zF5xJBFhuvwCh9LWjDWQ8gi38UzTUZJIig== X-Received: by 2002:a1c:720c:0:b0:401:38dc:891c with SMTP id n12-20020a1c720c000000b0040138dc891cmr2094342wmc.5.1695486221711; Sat, 23 Sep 2023 09:23:41 -0700 (PDT) Received: from [192.168.0.39] ([2a01:e0a:324:38a0:25d4:3d10:65f9:654e]) by smtp.gmail.com with ESMTPSA id y7-20020adfd087000000b00317f70240afsm7200595wrh.27.2023.09.23.09.23.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 09:23:41 -0700 (PDT) Date: Sat, 23 Sep 2023 18:23:34 +0200 MIME-Version: 1.0 Message-Id: <20230923-gralloc-api-v4-v3-4-9a9e039284ba@baylibre.com> References: <20230923-gralloc-api-v4-v3-0-9a9e039284ba@baylibre.com> In-Reply-To: <20230923-gralloc-api-v4-v3-0-9a9e039284ba@baylibre.com> To: libcamera-devel@lists.libcamera.org X-Mailer: b4 0.12.4-dev-6aa5d Subject: [libcamera-devel] [PATCH v3 4/4] android: mm: generic: Use GraphicBufferAllocator instead of gralloc.h X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Mattijs Korpershoek via libcamera-devel From: Mattijs Korpershoek Reply-To: Mattijs Korpershoek Cc: Jacopo Mondi , Guillaume La Roque Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" gralloc.h is a very old API that has been deprecated at least since Android P (9). Switch over to a higher level abstraction of gralloc from libui, which is compatible with Android 11 and up. Libui: * is provided in the VNDK (so it's available to vendors). * is also used in the camera vts test named VtsAidlHalCameraProvider_TargetTest. Drop the libhardware stub since we no longer need it. Notes: * GraphicsBufferAllocator being a Singleton, buffer lifecycle management is easier. * The imported headers from Android generate the -Wextra-semi warning. To avoid patching the files, a pragma has been added before inclusion. Signed-off-by: Mattijs Korpershoek Reviewed-by: Jacopo Mondi --- src/android/mm/generic_frame_buffer_allocator.cpp | 68 ++++++++--------------- src/android/mm/libhardware_stub.c | 17 ------ src/android/mm/meson.build | 8 --- 3 files changed, 22 insertions(+), 71 deletions(-) diff --git a/src/android/mm/generic_frame_buffer_allocator.cpp b/src/android/mm/generic_frame_buffer_allocator.cpp index 7ecef2c669df..1f2fbe334f2c 100644 --- a/src/android/mm/generic_frame_buffer_allocator.cpp +++ b/src/android/mm/generic_frame_buffer_allocator.cpp @@ -16,8 +16,11 @@ #include "libcamera/internal/framebuffer.h" #include -#include -#include +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wextra-semi" +#include +#pragma GCC diagnostic pop +#include #include "../camera_device.h" #include "../frame_buffer_allocator.h" @@ -33,35 +36,26 @@ class GenericFrameBufferData : public FrameBuffer::Private LIBCAMERA_DECLARE_PUBLIC(FrameBuffer) public: - GenericFrameBufferData(struct alloc_device_t *allocDevice, - buffer_handle_t handle, + GenericFrameBufferData(buffer_handle_t handle, const std::vector &planes) - : FrameBuffer::Private(planes), allocDevice_(allocDevice), - handle_(handle) + : FrameBuffer::Private(planes), handle_(handle) { - ASSERT(allocDevice_); ASSERT(handle_); } ~GenericFrameBufferData() override { /* - * allocDevice_ is used to destroy handle_. allocDevice_ is - * owned by PlatformFrameBufferAllocator::Private. - * GenericFrameBufferData must be destroyed before it is - * destroyed. - * - * \todo Consider managing alloc_device_t with std::shared_ptr - * if this is difficult to maintain. - * * \todo Thread safety against alloc_device_t is not documented. * Is it no problem to call alloc/free in parallel? */ - allocDevice_->free(allocDevice_, handle_); + auto &allocator = android::GraphicBufferAllocator::get(); + android::status_t status = allocator.free(handle_); + if (status != android::NO_ERROR) + LOG(HAL, Error) << "Error freeing framebuffer: " << status; } private: - struct alloc_device_t *allocDevice_; const buffer_handle_t handle_; }; } /* namespace */ @@ -72,51 +66,33 @@ class PlatformFrameBufferAllocator::Private : public Extensible::Private public: Private(CameraDevice *const cameraDevice) - : cameraDevice_(cameraDevice), - hardwareModule_(nullptr), - allocDevice_(nullptr) + : cameraDevice_(cameraDevice) { - hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &hardwareModule_); - ASSERT(hardwareModule_); } - ~Private() override; + ~Private() = default; std::unique_ptr allocate(int halPixelFormat, const libcamera::Size &size, uint32_t usage); private: const CameraDevice *const cameraDevice_; - const struct hw_module_t *hardwareModule_; - struct alloc_device_t *allocDevice_; }; -PlatformFrameBufferAllocator::Private::~Private() -{ - if (allocDevice_) - gralloc_close(allocDevice_); - dlclose(hardwareModule_->dso); -} - std::unique_ptr PlatformFrameBufferAllocator::Private::allocate(int halPixelFormat, const libcamera::Size &size, uint32_t usage) { - if (!allocDevice_) { - int ret = gralloc_open(hardwareModule_, &allocDevice_); - if (ret) { - LOG(HAL, Fatal) << "gralloc_open() failed: " << ret; - return nullptr; - } - } - - int stride = 0; + uint32_t stride = 0; buffer_handle_t handle = nullptr; - int ret = allocDevice_->alloc(allocDevice_, size.width, size.height, - halPixelFormat, usage, &handle, &stride); - if (ret) { - LOG(HAL, Error) << "failed buffer allocation: " << ret; + + auto &allocator = android::GraphicBufferAllocator::get(); + android::status_t status = allocator.allocate(size.width, size.height, halPixelFormat, + 1 /*layerCount*/, usage, &handle, &stride, + "libcameraHAL"); + if (status != android::NO_ERROR) { + LOG(HAL, Error) << "failed buffer allocation: " << status; return nullptr; } if (!handle) { @@ -143,7 +119,7 @@ PlatformFrameBufferAllocator::Private::allocate(int halPixelFormat, return std::make_unique( std::make_unique( - allocDevice_, handle, planes), + handle, planes), handle); } diff --git a/src/android/mm/libhardware_stub.c b/src/android/mm/libhardware_stub.c deleted file mode 100644 index 00f15cd90cac..000000000000 --- a/src/android/mm/libhardware_stub.c +++ /dev/null @@ -1,17 +0,0 @@ -/* SPDX-License-Identifier: Apache-2.0 */ -/* - * Copyright (C) 2023, Ideas on Board - * - * libhardware_stub.c - Android libhardware stub for test compilation - */ - -#include - -#include - -int hw_get_module(const char *id __attribute__((__unused__)), - const struct hw_module_t **module) -{ - *module = NULL; - return -ENOTSUP; -} diff --git a/src/android/mm/meson.build b/src/android/mm/meson.build index 4d1fb718e94e..301a2622008b 100644 --- a/src/android/mm/meson.build +++ b/src/android/mm/meson.build @@ -4,14 +4,6 @@ platform = get_option('android_platform') if platform == 'generic' android_hal_sources += files(['generic_camera_buffer.cpp', 'generic_frame_buffer_allocator.cpp']) - android_deps += [libdl] - - libhardware = dependency('libhardware', required : false) - if libhardware.found() - android_deps += [libhardware] - else - android_hal_sources += files(['libhardware_stub.c']) - endif libui = dependency('libui', required : false) if libui.found()