<commit_before>/*
 *			GPAC - Multimedia Framework C SDK
 *
 *			Authors: <NAME>
 *			Copyright (c) Telecom ParisTech 2000-2012
 *					All rights reserved
 *
 *  This file is part of GPAC / Scene Compositor sub-project
 *
 *  GPAC is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  GPAC is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#include "texturing.h"
#include "visual_manager.h"
#include "nodes_stacks.h"
#include "gl_inc.h"

#ifdef GPAC_USE_TINYGL
# define GLTEXENV	glTexEnvi
# define GLTEXPARAM	glTexParameteri
# define TexEnvType u32
#elif defined (GPAC_USE_GLES1X)
# define GLTEXENV	glTexEnvx
# define GLTEXPARAM	glTexParameterx
# define TexEnvType Fixed
#else
# define GLTEXENV	glTexEnvf
# define GLTEXPARAM	glTexParameteri
# define TexEnvType Float
#endif



/*tx flags*/
enum
{
	/*signal video data must be sent to 2D raster*/
	TX_NEEDS_RASTER_LOAD = (1<<1),
	/*signal video data must be sent to 3D hw*/
	TX_NEEDS_HW_LOAD = (1<<2),

	/*OpenGL texturing flags*/

	/*these 4 are exclusives*/
	TX_MUST_SCALE = (1<<10),
	TX_IS_POW2 = (1<<11),
	TX_IS_RECT = (1<<12),
	TX_EMULE_POW2 = (1<<13),
	TX_EMULE_FIRST_LOAD = (1<<14),

	TX_IS_FLIPPED = (1<<15),
};


struct __texture_wrapper
{
	u32 flags;

	/*2D texturing*/
	GF_STENCIL tx_raster;
	//0: not paused, 1: paused, 2: initial pause has been done
	u32 init_pause_status;
	Bool conv_to_8bit;
	/*3D texturing*/
#ifndef GPAC_DISABLE_3D
	/*opengl texture id*/
	u32 id;
	u32 blend_mode;
	u32 rescale_width, rescale_height;
	char *scale_data;
	char *conv_data;
	Fixed conv_wscale, conv_hscale;
	u32 conv_format, conv_w, conv_h;

	Bool use_external_textures;
	
	/*gl textures vars (gl_type: 2D texture or rectangle (NV ext) )*/
	u32 nb_comp, gl_format, gl_type, gl_dtype;
	Bool yuv_shader;
	u32 v_id, u_id;
	u32 pbo_id, u_pbo_id, v_pbo_id;
	Bool pbo_pushed;
#endif
#ifdef GF_SR_USE_DEPTH
	char *depth_data;
#endif

};

GF_Err gf_sc_texture_allocate(GF_TextureHandler *txh)
{
	if (txh->tx_io) return GF_OK;
	GF_SAFEALLOC(txh->tx_io, struct __texture_wrapper);
	if (!txh->tx_io) return GF_OUT_OF_MEM;
	return GF_OK;
}

GF_Err gf_sc_texture_configure_conversion(GF_TextureHandler *txh)
{
	if (txh->compositor->output_as_8bit) {

		if (txh->pixelformat == GF_PIXEL_YV12_10) {
			txh->stride /= 2;
			txh->tx_io->conv_to_8bit = GF_TRUE;
			txh->pixelformat = GF_PIXEL_YV12;
			if(txh->raw_memory)
				txh->tx_io->conv_data = (char*)gf_realloc(txh->tx_io->conv_data, 3 * sizeof(char)* txh->stride * txh->height / 2);
		}
		else if (txh->pixelformat == GF_PIXEL_YUV422_10) {
			txh->stride /= 2;
			txh->tx_io->conv_to_8bit = GF_TRUE;
			txh->pixelformat = GF_PIXEL_YUV422;
			
			if (txh->raw_memory)
				txh->tx_io->conv_data = (char*)gf_realloc(txh->tx_io->conv_data, 2 * sizeof(char)* txh->stride * txh->height);
		}
		else if (txh->pixelformat == GF_PIXEL_YUV444_10) {
			txh->stride /= 2;
			txh->tx_io->conv_to_8bit = GF_TRUE;
			txh->pixelformat = GF_PIXEL_YUV444;
			if (txh->raw_memory)
				txh->tx_io->conv_data = (char*)gf_realloc(txh->tx_io->conv_data, 3 * sizeof(char)* txh->stride * txh->height);
		}
	}
	return GF_OK;
}



