LaiFeng-Android / SopCastComponent

该项目不再维护,仅供学习参考
Other
2.48k stars 618 forks source link

start a thread for screenshots is blank at the onDrawFrame Method ? #25

Closed jianhui1012 closed 7 years ago

jianhui1012 commented 7 years ago

I found directly onDrawFrame Method for screenshots with 2 seconds delay,then try start a thread for screenshots is blank ,i don't know what's caused it ?

  public void onDrawFrame(GL10 gl) {
        synchronized (this) {
            if (updateSurface) {
                mSurfaceTexture.updateTexImage();
                mSurfaceTexture.getTransformMatrix(mTexMtx);
                updateSurface = false;
                if (isTakePicture) {
                    bmp = createBitmapFromGLSurface(0, 0, mSurfaceWidth,
                            mSurfaceHeight, gl);
                    mScreenShotHandler.handleScreenShot(bmp);
//                    synchronized (this) {
//                        if (mScreenShotHandler != null) {
//                            if (mCaptureShotThread == null) {
//                                mCaptureShotThread = new CaptureShotThread(mScreenShotHandler, mView, gl);
//                                mCaptureShotThread.start();
//                            }
//                        }
//                    }

                    isTakePicture = false;
                }
            }
        }
        mEffect.draw(mTexMtx);
        if (mRenderScreen != null) {
            mRenderScreen.draw();
        }
        if (mRenderSrfTex != null) {
            mRenderSrfTex.draw();
        }
    }

   private Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL10 gl) {
        int bitmapBuffer[] = new int[w * h];
        int bitmapSource[] = new int[w * h];
        IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
        intBuffer.position(0);
        try {
            gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE,
                    intBuffer);
            int offset1, offset2;
            for (int i = 0; i < h; i++) {
                offset1 = i * w;
                offset2 = (h - i - 1) * w;
                for (int j = 0; j < w; j++) {
                    int texturePixel = bitmapBuffer[offset1 + j];
                    int blue = (texturePixel >> 16) & 0xff;
                    int red = (texturePixel << 16) & 0x00ff0000;
                    int pixel = (texturePixel & 0xff00ff00) | red | blue;
                    bitmapSource[offset2 + j] = pixel;
                }
            }
        } catch (GLException e) {
            return null;
        }
        return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
    }
SuperJim123 commented 7 years ago

@jianhui1012 The texture is invalid when you use it in a new thread. If you want to take a screen shot, glReadPixels method will take a lot of time. I think using OpenGL PBO is a better way for screen shot.

jianhui1012 commented 7 years ago

Thank you for answer ! Are there any reference documents or examples available by the way of OpenGL PBO?

jianhui1012 commented 7 years ago

OtherWise,Why The texture is invalid when you use it in a new thread? I know The onDrawFrame Method is called at a GLThread. I guess if call gl.glReadPixels method in a new thread ,the gl object in the onDrawFrame Method is locked and cannot be read,I checked the API 23 related source code, did not find.