Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
Posts: 44
Registered: ‎01-06-2012
My Device: PlayBook

img_load breaks OpenGL

When I use the img_load to decode an image, it seems to somehow break OpenGL in such a way that the next OpenGL call will SIGSEGV segmentation fault. I have created quite a small reproduction case for this:

 

// Load JPEG test.
{
	// Attach to image library.
	img_lib_t ilib = NULL;
	int rc;

	if ( ( rc = img_lib_attach( &ilib ) ) != IMG_ERR_OK ) 
	{
		PiLog( "img_lib_attach( ) failed: %d", rc );
	}
	else
	{
		if ( FileSystem::FileHandle fh = Pi.MemFile.Open( "test.jpg", FileSystem::READ, FileSystem::ProductResources ) )
		{
			unsigned char * bufferStart = Pi.MemFile.GetFileBufferStart( fh );
			int fileSize = Pi.MemFile.Size( fh );

			// Create a stream from file (loaded into memory).
			io_stream_t * stream = NULL;

			int rc;
			img_t img;

			// Initialize an img_t by setting its flags to 0.
			img.flags = 0;

			PiLog( "Opening IO stream" );
			if ( stream = io_open( IO_MEM, IO_READ, fileSize, bufferStart ) )
			{

				static GLenum err1 = glGetError( );
				if ( err1 != GL_NO_ERROR )
				{
					fprintf( stderr, "GL Error : %d\n", err1 );
					fflush( stderr );
				}

				PiLog( "Loading image data" );
				if ( ( rc = img_load( ilib, stream, NULL, &img ) ) != IMG_ERR_OK ) 
				{
					PiLog( "img_load( 0x%x, 0x%x, NULL, 0x%x ) failed : %d", ilib, stream, &img, rc );
				}
				else
				{
					PiLog( "Image is %dx%dx%d\n", img.w, img.h, IMG_FMT_BPP( img.format ) );
				}

				static GLenum err2 = glGetError( );
				if ( err2 != GL_NO_ERROR )
				{
					fprintf( stderr, "GL Error : %d\n", err2 );
					fflush( stderr );
				}

				io_close( stream );
			}
			Pi.MemFile.Close( fh );
		}
		img_lib_detach( ilib );
	}
}

 So, essentially call img_lib_attach, io_open (with some memory that contains a JPEG), glGetError (which correctly returns GL_NO_ERROR), img_load (which I can confirm from the log 'Image is 512x512x32'), glGetError (and this call to glGetError now crashes). The glGetError callstack address is 'glGetError() at 0x78079572'.

 

It is worth noting that when the img_load function is called, GDB displays:

 

warning: Could not load shared library symbols for img_codec_png.so.
Do you need "set solib-search-path" or "set sysroot"?
warning: Could not load shared library symbols for img_codec_scaladojpg.so.
Do you need "set solib-search-path" or "set sysroot"?

 The warnings can (probably be ignored) but we can at least note from this that it loads the img_codec_png.so and then the img_codec_scaladojpg.so. It stops at the 'jpg' codec as this is the correct codec to use for the image in question.

 

For what it is worth, I have also tried to be forceful with codec selection by instead using (:

 

numCodecs = img_codec_list_bymime( ilib, "image/jpeg", codecs, sizeof( codecs ) );
if ( numCodecs > 0 )
{
	if ( img_decode_validate( codecs, numCodecs, stream, &validCodecIx ) == IMG_ERR_OK )
	{
		glGetError( );
	}
}

 However in this case, GL will work correctly until img_decode_validate is called, at which point the next GL call (glGetError in this example) will then SIGSEGV segmentation fault.

 

I have tried running this code both on a background thread, and on the main thread. In both cases it makes no difference.

 

Could anyone help me out here? I feel I am missing something obvious, but can't for the life of me think what it is?

Contributor
Posts: 44
Registered: ‎01-06-2012
My Device: PlayBook

Re: img_load breaks OpenGL

Small update, I tried loading various images, and have found that:

 

PNG - works

GIF/BMP/JPG - break Open GL

 

This doesn't overly help me as ultimlatey I am going to be receiving the image data dynamically and it could be in any of the above formats.

Developer
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook

Re: img_load breaks OpenGL

I did not get it - does your img_load() return success or not (with jpeg)?

Try simpler code:

img_decode_callouts_t Callouts;
img_lib_t ilib = NULL;
int rc;
char strFileName="filename.jpeg";

rc = img_lib_attach(&ilib);
if (rc != IMG_ERR_OK) {
fprintf(stderr,"img_lib_attach() failed\n");
return -1;
}

memset(&img, 0, sizeof(img));

memset(&Callouts, 0, sizeof(Callouts));
rc = img_load_file(ilib, strFileName, NULL, &img);
img_lib_detach(ilib);

 

Contributor
Posts: 44
Registered: ‎01-06-2012
My Device: PlayBook

Re: img_load breaks OpenGL

I did not get it - does your img_load() return success or not (with jpeg)?

 

It does successfully load the image (including the JPEG), however, once the img_load function has returned, I will no longer be able to make any calls to Open GL (the next call I make crashes).

 

I hope that helps explain the problem.