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

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

mercurial