Project

General

Profile

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

library / src / main / java / org / distorted / library / main / Distorted.java @ 8bfefd68

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
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.opengl.GLES31;
27

    
28
import org.distorted.library.effect.Effect;
29
import org.distorted.library.effect.PostprocessEffect;
30
import org.distorted.library.message.EffectMessageSender;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
/**
34
 * A singleton class used to control various global settings.
35
 */
36
public class Distorted 
37
  {
38
  public static final int GLSL = 310;
39
  public static final String GLSL_VERSION= "#version 310 es\n";
40
  /**
41
   * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
42
   * backing up our DistortedTexture.
43
   * <p>
44
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
45
   * apply different effects. Used in the copy constructor.
46
   */
47
  public static final int CLONE_SURFACE = 0x1;
48
  /**
49
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
50
   * <p>
51
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
52
   */
53
  public static final int CLONE_MATRIX = 0x2;
54
  /**
55
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
56
   * <p>
57
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
58
   */
59
  public static final int CLONE_VERTEX  = 0x4;
60
  /**
61
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
62
   * <p>
63
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
64
   */
65
  public static final int CLONE_FRAGMENT= 0x8;
66
   /**
67
   * When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
68
   * <p>
69
   * This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
70
   */
71
  public static final int CLONE_POSTPROCESS= 0x10;
72
  /**
73
   * When creating an instance of a DistortedNode from another instance, clone the children Nodes.
74
   * <p>
75
   * This is mainly useful for creating many similar sub-trees and rendering then at different places
76
   * on the screen with (optionally) different Effects.
77
   */
78
  public static final int CLONE_CHILDREN= 0x20;
79

    
80
  /**
81
   * Work around bugs in ARM Mali driver by, instead to a single FBO, rendering to a circular queue
82
   * of FBO_QUEUE_SIZE FBOs. (otherwise we sometimes get a 'full pipeline flush' and the end result
83
   * might be missing part of the Objects)
84
   *
85
   * This bug only exists on Mali driver r12.
86
   *
87
   * https://community.arm.com/graphics/f/discussions/10285/opengl-es-3-1-on-mali-t880-flashes
88
   */
89
  static final int FBO_QUEUE_SIZE = 4;
90
  /**
91
   * Number of Main program variants (ATM 3: MAIN, MAIN OIT, PREPROCESS)
92
   */
93
  static final int MAIN_VARIANTS = 3;
94

    
95
  private static boolean mInitialized=false;
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// private: hide this from Javadoc
99

    
100
  private Distorted()
101
    {
102

    
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
// ARM Mali driver r12 has problems when we keep swapping many FBOs (fixed in r22)
107
// PowerVR GE8100 compiler fails to compile OIT programs.
108

    
109
  private static void detectBuggyDrivers()
110
    {
111
    String vendor  = GLES31.glGetString(GLES31.GL_VENDOR);
112
    String version = GLES31.glGetString(GLES31.GL_VERSION);
113
    String renderer= GLES31.glGetString(GLES31.GL_RENDERER);
114

    
115
    /*
116
    android.util.Log.e("DISTORTED", "GLSL Version "+GLES31.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION));
117
    android.util.Log.e("DISTORTED", "GL Version "  +GLES31.glGetString(GLES31.GL_VERSION));
118
    android.util.Log.e("DISTORTED", "GL Vendor "   +GLES31.glGetString(GLES31.GL_VENDOR));
119
    android.util.Log.e("DISTORTED", "GL Renderer " +GLES31.glGetString(GLES31.GL_RENDERER));
120
    */
121

    
122
    if( vendor.contains("ARM") )
123
      {
124
      if( version.contains("r12") )
125
        {
126
        android.util.Log.e("DISTORTED", "You are running this on a ARM Mali driver r12.\nThis is a buggy driver, please update to r22. Problems with flashing expected.");
127
        }
128
      }
129
    else if( vendor.contains("Imagination") )
130
      {
131
      if( renderer.contains("GE8") )
132
        {
133
        android.util.Log.e("DISTORTED", "You are running this on a PowerVR GE8XXX.\nDue to a buggy compiler OIT rendering will not work");
134
        }
135
      }
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
/**
140
 * Have we called onCreate yet, ie have we initialized the library?
141
 * @return <code>true</code> if the library is initialized and ready for action.
142
 */
143
  public static boolean isInitialized()
144
    {
145
    return mInitialized;
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
/**
150
 * When OpenGL context gets created, call this method so that the library can initialise its internal data structures.
151
 * I.e. best called from GLSurfaceView.onCreate().
152
 * <p>
153
 * Needs to be called from a thread holding the OpenGL context.
154
 *   
155
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
156
 */
157
  public static void onCreate(final Context context) throws Exception
158
    {
159
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
160
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
161
    android.util.Log.e("DISTORTED", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
162

    
163
    mInitialized = true;
164

    
165
    detectBuggyDrivers();
166

    
167
    EffectMessageSender.startSending();
168

    
169
    final Resources resources = context.getResources();
170

    
171
    Exception exception=null;
172

    
173
    try
174
      {
175
      DistortedEffects.createPrograms(resources);
176
      }
177
    catch(Exception ex)
178
      {
179
      exception = ex;
180
      }
181

    
182
    try
183
      {
184
      DistortedEffects.createProgramsOIT(resources);
185
      }
186
    catch(Exception ex)
187
      {
188
      exception = ex;
189
      }
190

    
191
    try
192
      {
193
      EffectQueuePostprocess.createPrograms(resources);
194
      }
195
    catch(Exception ex)
196
      {
197
      exception = ex;
198
      }
199

    
200
    try
201
      {
202
      PostprocessEffect.createPrograms();
203
      }
204
    catch(Exception ex)
205
      {
206
      exception = ex;
207
      }
208

    
209
    if( exception!=null)
210
      {
211
      throw exception;
212
      }
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
/**
217
 * Call this so that the Library can release the OpenGL related data that needs to be recreated.
218
 * Must be called from Activity.onPause().
219
 */
220
  public static void onPause()
221
    {
222
    DistortedObject.onPause();
223
    DistortedEffects.onPause();
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
/**
228
 * Call this so that the Library can release its internal data structures.
229
 * Must be called from Activity.onDestroy(). 
230
 */
231
  public static void onDestroy()
232
    {
233
    DistortedObject.onDestroy();
234
    DistortedNodeData.onDestroy();
235
    DistortedEffects.onDestroy();
236
    DistortedMaster.onDestroy();
237
    DistortedOutputSurface.onDestroy();
238
    EffectQueue.onDestroy();
239
    Effect.onDestroy();
240
    EffectMessageSender.stopSending();
241

    
242
    mInitialized = false;
243
    }
244
  }
(1-1/18)