Also includes another bugfix when switching between surfaces.
Change-Id: Ieb1ccc59dd4d1bffb92a766372238799f7ba7cfe
return nativeIsActive();
}
+ public boolean isContextActive() {
+ return nativeIsContextActive();
+ }
+
+ public static boolean isAnyContextActive() {
+ return nativeIsAnyContextActive();
+ }
+
public void activate() {
if (Looper.myLooper() != null && Looper.myLooper().equals(Looper.getMainLooper())) {
Log.e("GLEnvironment", "Activating GL context in UI thread!");
private native boolean nativeIsActive();
+ private native boolean nativeIsContextActive();
+
+ private static native boolean nativeIsAnyContextActive();
+
private native boolean nativeActivate();
private native boolean nativeDeactivate();
@Override
public Object getObjectValue() {
+ assertGLEnvValid();
return ByteBuffer.wrap(getNativeData());
}
@Override
public void setInts(int[] ints) {
assertFrameMutable();
+ assertGLEnvValid();
if (!setNativeInts(ints)) {
throw new RuntimeException("Could not set int values for GL frame!");
}
@Override
public int[] getInts() {
+ assertGLEnvValid();
flushGPU("getInts");
return getNativeInts();
}
@Override
public void setFloats(float[] floats) {
assertFrameMutable();
+ assertGLEnvValid();
if (!setNativeFloats(floats)) {
throw new RuntimeException("Could not set int values for GL frame!");
}
@Override
public float[] getFloats() {
+ assertGLEnvValid();
flushGPU("getFloats");
return getNativeFloats();
}
@Override
public void setData(ByteBuffer buffer, int offset, int length) {
assertFrameMutable();
+ assertGLEnvValid();
byte[] bytes = buffer.array();
if (getFormat().getSize() != bytes.length) {
throw new RuntimeException("Data size in setData does not match GL frame size!");
@Override
public ByteBuffer getData() {
+ assertGLEnvValid();
flushGPU("getData");
return ByteBuffer.wrap(getNativeData());
}
@Override
public void setBitmap(Bitmap bitmap) {
assertFrameMutable();
+ assertGLEnvValid();
if (getFormat().getWidth() != bitmap.getWidth() ||
getFormat().getHeight() != bitmap.getHeight()) {
throw new RuntimeException("Bitmap dimensions do not match GL frame dimensions!");
@Override
public Bitmap getBitmap() {
+ assertGLEnvValid();
flushGPU("getBitmap");
Bitmap result = Bitmap.createBitmap(getFormat().getWidth(),
getFormat().getHeight(),
@Override
public void setDataFromFrame(Frame frame) {
+ assertGLEnvValid();
+
// Make sure frame fits
if (getFormat().getSize() < frame.getFormat().getSize()) {
throw new RuntimeException(
public void generateMipMap() {
assertFrameMutable();
+ assertGLEnvValid();
if (!generateNativeMipMap()) {
throw new RuntimeException("Could not generate mip-map for GL frame!");
}
public void setTextureParameter(int param, int value) {
assertFrameMutable();
+ assertGLEnvValid();
if (!setNativeTextureParam(param, value)) {
throw new RuntimeException("Could not set texture value " + param + " = " + value + " " +
"for GLFrame!");
+ ", FBO ID " + getFboId();
}
+ private void assertGLEnvValid() {
+ if (!mGLEnvironment.isContextActive()) {
+ if (GLEnvironment.isAnyContextActive()) {
+ throw new RuntimeException("Attempting to access " + this + " with foreign GL " +
+ "context active!");
+ } else {
+ throw new RuntimeException("Attempting to access " + this + " with no GL context " +
+ " active!");
+ }
+ }
+ }
+
static {
System.loadLibrary("filterfw");
}
return gl_env ? ToJBool(gl_env->IsActive()) : JNI_FALSE;
}
+jboolean Java_android_filterfw_core_GLEnvironment_nativeIsContextActive(JNIEnv* env, jobject thiz) {
+ GLEnv* gl_env = ConvertFromJava<GLEnv>(env, thiz);
+ return gl_env ? ToJBool(gl_env->IsContextActive()) : JNI_FALSE;
+}
+
+jboolean Java_android_filterfw_core_GLEnvironment_nativeIsAnyContextActive(JNIEnv* env,
+ jclass clazz) {
+ return ToJBool(GLEnv::IsAnyContextActive());
+}
+
jboolean Java_android_filterfw_core_GLEnvironment_nativeActivate(JNIEnv* env, jobject thiz) {
GLEnv* gl_env = ConvertFromJava<GLEnv>(env, thiz);
return gl_env ? ToJBool(gl_env->Activate()) : JNI_FALSE;
Java_android_filterfw_core_GLEnvironment_nativeIsActive(JNIEnv* env, jobject thiz);
JNIEXPORT jboolean JNICALL
+Java_android_filterfw_core_GLEnvironment_nativeIsContextActive(JNIEnv* env, jobject thiz);
+
+JNIEXPORT jboolean JNICALL
+Java_android_filterfw_core_GLEnvironment_nativeIsAnyContextActive(JNIEnv* env, jclass clazz);
+
+JNIEXPORT jboolean JNICALL
Java_android_filterfw_core_GLEnvironment_nativeActivate(JNIEnv* env, jobject thiz);
JNIEXPORT jboolean JNICALL
&& surface() == eglGetCurrentSurface(EGL_DRAW);
}
+bool GLEnv::IsContextActive() const {
+ return context() == eglGetCurrentContext();
+}
+
+bool GLEnv::IsAnyContextActive() {
+ return eglGetCurrentContext() != EGL_NO_CONTEXT;
+}
+
int GLEnv::AddWindowSurface(const EGLSurface& surface, WindowHandle* window_handle) {
const int id = ++max_surface_id_;
surfaces_[id] = SurfaceWindowPair(surface, window_handle);
}
bool GLEnv::SwitchToSurfaceId(int surface_id) {
- const SurfaceWindowPair* surface = FindOrNull(surfaces_, surface_id);
- if (surface) {
- if (surface_id_ != surface_id) {
+ if (surface_id_ != surface_id) {
+ const SurfaceWindowPair* surface = FindOrNull(surfaces_, surface_id);
+ if (surface) {
+ bool wasActive = IsActive();
surface_id_ = surface_id;
- return Activate();
+ return wasActive ? Activate() : true;
}
- return true;
+ return false;
}
- return false;
+ return true;
}
bool GLEnv::ReleaseSurfaceId(int surface_id) {
// Returns true if the environment is active in the current thread.
bool IsActive() const;
+ // Returns true if the environment's context is active in the curent thread.
+ bool IsContextActive() const;
+
+ // Returns true if there is any EGL context active in the current thread.
+ // This need not be a context created by a GLEnv instance.
+ static bool IsAnyContextActive();
+
// Attaching GL objects ////////////////////////////////////////////////////
// Attach a shader to the environment. The environment takes ownership of