<commit_before>/**
 * @file
 * @brief
 *
 * @date   11.08.2020
 * @author <NAME>
 */

#include <drivers/video/fb.h>
#include "rawfb.h"

#if defined STM32F746xx
#include "stm32746g_discovery_lcd.h"
#elif defined STM32F769xx
#include "stm32f769i_discovery_lcd.h"
#else
#error Unsupported platform
#endif

#if defined STM32F746xx
extern LTDC_HandleTypeDef hLtdcHandler;
#define hltdc_handler hLtdcHandler
#elif defined STM32F769xx
extern LTDC_HandleTypeDef  hltdc_discovery;
#define hltdc_handler hltdc_discovery
#else
#error Unsupported platform
#endif

static volatile int ltdc_li_triggered = 0;

void HAL_LTDC_LineEvenCallback(LTDC_HandleTypeDef *hltdc) {
	ltdc_li_triggered = 1;
}

void rawfb_init(struct rawfb_fb_info *rfb) {
	BSP_LCD_LayerDefaultInit(0, (uint32_t) rfb->fb_buf[0]);
	BSP_LCD_LayerDefaultInit(1, (uint32_t) rfb->fb_buf[1]);

	HAL_LTDC_ProgramLineEvent(&hltdc_handler, rfb->height - 1);
}

void rawfb_clear_screen(struct rawfb_fb_info *rfb) {
	BSP_LCD_Clear(rfb->clear_color);
}

void rawfb_swap_buffers(struct rawfb_fb_info *rfb) {
	if (rfb->fb_info->var.fmt != BGRA8888) {
		pix_fmt_convert(rfb->fb_buf[rfb->fb_buf_idx],
						rfb->fb_info->screen_base, rfb->width * rfb->height,
						BGRA8888, rfb->fb_info->var.fmt);
	} else {
		/* Here we wait for the next display refresh, and after it happened
		 * swap buffers. */

		ltdc_li_triggered = 0;
		
		HAL_LTDC_ProgramLineEvent(&hltdc_handler, rfb->height - 1);

		while (!ltdc_li_triggered) {
		}

		BSP_LCD_SetTransparency(rfb->fb_buf_idx, 0xff);
	}

	rfb->fb_buf_idx = (rfb->fb_buf_idx + 1) % 2;

	BSP_LCD_SetTransparency(rfb->fb_buf_idx, 0x00);

	BSP_LCD_SelectLayer(rfb->fb_buf_idx);
}<commit_msg>nuklear: Use dma2d fill in rawfb<commit_after>/**
 * @file
 * @brief
 *
 * @date   11.08.2020
 * @author <NAME>
 */

#include <drivers/video/fb.h>
#include "rawfb.h"

#if defined STM32F746xx
#include "stm32746g_discovery_lcd.h"
#elif defined STM32F769xx
#include "stm32f769i_discovery_lcd.h"
#else
#error Unsupported platform
#endif

#if defined STM32F746xx
extern LTDC_HandleTypeDef hLtdcHandler;
#define hltdc_handler hLtdcHandler
#elif defined STM32F769xx
extern LTDC_HandleTypeDef  hltdc_discovery;
#define hltdc_handler hltdc_discovery
#else
#error Unsupported platform
#endif

static volatile int ltdc_li_triggered = 0;

void HAL_LTDC_LineEvenCallback(LTDC_HandleTypeDef *hltdc) {
	ltdc_li_triggered = 1;
}

static void dma2d_fill(uint32_t *dst, uint16_t xsize, uint16_t ysize, uint32_t color) {
	DMA2D_HandleTypeDef hdma2d;

	/*##-1- Configure the DMA2D Mode, Color Mode and output offset #############*/
	hdma2d.Init.Mode          = DMA2D_R2M;
	hdma2d.Init.ColorMode     = DMA2D_OUTPUT_ARGB8888;
	hdma2d.Init.OutputOffset  = 0;
#if defined STM32F769xx
	hdma2d.Init.AlphaInverted = DMA2D_REGULAR_ALPHA;  /* No Output Alpha Inversion*/
	hdma2d.Init.RedBlueSwap   = DMA2D_RB_REGULAR;     /* No Output Red & Blue swap */
#endif

	/*##-2- DMA2D Callbacks Configuration ######################################*/
	hdma2d.XferCpltCallback  = NULL;

	/*##-3- Foreground Configuration ###########################################*/
	hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
	hdma2d.LayerCfg[1].InputAlpha = 0xFF;
	hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888;
	hdma2d.LayerCfg[1].InputOffset = 0;
#if defined STM32F769xx
	hdma2d.LayerCfg[1].RedBlueSwap = DMA2D_RB_REGULAR; /* No ForeGround Red/Blue swap */
	hdma2d.LayerCfg[1].AlphaInverted = DMA2D_REGULAR_ALPHA; /* No ForeGround Alpha inversion */
#endif

	hdma2d.Instance          = DMA2D;

	/* DMA2D Initialization */
	if (HAL_DMA2D_Init(&hdma2d) == HAL_OK)  {
		if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) == HAL_OK) {
			if (HAL_DMA2D_Start(&hdma2d, color, (uint32_t)dst, xsize, ysize) == HAL_OK) {
				/* Polling For DMA transfer */
				HAL_DMA2D_PollForTransfer(&hdma2d, 100);
			}
		}
	}
}

void rawfb_init(struct rawfb_fb_info *rfb) {
	BSP_LCD_SelectLayer(0);

	HAL_LTDC_ProgramLineEvent(&hltdc_handler, rfb->height - 1);
}

void rawfb_clear_screen(struct rawfb_fb_info *rfb) {
	dma2d_fill(rfb->fb_buf[rfb->fb_buf_idx],
	           rfb->width, rfb->height, rfb->clear_color);
}

void rawfb_swap_buffers(struct rawfb_fb_info *rfb) {
	if (rfb->fb_info->var.fmt != BGRA8888) {
		pix_fmt_convert(rfb->fb_buf[rfb->fb_buf_idx],
						rfb->fb_info->screen_base, rfb->width * rfb->height,
						BGRA8888, rfb->fb_info->var.fmt);
	} else {
		/* Here we wait for the next display refresh, and after it happened
		 * swap buffers. */

		ltdc_li_triggered = 0;
		
		HAL_LTDC_ProgramLineEvent(&hltdc_handler, rfb->height - 1);

		while (!ltdc_li_triggered) {
		}

		LTDC_LAYER(&hltdc_handler, 0)->CFBAR = ((uint32_t)rfb->fb_buf[rfb->fb_buf_idx]);
		__HAL_LTDC_RELOAD_CONFIG(&hltdc_handler);

	}

	rfb->fb_buf_idx = (rfb->fb_buf_idx + 1) % 2;
}
{'fraction_non_alphanumeric': 0.1379815745393635, 'fraction_numerical': 0.042922948073701846, 'mean_word_length': 5.001256281407035, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6074579', 'n_tokens_mistral': 2178, 'n_tokens_neox': 1984, 'n_words': 346}