Revert "Roll NDK to r11c and extract it into its own repository."
[android_tools.git] / ndk / samples / Teapot / src / com / sample / helper / NDKHelper.java
blob3385a5d98d8cfe46243bbb2578754a061c9695b6
1 /*
2 * Copyright 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.sample.helper;
19 import java.io.File;
20 import java.io.FileInputStream;
22 import javax.microedition.khronos.opengles.GL10;
24 import android.content.Context;
25 import android.content.pm.ApplicationInfo;
26 import android.graphics.Bitmap;
27 import android.graphics.BitmapFactory;
28 import android.graphics.Matrix;
29 import android.media.AudioManager;
30 import android.media.AudioTrack;
31 import android.opengl.GLUtils;
32 import android.util.Log;
34 public class NDKHelper
36 private static Context context;
38 public static void setContext(Context c)
40 Log.i("NDKHelper", "setContext:" + c);
41 context = c;
45 // Load Bitmap
46 // Java helper is useful decoding PNG, TIFF etc rather than linking libPng
47 // etc separately
49 private int nextPOT(int i)
51 int pot = 1;
52 while (pot < i)
53 pot <<= 1;
54 return pot;
57 private Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight)
59 if (bitmapToScale == null)
60 return null;
61 // get the original width and height
62 int width = bitmapToScale.getWidth();
63 int height = bitmapToScale.getHeight();
64 // create a matrix for the manipulation
65 Matrix matrix = new Matrix();
67 // resize the bit map
68 matrix.postScale(newWidth / width, newHeight / height);
70 // recreate the new Bitmap and set it back
71 return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(),
72 bitmapToScale.getHeight(), matrix, true);
75 public boolean loadTexture(String path)
77 Bitmap bitmap = null;
78 try
80 String str = path;
81 if (!path.startsWith("/"))
83 str = "/" + path;
86 File file = new File(context.getExternalFilesDir(null), str);
87 if (file.canRead())
89 bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
90 } else
92 bitmap = BitmapFactory.decodeStream(context.getResources().getAssets()
93 .open(path));
95 // Matrix matrix = new Matrix();
96 // // resize the bit map
97 // matrix.postScale(-1F, 1F);
99 // // recreate the new Bitmap and set it back
100 // bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
101 // bitmap.getHeight(), matrix, true);
103 } catch (Exception e)
105 Log.w("NDKHelper", "Coundn't load a file:" + path);
106 return false;
109 if (bitmap != null)
111 GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
113 return true;
117 public Bitmap openBitmap(String path, boolean iScalePOT)
119 Bitmap bitmap = null;
122 bitmap = BitmapFactory.decodeStream(context.getResources().getAssets()
123 .open(path));
124 if (iScalePOT)
126 int originalWidth = getBitmapWidth(bitmap);
127 int originalHeight = getBitmapHeight(bitmap);
128 int width = nextPOT(originalWidth);
129 int height = nextPOT(originalHeight);
130 if (originalWidth != width || originalHeight != height)
132 // Scale it
133 bitmap = scaleBitmap(bitmap, width, height);
137 } catch (Exception e)
139 Log.w("NDKHelper", "Coundn't load a file:" + path);
142 return bitmap;
145 public int getBitmapWidth(Bitmap bmp)
147 return bmp.getWidth();
150 public int getBitmapHeight(Bitmap bmp)
152 return bmp.getHeight();
155 public void getBitmapPixels(Bitmap bmp, int[] pixels)
157 int w = bmp.getWidth();
158 int h = bmp.getHeight();
159 bmp.getPixels(pixels, 0, w, 0, 0, w, h);
162 public void closeBitmap(Bitmap bmp)
164 bmp.recycle();
167 public static String getNativeLibraryDirectory(Context appContext)
169 ApplicationInfo ai = context.getApplicationInfo();
171 Log.w("NDKHelper", "ai.nativeLibraryDir:" + ai.nativeLibraryDir);
173 if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0
174 || (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0)
176 return ai.nativeLibraryDir;
178 return "/system/lib/";
181 public int getNativeAudioBufferSize()
183 int SDK_INT = android.os.Build.VERSION.SDK_INT;
184 if (SDK_INT >= 17)
186 AudioManager am = (AudioManager) context
187 .getSystemService(Context.AUDIO_SERVICE);
188 String framesPerBuffer = am
189 .getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
190 return Integer.parseInt(framesPerBuffer);
191 } else
193 return 0;
197 public int getNativeAudioSampleRate()
199 return AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM);