Patch Detail
Show a patch.
GET /api/patches/11210/?format=api
{ "id": 11210, "url": "https://patchwork.libcamera.org/api/patches/11210/?format=api", "web_url": "https://patchwork.libcamera.org/patch/11210/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20210210175830.32038-3-david.plowman@raspberrypi.com>", "date": "2021-02-10T17:58:30", "name": "[libcamera-devel,v2,2/2] ipa: raspberrypi: AWB: Fix race condition setting manual gains", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "8d82539add422a5fda40327c39381eef54d26340", "submitter": { "id": 42, "url": "https://patchwork.libcamera.org/api/people/42/?format=api", "name": "David Plowman", "email": "david.plowman@raspberrypi.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/11210/mbox/", "series": [ { "id": 1674, "url": "https://patchwork.libcamera.org/api/series/1674/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=1674", "date": "2021-02-10T17:58:28", "name": "Raspberry Pi AWB race condition and maintenance", "version": 2, "mbox": "https://patchwork.libcamera.org/series/1674/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/11210/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/11210/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 0E56EBD160\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 10 Feb 2021 17:58:40 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 6C8C861639;\n\tWed, 10 Feb 2021 18:58:39 +0100 (CET)", "from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com\n\t[IPv6:2a00:1450:4864:20::62b])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1452261637\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 10 Feb 2021 18:58:37 +0100 (CET)", "by mail-ej1-x62b.google.com with SMTP id l25so5734612eja.9\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 10 Feb 2021 09:58:37 -0800 (PST)", "from pi4-davidp.lan (plowpeople3.plus.com. [80.229.223.72])\n\tby smtp.gmail.com with ESMTPSA id\n\thc13sm1660483ejc.5.2021.02.10.09.58.35\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tWed, 10 Feb 2021 09:58:35 -0800 (PST)" ], "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"mU8s4a3b\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=s6vHWsx0xIwncY54ZT/xyjBtApBBVfKf/VoaVcjGM1Y=;\n\tb=mU8s4a3bqqGf7UnalEQAUeZigEsEXCOEcWb/1F6/kqi7LQhuJmY1+K/er1Rs++bwLK\n\ty0hoN9jtRA5dtGzRjqxjefkXQpNvLRMfRdzKTMFo+Tqvgr8bcu6qMXz7JemJlGW1+pNF\n\txG3OuxwrGT5kGwUTNIbX3TFlS5CgiYeOl/o8oJ/nqfMDo6K/ECtZc0eHsBdGG+KCGQrn\n\tSZcZZceXrw2uIxT24S/0fYen2k8+LJg2K9V0DhXP2xTzqRUvVaxv2BFFGl6uuF8Yy+Id\n\tIxaCOaBV/YqoG/fZX3zjjK7Ofolh1l7tnQ+8Hs5CQLERR2EP1SmTm9G79ZXwOnRSdDsm\n\tAHuw==", "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=s6vHWsx0xIwncY54ZT/xyjBtApBBVfKf/VoaVcjGM1Y=;\n\tb=crgnM3D9UeIcqfWlbzo78fr0K5tjWY2NwRLqNfPoOkjtMzv/XQM/rwHsdfGmMy332k\n\tNXIVPCmEzwCq4G0jGH8oRcYTZuxj5mg0vT3hxCGP8DQNHRl3yXw9NAk4hT4Fr3skeuy6\n\tLt/Lw3W9vZAEVltAV2Ild6usUGd9xbfueHoxn12VY772Qyg6Kcn9GXesXZ30MGzJJ00G\n\tigITpJwSasH4moi/tOAaqqwobGzC3AyWYtiZyt6ziE+dJpoW6jF/HH6u4kUWvwvRe/tN\n\tZdlo6iwbpJ8FXUt5jLbXiOUS8u1HezmJlD5c9v1QI/vram73w0PSI9/+IKygcWgpWwBE\n\tOHCg==", "X-Gm-Message-State": "AOAM531O8C7o+krqAIEezGEaVcu6KI0ZkCpUOMbS0/hokfrNPmAr/ijt\n\tz1nzEQO+dzlq4O/UJ+1Juj5ygioFtHNn0rrB", "X-Google-Smtp-Source": "ABdhPJye4g6mpZot2BurxR+24IlL90mhoitsCtmTes5NkquBm++w7M0Ji9ZuQ1Qi+/+T8MX7QoqPUA==", "X-Received": "by 2002:a17:906:3656:: with SMTP id\n\tr22mr4059275ejb.14.1612979916360; \n\tWed, 10 Feb 2021 09:58:36 -0800 (PST)", "From": "David Plowman <david.plowman@raspberrypi.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Wed, 10 Feb 2021 17:58:30 +0000", "Message-Id": "<20210210175830.32038-3-david.plowman@raspberrypi.com>", "X-Mailer": "git-send-email 2.20.1", "In-Reply-To": "<20210210175830.32038-1-david.plowman@raspberrypi.com>", "References": "<20210210175830.32038-1-david.plowman@raspberrypi.com>", "MIME-Version": "1.0", "Subject": "[libcamera-devel] [PATCH v2 2/2] ipa: raspberrypi: AWB: Fix race\n\tcondition setting manual gains", "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>", "Content-Type": "text/plain; charset=\"us-ascii\"", "Content-Transfer-Encoding": "7bit", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "Applying the manual_r_ and manual_b_ values is entirely removed from\nthe asynchronous thread where their use constituted a race hazard. The\nmain thread now deals with them entirely, involving the following\nchanges.\n\n1. SetManualGains() applies the new values directly to the\n\"sync_results\", meaning that Prepare() will jump to the new values\nimmediately (which is a better behaviour).\n\n2. Process() does not restart the asynchronous thread when manual\ngains are in force.\n\n3. The asynchronous thread might be running when manual gains are set,\nso we ignore the results produced in this case.\n\nSigned-off-by: David Plowman <david.plowman@raspberrypi.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Naushir Patuck <naush@raspberrypi.com>\n---\n src/ipa/raspberrypi/controller/rpi/awb.cpp | 86 +++++++++++-----------\n src/ipa/raspberrypi/controller/rpi/awb.hpp | 1 +\n 2 files changed, 44 insertions(+), 43 deletions(-)", "diff": "diff --git a/src/ipa/raspberrypi/controller/rpi/awb.cpp b/src/ipa/raspberrypi/controller/rpi/awb.cpp\nindex 1c65eda8..bb637f10 100644\n--- a/src/ipa/raspberrypi/controller/rpi/awb.cpp\n+++ b/src/ipa/raspberrypi/controller/rpi/awb.cpp\n@@ -175,9 +175,9 @@ void Awb::Initialise()\n \n unsigned int Awb::GetConvergenceFrames() const\n {\n-\t// If colour gains have been explicitly set, there is no convergence\n+\t// If not in auto mode, there is no convergence\n \t// to happen, so no need to drop any frames - return zero.\n-\tif (manual_r_ && manual_b_)\n+\tif (!isAutoEnabled())\n \t\treturn 0;\n \telse\n \t\treturn config_.convergence_frames;\n@@ -193,38 +193,47 @@ void Awb::SetManualGains(double manual_r, double manual_b)\n \t// If any of these are 0.0, we swich back to auto.\n \tmanual_r_ = manual_r;\n \tmanual_b_ = manual_b;\n+\t// If not in auto mode, set these values into the sync_results which\n+\t// means that Prepare() will adopt them immediately.\n+\tif (!isAutoEnabled()) {\n+\t\tsync_results_.gain_r = prev_sync_results_.gain_r = manual_r_;\n+\t\tsync_results_.gain_g = prev_sync_results_.gain_g = 1.0;\n+\t\tsync_results_.gain_b = prev_sync_results_.gain_b = manual_b_;\n+\t}\n }\n \n void Awb::SwitchMode([[maybe_unused]] CameraMode const &camera_mode,\n \t\t Metadata *metadata)\n {\n-\t// If fixed colour gains have been set, we should let other algorithms\n-\t// know by writing it into the image metadata.\n-\tif (manual_r_ != 0.0 && manual_b_ != 0.0) {\n-\t\tprev_sync_results_.gain_r = manual_r_;\n-\t\tprev_sync_results_.gain_g = 1.0;\n-\t\tprev_sync_results_.gain_b = manual_b_;\n-\t\t// If we're starting up for the first time, try and\n-\t\t// \"dead reckon\" the corresponding colour temperature.\n-\t\tif (first_switch_mode_ && config_.bayes) {\n-\t\t\tPwl ct_r_inverse = config_.ct_r.Inverse();\n-\t\t\tPwl ct_b_inverse = config_.ct_b.Inverse();\n-\t\t\tdouble ct_r = ct_r_inverse.Eval(ct_r_inverse.Domain().Clip(1 / manual_r_));\n-\t\t\tdouble ct_b = ct_b_inverse.Eval(ct_b_inverse.Domain().Clip(1 / manual_b_));\n-\t\t\tprev_sync_results_.temperature_K = (ct_r + ct_b) / 2;\n-\t\t}\n-\t\tsync_results_ = prev_sync_results_;\n+\t// On the first mode switch we'll have no meaningful colour\n+\t// temperature, so try to dead reckon one if in manual mode.\n+\tif (!isAutoEnabled() && first_switch_mode_ && config_.bayes) {\n+\t\tPwl ct_r_inverse = config_.ct_r.Inverse();\n+\t\tPwl ct_b_inverse = config_.ct_b.Inverse();\n+\t\tdouble ct_r = ct_r_inverse.Eval(ct_r_inverse.Domain().Clip(1 / manual_r_));\n+\t\tdouble ct_b = ct_b_inverse.Eval(ct_b_inverse.Domain().Clip(1 / manual_b_));\n+\t\tprev_sync_results_.temperature_K = (ct_r + ct_b) / 2;\n+\t\tsync_results_.temperature_K = prev_sync_results_.temperature_K;\n \t}\n+\t// Let other algorithms know the current white balance values.\n \tmetadata->Set(\"awb.status\", prev_sync_results_);\n \tfirst_switch_mode_ = false;\n }\n \n+bool Awb::isAutoEnabled() const\n+{\n+\treturn manual_r_ == 0.0 || manual_b_ == 0.0;\n+}\n+\n void Awb::fetchAsyncResults()\n {\n \tLOG(RPiAwb, Debug) << \"Fetch AWB results\";\n \tasync_finished_ = false;\n \tasync_started_ = false;\n-\tsync_results_ = async_results_;\n+\t// It's possible manual gains could be set even while the async\n+\t// thread was running, so only copy the results if still in auto mode.\n+\tif (isAutoEnabled())\n+\t\tsync_results_ = async_results_;\n }\n \n void Awb::restartAsync(StatisticsPtr &stats, double lux)\n@@ -289,8 +298,10 @@ void Awb::Process(StatisticsPtr &stats, Metadata *image_metadata)\n \tif (frame_phase_ < (int)config_.frame_period)\n \t\tframe_phase_++;\n \tLOG(RPiAwb, Debug) << \"frame_phase \" << frame_phase_;\n-\tif (frame_phase_ >= (int)config_.frame_period ||\n-\t frame_count_ < (int)config_.startup_frames) {\n+\t// We do not restart the async thread if we're not in auto mode.\n+\tif (isAutoEnabled() &&\n+\t (frame_phase_ >= (int)config_.frame_period ||\n+\t frame_count_ < (int)config_.startup_frames)) {\n \t\t// Update any settings and any image metadata that we need.\n \t\tstruct LuxStatus lux_status = {};\n \t\tlux_status.lux = 400; // in case no metadata\n@@ -614,29 +625,18 @@ void Awb::awbGrey()\n \n void Awb::doAwb()\n {\n-\tif (manual_r_ != 0.0 && manual_b_ != 0.0) {\n-\t\tasync_results_.temperature_K = 4500; // don't know what it is\n-\t\tasync_results_.gain_r = manual_r_;\n-\t\tasync_results_.gain_g = 1.0;\n-\t\tasync_results_.gain_b = manual_b_;\n+\tprepareStats();\n+\tLOG(RPiAwb, Debug) << \"Valid zones: \" << zones_.size();\n+\tif (zones_.size() > config_.min_regions) {\n+\t\tif (config_.bayes)\n+\t\t\tawbBayes();\n+\t\telse\n+\t\t\tawbGrey();\n \t\tLOG(RPiAwb, Debug)\n-\t\t\t<< \"Using manual white balance: gain_r \"\n-\t\t\t<< async_results_.gain_r << \" gain_b \"\n-\t\t\t<< async_results_.gain_b;\n-\t} else {\n-\t\tprepareStats();\n-\t\tLOG(RPiAwb, Debug) << \"Valid zones: \" << zones_.size();\n-\t\tif (zones_.size() > config_.min_regions) {\n-\t\t\tif (config_.bayes)\n-\t\t\t\tawbBayes();\n-\t\t\telse\n-\t\t\t\tawbGrey();\n-\t\t\tLOG(RPiAwb, Debug)\n-\t\t\t\t<< \"CT found is \"\n-\t\t\t\t<< async_results_.temperature_K\n-\t\t\t\t<< \" with gains r \" << async_results_.gain_r\n-\t\t\t\t<< \" and b \" << async_results_.gain_b;\n-\t\t}\n+\t\t\t<< \"CT found is \"\n+\t\t\t<< async_results_.temperature_K\n+\t\t\t<< \" with gains r \" << async_results_.gain_r\n+\t\t\t<< \" and b \" << async_results_.gain_b;\n \t}\n }\n \ndiff --git a/src/ipa/raspberrypi/controller/rpi/awb.hpp b/src/ipa/raspberrypi/controller/rpi/awb.hpp\nindex f113c642..45ba9e25 100644\n--- a/src/ipa/raspberrypi/controller/rpi/awb.hpp\n+++ b/src/ipa/raspberrypi/controller/rpi/awb.hpp\n@@ -108,6 +108,7 @@ public:\n \t};\n \n private:\n+\tbool isAutoEnabled() const;\n \t// configuration is read-only, and available to both threads\n \tAwbConfig config_;\n \tstd::thread async_thread_;\n", "prefixes": [ "libcamera-devel", "v2", "2/2" ] }