xiaoxiang781216 commented on code in PR #3867:
URL: https://github.com/apache/nuttx-apps/pull/3867#discussion_r4306040103


##########
examples/lvgldemo/lv_port_fbdev.c:
##########
@@ -0,0 +1,424 @@
+/****************************************************************************
+ * apps/examples/lvgldemo/lv_port_fbdev.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <nuttx/video/fb.h>
+#include <lvgl/lvgl.h>
+
+#include "lv_port_fbdev.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifndef CONFIG_EXAMPLES_LVGLDEMO_FBDEV_PATH
+#  define CONFIG_EXAMPLES_LVGLDEMO_FBDEV_PATH "/dev/fb0"
+#endif
+
+#if (LV_COLOR_DEPTH != 32) && (LV_COLOR_DEPTH != 16)
+#  error "Only LV_COLOR_DEPTH 32 or 16 is supported by lv_port_fbdev"
+#endif
+
+/* SAFEGUARD STEP 3: RAM Sizing
+ * Partial buffer (40 rows) to prevent heap exhaustion.
+ * 1024 width * 40 lines * 4 bytes/pixel = ~160 KB RAM
+ */
+
+#define BUFFER_ROWS 40
+#define MAX_HOR_RES 1024
+#define BUF_BYTES   (MAX_HOR_RES * BUFFER_ROWS * (LV_COLOR_DEPTH / 8))
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static int g_fbfd = -1;
+static uint8_t *g_fb_mem = NULL;
+static struct fb_videoinfo_s g_vinfo;
+static struct fb_planeinfo_s g_pinfo;
+static lv_display_t *g_disp = NULL;
+
+static uint8_t g_buf_1[BUF_BYTES] aligned_data(LV_DRAW_BUF_ALIGN);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fbdev_color_probe_test
+ *
+ * Description:
+ *   SAFEGUARD STEP 1: The Color Probe Test
+ *   Writes 3 test pixels directly into mapped framebuffer memory:
+ *     32-bit: Red=0xffff0000, Green=0xff00ff00, Blue=0xff0000ff
+ *     16-bit: Red=0xf800,     Green=0x07e0,     Blue=0x001f
+ ****************************************************************************/
+
+#ifdef CONFIG_EXAMPLES_LVGLDEMO_COLOR_PROBE
+static void fbdev_color_probe_test(void)
+{
+  if (g_fb_mem == NULL)
+    {
+      return;
+    }
+
+  if (g_pinfo.bpp == 32)
+    {
+      uint32_t *probe = (uint32_t *)g_fb_mem;
+
+      /* Probe 0: Red test pixel (ARGB32) */
+
+      probe[0] = 0xffff0000;
+
+      /* Probe 1: Green test pixel (ARGB32) */
+
+      probe[1] = 0xff00ff00;
+
+      /* Probe 2: Blue test pixel (ARGB32) */
+
+      probe[2] = 0xff0000ff;
+    }
+  else if (g_pinfo.bpp == 16)
+    {
+      uint16_t *probe = (uint16_t *)g_fb_mem;
+
+      /* Probe 0: Red test pixel (RGB565) */
+
+      probe[0] = 0xf800;
+
+      /* Probe 1: Green test pixel (RGB565) */
+
+      probe[1] = 0x07e0;
+
+      /* Probe 2: Blue test pixel (RGB565) */
+
+      probe[2] = 0x001f;
+    }
+
+  LV_LOG_USER("[SAFEGUARD] Color probe test written: 3 pixels\n");
+}
+#endif
+
+/****************************************************************************
+ * Name: fbdev_flush
+ *
+ * Description:
+ *   SAFEGUARD STEP 2: Smart Flush Callback with Byte-Order Adaptation
+ *   - Copies line-by-line using hardware stride and visible offsets.
+ *   - Matches writes to reported bpp (32-bit vs 16-bit).
+ *   - Forces opaque alpha (0xff) on 32-bit to prevent black screen traps.
+ ****************************************************************************/
+
+static void fbdev_flush(lv_display_t *disp, const lv_area_t *area,
+                        uint8_t *px_map)
+{
+  lv_coord_t w = lv_area_get_width(area);
+  lv_coord_t y;
+  uint8_t px_size;
+
+  if (g_fb_mem == NULL)
+    {
+      lv_display_flush_ready(disp);
+      return;
+    }
+
+  px_size = lv_color_format_get_size(lv_display_get_color_format(disp));
+
+  if (g_pinfo.bpp == 32)
+    {
+      for (y = area->y1; y <= area->y2; y++)
+        {
+          uint32_t *dst = (uint32_t *)(g_fb_mem +
+                                        ((y + g_pinfo.yoffset) *
+                                         g_pinfo.stride)) +
+                          (area->x1 + g_pinfo.xoffset);
+
+          if (px_size == 4)
+            {
+              uint32_t *src = (uint32_t *)px_map;
+              lv_coord_t x;
+
+              for (x = 0; x < w; x++)
+                {
+                  uint32_t c = src[x];
+
+                  /* LVGL 8.3.2 produces BGRA in memory on little-endian
+                   * ARM64. Extract color components:
+                   */
+
+                  uint8_t b = (c >> 0)  & 0xff;
+                  uint8_t g = (c >> 8)  & 0xff;
+                  uint8_t r = (c >> 16) & 0xff;
+
+                  /* Swap Red and Blue channels to match RGBA/ARGB hardware,
+                   * and force Opaque Alpha (0xff) to prevent black screen.
+                   */
+
+                  dst[x] = 0xff000000u |
+                           ((uint32_t)b << 16) |
+                           ((uint32_t)g << 8) |
+                           (uint32_t)r;
+                }
+            }
+          else if (px_size == 2)
+            {
+              uint16_t *src = (uint16_t *)px_map;
+              lv_coord_t x;
+
+              for (x = 0; x < w; x++)
+                {
+                  uint16_t c = src[x];
+                  uint8_t r = ((c >> 11) & 0x1f) * 255 / 31;
+                  uint8_t g = ((c >> 5) & 0x3f) * 255 / 63;
+                  uint8_t b = (c & 0x1f) * 255 / 31;
+
+                  dst[x] = 0xff000000u | ((uint32_t)r << 16) |
+                           ((uint32_t)g << 8) | (uint32_t)b;
+                }
+            }
+
+          px_map += w * px_size;
+        }
+    }
+  else if (g_pinfo.bpp == 16)
+    {
+      for (y = area->y1; y <= area->y2; y++)
+        {
+          uint16_t *dst = (uint16_t *)(g_fb_mem +
+                                        ((y + g_pinfo.yoffset) *
+                                         g_pinfo.stride)) +
+                          (area->x1 + g_pinfo.xoffset);
+
+          if (px_size == 2)
+            {
+              memcpy(dst, px_map, w * sizeof(uint16_t));
+            }
+          else if (px_size == 4)
+            {
+              uint32_t *src = (uint32_t *)px_map;
+              lv_coord_t x;
+
+              for (x = 0; x < w; x++)
+                {
+                  uint32_t c = src[x];
+                  uint8_t r = (c >> 16) & 0xff;
+                  uint8_t g = (c >> 8) & 0xff;
+                  uint8_t b = c & 0xff;
+
+                  dst[x] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
+                }
+            }
+
+          px_map += w * px_size;
+        }
+    }
+
+#if defined(CONFIG_FB_UPDATE) && defined(FBIO_UPDATE)
+  /* Trigger hardware display update if driver supports it */
+
+  struct fb_area_s fb_area;
+
+  fb_area.x = area->x1;
+  fb_area.y = area->y1;
+  fb_area.w = w;
+  fb_area.h = lv_area_get_height(area);
+  ioctl(g_fbfd, FBIO_UPDATE, (unsigned long)((uintptr_t)&fb_area));
+#endif
+
+  lv_display_flush_ready(disp);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: lv_port_disp_init
+ ****************************************************************************/
+
+lv_display_t *lv_port_disp_init(void)

Review Comment:
   why not fix the official nuttx fb driver in lvgl.git directly



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to