Project

General

Profile

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

library / src / main / java / org / distorted / library / main / Distorted.java @ 466450b5

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 6a06a912 Leszek Koltunski
22 30beb34f Leszek Koltunski
import android.app.ActivityManager;
23 7845dc66 Leszek Koltunski
import android.content.Context;
24 30beb34f Leszek Koltunski
import android.content.pm.ConfigurationInfo;
25 d6e94c84 Leszek Koltunski
import android.content.res.Resources;
26 edea9cf3 Leszek Koltunski
import android.opengl.GLES31;
27 310e14fb leszek
28 26a4e5f6 leszek
import org.distorted.library.effect.Effect;
29 7cd24173 leszek
import org.distorted.library.effect.FragmentEffect;
30 aa2f0486 Leszek Koltunski
import org.distorted.library.effect.PostprocessEffect;
31 7cd24173 leszek
import org.distorted.library.effect.VertexEffect;
32 6a06a912 Leszek Koltunski
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
/**
35
 * A singleton class used to control various global settings.
36
 */
37
public class Distorted 
38 39cbf9dc Leszek Koltunski
  {
39 c25273e0 Leszek Koltunski
  public static final int GLSL = 310;
40
  public static final String GLSL_VERSION= "#version 310 es\n";
41 6a06a912 Leszek Koltunski
  /**
42 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
43
   * backing up our DistortedTexture.
44 6a06a912 Leszek Koltunski
   * <p>
45 cacc63de Leszek Koltunski
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
46 6a06a912 Leszek Koltunski
   * apply different effects. Used in the copy constructor.
47
   */
48 29a06526 Leszek Koltunski
  public static final int CLONE_SURFACE = 0x1;
49 6a06a912 Leszek Koltunski
  /**
50 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
51 6a06a912 Leszek Koltunski
   * <p>
52 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
53 6a06a912 Leszek Koltunski
   */
54 015642fb Leszek Koltunski
  public static final int CLONE_MATRIX = 0x2;
55 6a06a912 Leszek Koltunski
  /**
56 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
57 6a06a912 Leszek Koltunski
   * <p>
58 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
59 6a06a912 Leszek Koltunski
   */
60
  public static final int CLONE_VERTEX  = 0x4;
61
  /**
62 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
63 6a06a912 Leszek Koltunski
   * <p>
64 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
65 6a06a912 Leszek Koltunski
   */
66
  public static final int CLONE_FRAGMENT= 0x8;
67 d6e94c84 Leszek Koltunski
   /**
68
   * When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
69
   * <p>
70
   * This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
71
   */
72
  public static final int CLONE_POSTPROCESS= 0x10;
73 6a06a912 Leszek Koltunski
  /**
74 a09ada4c Leszek Koltunski
   * When creating an instance of a DistortedNode from another instance, clone the children Nodes.
75 6a06a912 Leszek Koltunski
   * <p>
76 cacc63de Leszek Koltunski
   * This is mainly useful for creating many similar sub-trees and rendering then at different places
77
   * on the screen with (optionally) different Effects.
78 6a06a912 Leszek Koltunski
   */
79 d6e94c84 Leszek Koltunski
  public static final int CLONE_CHILDREN= 0x20;
80 b3618cb5 Leszek Koltunski
81 586b5fa1 Leszek Koltunski
  /**
82
   * Work around bugs in ARM Mali driver by, instead to a single FBO, rendering to a circular queue
83
   * of FBO_QUEUE_SIZE FBOs. (otherwise we sometimes get a 'full pipeline flush' and the end result
84
   * might be missing part of the Objects)
85 6672d895 Leszek Koltunski
   *
86 12f9e4bb Leszek Koltunski
   * This bug only exists on Mali driver r12.
87 6672d895 Leszek Koltunski
   *
88
   * https://community.arm.com/graphics/f/discussions/10285/opengl-es-3-1-on-mali-t880-flashes
89 586b5fa1 Leszek Koltunski
   */
90 1f19fba8 Leszek Koltunski
  public static final int FBO_QUEUE_SIZE = 4;
91 86c352e0 Leszek Koltunski
  /**
92 a13dde77 Leszek Koltunski
   * Number of Main program variants (ATM 3: MAIN, MAIN OIT, PREPROCESS)
93 86c352e0 Leszek Koltunski
   */
94 a13dde77 Leszek Koltunski
  public static final int MAIN_VARIANTS = 3;
95 586b5fa1 Leszek Koltunski
96 55c14a19 Leszek Koltunski
  private static boolean mInitialized=false;
97 6a06a912 Leszek Koltunski
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99 c638c1b0 Leszek Koltunski
// private: hide this from Javadoc
100 6a06a912 Leszek Koltunski
101
  private Distorted()
102
    {
103 8eccf334 Leszek Koltunski
104 6a06a912 Leszek Koltunski
    }
105
106 edea9cf3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
107
// ARM Mali driver r12 has problems when we keep swapping many FBOs (fixed in r22)
108
// PowerVR GE8100 compiler fails to compile OIT programs.
109
110
  static void detectBuggyDrivers()
111
    {
112
    String vendor  = GLES31.glGetString(GLES31.GL_VENDOR);
113
    String version = GLES31.glGetString(GLES31.GL_VERSION);
114
    String renderer= GLES31.glGetString(GLES31.GL_RENDERER);
115
116
    /*
117
    android.util.Log.e("DISTORTED", "GLSL Version "+GLES31.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION));
118
    android.util.Log.e("DISTORTED", "GL Version "  +GLES31.glGetString(GLES31.GL_VERSION));
119
    android.util.Log.e("DISTORTED", "GL Vendor "   +GLES31.glGetString(GLES31.GL_VENDOR));
120
    android.util.Log.e("DISTORTED", "GL Renderer " +GLES31.glGetString(GLES31.GL_RENDERER));
121
    */
122
123
    if( vendor.contains("ARM") )
124
      {
125
      if( version.contains("r12") )
126
        {
127 c1a38ba3 Leszek Koltunski
        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.");
128 edea9cf3 Leszek Koltunski
        }
129
      }
130
    else if( vendor.contains("Imagination") )
131
      {
132
      if( renderer.contains("GE8") )
133
        {
134 c1a38ba3 Leszek Koltunski
        android.util.Log.e("DISTORTED", "You are running this on a PowerVR GE8XXX.\nDue to a buggy compiler OIT rendering will not work");
135 edea9cf3 Leszek Koltunski
        }
136
      }
137
    }
138
139 421c2728 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
140 310e14fb leszek
/**
141
 * Have we called onCreate yet, ie have we initialized the library?
142 1dfc9074 leszek
 * @return <code>true</code> if the library is initialized and ready for action.
143 310e14fb leszek
 */
144
  public static boolean isInitialized()
145 421c2728 Leszek Koltunski
    {
146 55c14a19 Leszek Koltunski
    return mInitialized;
147 421c2728 Leszek Koltunski
    }
148
149 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
150
/**
151
 * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
152 432442f9 Leszek Koltunski
 * I.e. best called from GLSurfaceView.onCreate().
153 6a06a912 Leszek Koltunski
 * <p>
154 432442f9 Leszek Koltunski
 * Needs to be called from a thread holding the OpenGL context.
155 6a06a912 Leszek Koltunski
 *   
156 015642fb Leszek Koltunski
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
157 6a06a912 Leszek Koltunski
 */
158 edea9cf3 Leszek Koltunski
  public static void onCreate(final Context context) throws Exception
159 d6e94c84 Leszek Koltunski
    {
160 30beb34f Leszek Koltunski
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
161
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
162
    android.util.Log.e("DISTORTED", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
163 edea9cf3 Leszek Koltunski
164
    mInitialized = true;
165
166
    detectBuggyDrivers();
167
168 0e924ba5 Leszek Koltunski
    EffectMessageSender.startSending();
169
170 d6e94c84 Leszek Koltunski
    final Resources resources = context.getResources();
171 c90b9e01 Leszek Koltunski
172 edea9cf3 Leszek Koltunski
    Exception exception=null;
173
174
    try
175
      {
176
      DistortedEffects.createPrograms(resources);
177
      }
178
    catch(Exception ex)
179
      {
180
      exception = ex;
181
      }
182
183
    try
184
      {
185
      DistortedEffects.createProgramsOIT(resources);
186
      }
187
    catch(Exception ex)
188
      {
189
      exception = ex;
190
      }
191
192 a13dde77 Leszek Koltunski
    try
193
      {
194
      EffectQueuePostprocess.createPrograms(resources);
195
      }
196
    catch(Exception ex)
197
      {
198
      exception = ex;
199
      }
200
201 edea9cf3 Leszek Koltunski
    try
202
      {
203
      PostprocessEffect.createPrograms();
204
      }
205
    catch(Exception ex)
206
      {
207
      exception = ex;
208
      }
209
210
    if( exception!=null)
211
      {
212
      throw exception;
213
      }
214 6a06a912 Leszek Koltunski
    }
215
216 05ecc6fe Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
217
/**
218 78e89fb5 Leszek Koltunski
 * Call this so that the Library can release the OpenGL related data that needs to be recreated.
219 05ecc6fe Leszek Koltunski
 * Must be called from Activity.onPause().
220
 */
221
  public static void onPause()
222
    {
223 226144d0 leszek
    DistortedObject.onPause();
224 78e89fb5 Leszek Koltunski
    DistortedEffects.onPause();
225 05ecc6fe Leszek Koltunski
    }
226
227 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
228
/**
229
 * Call this so that the Library can release its internal data structures.
230
 * Must be called from Activity.onDestroy(). 
231
 */
232
  public static void onDestroy()
233
    {
234 226144d0 leszek
    DistortedObject.onDestroy();
235 a09ada4c Leszek Koltunski
    DistortedNode.onDestroy();
236 421c2728 Leszek Koltunski
    DistortedEffects.onDestroy();
237 efe3d8fe leszek
    DistortedMaster.onDestroy();
238 ce154014 Leszek Koltunski
    DistortedOutputSurface.onDestroy();
239 42571056 Leszek Koltunski
    EffectQueue.onDestroy();
240 26a4e5f6 leszek
    Effect.onDestroy();
241 7cd24173 leszek
    VertexEffect.onDestroy();
242
    FragmentEffect.onDestroy();
243 6a06a912 Leszek Koltunski
    EffectMessageSender.stopSending();
244 f8686932 Leszek Koltunski
245 55c14a19 Leszek Koltunski
    mInitialized = false;
246 6a06a912 Leszek Koltunski
    }
247 f8f6d457 leszek
  }