GLRenderer.java
Go to the documentation of this file.
1 package com.intel.realsense.librealsense;
2 
3 import android.graphics.Point;
4 import android.graphics.Rect;
5 import android.opengl.GLES10;
6 import android.opengl.GLSurfaceView;
7 import android.util.Pair;
8 
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13 
14 import javax.microedition.khronos.egl.EGLConfig;
15 import javax.microedition.khronos.opengles.GL10;
16 
17 public class GLRenderer implements GLSurfaceView.Renderer, AutoCloseable{
18 
19  private final Map<Integer,GLFrame> mFrames = new HashMap<>();
20  private int mWindowHeight = 0;
21  private int mWindowWidth = 0;
22  private float mDeltaX = 0;
23  private float mDeltaY = 0;
25  private boolean mHasColorRbg8 = false;
26  private Colorizer mColorizer = new Colorizer();
27  private Map<StreamType,Pointcloud> mPointcloud = null;
28  private boolean mHasColorizedDepth = false;
29 
30  private boolean mHasColorYuy = false;
32 
33  public Map<Integer, Pair<String,Rect>> getRectangles() {
34  return calcRectangles();
35  }
36 
37  private boolean showPoints(){
38  return mPointcloud != null;
39  }
40 
41  private List<FilterInterface> createProcessingPipe(){
42  List<FilterInterface> rv = new ArrayList<>();
43  if(!mHasColorizedDepth && !showPoints())
44  rv.add(mColorizer);
45 
46  // convert yuyv into rgb8 for display and uv mapping
47  if(mHasColorYuy)
48  rv.add(mYuyDecoder);
49 
50  if(showPoints()){
51  if(mHasColorRbg8 || mHasColorYuy)
52  rv.add(mPointcloud.get(StreamType.COLOR));
53  else
54  rv.add(mPointcloud.get(StreamType.DEPTH));
55  }
56  return rv;
57  }
58 
59  private FrameSet applyFilters(FrameSet frameSet, List<FilterInterface> filters){
60  frameSet = frameSet.clone();
61  for(FilterInterface f : filters){
62  FrameSet newSet = frameSet.applyFilter(f);
63  frameSet.close();
64  frameSet = newSet;
65  }
66  return frameSet;
67  }
68 
69  public void upload(FrameSet frameSet) {
70  mHasColorRbg8 = mHasColorizedDepth = mHasColorYuy = false;
71  frameSet.foreach(new FrameCallback() {
72  @Override
73  public void onFrame(Frame f) {
74  getTexture(f);
75  }
76  });
77 
78  List<FilterInterface> filters = createProcessingPipe();
79  try(FrameSet processed = applyFilters(frameSet, filters)){
80  choosePointsTexture(processed);
81  processed.foreach(new FrameCallback() {
82  @Override
83  public void onFrame(Frame f) {
84  addFrame(f);
85  upload(f);
86  }
87  });
88  }
89  }
90 
91  private void choosePointsTexture(FrameSet frameSet){
92  if(!showPoints())
93  return;
94  if(mHasColorRbg8 || mHasColorYuy)
95  mPointsTexture = frameSet.first(StreamType.COLOR, StreamFormat.RGB8);
96  else{
97  try (Frame d = frameSet.first(StreamType.DEPTH, StreamFormat.Z16)) {
98  if(d != null)
99  mPointsTexture = mColorizer.process(d);
100  }
101  }
102  }
103 
104  private void getTexture(Frame f){
105  try(StreamProfile sp = f.getProfile()){
106  if(sp.getType() == StreamType.COLOR && sp.getFormat() == StreamFormat.RGB8) {
107  mHasColorRbg8 = true;
108  }
109 
110  if(sp.getType() == StreamType.COLOR && sp.getFormat() == StreamFormat.YUYV) {
111  mHasColorYuy = true;
112  }
113 
114  if(sp.getType() == StreamType.DEPTH && sp.getFormat() == StreamFormat.RGB8) {
115  mHasColorizedDepth = true;
116  }
117  }
118  }
119 
120  private void addFrame(Frame f){
121  try(StreamProfile sp = f.getProfile()){
122  if(!isFormatSupported(sp.getFormat()))
123  return;
124  int uid = sp.getUniqueId();
125  if(!mFrames.containsKey(uid)){
126  synchronized (mFrames) {
127  if(f.is(Extension.VIDEO_FRAME) && !showPoints())
128  mFrames.put(uid, new GLVideoFrame());
129  if(f.is(Extension.MOTION_FRAME) && !showPoints())
130  mFrames.put(uid, new GLMotionFrame());
131  if(f.is(Extension.POINTS))
132  mFrames.put(uid, new GLPointsFrame());
133  }
134  }
135  }
136  }
137 
138  public void upload(Frame f) {
139  if(f == null)
140  return;
141 
142  try(StreamProfile sp = f.getProfile()){
143  if(!isFormatSupported(sp.getFormat()))
144  return;
145 
146  addFrame(f);
147  int uid = sp.getUniqueId();
148 
149  GLFrame curr = mFrames.get(uid);
150  if(curr == null)
151  return;
152  curr.setFrame(f);
153 
154  if(mPointsTexture != null && curr instanceof GLPointsFrame){
155  ((GLPointsFrame) curr).setTextureFrame(mPointsTexture);
156  mPointsTexture.close();
157  mPointsTexture = null;
158  }
159  }
160  }
161 
162  public void clear() {
163  synchronized (mFrames) {
164  for(Map.Entry<Integer,GLFrame> f : mFrames.entrySet())
165  f.getValue().close();
166  mFrames.clear();
167  mDeltaX = 0;
168  mDeltaY = 0;
169  mPointcloud = null;
170  if(mPointsTexture != null) mPointsTexture.close();
171  mPointsTexture = null;
172  }
173  }
174 
175  private Map<Integer, Pair<String,Rect>> calcRectangles(){
176  Map<Integer, Pair<String,Rect>> rv = new HashMap<>();
177 
178  int i = 0;
179  for (Map.Entry<Integer, GLFrame> entry : mFrames.entrySet()){
180  Point size = mWindowWidth > mWindowHeight ?
181  new Point(mWindowWidth / mFrames.size(), mWindowHeight) :
182  new Point(mWindowWidth, mWindowHeight / mFrames.size());
183  Point pos = mWindowWidth > mWindowHeight ?
184  new Point(i++ * size.x, 0) :
185  new Point(0, i++ * size.y);
186  rv.put(entry.getKey(), new Pair<>(entry.getValue().getLabel(), new Rect(pos.x, pos.y, pos.x + size.x, pos.y + size.y)));
187  }
188  return rv;
189  }
190 
191  @Override
192  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
193 
194  }
195 
196  @Override
197  public void onSurfaceChanged(GL10 gl, int width, int height) {
198  mWindowWidth = width;
199  mWindowHeight = height;
200  }
201 
202  @Override
203  public void onDrawFrame(GL10 gl) {
204  synchronized (mFrames) {
205  GLES10.glViewport(0, 0, mWindowWidth, mWindowHeight);
206  GLES10.glClearColor(0, 0, 0, 1);
207  GLES10.glClear(GLES10.GL_COLOR_BUFFER_BIT | GLES10.GL_DEPTH_BUFFER_BIT);
208 
209  if (mFrames.size() == 0)
210  return;
211 
212  Map<Integer, Pair<String, Rect>> rects = calcRectangles();
213 
214  for(Integer uid : mFrames.keySet()){
215  GLFrame fl = mFrames.get(uid);
216 
217  Rect r = rects.get(uid).second;
218  if(mWindowHeight > mWindowWidth){// TODO: remove, w/a for misaligned labels
219  int newTop = mWindowHeight - r.height() - r.top;
220  r = new Rect(r.left, newTop, r.right, newTop + r.height());
221  }
222  fl.draw(r);
223  if(fl instanceof GLPointsFrame){
224  ((GLPointsFrame)fl).rotate(mDeltaX, mDeltaY);
225  mDeltaX = 0;
226  mDeltaY = 0;
227  }
228  }
229  }
230  }
231 
233  switch (format){
234  case RGB8:
235  case RGBA8:
236  case Y8:
237  case MOTION_XYZ32F:
238  case XYZ32F: return true;
239  default: return false;
240  }
241  }
242 
243  public void onTouchEvent(float dx, float dy) {
244  synchronized (mFrames) {
245  mDeltaX = dx;
246  mDeltaY = dy;
247  }
248  }
249 
250  public void showPointcloud(boolean showPoints) {
251  if(showPoints){
252  if(mPointcloud != null)
253  return;
254  mPointcloud = new HashMap<>();
255  mPointcloud.put(StreamType.COLOR, new Pointcloud(StreamType.COLOR));
256  mPointcloud.put(StreamType.DEPTH, new Pointcloud(StreamType.DEPTH));
257  }
258  else {
259  if(mPointcloud == null)
260  return;
261  for(Pointcloud pc : mPointcloud.values()){
262  pc.close();
263  }
264  mPointcloud = null;
265  }
266  }
267 
268  @Override
269  public void close() {
270  clear();
271  mColorizer.close();
272  }
273 }
boolean isFormatSupported(StreamFormat format)
void onSurfaceChanged(GL10 gl, int width, int height)
Map< Integer, Pair< String, Rect > > calcRectangles()
d
Definition: rmse.py:171
Frame process(Frame original)
Definition: Filter.java:7
GLuint entry
Definition: glext.h:10991
void choosePointsTexture(FrameSet frameSet)
Definition: GLRenderer.java:91
GLdouble f
GLsizeiptr size
GLdouble GLdouble r
void * EGLConfig
Definition: egl_context.h:113
GLint GLsizei GLsizei height
GLint GLint GLsizei GLint GLenum format
FrameSet applyFilter(FilterInterface filter)
Definition: FrameSet.java:37
FrameSet applyFilters(FrameSet frameSet, List< FilterInterface > filters)
Definition: GLRenderer.java:59
synchronized void setFrame(Frame frame)
Definition: GLFrame.java:14
final Map< Integer, GLFrame > mFrames
Definition: GLRenderer.java:19
void foreach(FrameCallback callback)
Definition: FrameSet.java:27
List< FilterInterface > createProcessingPipe()
Definition: GLRenderer.java:41
void onSurfaceCreated(GL10 gl, EGLConfig config)
float rs2_vector::* pos
boolean is(Extension extension)
Definition: Frame.java:9
int i
Map< StreamType, Pointcloud > mPointcloud
Definition: GLRenderer.java:27
Map< Integer, Pair< String, Rect > > getRectangles()
Definition: GLRenderer.java:33
GLint GLsizei width
::geometry_msgs::Point_< std::allocator< void > > Point
Definition: Point.h:57


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