mobile/android/base/gfx/TextureGenerator.java

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:57cd5fc75dcd
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/. */
5
6 package org.mozilla.gecko.gfx;
7
8 import android.opengl.GLES20;
9 import android.util.Log;
10
11 import java.util.concurrent.ArrayBlockingQueue;
12
13 import javax.microedition.khronos.egl.EGL10;
14 import javax.microedition.khronos.egl.EGLContext;
15
16 public class TextureGenerator {
17 private static final String LOGTAG = "TextureGenerator";
18 private static final int POOL_SIZE = 5;
19
20 private static TextureGenerator sSharedInstance;
21
22 private ArrayBlockingQueue<Integer> mTextureIds;
23 private EGLContext mContext;
24
25 private TextureGenerator() { mTextureIds = new ArrayBlockingQueue<Integer>(POOL_SIZE); }
26
27 public static TextureGenerator get() {
28 if (sSharedInstance == null)
29 sSharedInstance = new TextureGenerator();
30 return sSharedInstance;
31 }
32
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 }
41
42 public synchronized void fill() {
43 EGL10 egl = (EGL10)EGLContext.getEGL();
44 EGLContext context = egl.eglGetCurrentContext();
45
46 if (mContext != null && mContext != context) {
47 mTextureIds.clear();
48 }
49
50 mContext = context;
51
52 int numNeeded = mTextureIds.remainingCapacity();
53 if (numNeeded == 0)
54 return;
55
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 }
61
62 int[] textures = new int[numNeeded];
63 GLES20.glGenTextures(numNeeded, textures, 0);
64
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 }
70
71 for (int i = 0; i < numNeeded; i++) {
72 mTextureIds.offer(textures[i]);
73 }
74 }
75 }
76
77

mercurial