static void release_txio(struct __texture_wrapper *tx_io)
{

#ifndef GPAC_DISABLE_3D
	if (!tx_io->use_external_textures) {
		if (tx_io->id) glDeleteTextures(1, &tx_io->id);
		if (tx_io->u_id) glDeleteTextures(1, &tx_io->u_id);
		if (tx_io->v_id) glDeleteTextures(1, &tx_io->v_id);
	}
	if (tx_io->pbo_id) glDeleteBuffers(1, &tx_io->pbo_id);
	if (tx_io->u_pbo_id) glDeleteBuffers(1, &tx_io->u_pbo_id);
	if (tx_io->v_pbo_id) glDeleteBuffers(1, &tx_io->v_pbo_id);

	if (tx_io->scale_data) gf_free(tx_io->scale_data);
	if (tx_io->conv_data) gf_free(tx_io->conv_data);
#endif

#ifdef GF_SR_USE_DEPTH
	if (tx_io->depth_data) gf_free(tx_io->depth_data);
#endif

	gf_free(tx_io);
}

void gf_sc_texture_release(GF_TextureHandler *txh)
{
	if (txh->vout_udta && txh->compositor->video_out->ReleaseTexture) {
		txh->compositor->video_out->ReleaseTexture(txh->compositor->video_out, txh);
		txh->vout_udta = NULL;
	}

	if (txh->tx_io) {
		gf_sc_lock(txh->compositor, 1);

		if (txh->tx_io->tx_raster) {
			txh->compositor->rasterizer->stencil_delete(txh->tx_io->tx_raster);
			txh->tx_io->tx_raster = NULL;
		}

		if (gf_th_id()==txh->compositor->video_th_id) {
			release_txio(txh->tx_io);
		} else {
			gf_list_add(txh->compositor->textures_gc, txh->tx_io);<commit_msg>Fixed compilation when 3D disabled<commit_after>/*
 *			GPAC - Multimedia Framework C SDK
 *
 *			Authors: <NAME>
 *			Copyright (c) Telecom ParisTech 2000-2012
 *					All rights reserved
 *
 *  This file is part of GPAC / Scene Compositor sub-project
 *
 *  GPAC is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  GPAC is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#include "texturing.h"
#include "visual_manager.h"
#include "nodes_stacks.h"
#include "gl_inc.h"

#ifdef GPAC_USE_TINYGL
# define GLTEXENV	glTexEnvi
# define GLTEXPARAM	glTexParameteri
# define TexEnvType u32
#elif defined (GPAC_USE_GLES1X)
# define GLTEXENV	glTexEnvx
# define GLTEXPARAM	glTexParameterx
# define TexEnvType Fixed
#else
# define GLTEXENV	glTexEnvf
# define GLTEXPARAM	glTexParameteri
# define TexEnvType Float
#endif



/*tx flags*/
enum
{
	/*signal video data must be sent to 2D raster*/
	TX_NEEDS_RASTER_LOAD = (1<<1),
	/*signal video data must be sent to 3D hw*/
	TX_NEEDS_HW_LOAD = (1<<2),

	/*OpenGL texturing flags*/

	/*these 4 are exclusives*/
	TX_MUST_SCALE = (1<<10),
	TX_IS_POW2 = (1<<11),
	TX_IS_RECT = (1<<12),
	TX_EMULE_POW2 = (1<<13),
	TX_EMULE_FIRST_LOAD = (1<<14),

	TX_IS_FLIPPED = (1<<15),
};


struct __texture_wrapper
{
	u32 flags;

	/*2D texturing*/
	GF_STENCIL tx_raster;
	//0: not paused, 1: paused, 2: initial pause has been done
	u32 init_pause_status;
	Bool conv_to_8bit;
	char *conv_data;

	/*3D texturing*/
#ifndef GPAC_DISABLE_3D
	/*opengl texture id*/
	u32 id;
	u32 blend_mode;
	u32 rescale_width, rescale_height;
	char *scale_data;
	Fixed conv_wscale, conv_hscale;
	u32 conv_format, conv_w, conv_h;

	Bool use_external_textures;
	
	/*gl textures vars (gl_type: 2D texture or rectangle (NV ext) )*/
	u32 nb_comp, gl_format, gl_type, gl_dtype;
	Bool yuv_shader;
	u32 v_id, u_id;
	u32 pbo_id, u_pbo_id, v_pbo_id;
	Bool pbo_pushed;
#endif
#ifdef GF_SR_USE_DEPTH
	char *depth_data;
#endif

};

GF_Err gf_sc_texture_allocate(GF_TextureHandler *txh)
{
	if (txh->tx_io) return GF_OK;
	GF_SAFEALLOC(txh->tx_io, struct __texture_wrapper);
	if (!txh->tx_io) return GF_OUT_OF_MEM;
	return GF_OK;
}

GF_Err gf_sc_texture_configure_conversion(GF_TextureHandler *txh)
{
	if (txh->compositor->output_as_8bit) {

		if (txh->pixelformat == GF_PIXEL_YV12_10) {
			txh->stride /= 2;
			txh->tx_io->conv_to_8bit = GF_TRUE;
			txh->pixelformat = GF_PIXEL_YV12;
			if(txh->raw_memory)
				txh->tx_io->conv_data = (char*)gf_realloc(txh->tx_io->conv_data, 3 * sizeof(char)* txh->stride * txh->height / 2);
		}
		else if (txh->pixelformat == GF_PIXEL_YUV422_10) {
			txh->stride /= 2;
			txh->tx_io->conv_to_8bit = GF_TRUE;
			txh->pixelformat = GF_PIXEL_YUV422;
			
			if (txh->raw_memory)
				txh->tx_io->conv_data = (char*)gf_realloc(txh->tx_io->conv_data, 2 * sizeof(char)* txh->stride * txh->height);
		}
		else if (txh->pixelformat == GF_PIXEL_YUV444_10) {
			txh->stride /= 2;
			txh->tx_io->conv_to_8bit = GF_TRUE;
			txh->pixelformat = GF_PIXEL_YUV444;
			if (txh->raw_memory)
				txh->tx_io->conv_data = (char*)gf_realloc(txh->tx_io->conv_data, 3 * sizeof(char)* txh->stride * txh->height);
		}
	}
	return GF_OK;
}



static void release_txio(struct __texture_wrapper *tx_io)
{

#ifndef GPAC_DISABLE_3D
	if (!tx_io->use_external_textures) {
		if (tx_io->id) glDeleteTextures(1, &tx_io->id);
		if (tx_io->u_id) glDeleteTextures(1, &tx_io->u_id);
		if (tx_io->v_id) glDeleteTextures(1, &tx_io->v_id);
	}
	if (tx_io->pbo_id) glDeleteBuffers(1, &tx_io->pbo_id);
	if (tx_io->u_pbo_id) glDeleteBuffers(1, &tx_io->u_pbo_id);
	if (tx_io->v_pbo_id) glDeleteBuffers(1, &tx_io->v_pbo_id);

	if (tx_io->scale_data) gf_free(tx_io->scale_data);
#endif

	if (tx_io->conv_data) gf_free(tx_io->conv_data);

#ifdef GF_SR_USE_DEPTH
	if (tx_io->depth_data) gf_free(tx_io->depth_data);
#endif

	gf_free(tx_io);
}

void gf_sc_texture_release(GF_TextureHandler *txh)
{
	if (txh->vout_udta && txh->compositor->video_out->ReleaseTexture) {
		txh->compositor->video_out->ReleaseTexture(txh->compositor->video_out, txh);
		txh->vout_udta = NULL;
	}

	if (txh->tx_io) {
		gf_sc_lock(txh->compositor, 1);

		if (txh->tx_io->tx_raster) {
			txh->compositor->rasterizer->stencil_delete(txh->tx_io->tx_raster);
			txh->tx_io->tx_raster = NULL;
		}

		if (gf_th_id()==txh->compositor->video_th_id) {
			release_txio(txh->tx_io);
		} else {
			gf_list_add(txh->compositor->textures_gc, txh->tx_io);
{'fraction_non_alphanumeric': 0.12750528754154497, 'fraction_numerical': 0.023063752643770773, 'mean_word_length': 4.477109762824049, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 179, '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': '2620368', 'n_tokens_mistral': 4487, 'n_tokens_neox': 4143, 'n_words': 909}