2008-11-27 23:56:45 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* graphics/nxbe/nxbe_clipper.c
|
|
|
|
*
|
2021-02-05 11:12:53 +01:00
|
|
|
* 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
|
2008-11-27 23:56:45 +01:00
|
|
|
*
|
2021-02-05 11:12:53 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-27 23:56:45 +01:00
|
|
|
*
|
2021-02-05 11:12:53 +01:00
|
|
|
* 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.
|
2008-11-27 23:56:45 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2009-12-15 19:12:29 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2008-11-27 23:56:45 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2013-03-09 22:12:20 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2011-07-24 22:49:01 +02:00
|
|
|
#include <nuttx/nx/nxglib.h>
|
2013-03-09 22:12:20 +01:00
|
|
|
|
2008-11-27 23:56:45 +01:00
|
|
|
#include "nxbe.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-04-08 16:32:05 +02:00
|
|
|
* Pre-processor Definitions
|
2008-11-27 23:56:45 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define NX_INITIAL_STACKSIZE (32)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure is a container that retains an associated between a
|
|
|
|
* window instance and a rectangle.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct nxbe_cliprect_s
|
|
|
|
{
|
|
|
|
FAR struct nxbe_window_s *wnd;
|
|
|
|
struct nxgl_rect_s rect;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This is the stack of pending clip operations */
|
|
|
|
|
|
|
|
struct nxbe_clipstack_s
|
|
|
|
{
|
2009-12-15 19:12:29 +01:00
|
|
|
uint16_t npushed; /* Number of deferred rectangles in stack */
|
|
|
|
uint16_t mxrects; /* The capacity of the stack */
|
2021-02-05 09:05:48 +01:00
|
|
|
struct nxbe_cliprect_s *stack; /* The stack of deferred rectangles */
|
2008-11-27 23:56:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 19:12:29 +01:00
|
|
|
static const uint8_t g_nxcliporder[4][4] =
|
2008-11-27 23:56:45 +01:00
|
|
|
{
|
|
|
|
{ NX_TOP_NDX, NX_LEFT_NDX, NX_RIGHT_NDX, NX_BOTTOM_NDX }, /* index = NX_CLIPORDER_TLRB */
|
|
|
|
{ NX_TOP_NDX, NX_RIGHT_NDX, NX_LEFT_NDX, NX_BOTTOM_NDX }, /* NX_CLIPORDER_TRLB */
|
|
|
|
{ NX_BOTTOM_NDX, NX_LEFT_NDX, NX_RIGHT_NDX, NX_TOP_NDX }, /* NX_CLIPORDER_BLRT */
|
2021-02-05 09:05:48 +01:00
|
|
|
{ NX_BOTTOM_NDX, NX_RIGHT_NDX, NX_LEFT_NDX, NX_TOP_NDX }, /* NX_CLIPORDER_BRLT */
|
2008-11-27 23:56:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxbe_pushrectangle
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void nxbe_pushrectangle(FAR struct nxbe_clipstack_s *stack,
|
|
|
|
FAR struct nxbe_window_s *wnd,
|
|
|
|
FAR const struct nxgl_rect_s *rect)
|
|
|
|
{
|
|
|
|
/* Check if there is room on the stack to hold another rectangle */
|
|
|
|
|
|
|
|
if ((stack->npushed + 1) > stack->mxrects)
|
|
|
|
{
|
2013-12-30 19:42:18 +01:00
|
|
|
/* No then we will need to reallocate the stack to hold more */
|
2008-11-27 23:56:45 +01:00
|
|
|
|
2021-02-05 09:05:48 +01:00
|
|
|
int mxrects = stack->mxrects ? 2 *
|
|
|
|
stack->mxrects : NX_INITIAL_STACKSIZE;
|
2008-11-27 23:56:45 +01:00
|
|
|
struct nxbe_cliprect_s *newstack;
|
|
|
|
|
2014-08-31 23:27:37 +02:00
|
|
|
newstack = kmm_realloc(stack->stack,
|
|
|
|
sizeof(struct nxbe_cliprect_s) * mxrects);
|
2008-11-27 23:56:45 +01:00
|
|
|
if (!newstack)
|
|
|
|
{
|
2016-06-12 19:11:57 +02:00
|
|
|
gerr("ERROR: Failed to reallocate stack\n");
|
2008-11-27 23:56:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stack->stack = newstack;
|
|
|
|
stack->mxrects = mxrects;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then push the new rectangle onto the stack */
|
|
|
|
|
|
|
|
stack->stack[stack->npushed].wnd = wnd;
|
|
|
|
nxgl_rectcopy(&stack->stack[stack->npushed].rect, rect);
|
|
|
|
stack->npushed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxbe_poprectangle
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 19:12:29 +01:00
|
|
|
static inline bool nxbe_poprectangle(struct nxbe_clipstack_s *stack,
|
|
|
|
FAR struct nxbe_window_s **wnd,
|
|
|
|
struct nxgl_rect_s *rect)
|
2008-11-27 23:56:45 +01:00
|
|
|
{
|
2014-04-12 20:53:19 +02:00
|
|
|
if (stack->npushed > 0)
|
2008-11-27 23:56:45 +01:00
|
|
|
{
|
|
|
|
stack->npushed--;
|
|
|
|
*wnd = stack->stack[stack->npushed].wnd;
|
|
|
|
nxgl_rectcopy(rect, &stack->stack[stack->npushed].rect);
|
2009-12-15 19:12:29 +01:00
|
|
|
return true;
|
2008-11-27 23:56:45 +01:00
|
|
|
}
|
2014-04-12 20:53:19 +02:00
|
|
|
|
2009-12-15 19:12:29 +01:00
|
|
|
return false;
|
2008-11-27 23:56:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxbe_clipper
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2008-11-27 23:56:45 +01:00
|
|
|
* Perform flexible clipping operations. Callbacks are executed for
|
|
|
|
* each oscured and visible portions of the window.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* wnd - The window to be clipped.
|
|
|
|
* rect - The region of concern within the window
|
2021-02-05 09:05:48 +01:00
|
|
|
* order - Specifies the order to process the parts of the
|
|
|
|
* non-intersecting sub-rectangles.
|
2008-11-27 23:56:45 +01:00
|
|
|
* cops - The callbacks to handle obscured and visible parts of the
|
|
|
|
* sub-rectangles.
|
|
|
|
* plane - The raster operations to be used by the callback functions.
|
|
|
|
* These may vary with different color formats.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void nxbe_clipper(FAR struct nxbe_window_s *wnd,
|
2009-12-15 19:12:29 +01:00
|
|
|
FAR const struct nxgl_rect_s *dest, uint8_t order,
|
2008-11-27 23:56:45 +01:00
|
|
|
FAR struct nxbe_clipops_s *cops,
|
|
|
|
FAR struct nxbe_plane_s *plane)
|
|
|
|
{
|
2008-12-05 00:03:33 +01:00
|
|
|
struct nxbe_clipstack_s stack;
|
2008-11-27 23:56:45 +01:00
|
|
|
FAR struct nxbe_window_s *currw;
|
|
|
|
struct nxgl_rect_s rect;
|
|
|
|
struct nxgl_rect_s obscuredrect;
|
|
|
|
struct nxgl_rect_s currbounds;
|
|
|
|
struct nxgl_rect_s nonoverlapped[4];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Initialize the stack where we will keep deferred rectangle operations */
|
|
|
|
|
|
|
|
stack.npushed = 0;
|
|
|
|
stack.mxrects = 0;
|
|
|
|
stack.stack = NULL;
|
|
|
|
|
|
|
|
/* Loop until there are no further pending operations */
|
|
|
|
|
|
|
|
nxgl_rectcopy(&rect, dest); /* Start with the whole dest rectangle */
|
|
|
|
do
|
|
|
|
{
|
2021-02-05 09:05:48 +01:00
|
|
|
/* Loop for every window from the current window and above.
|
|
|
|
* Only windows above the current window can obscure the current window
|
2008-11-27 23:56:45 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
for (currw = wnd; currw; currw = currw->above)
|
|
|
|
{
|
|
|
|
/* Does the current window overlap the dest rectangle? */
|
|
|
|
|
|
|
|
currbounds = currw->bounds;
|
|
|
|
if (nxgl_rectoverlap(&rect, &currbounds))
|
|
|
|
{
|
|
|
|
/* Yes.. then it obscures all or part of the dest rectangle.
|
2021-02-05 09:05:48 +01:00
|
|
|
* Divide the potentially visible, non-overlapping regions into
|
|
|
|
* 4 smaller rectangles and push them onto the stack for
|
|
|
|
* processing on the next time through the outer loop.
|
2008-11-27 23:56:45 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
nxgl_nonintersecting(nonoverlapped, &rect, &currbounds);
|
|
|
|
for (i = 3; i >= 0; i--)
|
|
|
|
{
|
|
|
|
/* Push the rectangles in the order specific by the input
|
|
|
|
* argument of that name.
|
|
|
|
*/
|
|
|
|
|
2021-02-05 09:05:48 +01:00
|
|
|
struct nxgl_rect_s *candidate =
|
|
|
|
&nonoverlapped[g_nxcliporder[order][i]];
|
2008-11-27 23:56:45 +01:00
|
|
|
if (!nxgl_nullrect(candidate))
|
|
|
|
{
|
|
|
|
nxbe_pushrectangle(&stack, currw->above, candidate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now performed any required processing on the obscurred,
|
|
|
|
* overlapped region.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxgl_rectintersect(&obscuredrect, &rect, &currbounds);
|
|
|
|
cops->obscured(cops, plane, &obscuredrect);
|
|
|
|
|
|
|
|
/* Break out of the loop to process the pushed rectangles */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there are no other windows overlapping this rectangle, then this
|
|
|
|
* rectangular region must be visible.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!currw && !nxgl_nullrect(&rect))
|
|
|
|
{
|
|
|
|
cops->visible(cops, plane, &rect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nxbe_poprectangle(&stack, &wnd, &rect));
|
|
|
|
|
|
|
|
/* Done! If any stack was allocated, then free it before exit-ting */
|
|
|
|
|
|
|
|
if (stack.stack)
|
|
|
|
{
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(stack.stack);
|
2008-11-27 23:56:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxbe_clipnull
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2008-11-27 23:56:45 +01:00
|
|
|
* The do-nothing clipping callback function
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void nxbe_clipnull(FAR struct nxbe_clipops_s *cops,
|
|
|
|
FAR struct nxbe_plane_s *plane,
|
|
|
|
FAR const struct nxgl_rect_s *rect)
|
|
|
|
{
|
|
|
|
}
|