Skip to content

Commit

Permalink
Make HardwareRenderer able to target generic Surface objects
Browse files Browse the repository at this point in the history
Change-Id: I4b7199a1eb30e0df354ae12c4819adc69db5df40
  • Loading branch information
romainguy committed Jul 24, 2012
1 parent d8031f0 commit 786fc93
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
44 changes: 22 additions & 22 deletions core/java/android/view/HardwareRenderer.java
Expand Up @@ -197,18 +197,18 @@ public static boolean isAvailable() {
/**
* Initializes the hardware renderer for the specified surface.
*
* @param holder The holder for the surface to hardware accelerate.
* @param surface The surface to hardware accelerate
*
* @return True if the initialization was successful, false otherwise.
*/
abstract boolean initialize(SurfaceHolder holder) throws Surface.OutOfResourcesException;
abstract boolean initialize(Surface surface) throws Surface.OutOfResourcesException;

/**
* Updates the hardware renderer for the specified surface.
*
* @param holder The holder for the surface to hardware accelerate
*
* @param surface The surface to hardware accelerate
*/
abstract void updateSurface(SurfaceHolder holder) throws Surface.OutOfResourcesException;
abstract void updateSurface(Surface surface) throws Surface.OutOfResourcesException;

/**
* Destroys the layers used by the specified view hierarchy.
Expand All @@ -228,10 +228,10 @@ public static boolean isAvailable() {
/**
* This method should be invoked whenever the current hardware renderer
* context should be reset.
*
* @param holder The holder for the surface to hardware accelerate
*
* @param surface The surface to hardware accelerate
*/
abstract void invalidate(SurfaceHolder holder);
abstract void invalidate(Surface surface);

/**
* This method should be invoked to ensure the hardware renderer is in
Expand Down Expand Up @@ -474,14 +474,14 @@ abstract boolean draw(View view, View.AttachInfo attachInfo, HardwareDrawCallbac
*
* @param width The width of the drawing surface.
* @param height The height of the drawing surface.
* @param holder The target surface
* @param surface The surface to hardware accelerate
*/
void initializeIfNeeded(int width, int height, SurfaceHolder holder)
void initializeIfNeeded(int width, int height, Surface surface)
throws Surface.OutOfResourcesException {
if (isRequested()) {
// We lost the gl context, so recreate it.
if (!isEnabled()) {
if (initialize(holder)) {
if (initialize(surface)) {
setup(width, height);
}
}
Expand Down Expand Up @@ -742,10 +742,10 @@ private void fallback(boolean fallback) {
}

@Override
boolean initialize(SurfaceHolder holder) throws Surface.OutOfResourcesException {
boolean initialize(Surface surface) throws Surface.OutOfResourcesException {
if (isRequested() && !isEnabled()) {
initializeEgl();
mGl = createEglSurface(holder);
mGl = createEglSurface(surface);
mDestroyed = false;

if (mGl != null) {
Expand All @@ -771,9 +771,9 @@ boolean initialize(SurfaceHolder holder) throws Surface.OutOfResourcesException
}

@Override
void updateSurface(SurfaceHolder holder) throws Surface.OutOfResourcesException {
void updateSurface(Surface surface) throws Surface.OutOfResourcesException {
if (isRequested() && isEnabled()) {
createEglSurface(holder);
createEglSurface(surface);
}
}

Expand Down Expand Up @@ -888,7 +888,7 @@ private static void printConfig(EGLConfig config) {
Log.d(LOG_TAG, " SURFACE_TYPE = 0x" + Integer.toHexString(value[0]));
}

GL createEglSurface(SurfaceHolder holder) throws Surface.OutOfResourcesException {
GL createEglSurface(Surface surface) throws Surface.OutOfResourcesException {
// Check preconditions.
if (sEgl == null) {
throw new RuntimeException("egl not initialized");
Expand All @@ -908,7 +908,7 @@ GL createEglSurface(SurfaceHolder holder) throws Surface.OutOfResourcesException
destroySurface();

// Create an EGL surface we can render into.
if (!createSurface(holder)) {
if (!createSurface(surface)) {
return null;
}

Expand Down Expand Up @@ -982,7 +982,7 @@ void destroySurface() {
}

@Override
void invalidate(SurfaceHolder holder) {
void invalidate(Surface surface) {
// Cancels any existing buffer to ensure we'll get a buffer
// of the right size before we call eglSwapBuffers
sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
Expand All @@ -993,8 +993,8 @@ void invalidate(SurfaceHolder holder) {
setEnabled(false);
}

if (holder.getSurface().isValid()) {
if (!createSurface(holder)) {
if (surface.isValid()) {
if (!createSurface(surface)) {
return;
}

Expand All @@ -1006,8 +1006,8 @@ void invalidate(SurfaceHolder holder) {
}
}

private boolean createSurface(SurfaceHolder holder) {
mEglSurface = sEgl.eglCreateWindowSurface(sEglDisplay, sEglConfig, holder, null);
private boolean createSurface(Surface surface) {
mEglSurface = sEgl.eglCreateWindowSurface(sEglDisplay, sEglConfig, surface, null);

if (mEglSurface == null || mEglSurface == EGL_NO_SURFACE) {
int error = sEgl.eglGetError();
Expand Down
9 changes: 5 additions & 4 deletions core/java/android/view/ViewRootImpl.java
Expand Up @@ -1502,7 +1502,8 @@ private void performTraversals() {

if (mAttachInfo.mHardwareRenderer != null) {
try {
hwInitialized = mAttachInfo.mHardwareRenderer.initialize(mHolder);
hwInitialized = mAttachInfo.mHardwareRenderer.initialize(
mHolder.getSurface());
} catch (Surface.OutOfResourcesException e) {
Log.e(TAG, "OutOfResourcesException initializing HW surface", e);
try {
Expand Down Expand Up @@ -1535,7 +1536,7 @@ private void performTraversals() {
mSurfaceHolder == null && mAttachInfo.mHardwareRenderer != null) {
mFullRedrawNeeded = true;
try {
mAttachInfo.mHardwareRenderer.updateSurface(mHolder);
mAttachInfo.mHardwareRenderer.updateSurface(mHolder.getSurface());
} catch (Surface.OutOfResourcesException e) {
Log.e(TAG, "OutOfResourcesException updating HW surface", e);
try {
Expand Down Expand Up @@ -1626,7 +1627,7 @@ private void performTraversals() {
mHeight != mAttachInfo.mHardwareRenderer.getHeight()) {
mAttachInfo.mHardwareRenderer.setup(mWidth, mHeight);
if (!hwInitialized) {
mAttachInfo.mHardwareRenderer.invalidate(mHolder);
mAttachInfo.mHardwareRenderer.invalidate(mHolder.getSurface());
}
}
}
Expand Down Expand Up @@ -2889,7 +2890,7 @@ public void handleMessage(Message msg) {
mFullRedrawNeeded = true;
try {
mAttachInfo.mHardwareRenderer.initializeIfNeeded(mWidth, mHeight,
mHolder);
mHolder.getSurface());
} catch (Surface.OutOfResourcesException e) {
Log.e(TAG, "OutOfResourcesException locking surface", e);
try {
Expand Down
5 changes: 3 additions & 2 deletions opengl/java/com/google/android/gles_jni/EGLImpl.java
Expand Up @@ -83,6 +83,8 @@ public EGLSurface eglCreateWindowSurface(EGLDisplay display, EGLConfig config, O
} else if (native_window instanceof SurfaceHolder) {
SurfaceHolder holder = (SurfaceHolder)native_window;
sur = holder.getSurface();
} else if (native_window instanceof Surface) {
sur = (Surface) native_window;
}

int eglSurfaceId;
Expand All @@ -94,8 +96,7 @@ public EGLSurface eglCreateWindowSurface(EGLDisplay display, EGLConfig config, O
} else {
throw new java.lang.UnsupportedOperationException(
"eglCreateWindowSurface() can only be called with an instance of " +
"SurfaceView, SurfaceHolder or SurfaceTexture at the moment, " +
"this will be fixed later.");
"Surface, SurfaceView, SurfaceHolder or SurfaceTexture at the moment.");
}

if (eglSurfaceId == 0) {
Expand Down
Expand Up @@ -22,7 +22,6 @@
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.SurfaceTexture;
import android.opengl.GLUtils;
import android.os.Bundle;
Expand Down Expand Up @@ -278,7 +277,7 @@ private int loadTexture(int resource) {
return texture;
}

private int buildProgram(String vertex, String fragment) {
private static int buildProgram(String vertex, String fragment) {
int vertexShader = buildShader(vertex, GL_VERTEX_SHADER);
if (vertexShader == 0) return 0;

Expand Down Expand Up @@ -309,7 +308,7 @@ private int buildProgram(String vertex, String fragment) {
return program;
}

private int buildShader(String source, int type) {
private static int buildShader(String source, int type) {
int shader = glCreateShader(type);

glShaderSource(shader, source);
Expand Down Expand Up @@ -337,7 +336,7 @@ private void checkEglError() {
}
}

private void checkGlError() {
private static void checkGlError() {
int error = glGetError();
if (error != GL_NO_ERROR) {
Log.w(LOG_TAG, "GL error = 0x" + Integer.toHexString(error));
Expand Down Expand Up @@ -420,7 +419,7 @@ private EGLConfig chooseEglConfig() {
return null;
}

private int[] getConfig() {
private static int[] getConfig() {
return new int[] {
EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL10.EGL_RED_SIZE, 8,
Expand Down

0 comments on commit 786fc93

Please sign in to comment.