簡體   English   中英

從NDK抓取SurfaceTexture的GL上下文

[英]Grabbing SurfaceTexture's GL context from NDK

目的:我正在使用SurfaceTexture來顯示相機預覽,並且需要通過從NDK獲取GL上下文在表面上繪制。 我選擇了SurfaceTexture方法,因為可以避免將相機幀緩沖區的手冊從java傳遞到NDK,以節省一些性能。

public class MainActivity extends Activity implements SurfaceTextureListener {

private Camera mCamera;
private TextureView mTextureView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mTextureView = new TextureView(this);
    mTextureView.setSurfaceTextureListener(this);

    setContentView(mTextureView);
}

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    mCamera = Camera.open();
    Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
    mTextureView.setLayoutParams(new FrameLayout.LayoutParams(previewSize.width, previewSize.height, Gravity.CENTER));

    try {
        mCamera.setPreviewTexture(surface);
    } catch (IOException t) {
    }

    mCamera.startPreview();
}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    // Ignored, the Camera does all the work for us
}

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    mCamera.stopPreview();
    mCamera.release();
    return true;
}

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    // Update your view here
}

我已經嘗試過:我想SurfaceTexture內部使用GL功能來繪制上下文。 從NDK獲取默認顯示失敗, BAD_DISPLAY出現BAD_DISPLAY錯誤。

EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

當然,我能夠初始化一個新的GL上下文並進行繪制,但是我仍然希望將Java代碼中顯示的紋理保留在后台。

問題:使用SurfaceTexture時,是否可以從NDK中獲取GL上下文? 可能我必須在GLSurfaceView上使用,從Java代碼手動初始化GL上下文,然后從NDK中獲取它?

您的問題對我來說並不完全有意義,所以讓我列出幾件事。

  • SurfaceTexture不繪制任何內容。 當將Camera作為生產者連接時,SurfaceTexture將接收YUV幀,並使用EGL函數將其設置為“外部”紋理。 然后可以使用GLES渲染該紋理。

  • EGL上下文可以一次在一個線程中為“當前”。 指向當前上下文的指針保存在本地線程本地存儲中。 Java語言的GLES綁定是本機代碼的薄包裝,因此在使用GLES時Java和C ++之間在概念上沒有什么區別。

  • 創建對象時,SurfaceTexture的紋理將與當前的任何上下文相關聯,但是您可以使用attach / detach調用將其切換到其他上下文。 您不能“抓住” SurfaceTexture的EGL上下文,但是可以告訴它要使用哪一個。

  • SurfaceTexture(通常是Surface)只能有一個生產者。 您無法將“相機”幀發送到使用GLES渲染到的Surface。 您可以在它們之間來回切換,但是通常最好使用兩個不同的Surface對象。

  • TextureView是具有嵌入式SurfaceTexture的視圖。 當要求重繪時,它使用GLES從紋理進行渲染(這就是為什么如果禁用硬件渲染則根本看不到任何東西的原因)。

如果我正確理解了您的問題,我想您想做的是:

  • 將Camera輸出發送到在渲染器線程上創建的新SurfaceTexture。
  • 為TextureView的SurfaceTexture創建EGLSurface。
  • 使用相機的紋理作為樣本源,使用GLES渲染到TextureView上。 添加任何其他所需的GLES渲染。

可以在Grafika中找到各種示例,例如“攝影機中的紋理”活動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM