1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/gfx/TextureGenerator.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,77 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.gfx; 1.10 + 1.11 +import android.opengl.GLES20; 1.12 +import android.util.Log; 1.13 + 1.14 +import java.util.concurrent.ArrayBlockingQueue; 1.15 + 1.16 +import javax.microedition.khronos.egl.EGL10; 1.17 +import javax.microedition.khronos.egl.EGLContext; 1.18 + 1.19 +public class TextureGenerator { 1.20 + private static final String LOGTAG = "TextureGenerator"; 1.21 + private static final int POOL_SIZE = 5; 1.22 + 1.23 + private static TextureGenerator sSharedInstance; 1.24 + 1.25 + private ArrayBlockingQueue<Integer> mTextureIds; 1.26 + private EGLContext mContext; 1.27 + 1.28 + private TextureGenerator() { mTextureIds = new ArrayBlockingQueue<Integer>(POOL_SIZE); } 1.29 + 1.30 + public static TextureGenerator get() { 1.31 + if (sSharedInstance == null) 1.32 + sSharedInstance = new TextureGenerator(); 1.33 + return sSharedInstance; 1.34 + } 1.35 + 1.36 + public synchronized int take() { 1.37 + try { 1.38 + // Will block until one becomes available 1.39 + return (int)mTextureIds.take(); 1.40 + } catch (InterruptedException e) { 1.41 + return 0; 1.42 + } 1.43 + } 1.44 + 1.45 + public synchronized void fill() { 1.46 + EGL10 egl = (EGL10)EGLContext.getEGL(); 1.47 + EGLContext context = egl.eglGetCurrentContext(); 1.48 + 1.49 + if (mContext != null && mContext != context) { 1.50 + mTextureIds.clear(); 1.51 + } 1.52 + 1.53 + mContext = context; 1.54 + 1.55 + int numNeeded = mTextureIds.remainingCapacity(); 1.56 + if (numNeeded == 0) 1.57 + return; 1.58 + 1.59 + // Clear existing GL errors 1.60 + int error; 1.61 + while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) { 1.62 + Log.w(LOGTAG, String.format("Clearing GL error: %#x", error)); 1.63 + } 1.64 + 1.65 + int[] textures = new int[numNeeded]; 1.66 + GLES20.glGenTextures(numNeeded, textures, 0); 1.67 + 1.68 + error = GLES20.glGetError(); 1.69 + if (error != GLES20.GL_NO_ERROR) { 1.70 + Log.e(LOGTAG, String.format("Failed to generate textures: %#x", error), new Exception()); 1.71 + return; 1.72 + } 1.73 + 1.74 + for (int i = 0; i < numNeeded; i++) { 1.75 + mTextureIds.offer(textures[i]); 1.76 + } 1.77 + } 1.78 +} 1.79 + 1.80 +