xiaoxiang781216 commented on code in PR #3801: URL: https://github.com/apache/nuttx-apps/pull/3801#discussion_r4107568912
########## graphics/tinygl/0003-zbuffer-do-not-over-read-the-source-in-ZB_copyFrameB.patch: ########## @@ -0,0 +1,55 @@ +From 825d0f3cda7f466a4150e42bc42bf615a3c48cc6 Mon Sep 17 00:00:00 2001 +From: Alan Carvalho de Assis <[email protected]> +Date: Thu, 24 Sep 2026 09:47:54 -0300 +Subject: [PATCH] zbuffer: do not over-read the source in ZB_copyFrameBuffer() + +ZB_copyBuffer() copied `linesize` bytes per line, where linesize is the +pitch of the destination buffer. When the destination is wider than +the TinyGL buffer (e.g. a hardware framebuffer whose lines are padded), +this read past the end of every source line and, on the last line, past +the end of the TinyGL pixel buffer. + +Copy only the visible width of a TinyGL line, clamped to the +destination pitch. +--- + src/zbuffer.c | 10 ++++++++-- + 1 file changed, 8 insertions(+), 2 deletions(-) + +diff --git a/src/zbuffer.c b/src/zbuffer.c +index 6321340..0fc9e01 100644 +--- a/src/zbuffer.c ++++ b/src/zbuffer.c +@@ -116,6 +116,12 @@ void ZB_resize(ZBuffer* zb, void* frame_buffer, GLint xsize, GLint ysize) { + + static void ZB_copyBuffer(ZBuffer* zb, void* buf, GLint linesize) { + GLint y, i; ++ /* Only copy the visible part of each line: the destination pitch ++ (linesize) may be larger than a TinyGL line, e.g. for a padded ++ hardware framebuffer. */ ++ GLint copysize = zb->xsize * PSZB; ++ if (copysize > linesize) ++ copysize = linesize; Review Comment: why not change linesize instead ########## examples/tinygl/tinygl_main.c: ########## @@ -0,0 +1,532 @@ +/**************************************************************************** + * apps/examples/tinygl/tinygl_main.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. + * + ****************************************************************************/ + +/* The gear drawing code is based on the classic "gears" demo by Brian Paul, + * which is in the public domain, as shipped with TinyGL in + * Raw_Demos/gears.c. + */ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <sys/ioctl.h> +#include <sys/mman.h> +#include <sys/time.h> + +#include <errno.h> +#include <fcntl.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <nuttx/video/fb.h> + +#include <GL/gl.h> +#include <zbuffer.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef M_PI +# define M_PI 3.14159265358979323846 +#endif + +#if TGL_FEATURE_RENDER_BITS == 32 +# define TINYGL_FB_FMT FB_FMT_RGB32 +# define TINYGL_ZB_MODE ZB_MODE_RGBA +#else +# define TINYGL_FB_FMT FB_FMT_RGB16_565 +# define TINYGL_ZB_MODE ZB_MODE_5R6G5B +#endif + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct tinygl_fb_s +{ + int fd; + FAR uint8_t *fbmem; + struct fb_videoinfo_s vinfo; + struct fb_planeinfo_s pinfo; +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static GLfloat g_view_rotx = 20.0f; +static GLfloat g_view_roty = 30.0f; +static GLfloat g_angle; +static GLint g_gear1; +static GLint g_gear2; +static GLint g_gear3; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: gear + * + * Description: + * Draw a gear wheel into the current display list. + * + * Input Parameters: + * inner_radius - radius of the hole at the center + * outer_radius - radius at the center of the teeth + * width - width of the gear + * teeth - number of teeth + * tooth_depth - depth of a tooth + * + ****************************************************************************/ + +static void gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width, + GLint teeth, GLfloat tooth_depth) +{ + GLfloat r0 = inner_radius; + GLfloat r1 = outer_radius - tooth_depth / 2.0f; + GLfloat r2 = outer_radius + tooth_depth / 2.0f; + GLfloat da = 2.0f * M_PI / teeth / 4.0f; + GLfloat angle; + GLfloat u; + GLfloat v; + GLfloat len; + GLint i; + + glNormal3f(0.0f, 0.0f, 1.0f); + + /* Front face */ + + glBegin(GL_QUAD_STRIP); + for (i = 0; i <= teeth; i++) + { + angle = i * 2.0f * M_PI / teeth; + glVertex3f(r0 * cosf(angle), r0 * sinf(angle), width * 0.5f); + glVertex3f(r1 * cosf(angle), r1 * sinf(angle), width * 0.5f); + glVertex3f(r0 * cosf(angle), r0 * sinf(angle), width * 0.5f); + glVertex3f(r1 * cosf(angle + 3 * da), r1 * sinf(angle + 3 * da), + width * 0.5f); + } + + glEnd(); + + /* Front sides of the teeth */ + + glBegin(GL_QUADS); + for (i = 0; i < teeth; i++) + { + angle = i * 2.0f * M_PI / teeth; + glVertex3f(r1 * cosf(angle), r1 * sinf(angle), width * 0.5f); + glVertex3f(r2 * cosf(angle + da), r2 * sinf(angle + da), + width * 0.5f); + glVertex3f(r2 * cosf(angle + 2 * da), r2 * sinf(angle + 2 * da), + width * 0.5f); + glVertex3f(r1 * cosf(angle + 3 * da), r1 * sinf(angle + 3 * da), + width * 0.5f); + } + + glEnd(); + + glNormal3f(0.0f, 0.0f, -1.0f); + + /* Back face */ + + glBegin(GL_QUAD_STRIP); + for (i = 0; i <= teeth; i++) + { + angle = i * 2.0f * M_PI / teeth; + glVertex3f(r1 * cosf(angle), r1 * sinf(angle), -width * 0.5f); + glVertex3f(r0 * cosf(angle), r0 * sinf(angle), -width * 0.5f); + glVertex3f(r1 * cosf(angle + 3 * da), r1 * sinf(angle + 3 * da), + -width * 0.5f); + glVertex3f(r0 * cosf(angle), r0 * sinf(angle), -width * 0.5f); + } + + glEnd(); + + /* Back sides of the teeth */ + + glBegin(GL_QUADS); + for (i = 0; i < teeth; i++) + { + angle = i * 2.0f * M_PI / teeth; + glVertex3f(r1 * cosf(angle + 3 * da), r1 * sinf(angle + 3 * da), + -width * 0.5f); + glVertex3f(r2 * cosf(angle + 2 * da), r2 * sinf(angle + 2 * da), + -width * 0.5f); + glVertex3f(r2 * cosf(angle + da), r2 * sinf(angle + da), + -width * 0.5f); + glVertex3f(r1 * cosf(angle), r1 * sinf(angle), -width * 0.5f); + } + + glEnd(); + + /* Outward faces of the teeth */ + + glBegin(GL_QUAD_STRIP); + for (i = 0; i < teeth; i++) + { + angle = i * 2.0f * M_PI / teeth; + glVertex3f(r1 * cosf(angle), r1 * sinf(angle), width * 0.5f); + glVertex3f(r1 * cosf(angle), r1 * sinf(angle), -width * 0.5f); + u = r2 * cosf(angle + da) - r1 * cosf(angle); + v = r2 * sinf(angle + da) - r1 * sinf(angle); + len = sqrtf(u * u + v * v); + u /= len; + v /= len; + glNormal3f(v, -u, 0.0f); + glVertex3f(r2 * cosf(angle + da), r2 * sinf(angle + da), + width * 0.5f); + glVertex3f(r2 * cosf(angle + da), r2 * sinf(angle + da), + -width * 0.5f); + glNormal3f(cosf(angle), sinf(angle), 0.0f); + glVertex3f(r2 * cosf(angle + 2 * da), r2 * sinf(angle + 2 * da), + width * 0.5f); + glVertex3f(r2 * cosf(angle + 2 * da), r2 * sinf(angle + 2 * da), + -width * 0.5f); + u = r1 * cosf(angle + 3 * da) - r2 * cosf(angle + 2 * da); + v = r1 * sinf(angle + 3 * da) - r2 * sinf(angle + 2 * da); + glNormal3f(v, -u, 0.0f); + glVertex3f(r1 * cosf(angle + 3 * da), r1 * sinf(angle + 3 * da), + width * 0.5f); + glVertex3f(r1 * cosf(angle + 3 * da), r1 * sinf(angle + 3 * da), + -width * 0.5f); + glNormal3f(cosf(angle), sinf(angle), 0.0f); + } + + glVertex3f(r1, 0.0f, width * 0.5f); + glVertex3f(r1, 0.0f, -width * 0.5f); + glEnd(); + + /* Inside radius cylinder */ + + glBegin(GL_QUAD_STRIP); + for (i = 0; i <= teeth; i++) + { + angle = i * 2.0f * M_PI / teeth; + glNormal3f(-cosf(angle), -sinf(angle), 0.0f); + glVertex3f(r0 * cosf(angle), r0 * sinf(angle), -width * 0.5f); + glVertex3f(r0 * cosf(angle), r0 * sinf(angle), width * 0.5f); + } + + glEnd(); +} + +/**************************************************************************** + * Name: gears_init + ****************************************************************************/ + +static void gears_init(int width, int height) +{ + static GLfloat pos[4] = Review Comment: add const to all static variables -- 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]
