Skip to content

Commit

Permalink
Create GlOnlyTextureData.
Browse files Browse the repository at this point in the history
Use this new TextureData for FrameBuffer and FrameBufferCubemap.
Allow to save wasted memory.
  • Loading branch information
realitix committed Nov 5, 2015
1 parent 932ee64 commit ef0c210
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 67 deletions.
127 changes: 68 additions & 59 deletions gdx/src/com/badlogic/gdx/graphics/g2d/Gdx2DPixmap.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
*
* Licensed 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.
Expand Down Expand Up @@ -40,6 +40,39 @@ public class Gdx2DPixmap implements Disposable {
public static final int GDX2D_BLEND_NONE = 0;
public static final int GDX2D_BLEND_SRC_OVER = 1;

public static int toGlFormat (int format) {
switch (format) {
case GDX2D_FORMAT_ALPHA:
return GL20.GL_ALPHA;
case GDX2D_FORMAT_LUMINANCE_ALPHA:
return GL20.GL_LUMINANCE_ALPHA;
case GDX2D_FORMAT_RGB888:
case GDX2D_FORMAT_RGB565:
return GL20.GL_RGB;
case GDX2D_FORMAT_RGBA8888:
case GDX2D_FORMAT_RGBA4444:
return GL20.GL_RGBA;
default:
throw new GdxRuntimeException("unknown format: " + format);
}
}

public static int toGlType (int format) {
switch (format) {
case GDX2D_FORMAT_ALPHA:
case GDX2D_FORMAT_LUMINANCE_ALPHA:
case GDX2D_FORMAT_RGB888:
case GDX2D_FORMAT_RGBA8888:
return GL20.GL_UNSIGNED_BYTE;
case GDX2D_FORMAT_RGB565:
return GL20.GL_UNSIGNED_SHORT_5_6_5;
case GDX2D_FORMAT_RGBA4444:
return GL20.GL_UNSIGNED_SHORT_4_4_4_4;
default:
throw new GdxRuntimeException("unknown format: " + format);
}
}

long basePtr;
int width;
int height;
Expand All @@ -60,12 +93,12 @@ public Gdx2DPixmap (byte[] encodedData, int offset, int len, int requestedFormat
width = (int)nativeData[1];
height = (int)nativeData[2];
format = (int)nativeData[3];
if(requestedFormat != 0 && requestedFormat != format) {

if (requestedFormat != 0 && requestedFormat != format) {
convert(requestedFormat);
}
}

public Gdx2DPixmap (InputStream in, int requestedFormat) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
Expand All @@ -83,8 +116,8 @@ public Gdx2DPixmap (InputStream in, int requestedFormat) throws IOException {
width = (int)nativeData[1];
height = (int)nativeData[2];
format = (int)nativeData[3];
if(requestedFormat != 0 && requestedFormat != format) {

if (requestedFormat != 0 && requestedFormat != format) {
convert(requestedFormat);
}
}
Expand Down Expand Up @@ -120,6 +153,7 @@ private void convert (int requestedFormat) {
this.width = pixmap.width;
}

@Override
public void dispose () {
free(basePtr);
}
Expand Down Expand Up @@ -202,40 +236,15 @@ public int getFormat () {
}

public int getGLInternalFormat () {
switch (format) {
case GDX2D_FORMAT_ALPHA:
return GL20.GL_ALPHA;
case GDX2D_FORMAT_LUMINANCE_ALPHA:
return GL20.GL_LUMINANCE_ALPHA;
case GDX2D_FORMAT_RGB888:
case GDX2D_FORMAT_RGB565:
return GL20.GL_RGB;
case GDX2D_FORMAT_RGBA8888:
case GDX2D_FORMAT_RGBA4444:
return GL20.GL_RGBA;
default:
throw new GdxRuntimeException("unknown format: " + format);
}
return toGlFormat(format);
}

public int getGLFormat () {
return getGLInternalFormat();
}

public int getGLType () {
switch (format) {
case GDX2D_FORMAT_ALPHA:
case GDX2D_FORMAT_LUMINANCE_ALPHA:
case GDX2D_FORMAT_RGB888:
case GDX2D_FORMAT_RGBA8888:
return GL20.GL_UNSIGNED_BYTE;
case GDX2D_FORMAT_RGB565:
return GL20.GL_UNSIGNED_SHORT_5_6_5;
case GDX2D_FORMAT_RGBA4444:
return GL20.GL_UNSIGNED_SHORT_4_4_4_4;
default:
throw new GdxRuntimeException("unknown format: " + format);
}
return toGlType(format);
}

public String getFormatString () {
Expand All @@ -262,96 +271,96 @@ public String getFormatString () {
#include <gdx2d/gdx2d.h>
#include <stdlib.h>
*/
private static native ByteBuffer load (long[] nativeData, byte[] buffer, int offset, int len); /*MANUAL

private static native ByteBuffer load (long[] nativeData, byte[] buffer, int offset, int len); /*MANUAL
const unsigned char* p_buffer = (const unsigned char*)env->GetPrimitiveArrayCritical(buffer, 0);
gdx2d_pixmap* pixmap = gdx2d_load(p_buffer + offset, len);
env->ReleasePrimitiveArrayCritical(buffer, (char*)p_buffer, 0);
if(pixmap==0)
return 0;
jobject pixel_buffer = env->NewDirectByteBuffer((void*)pixmap->pixels, pixmap->width * pixmap->height * gdx2d_bytes_per_pixel(pixmap->format));
jlong* p_native_data = (jlong*)env->GetPrimitiveArrayCritical(nativeData, 0);
p_native_data[0] = (jlong)pixmap;
p_native_data[1] = pixmap->width;
p_native_data[2] = pixmap->height;
p_native_data[3] = pixmap->format;
env->ReleasePrimitiveArrayCritical(nativeData, p_native_data, 0);
return pixel_buffer;
*/
*/

private static native ByteBuffer newPixmap (long[] nativeData, int width, int height, int format); /*MANUAL
gdx2d_pixmap* pixmap = gdx2d_new(width, height, format);
if(pixmap==0)
return 0;
jobject pixel_buffer = env->NewDirectByteBuffer((void*)pixmap->pixels, pixmap->width * pixmap->height * gdx2d_bytes_per_pixel(pixmap->format));
jlong* p_native_data = (jlong*)env->GetPrimitiveArrayCritical(nativeData, 0);
p_native_data[0] = (jlong)pixmap;
p_native_data[1] = pixmap->width;
p_native_data[2] = pixmap->height;
p_native_data[3] = pixmap->format;
env->ReleasePrimitiveArrayCritical(nativeData, p_native_data, 0);
return pixel_buffer;
*/
*/

private static native void free (long pixmap); /*
gdx2d_free((gdx2d_pixmap*)pixmap);
*/
*/

private static native void clear (long pixmap, int color); /*
gdx2d_clear((gdx2d_pixmap*)pixmap, color);
*/
*/

private static native void setPixel (long pixmap, int x, int y, int color); /*
gdx2d_set_pixel((gdx2d_pixmap*)pixmap, x, y, color);
*/
*/

private static native int getPixel (long pixmap, int x, int y); /*
return gdx2d_get_pixel((gdx2d_pixmap*)pixmap, x, y);
*/
*/

private static native void drawLine (long pixmap, int x, int y, int x2, int y2, int color); /*
gdx2d_draw_line((gdx2d_pixmap*)pixmap, x, y, x2, y2, color);
*/
*/

private static native void drawRect (long pixmap, int x, int y, int width, int height, int color); /*
gdx2d_draw_rect((gdx2d_pixmap*)pixmap, x, y, width, height, color);
*/
*/

private static native void drawCircle (long pixmap, int x, int y, int radius, int color); /*
gdx2d_draw_circle((gdx2d_pixmap*)pixmap, x, y, radius, color);
*/
gdx2d_draw_circle((gdx2d_pixmap*)pixmap, x, y, radius, color);
*/

private static native void fillRect (long pixmap, int x, int y, int width, int height, int color); /*
gdx2d_fill_rect((gdx2d_pixmap*)pixmap, x, y, width, height, color);
*/
*/

private static native void fillCircle (long pixmap, int x, int y, int radius, int color); /*
gdx2d_fill_circle((gdx2d_pixmap*)pixmap, x, y, radius, color);
*/
*/

private static native void fillTriangle (long pixmap, int x1, int y1, int x2, int y2, int x3, int y3, int color); /*
gdx2d_fill_triangle((gdx2d_pixmap*)pixmap, x1, y1, x2, y2, x3, y3, color);
*/
*/

private static native void drawPixmap (long src, long dst, int srcX, int srcY, int srcWidth, int srcHeight, int dstX,
int dstY, int dstWidth, int dstHeight); /*
gdx2d_draw_pixmap((gdx2d_pixmap*)src, (gdx2d_pixmap*)dst, srcX, srcY, srcWidth, srcHeight, dstX, dstY, dstWidth, dstHeight);
*/
*/

public static native void setBlend (int blend); /*
gdx2d_set_blend(blend);
*/
*/

public static native void setScale (int scale); /*
gdx2d_set_scale(scale);
*/
*/

public static native String getFailureReason (); /*
return env->NewStringUTF(gdx2d_get_failure_reason());
*/
*/
}
14 changes: 8 additions & 6 deletions gdx/src/com/badlogic/gdx/graphics/glutils/FrameBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.Gdx2DPixmap;

/** <p>
* Encapsulates OpenGL ES 2.0 frame buffer objects. This is a simple helper class which should cover most FBO uses. It will
Expand All @@ -44,13 +45,11 @@ public class FrameBuffer extends GLFrameBuffer<Texture> {
* @param format
* @param width
* @param height
* @param hasDepth
*/
* @param hasDepth */
public FrameBuffer (Pixmap.Format format, int width, int height, boolean hasDepth) {
this(format, width, height, hasDepth, false);
}


/** Creates a new FrameBuffer having the given dimensions and potentially a depth and a stencil buffer attached.
*
* @param format the format of the color buffer; according to the OpenGL ES 2.0 spec, only RGB565, RGBA4444 and RGB5_A1 are
Expand All @@ -65,17 +64,20 @@ public FrameBuffer (Pixmap.Format format, int width, int height, boolean hasDept

@Override
protected Texture createColorTexture () {
Texture result = new Texture(width, height, format);
int glFormat = Gdx2DPixmap.toGlFormat(Pixmap.Format.toGdx2DPixmapFormat(format));
int glType = Gdx2DPixmap.toGlType(Pixmap.Format.toGdx2DPixmapFormat(format));
GlOnlyTextureData data = new GlOnlyTextureData(width, height, 0, glFormat, glFormat, glType);
Texture result = new Texture(data);
result.setFilter(TextureFilter.Linear, TextureFilter.Linear);
result.setWrap(TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
return result;
}

@Override
protected void disposeColorTexture (Texture colorTexture) {
colorTexture.dispose();
}

/** See {@link GLFrameBuffer#unbind()} */
public static void unbind () {
GLFrameBuffer.unbind();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.Gdx2DPixmap;
import com.badlogic.gdx.utils.GdxRuntimeException;

/** <p>
Expand Down Expand Up @@ -89,12 +90,15 @@ public FrameBufferCubemap (Pixmap.Format format, int width, int height, boolean

@Override
protected Cubemap createColorTexture () {
Cubemap result = new Cubemap(width, height, width, format);
int glFormat = Gdx2DPixmap.toGlFormat(Pixmap.Format.toGdx2DPixmapFormat(format));
int glType = Gdx2DPixmap.toGlType(Pixmap.Format.toGdx2DPixmapFormat(format));
GlOnlyTextureData data = new GlOnlyTextureData(width, height, 0, glFormat, glFormat, glType);
Cubemap result = new Cubemap(data, data, data, data, data, data);
result.setFilter(TextureFilter.Linear, TextureFilter.Linear);
result.setWrap(TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
return result;
}

@Override
protected void disposeColorTexture (Cubemap colorTexture) {
colorTexture.dispose();
Expand Down

0 comments on commit ef0c210

Please sign in to comment.