mobile/android/base/gfx/TextureGenerator.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.gfx;
michael@0 7
michael@0 8 import android.opengl.GLES20;
michael@0 9 import android.util.Log;
michael@0 10
michael@0 11 import java.util.concurrent.ArrayBlockingQueue;
michael@0 12
michael@0 13 import javax.microedition.khronos.egl.EGL10;
michael@0 14 import javax.microedition.khronos.egl.EGLContext;
michael@0 15
michael@0 16 public class TextureGenerator {
michael@0 17 private static final String LOGTAG = "TextureGenerator";
michael@0 18 private static final int POOL_SIZE = 5;
michael@0 19
michael@0 20 private static TextureGenerator sSharedInstance;
michael@0 21
michael@0 22 private ArrayBlockingQueue<Integer> mTextureIds;
michael@0 23 private EGLContext mContext;
michael@0 24
michael@0 25 private TextureGenerator() { mTextureIds = new ArrayBlockingQueue<Integer>(POOL_SIZE); }
michael@0 26
michael@0 27 public static TextureGenerator get() {
michael@0 28 if (sSharedInstance == null)
michael@0 29 sSharedInstance = new TextureGenerator();
michael@0 30 return sSharedInstance;
michael@0 31 }
michael@0 32
michael@0 33 public synchronized int take() {
michael@0 34 try {
michael@0 35 // Will block until one becomes available
michael@0 36 return (int)mTextureIds.take();
michael@0 37 } catch (InterruptedException e) {
michael@0 38 return 0;
michael@0 39 }
michael@0 40 }
michael@0 41
michael@0 42 public synchronized void fill() {
michael@0 43 EGL10 egl = (EGL10)EGLContext.getEGL();
michael@0 44 EGLContext context = egl.eglGetCurrentContext();
michael@0 45
michael@0 46 if (mContext != null && mContext != context) {
michael@0 47 mTextureIds.clear();
michael@0 48 }
michael@0 49
michael@0 50 mContext = context;
michael@0 51
michael@0 52 int numNeeded = mTextureIds.remainingCapacity();
michael@0 53 if (numNeeded == 0)
michael@0 54 return;
michael@0 55
michael@0 56 // Clear existing GL errors
michael@0 57 int error;
michael@0 58 while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
michael@0 59 Log.w(LOGTAG, String.format("Clearing GL error: %#x", error));
michael@0 60 }
michael@0 61
michael@0 62 int[] textures = new int[numNeeded];
michael@0 63 GLES20.glGenTextures(numNeeded, textures, 0);
michael@0 64
michael@0 65 error = GLES20.glGetError();
michael@0 66 if (error != GLES20.GL_NO_ERROR) {
michael@0 67 Log.e(LOGTAG, String.format("Failed to generate textures: %#x", error), new Exception());
michael@0 68 return;
michael@0 69 }
michael@0 70
michael@0 71 for (int i = 0; i < numNeeded; i++) {
michael@0 72 mTextureIds.offer(textures[i]);
michael@0 73 }
michael@0 74 }
michael@0 75 }
michael@0 76
michael@0 77

mercurial