1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.media.filterfw;
18
19import android.graphics.Bitmap;
20import android.opengl.GLES11Ext;
21import android.opengl.GLES20;
22
23import java.nio.ByteBuffer;
24
25public class TextureSource {
26
27    private int mTexId;
28    private int mTarget;
29    private boolean mIsOwner;
30    private boolean mIsAllocated = false;
31
32    public static TextureSource fromTexture(int texId, int target) {
33        return new TextureSource(texId, target, false);
34    }
35
36    public static TextureSource fromTexture(int texId) {
37        return new TextureSource(texId, GLES20.GL_TEXTURE_2D, false);
38    }
39
40    public static TextureSource newTexture() {
41        return new TextureSource(GLToolbox.generateTexture(), GLES20.GL_TEXTURE_2D, true);
42    }
43
44    public static TextureSource newExternalTexture() {
45        return new TextureSource(GLToolbox.generateTexture(),
46                                 GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
47                                 true);
48    }
49
50    public int getTextureId() {
51        return mTexId;
52    }
53
54    public int getTarget() {
55        return mTarget;
56    }
57
58    public void bind() {
59        GLES20.glBindTexture(mTarget, mTexId);
60        GLToolbox.checkGlError("glBindTexture");
61    }
62
63    public void allocate(int width, int height) {
64        //Log.i("TextureSource", "Allocating empty texture " + mTexId + ": " + width + "x" + height + ".");
65        GLToolbox.allocateTexturePixels(mTexId, mTarget, width, height);
66        mIsAllocated = true;
67    }
68
69    public void allocateWithPixels(ByteBuffer pixels, int width, int height) {
70        //Log.i("TextureSource", "Uploading pixels to texture " + mTexId + ": " + width + "x" + height + ".");
71        GLToolbox.setTexturePixels(mTexId, mTarget, pixels, width, height);
72        mIsAllocated = true;
73    }
74
75    public void allocateWithBitmapPixels(Bitmap bitmap) {
76        //Log.i("TextureSource", "Uploading pixels to texture " + mTexId + "!");
77        GLToolbox.setTexturePixels(mTexId, mTarget, bitmap);
78        mIsAllocated = true;
79    }
80
81    public void generateMipmaps() {
82        GLES20.glBindTexture(mTarget, mTexId);
83        GLES20.glTexParameteri(mTarget,
84                               GLES20.GL_TEXTURE_MIN_FILTER,
85                               GLES20.GL_LINEAR_MIPMAP_LINEAR);
86        GLES20.glGenerateMipmap(mTarget);
87        GLES20.glBindTexture(mTarget, 0);
88    }
89
90    public void setParameter(int parameter, int value) {
91        GLES20.glBindTexture(mTarget, mTexId);
92        GLES20.glTexParameteri(mTarget, parameter, value);
93        GLES20.glBindTexture(mTarget, 0);
94    }
95
96    /**
97     * @hide
98     */
99    public void release() {
100        if (GLToolbox.isTexture(mTexId) && mIsOwner) {
101            GLToolbox.deleteTexture(mTexId);
102        }
103        mTexId = GLToolbox.textureNone();
104    }
105
106    @Override
107    public String toString() {
108        return "TextureSource(id=" + mTexId + ", target=" + mTarget + ")";
109    }
110
111    boolean isAllocated() {
112        return mIsAllocated;
113    }
114
115    private TextureSource(int texId, int target, boolean isOwner) {
116        mTexId = texId;
117        mTarget = target;
118        mIsOwner = isOwner;
119    }
120}
121
122