CameraX does not guarantee the resolution set in Preview#setTargetResolution() due to device limitations. Preview#attachedSurfaceResolution does return the resolution currently being used by the camera but it's only available after the Preview is bound.
A simpler way would be just using the resolution in SurfaceRequest. I updated your code sample:
preview.setSurfaceProvider {
request -> {
val textureId = GLUtils.genTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, false)
val surfaceTexture = SurfaceTexture(textureId) (setOnFrameAvailableListener { ..updateTexImage() ..redraw() })
// set the resolution.
surfaceTexture.setDefaultBufferSize(request.resolution.size.getWidth(), request.resolution.getHeight());
val surface = Surface(surfaceTexture)
request.provideSurface(surface, executor) {
// Don't forget to release the Surface! (And only after this callback is invoked)
surfaceTexture.release()
surface.release()
}
}
}