@@ -26,21 +26,23 @@ public class GLRenderer implements GLSurfaceView.Renderer, AutoCloseable{
2626 private Colorizer mColorizer = new Colorizer ();
2727 private Map <StreamType ,Pointcloud > mPointcloud = null ;
2828 private boolean mHasColorizedDepth = false ;
29+ private boolean mHasDepth = false ;
2930
3031 private boolean mHasColorYuy = false ;
3132 private YuyDecoder mYuyDecoder = new YuyDecoder ();
33+ private boolean mShowPoints = false ;
3234
3335 public Map <Integer , Pair <String ,Rect >> getRectangles () {
3436 return calcRectangles ();
3537 }
3638
3739 private boolean showPoints (){
38- return mPointcloud != null ;
40+ return mShowPoints ;
3941 }
4042
4143 private List <FilterInterface > createProcessingPipe (){
4244 List <FilterInterface > rv = new ArrayList <>();
43- if (!mHasColorizedDepth && !showPoints ())
45+ if (mHasDepth && !mHasColorizedDepth && !showPoints ())
4446 rv .add (mColorizer );
4547
4648 // convert yuyv into rgb8 for display and uv mapping
@@ -50,14 +52,15 @@ private List<FilterInterface> createProcessingPipe(){
5052 if (showPoints ()){
5153 if (mHasColorRbg8 || mHasColorYuy )
5254 rv .add (mPointcloud .get (StreamType .COLOR ));
53- else
55+ else if ( mHasDepth )
5456 rv .add (mPointcloud .get (StreamType .DEPTH ));
5557 }
5658 return rv ;
5759 }
5860
5961 private FrameSet applyFilters (FrameSet frameSet , List <FilterInterface > filters ){
6062 frameSet = frameSet .clone ();
63+
6164 for (FilterInterface f : filters ){
6265 FrameSet newSet = frameSet .applyFilter (f );
6366 frameSet .close ();
@@ -67,7 +70,8 @@ private FrameSet applyFilters(FrameSet frameSet, List<FilterInterface> filters){
6770 }
6871
6972 public void upload (FrameSet frameSet ) {
70- mHasColorRbg8 = mHasColorizedDepth = mHasColorYuy = false ;
73+ mHasColorRbg8 = mHasColorizedDepth = mHasColorYuy = mHasDepth = false ;
74+
7175 frameSet .foreach (new FrameCallback () {
7276 @ Override
7377 public void onFrame (Frame f ) {
@@ -76,27 +80,36 @@ public void onFrame(Frame f) {
7680 });
7781
7882 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- });
83+
84+ if (!showPoints () || (showPoints () && filters .size () > 0 && mHasDepth )) {
85+ try (FrameSet processed = applyFilters (frameSet , filters )) {
86+ choosePointsTexture (processed );
87+ processed .foreach (new FrameCallback () {
88+ @ Override
89+ public void onFrame (Frame f ) {
90+ upload (f );
91+ }
92+ });
93+ }
8894 }
8995 }
9096
9197 private void choosePointsTexture (FrameSet frameSet ){
92- if (!showPoints ())
98+ if (mPointsTexture != null ) mPointsTexture .close ();
99+ mPointsTexture = null ;
100+
101+ if (!showPoints ()) {
93102 return ;
103+ }
104+
94105 if (mHasColorRbg8 || mHasColorYuy )
95106 mPointsTexture = frameSet .first (StreamType .COLOR , StreamFormat .RGB8 );
96107 else {
97108 try (Frame d = frameSet .first (StreamType .DEPTH , StreamFormat .Z16 )) {
98- if (d != null )
109+ if (d != null ) {
99110 mPointsTexture = mColorizer .process (d );
111+ d .close ();
112+ }
100113 }
101114 }
102115 }
@@ -111,8 +124,12 @@ private void getTexture(Frame f){
111124 mHasColorYuy = true ;
112125 }
113126
114- if (sp .getType () == StreamType .DEPTH && sp .getFormat () == StreamFormat .RGB8 ) {
115- mHasColorizedDepth = true ;
127+ if (sp .getType () == StreamType .DEPTH ) {
128+ mHasDepth = true ;
129+
130+ if (sp .getFormat () == StreamFormat .RGB8 ) {
131+ mHasColorizedDepth = true ;
132+ }
116133 }
117134 }
118135 }
@@ -122,13 +139,14 @@ private void addFrame(Frame f){
122139 if (!isFormatSupported (sp .getFormat ()))
123140 return ;
124141 int uid = sp .getUniqueId ();
142+
125143 if (!mFrames .containsKey (uid )){
126144 synchronized (mFrames ) {
127145 if (f .is (Extension .VIDEO_FRAME ) && !showPoints ())
128146 mFrames .put (uid , new GLVideoFrame ());
129- if (f .is (Extension .MOTION_FRAME ) && !showPoints ())
147+ else if (f .is (Extension .MOTION_FRAME ) && !showPoints ())
130148 mFrames .put (uid , new GLMotionFrame ());
131- if (f .is (Extension .POINTS ))
149+ else if (f .is (Extension .POINTS ))
132150 mFrames .put (uid , new GLPointsFrame ());
133151 }
134152 }
@@ -149,6 +167,7 @@ public void upload(Frame f) {
149167 GLFrame curr = mFrames .get (uid );
150168 if (curr == null )
151169 return ;
170+
152171 curr .setFrame (f );
153172
154173 if (mPointsTexture != null && curr instanceof GLPointsFrame ){
@@ -161,8 +180,13 @@ public void upload(Frame f) {
161180
162181 public void clear () {
163182 synchronized (mFrames ) {
164- for (Map .Entry <Integer ,GLFrame > f : mFrames .entrySet ())
165- f .getValue ().close ();
183+ for (Map .Entry <Integer ,GLFrame > f : mFrames .entrySet ()) {
184+ GLFrame frame = f .getValue ();
185+ if (frame instanceof GLPointsFrame )
186+ ((GLPointsFrame ) frame ).close ();
187+ else
188+ frame .close ();
189+ }
166190 mFrames .clear ();
167191 mDeltaX = 0 ;
168192 mDeltaY = 0 ;
@@ -178,6 +202,8 @@ public void clear() {
178202
179203 if (mPointsTexture != null ) mPointsTexture .close ();
180204 mPointsTexture = null ;
205+
206+ mHasColorRbg8 = mHasColorizedDepth = mHasColorYuy = mHasDepth = false ;
181207 }
182208 }
183209
@@ -257,6 +283,8 @@ public void onTouchEvent(float dx, float dy) {
257283 }
258284
259285 public void showPointcloud (boolean showPoints ) {
286+ mShowPoints = showPoints ;
287+
260288 if (showPoints ){
261289 if (mPointcloud != null )
262290 return ;
@@ -271,6 +299,9 @@ public void showPointcloud(boolean showPoints) {
271299 pc .close ();
272300 }
273301 mPointcloud = null ;
302+
303+ if (mPointsTexture != null ) mPointsTexture .close ();
304+ mPointsTexture = null ;
274305 }
275306 }
276307
0 commit comments