Advertisement
Guest User

Untitled

a guest
Jun 10th, 2012
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.02 KB | None | 0 0
  1. import static javax.microedition.khronos.egl.EGL10.*;
  2. import static javax.microedition.khronos.opengles.GL10.*;
  3.  
  4. import java.nio.IntBuffer;
  5.  
  6. import javax.microedition.khronos.egl.EGL10;
  7. import javax.microedition.khronos.egl.EGLConfig;
  8. import javax.microedition.khronos.egl.EGLContext;
  9. import javax.microedition.khronos.egl.EGLDisplay;
  10. import javax.microedition.khronos.egl.EGLSurface;
  11. import javax.microedition.khronos.opengles.GL10;
  12.  
  13. import android.graphics.Bitmap;
  14. import android.opengl.GLSurfaceView;
  15. import android.util.Log;
  16.  
  17. public class CPPixelBuffer {
  18.     final static String TAG = "PixelBuffer";
  19.     final static boolean LIST_CONFIGS = false;
  20.    
  21.     GLSurfaceView.Renderer mRenderer; // borrow this interface
  22.     int mWidth, mHeight;
  23.     Bitmap mBitmap;
  24.    
  25.     EGL10 mEGL;
  26.     EGLDisplay mEGLDisplay;
  27.     EGLConfig[] mEGLConfigs;
  28.     EGLConfig mEGLConfig;
  29.     EGLContext mEGLContext;
  30.     EGLSurface mEGLSurface;
  31.     GL10 mGL;
  32.    
  33.     String mThreadOwner;
  34.    
  35.     public CPPixelBuffer(int width, int height) {
  36.         mWidth = width;
  37.         mHeight = height;
  38.        
  39.         int[] version = new int[] {
  40.             1, 1
  41.         };
  42.         int[] attribList = new int[] {
  43.             EGL_WIDTH, mWidth,
  44.             EGL_HEIGHT, mHeight,
  45.             EGL_VERSION, 1,
  46.             EGL_NONE
  47.         };
  48.        
  49.         // No error checking performed, minimum required code to elucidate logic
  50.         mEGL = (EGL10) EGLContext.getEGL();
  51.         mEGLDisplay = mEGL.eglGetDisplay(EGL_DEFAULT_DISPLAY);
  52.         mEGL.eglInitialize(mEGLDisplay, version);
  53.         mEGLConfig = chooseConfig(); // Choosing a config is a little more complicated
  54.         mEGLContext = mEGL.eglCreateContext(mEGLDisplay, mEGLConfig, EGL_NO_CONTEXT, null);
  55.         mEGLSurface = mEGL.eglCreatePbufferSurface(mEGLDisplay, mEGLConfig, attribList);
  56.         mEGL.eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext);
  57.         mGL = (GL10) mEGLContext.getGL();
  58.        
  59.         // Record thread owner of OpenGL context
  60.         mThreadOwner = Thread.currentThread().getName();
  61.     }
  62.    
  63.     public void setRenderer(GLSurfaceView.Renderer renderer) {
  64.         mRenderer = renderer;
  65.        
  66.         // Does this thread own the OpenGL context?
  67.         if (!Thread.currentThread().getName().equals(mThreadOwner)) {
  68.             Log.e(TAG, "setRenderer: This thread does not own the OpenGL context.");
  69.             return;
  70.         }
  71.        
  72.         // Call the renderer initialization routines
  73.         mRenderer.onSurfaceCreated(mGL, mEGLConfig);
  74.         mRenderer.onSurfaceChanged(mGL, mWidth, mHeight);
  75.     }
  76.    
  77.     public Bitmap getBitmap() {
  78.         // Do we have a renderer?
  79.         if (mRenderer == null) {
  80.             Log.e(TAG, "getBitmap: Renderer was not set.");
  81.             return null;
  82.         }
  83.        
  84.         // Does this thread own the OpenGL context?
  85.         if (!Thread.currentThread().getName().equals(mThreadOwner)) {
  86.             Log.e(TAG, "getBitmap: This thread does not own the OpenGL context.");
  87.             return null;
  88.         }
  89.        
  90.         // Call the renderer draw routine
  91.         mRenderer.onDrawFrame(mGL);
  92.         convertToBitmap();
  93.         return mBitmap;
  94.     }
  95.    
  96.     private EGLConfig chooseConfig() {
  97.         int[] attribList = new int[] {                  
  98.             EGL_DEPTH_SIZE, 0,
  99.             EGL_STENCIL_SIZE, 0,
  100.             EGL_RED_SIZE, 8,
  101.             EGL_GREEN_SIZE, 8,
  102.             EGL_BLUE_SIZE, 8,
  103.             EGL_ALPHA_SIZE, 8,
  104.             EGL_NONE
  105.         };
  106.        
  107.         // No error checking performed, minimum required code to elucidate logic
  108.         // Expand on this logic to be more selective in choosing a configuration
  109.         int[] numConfig = new int[1];
  110.         mEGL.eglChooseConfig(mEGLDisplay, attribList, null, 0, numConfig);
  111.         int configSize = numConfig[0];
  112.         mEGLConfigs = new EGLConfig[configSize];
  113.         mEGL.eglChooseConfig(mEGLDisplay, attribList, mEGLConfigs, configSize, numConfig);
  114.        
  115.         if (LIST_CONFIGS) {
  116.             listConfig();
  117.         }
  118.        
  119.         return mEGLConfigs[0]; // Best match is probably the first configuration
  120.     }
  121.    
  122.     private void listConfig() {
  123.         Log.i(TAG, "Config List {");
  124.        
  125.         for (EGLConfig config : mEGLConfigs) {
  126.             int d, s, r, g, b, a;
  127.            
  128.             // Expand on this logic to dump other attributes
  129.             d = getConfigAttrib(config, EGL_DEPTH_SIZE);
  130.             s = getConfigAttrib(config, EGL_STENCIL_SIZE);
  131.             r = getConfigAttrib(config, EGL_RED_SIZE);
  132.             g = getConfigAttrib(config, EGL_GREEN_SIZE);
  133.             b = getConfigAttrib(config, EGL_BLUE_SIZE);
  134.             a = getConfigAttrib(config, EGL_ALPHA_SIZE);
  135.             Log.i(TAG, "    <d,s,r,g,b,a> = <" + d + "," + s + "," + r + "," + g + "," + b + "," + a + ">");
  136.         }
  137.        
  138.         Log.i(TAG, "}");
  139.     }
  140.    
  141.     private int getConfigAttrib(EGLConfig config, int attribute) {
  142.         int[] value = new int[1];
  143.         return mEGL.eglGetConfigAttrib(mEGLDisplay, config, attribute, value) ? value[0] : 0;
  144.     }
  145.    
  146.     private void convertToBitmap() {
  147.         IntBuffer ib = IntBuffer.allocate(mWidth * mHeight);
  148.         //IntBuffer ibt = IntBuffer.allocate(mWidth * mHeight);
  149.         mGL.glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, ib);
  150.        
  151.         // Convert upside down mirror-reversed image to right-side up normal image.
  152.         //for (int i = 0; i < mHeight; i++) {
  153.         //  for (int j = 0; j < mWidth; j++) {
  154.         //      ibt.put((mHeight - i - 1) * mWidth + j, ib.get(i * mWidth + j));
  155.         //  }
  156.         //}
  157.        
  158.         mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
  159.         //mBitmap.copyPixelsFromBuffer(ibt);
  160.         mBitmap.copyPixelsFromBuffer(ib);
  161.        
  162.         // TODO Scale -1*y.
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement