GLVideoFrame.java
Go to the documentation of this file.
1 package com.intel.realsense.librealsense;
2 
3 import android.graphics.Rect;
4 import android.opengl.GLES10;
5 
6 import java.nio.ByteBuffer;
7 import java.nio.ByteOrder;
8 import java.nio.IntBuffer;
9 
10 public class GLVideoFrame extends GLFrame {
11  private IntBuffer mGlTexture;
12 
13  public GLVideoFrame(){
14  mGlTexture = IntBuffer.allocate(1);
15  GLES10.glGenTextures(1, mGlTexture);
16  }
17 
18  public int getTexture() { return mGlTexture.array()[0]; }
19 
20  private Rect adjustRatio(Rect in){
21  if (mFrame == null || !(mFrame.is(Extension.VIDEO_FRAME)))
22  return null;
23 
24  float ratio = 0;
25  try(VideoFrame vf = mFrame.as(Extension.VIDEO_FRAME)) {
26  ratio = (float)vf.getWidth() / (float)vf.getHeight();
27  }
28  float newHeight = in.height();
29  float newWidth = in.height() * ratio;
30  if(newWidth > in.width()){
31  ratio = in.width() / newWidth;
32  newWidth *= ratio;
33  newHeight *= ratio;
34  }
35 
36  float newLeft = in.left + (in.width() - newWidth) / 2f;
37  float newTop = in.top + (in.height() - newHeight) / 2f;
38  float newRight = newLeft + newWidth;
39  float newBottom = newTop + newHeight;
40  return new Rect((int)newLeft, (int)newTop, (int)newRight, (int)newBottom);
41  }
42 
43  @Override
44  public synchronized void draw(Rect rect)
45  {
46  if (mFrame == null || !(mFrame.is(Extension.VIDEO_FRAME)))
47  return;
48 
49  try(VideoFrame vf = mFrame.as(Extension.VIDEO_FRAME)) {
50  int size = vf.getStride() * vf.getHeight();
51  if(mBuffer == null || mBuffer.array().length != size){
52  mBuffer = ByteBuffer.allocate(size);
53  mBuffer.order(ByteOrder.LITTLE_ENDIAN);
54  }
55  mFrame.getData(mBuffer.array());
56  mBuffer.rewind();
57 
58  upload(vf, mBuffer, mGlTexture.get(0));
59  Rect r = adjustRatio(rect);
60  draw(r, mGlTexture.get(0));
61  }
62  }
63 
64  @Override
65  public synchronized void close() {
66  if(mFrame != null)
67  mFrame.close();
68  GLES10.glDeleteTextures(1, mGlTexture);
69  }
70 
71  public static void upload(VideoFrame vf, ByteBuffer buffer, int texture)
72  {
73  GLES10.glBindTexture(GLES10.GL_TEXTURE_2D, texture);
75  switch (profile.getFormat())
76  {
77  case RGB8:
78  case BGR8:
79  GLES10.glTexImage2D(GLES10.GL_TEXTURE_2D, 0, GLES10.GL_RGB, vf.getWidth(), vf.getHeight(), 0, GLES10.GL_RGB, GLES10.GL_UNSIGNED_BYTE, buffer);
80  break;
81  case RGBA8:
82  GLES10.glTexImage2D(GLES10.GL_TEXTURE_2D, 0, GLES10.GL_RGBA, vf.getWidth(), vf.getHeight(), 0, GLES10.GL_RGBA, GLES10.GL_UNSIGNED_BYTE, buffer);
83  break;
84  case Y8:
85  GLES10.glTexImage2D(GLES10.GL_TEXTURE_2D, 0, GLES10.GL_LUMINANCE, vf.getWidth(), vf.getHeight(), 0, GLES10.GL_LUMINANCE, GLES10.GL_UNSIGNED_BYTE, buffer);
86  break;
87  default:
88  throw new RuntimeException("The requested format is not supported by the viewer");
89  }
90  }
91 
92 
93  GLES10.glTexParameterx(GLES10.GL_TEXTURE_2D, GLES10.GL_TEXTURE_MAG_FILTER, GLES10.GL_LINEAR);
94  GLES10.glTexParameterx(GLES10.GL_TEXTURE_2D, GLES10.GL_TEXTURE_MIN_FILTER, GLES10.GL_LINEAR);
95  GLES10.glTexParameterx(GLES10.GL_TEXTURE_2D, GLES10.GL_TEXTURE_WRAP_S, 0x2900);
96  GLES10.glTexParameterx(GLES10.GL_TEXTURE_2D, GLES10.GL_TEXTURE_WRAP_T, 0x2900);
97  GLES10.glPixelStorei(0x0CF2, 0);
98  GLES10.glBindTexture(GLES10.GL_TEXTURE_2D, 0);
99  }
100 
101  public static void draw(Rect r, int texture)
102  {
103  setViewport(r);
104 
105  GLES10.glBindTexture(GLES10.GL_TEXTURE_2D, texture);
106  GLES10.glEnable(GLES10.GL_TEXTURE_2D);
107 
108  float[] verArray = {
109  0, 0,
110  0, r.height(),
111  r.width(), r.height(),
112  r.width(), 0
113  };
114  ByteBuffer ver = ByteBuffer.allocateDirect(verArray.length * 4);
115  ver.order(ByteOrder.nativeOrder());
116 
117  float[] texArray = {
118  0,0,
119  0,1,
120  1,1,
121  1,0
122  };
123  ByteBuffer tex = ByteBuffer.allocateDirect(texArray.length * 4);
124  tex.order(ByteOrder.nativeOrder());
125 
126  ver.asFloatBuffer().put(verArray);
127  tex.asFloatBuffer().put(texArray);
128 
129  GLES10.glEnableClientState(GLES10.GL_VERTEX_ARRAY);
130  GLES10.glEnableClientState(GLES10.GL_TEXTURE_COORD_ARRAY);
131 
132  GLES10.glVertexPointer(2, GLES10.GL_FLOAT, 0, ver);
133  GLES10.glTexCoordPointer(2, GLES10.GL_FLOAT, 0, tex);
134  GLES10.glDrawArrays(GLES10.GL_TRIANGLE_FAN,0,4);
135 
136  GLES10.glDisableClientState(GLES10.GL_VERTEX_ARRAY);
137  GLES10.glDisableClientState(GLES10.GL_TEXTURE_COORD_ARRAY);
138 
139  GLES10.glDisable(GLES10.GL_TEXTURE_2D);
140  GLES10.glBindTexture(GLES10.GL_TEXTURE_2D, 0);
141  }
142 }
GLenum GLfloat * buffer
The texture class.
Definition: example.hpp:402
GLdouble f
GLsizeiptr size
GLdouble GLdouble r
static void draw(Rect r, int texture)
Definition: example.hpp:70
GLuint in
Definition: glext.h:8859
static void upload(VideoFrame vf, ByteBuffer buffer, int texture)
boolean is(Extension extension)
Definition: Frame.java:9


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:16