简体   繁体   English

Android:OpenGL纹理太大,无法找出原因

[英]Android: OpenGL textures too large, can't figure out why

Why do my textures seemingly take up so much space? 为什么我的纹理似乎占据了这么多空间?

My app, which uses opengl heavily, produces the following heap stats* during operation: 我的应用大量使用opengl,在运行期间会产生以下堆统计信息*:

Used heap dump 1.8 MB 
Number of objects 49,447 
Number of classes 2,257 
Number of class loaders 4 
Number of GC roots 8,551 
Format hprof 
JVM version  
Time 1:08:15 AM GMT+02:00 
Date Oct 2, 2011 
Identifier size 32-bit 

But, when I use the task manager on my phone to look at the ram use of my application it says my app uses 44.42MB. 但是,当我使用手机上的任务管理器查看应用程序的内存使用情况时,它说我的应用程序使用了44.42MB。 Is there any relationship between heap size use and ram use? 堆大小使用和ram使用之间有任何关系吗? I think much of that 42MB must be my open GL textures, but I can't figure out why they take up so much space, because on disk all the files together take only take up 24MB (and they are not all loaded at the same time). 我认为42MB的大部分内容必须是我的开放式GL纹理,但是我不知道为什么它们会占用这么多的空间,因为在磁盘上,所有文件加在一起仅占用24MB(而且它们并非全部以相同的方式加载)时间)。 And I'm even making many of them smaller by resizing the bitmap prior to texture loading. 我什至通过在加载纹理之前调整位图的大小来缩小其中的许多尺寸。 I also dynamically create some textures, but also destroy those textures after use. 我还动态创建了一些纹理,但使用后也​​破坏了这些纹理。

I am using OpenGL 1.0 and Android 2.2, typical code that I use to load a texture looks like this: 我使用的是OpenGL 1.0和Android 2.2,用于加载纹理的典型代码如下所示:

static int set_gl_texture(Bitmap bitmap){

        bitmap = Bitmap.createScaledBitmap(bitmap, 256, 256, true);
        // generate one texture pointer
        mGL.glGenTextures(1, mTextures, 0);
        mGL.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[0]); // A bound texture is
                                                            // an active texture


        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0,GL10.GL_RGBA, bitmap, 0);

        // create nearest filtered texture
        mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
                GL10.GL_LINEAR); // This is where the scaling algorithms are
        mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
                GL10.GL_LINEAR); // This is where the scaling algorithms are
        mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
                GL10.GL_CLAMP_TO_EDGE);
        mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
                GL10.GL_CLAMP_TO_EDGE);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

        bitmap.recycle();
        Log.v("GLSurfaceView", "Loading Texture Finished, Error Codes:"+mGL.glGetError());
        return mTextures[0];
    }

Code which I use to load bitmaps looks like the following: 我用来加载位图的代码如下所示:

public int load_texture(int res_id){

            if(mBitmapOpts==null){
                mBitmapOpts = new BitmapFactory.Options();
                mBitmapOpts.inScaled = false;
            }

            mBtoLoad = BitmapFactory.decodeResource(MyApplicationObject.getContext().getResources(),res_id, mBitmapOpts);

            assert mBtoLoad != null;

            return GraphicsOperations.set_gl_texture(mBtoLoad);

        }

*hprof file analyzed using mat, same data is generated by eclipse ddms *使用mat分析的hprof文件,eclipse ddms生成相同的数据

PNGs are compressed images. PNG是压缩图像。 In order for OpenGL to use them, the pngs must be decompressed. 为了让OpenGL使用它们,必须将png解压缩。 This decompression will increase the memory size. 此解压缩将增加内存大小。

You may want to decrease the size of some of the textures somehow. 您可能希望以某种方式减小某些纹理的大小。 Maybe instead of using 512x512 images, use 256x256 or 128x128. 也许不使用512x512图像,而是使用256x256或128x128。 Some textures that you use may not need to be so large since they are going onto a mobile device with a limited screen size. 您使用的某些纹理可能不需要太大,因为它们要在屏幕尺寸有限的移动设备上使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM