{"id":18988,"url":"https://patchwork.libcamera.org/api/patches/18988/?format=json","web_url":"https://patchwork.libcamera.org/patch/18988/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20230912-gralloc-api-v4-v1-4-0f80402d8e7a@baylibre.com>","date":"2023-09-12T09:36:13","name":"[libcamera-devel,4/4] android: mm: generic: Use GraphicBufferAllocator instead of gralloc.h","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"f5bc53f1a3840776c95db81954db4f27119a4889","submitter":{"id":153,"url":"https://patchwork.libcamera.org/api/people/153/?format=json","name":"Mattijs Korpershoek","email":"mkorpershoek@baylibre.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/18988/mbox/","series":[{"id":4016,"url":"https://patchwork.libcamera.org/api/series/4016/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4016","date":"2023-09-12T09:36:09","name":"android: switch over to modern gralloc API via libui","version":1,"mbox":"https://patchwork.libcamera.org/series/4016/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18988/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18988/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 9E1C2C32B2\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 12 Sep 2023 09:36:25 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0E891628F3;\n\tTue, 12 Sep 2023 11:36:25 +0200 (CEST)","from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com\n\t[IPv6:2a00:1450:4864:20::32b])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E2CA4628E9\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 12 Sep 2023 11:36:21 +0200 (CEST)","by mail-wm1-x32b.google.com with SMTP id\n\t5b1f17b1804b1-401d10e3e54so58665735e9.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 12 Sep 2023 02:36:21 -0700 (PDT)","from [192.168.2.39] ([82.66.159.240])\n\tby smtp.gmail.com with ESMTPSA id\n\ty10-20020a05600c364a00b00401b242e2e6sm15578253wmq.47.2023.09.12.02.36.20\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 12 Sep 2023 02:36:20 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1694511385;\n\tbh=IvrwO4X8OhwwBanp8/xv9uVqcURnEWKjBFJLEhmQ/08=;\n\th=Date:References:In-Reply-To:To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=a6P64C35cKVxCv2ORRbHWpEOhqyutnZ3sg0Pl4IpgfmVBLV0LoyfclTjD1i7mg19Z\n\tPASSqsWwr9lhJyZvAMn7humvoVxpTqHB9pZ6KcOCLag9Qs+tRqZhflR6WAjPr11jZs\n\t7JJBGA/ZHSQeXKzdKvINjaHQTRQM8TGqHFtmj3ulTDdwqD8dMcN85w+8wEf/pjbkNJ\n\tvrw9IBuoyB3PEvA/QZfCOOkW/8PWXH9/mwsidrqpHrZDSIOWN1sjYbguWw8ReEH6Zy\n\tbtINewUwjlkEdWt/z2jytj6impYiOr/fRmk11Jx8DXUW6gVrexyShYxuTYto3+SIpe\n\tJlM9WyvBATJmA==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1694511381;\n\tx=1695116181; darn=lists.libcamera.org; \n\th=cc:to:in-reply-to:references:message-id:content-transfer-encoding\n\t:mime-version:subject:date:from:from:to:cc:subject:date:message-id\n\t:reply-to; bh=TL7NM1hWUBHdqI96CSO+Pt6HsQq7zw2HF+zyFI8s/0Q=;\n\tb=Gzg5zL7JEQGmsQIIplJv+uvz/QP37k4DjuoTjs6w5HbxqAk+X7FKUsOI+xkedHZXuk\n\tC5Igr9ETO0omgzo0rLeVNPM6I0LFEVQnS/UwdKylWtIkHaHyRmfBIGo1nNM4RKBktHoj\n\tZUxm93yK7sB5MEVvuHJmcxFgFBOT0zdRYuzzYU3FPF5yduQ/onaPelLm30e3s2Bdq7BU\n\tq67ntywmF6tS4xOUaKAP7wyH3k4ItgbUvriap1PzzHUurq9/ypIMeJnofaGFCaxBNoBQ\n\tyqAhHfCwqRoW43GmFIYvXkNqDJq5io5Ha21Rnc7Ne/3Lv5qUpsIbdMO152ZSYSRetTMp\n\tF3XA=="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected)\n\theader.d=baylibre-com.20230601.gappssmtp.com\n\theader.i=@baylibre-com.20230601.gappssmtp.com header.b=\"Gzg5zL7J\"; \n\tdkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1694511381; x=1695116181;\n\th=cc:to:in-reply-to:references:message-id:content-transfer-encoding\n\t:mime-version:subject:date:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=TL7NM1hWUBHdqI96CSO+Pt6HsQq7zw2HF+zyFI8s/0Q=;\n\tb=dOAi4Me8Lc/3EfJyMsE49r5EZGNR1Si1Xe2ROn8NOpSN/UKVdzibYhsZ5e+5n6J5LE\n\t0/JN15y2uMi3bkGOza8nj7htvq8KDFkp89W/eUTPTnB6ZMpA3m+zT3HjusOTSTHgkAwW\n\t1pfapWkQPNPJb7smb5Q1L0OgUdsEFaPwKP7iGxZncgmKYKoQe+j0MLeZBQnUl4yw81iw\n\tcsIxtd6/ex+YQLomEk/KcXddNI8pNsX3oWYoqBrFaH9XnIfJlAOglrQR6lzHjE6PhxOX\n\txL53V8zknJ1NWH3tkV6pQv/FPaPWylScIi55RexO0mBQJuB0G0W6QHxNFts2J6hndS0p\n\tfb/Q==","X-Gm-Message-State":"AOJu0YyctqUmG6VGMk6281cSfXOsO14jq2BN40fgLtbsd8tysYAHcw61\n\tl/GG978qXdj92tAhuDXRW+E2z0uaKA9KdBBGsfQ=","X-Google-Smtp-Source":"AGHT+IEayMRM2B6/PzAW8ain3P7K15oU4PK6db3MHiigRckU7wzWRand3pzLe5rWo/SFHtMrQfdpFw==","X-Received":"by 2002:a05:600c:245:b0:3fb:e2af:49f6 with SMTP id\n\t5-20020a05600c024500b003fbe2af49f6mr10369367wmj.39.1694511381266; \n\tTue, 12 Sep 2023 02:36:21 -0700 (PDT)","Date":"Tue, 12 Sep 2023 11:36:13 +0200","MIME-Version":"1.0","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"7bit","Message-Id":"<20230912-gralloc-api-v4-v1-4-0f80402d8e7a@baylibre.com>","References":"<20230912-gralloc-api-v4-v1-0-0f80402d8e7a@baylibre.com>","In-Reply-To":"<20230912-gralloc-api-v4-v1-0-0f80402d8e7a@baylibre.com>","To":"libcamera-devel@lists.libcamera.org","X-Mailer":"b4 0.12.3","Subject":"[libcamera-devel] [PATCH 4/4] android: mm: generic: Use\n\tGraphicBufferAllocator instead of gralloc.h","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Mattijs Korpershoek via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Mattijs Korpershoek <mkorpershoek@baylibre.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"gralloc.h is a very old API that has been deprecated at least since\nAndroid P (9).\n\nSwitch over to a higher level abstraction of gralloc from libui, which\nis compatible with Android 11 and up.\nLibui:\n* is provided in the VNDK (so it's available to vendors).\n* is also used in the camera vts test named VtsAidlHalCameraProvider_TargetTest.\n\nDrop the libhardware stub since we no longer need it.\n\nNote: GraphicsBufferAllocator being a Singleton, buffer lifecycle\nmanagement is easier.\n\nSigned-off-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>\n---\n src/android/mm/generic_frame_buffer_allocator.cpp | 58 +++++++----------------\n src/android/mm/libhardware_stub.c                 | 17 -------\n src/android/mm/meson.build                        |  8 ++--\n 3 files changed, 20 insertions(+), 63 deletions(-)","diff":"diff --git a/src/android/mm/generic_frame_buffer_allocator.cpp b/src/android/mm/generic_frame_buffer_allocator.cpp\nindex 7ecef2c669df..43e1539aac89 100644\n--- a/src/android/mm/generic_frame_buffer_allocator.cpp\n+++ b/src/android/mm/generic_frame_buffer_allocator.cpp\n@@ -16,8 +16,8 @@\n #include \"libcamera/internal/framebuffer.h\"\n \n #include <hardware/camera3.h>\n-#include <hardware/gralloc.h>\n-#include <hardware/hardware.h>\n+#include <ui/GraphicBufferAllocator.h>\n+#include <utils/Errors.h>\n \n #include \"../camera_device.h\"\n #include \"../frame_buffer_allocator.h\"\n@@ -33,35 +33,28 @@ class GenericFrameBufferData : public FrameBuffer::Private\n \tLIBCAMERA_DECLARE_PUBLIC(FrameBuffer)\n \n public:\n-\tGenericFrameBufferData(struct alloc_device_t *allocDevice,\n+\tGenericFrameBufferData(android::GraphicBufferAllocator &allocDevice,\n \t\t\t       buffer_handle_t handle,\n \t\t\t       const std::vector<FrameBuffer::Plane> &planes)\n \t\t: FrameBuffer::Private(planes), allocDevice_(allocDevice),\n \t\t  handle_(handle)\n \t{\n-\t\tASSERT(allocDevice_);\n \t\tASSERT(handle_);\n \t}\n \n \t~GenericFrameBufferData() override\n \t{\n \t\t/*\n-\t\t * allocDevice_ is used to destroy handle_. allocDevice_ is\n-\t\t * owned by PlatformFrameBufferAllocator::Private.\n-\t\t * GenericFrameBufferData must be destroyed before it is\n-\t\t * destroyed.\n-\t\t *\n-\t\t * \\todo Consider managing alloc_device_t with std::shared_ptr\n-\t\t * if this is difficult to maintain.\n-\t\t *\n \t\t * \\todo Thread safety against alloc_device_t is not documented.\n \t\t * Is it no problem to call alloc/free in parallel?\n \t\t */\n-\t\tallocDevice_->free(allocDevice_, handle_);\n+\t\tandroid::status_t status = allocDevice_.free(handle_);\n+\t\tif (status != android::NO_ERROR)\n+\t\t\tLOG(HAL, Error) << \"Error freeing framebuffer: \" << status;\n \t}\n \n private:\n-\tstruct alloc_device_t *allocDevice_;\n+\tandroid::GraphicBufferAllocator &allocDevice_;\n \tconst buffer_handle_t handle_;\n };\n } /* namespace */\n@@ -73,50 +66,33 @@ class PlatformFrameBufferAllocator::Private : public Extensible::Private\n public:\n \tPrivate(CameraDevice *const cameraDevice)\n \t\t: cameraDevice_(cameraDevice),\n-\t\t  hardwareModule_(nullptr),\n-\t\t  allocDevice_(nullptr)\n+\t\t  allocDevice_(android::GraphicBufferAllocator::get())\n \t{\n-\t\thw_get_module(GRALLOC_HARDWARE_MODULE_ID, &hardwareModule_);\n-\t\tASSERT(hardwareModule_);\n \t}\n \n-\t~Private() override;\n+\t~Private() = default;\n \n \tstd::unique_ptr<HALFrameBuffer>\n \tallocate(int halPixelFormat, const libcamera::Size &size, uint32_t usage);\n \n private:\n \tconst CameraDevice *const cameraDevice_;\n-\tconst struct hw_module_t *hardwareModule_;\n-\tstruct alloc_device_t *allocDevice_;\n+\tandroid::GraphicBufferAllocator &allocDevice_;\n };\n \n-PlatformFrameBufferAllocator::Private::~Private()\n-{\n-\tif (allocDevice_)\n-\t\tgralloc_close(allocDevice_);\n-\tdlclose(hardwareModule_->dso);\n-}\n-\n std::unique_ptr<HALFrameBuffer>\n PlatformFrameBufferAllocator::Private::allocate(int halPixelFormat,\n \t\t\t\t\t\tconst libcamera::Size &size,\n \t\t\t\t\t\tuint32_t usage)\n {\n-\tif (!allocDevice_) {\n-\t\tint ret = gralloc_open(hardwareModule_, &allocDevice_);\n-\t\tif (ret) {\n-\t\t\tLOG(HAL, Fatal) << \"gralloc_open() failed: \" << ret;\n-\t\t\treturn nullptr;\n-\t\t}\n-\t}\n-\n-\tint stride = 0;\n+\tuint32_t stride = 0;\n \tbuffer_handle_t handle = nullptr;\n-\tint ret = allocDevice_->alloc(allocDevice_, size.width, size.height,\n-\t\t\t\t      halPixelFormat, usage, &handle, &stride);\n-\tif (ret) {\n-\t\tLOG(HAL, Error) << \"failed buffer allocation: \" << ret;\n+\n+\tandroid::status_t status = allocDevice_.allocate(size.width, size.height, halPixelFormat,\n+\t\t\t\t\t\t\t 1 /*layerCount*/, usage, &handle, &stride,\n+\t\t\t\t\t\t\t \"libcameraHAL\");\n+\tif (status != android::NO_ERROR) {\n+\t\tLOG(HAL, Error) << \"failed buffer allocation: \" << status;\n \t\treturn nullptr;\n \t}\n \tif (!handle) {\ndiff --git a/src/android/mm/libhardware_stub.c b/src/android/mm/libhardware_stub.c\ndeleted file mode 100644\nindex 00f15cd90cac..000000000000\n--- a/src/android/mm/libhardware_stub.c\n+++ /dev/null\n@@ -1,17 +0,0 @@\n-/* SPDX-License-Identifier: Apache-2.0 */\n-/*\n- * Copyright (C) 2023, Ideas on Board\n- *\n- * libhardware_stub.c - Android libhardware stub for test compilation\n- */\n-\n-#include <errno.h>\n-\n-#include <hardware/hardware.h>\n-\n-int hw_get_module(const char *id __attribute__((__unused__)),\n-\t\t  const struct hw_module_t **module)\n-{\n-\t*module = NULL;\n-\treturn -ENOTSUP;\n-}\ndiff --git a/src/android/mm/meson.build b/src/android/mm/meson.build\nindex e9ceb3afba67..203b8c3e5804 100644\n--- a/src/android/mm/meson.build\n+++ b/src/android/mm/meson.build\n@@ -4,13 +4,11 @@ platform = get_option('android_platform')\n if platform == 'generic'\n     android_hal_sources += files(['generic_camera_buffer.cpp',\n                                   'generic_frame_buffer_allocator.cpp'])\n-    android_deps += [libdl]\n \n-    libhardware = dependency('libhardware', required : false)\n-    if libhardware.found()\n-        android_deps += [libhardware]\n+    libui = dependency('libui', required : false)\n+    if libui.found()\n+        android_deps += [libui]\n     else\n-        android_hal_sources += files(['libhardware_stub.c'])\n         android_hal_sources += files(['graphic_buffer_allocator_stub.cpp'])\n     endif\n elif platform == 'cros'\n","prefixes":["libcamera-devel","4/4"]}