Project

General

Profile

Download (6.93 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / main / DistortedScreen.java @ e1e94682

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library.main;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

    
24
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.opengl.GLES31;
28

    
29
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.mesh.MeshQuad;
31
import org.distorted.library.type.Static3D;
32

    
33
/**
34
 * Class which represents the Screen.
35
 * <p>
36
 * User is able to render to it just like to a DistortedFramebuffer.
37
 */
38
public class DistortedScreen extends DistortedFramebuffer
39
  {
40
  ///// DEBUGGING ONLY /////////////////////////
41
  private boolean mShowFPS;
42

    
43
  private static final int NUM_FRAMES  = 100;
44

    
45
  private MeshQuad fpsMesh;
46
  private DistortedTexture fpsTexture;
47
  private DistortedEffects fpsEffects;
48
  private Canvas fpsCanvas;
49
  private Bitmap fpsBitmap;
50
  private Paint mPaint;
51
  private int fpsH, fpsW;
52
  private String fpsString;
53
  private long lastTime=0;
54
  private long[] durations;
55
  private int currDuration;
56
  private static MatrixEffectMove mMoveEffect = new MatrixEffectMove( new Static3D(5,5,0) );
57
  ///// END DEBUGGING //////////////////////////
58

    
59
  private int mCurRenderedFBO;    // During the first FBO_QUEUE_SIZE frames, we blit the very first
60
  private int mToBeBlittedFBO;    // FBO one we have rendered. Then, we keep blitting the one we
61
  private boolean mFirstCircle;   // rendered FBO_QUEUE_SIZE ago.
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
// PUBLIC API
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
/**
67
 * Create a new Screen. Initially 1x1 in size.
68
 * <p>
69
 * Has to be followed by a 'resize()' to set the size.
70
 */
71
  public DistortedScreen()
72
    {
73
    super(Distorted.FBO_QUEUE_SIZE,1,BOTH_DEPTH_STENCIL, TYPE_SYST, 1,1);
74
    mShowFPS = false;
75
    mCurRenderedFBO = 0;
76
    mToBeBlittedFBO = 0;
77
    mFirstCircle = true;
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
/**
82
 * Draws all the attached children to this OutputSurface.
83
 * <p>
84
 * Must be called from a thread holding OpenGL Context.
85
 *
86
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
87
 * @return Number of objects rendered.
88
 */
89
  public int render(long time)
90
    {
91
    if( mShowFPS )
92
      {
93
      if( lastTime==0 ) lastTime = time;
94

    
95
      currDuration++;
96
      if (currDuration >= NUM_FRAMES) currDuration = 0;
97
      durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
98
      durations[currDuration] = time - lastTime;
99

    
100
      fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
101

    
102
      mPaint.setColor(0xffffffff);
103
      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
104
      mPaint.setColor(0xff000000);
105
      fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
106
      fpsTexture.setTexture(fpsBitmap);
107

    
108
      lastTime = time;
109
      }
110

    
111
    int numrender = super.render(time,mCurRenderedFBO);
112

    
113
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
114

    
115
    // workaround for the Mali issues: blit the framebuffer we have computed Distorted.FBO_QUEUE_SIZE
116
    // frames ago. Looks like FBO_QUEUE_SIZE=2 solves the issue already but I decided to play it safe and
117
    // make it equal to 3.
118
    // This of course introduces a delay and uses more memory, but it does not appear to have any effect
119
    // on speed. Maybe a slight positive effect if any!
120
    setAsInput(mToBeBlittedFBO,0);
121

    
122
    GLES31.glColorMask(true,true,true,true);
123
    GLES31.glDepthMask(false);
124
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
125
    GLES31.glDisable(GLES31.GL_DEPTH_TEST);
126
    GLES31.glDisable(GLES31.GL_BLEND);
127

    
128
    DistortedEffects.blitPriv(this);
129

    
130
    if( mShowFPS && fpsTexture.setAsInput())
131
      {
132
      fpsEffects.drawPriv(fpsW / 2.0f, fpsH / 2.0f, fpsMesh, this, time);
133
      }
134

    
135
    if( ++mCurRenderedFBO>=Distorted.FBO_QUEUE_SIZE )
136
      {
137
      mCurRenderedFBO = 0;
138
      if (mFirstCircle) mFirstCircle = false;
139
      }
140
    if( !mFirstCircle && ++mToBeBlittedFBO>=Distorted.FBO_QUEUE_SIZE )
141
      {
142
      mToBeBlittedFBO=0;
143
      }
144

    
145
    return numrender+1;
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
/**
150
 * Make the library show Frames Per Second in the upper-left corner.
151
 * <p>
152
 */
153
  public void showFPS()
154
    {
155
    if( !mShowFPS )
156
      {
157
      mShowFPS = true;
158

    
159
      fpsW = 120;
160
      fpsH = 70;
161

    
162
      fpsString = "";
163
      fpsBitmap = Bitmap.createBitmap(fpsW, fpsH, Bitmap.Config.ARGB_8888);
164
      fpsMesh = new MeshQuad();
165
      fpsTexture = new DistortedTexture(fpsW, fpsH);
166
      fpsTexture.setTexture(fpsBitmap);
167
      fpsCanvas = new Canvas(fpsBitmap);
168
      fpsEffects = new DistortedEffects();
169
      fpsEffects.apply(mMoveEffect);
170

    
171
      mPaint = new Paint();
172
      mPaint.setAntiAlias(true);
173
      mPaint.setTextAlign(Paint.Align.CENTER);
174
      mPaint.setTextSize(0.7f * fpsH);
175

    
176
      durations = new long[NUM_FRAMES + 1];
177
      currDuration = 0;
178

    
179
      for (int i = 0; i < NUM_FRAMES + 1; i++) durations[i] = 16;  // Assume FPS will be
180
      durations[NUM_FRAMES] = NUM_FRAMES * 16;              // close to 1000/16 ~ 60
181
      }
182
    }
183
  }
(10-10/17)