1 /* 2 * Copyright (C) 2012 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 17 package com.android.ide.eclipse.gltrace; 18 19 20 import java.nio.ByteBuffer; 21 import java.nio.ByteOrder; 22 import java.nio.FloatBuffer; 23 import java.nio.IntBuffer; 24 import java.nio.ShortBuffer; 25 26 public class GLUtils { formatData(byte[] data, GLEnum format)27 public static String formatData(byte[] data, GLEnum format) { 28 switch (format) { 29 case GL_BYTE: 30 return formatBytes(data, false); 31 case GL_UNSIGNED_BYTE: 32 return formatBytes(data, true); 33 case GL_SHORT: 34 return formatShorts(data, false); 35 case GL_UNSIGNED_SHORT: 36 return formatShorts(data, true); 37 case GL_FIXED: 38 return formatInts(data); 39 case GL_FLOAT: 40 return formatFloats(data); 41 default: 42 return ""; //$NON-NLS-1$ 43 } 44 } 45 formatFloats(byte[] data)46 private static String formatFloats(byte[] data) { 47 FloatBuffer bb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer(); 48 49 StringBuilder sb = new StringBuilder(bb.capacity() * 3); 50 51 while (bb.remaining() > 0) { 52 sb.append(String.format("%.4f", bb.get())); 53 sb.append(','); 54 sb.append('\n'); 55 } 56 57 return sb.toString(); 58 } 59 formatInts(byte[] data)60 private static String formatInts(byte[] data) { 61 IntBuffer bb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer(); 62 63 StringBuilder sb = new StringBuilder(bb.capacity() * 3); 64 65 while (bb.remaining() > 0) { 66 sb.append(bb.get()); 67 sb.append(','); 68 sb.append('\n'); 69 } 70 71 return sb.toString(); 72 } 73 formatShorts(byte[] data, boolean unsigned)74 private static String formatShorts(byte[] data, boolean unsigned) { 75 ShortBuffer bb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer(); 76 77 StringBuilder sb = new StringBuilder(bb.capacity() * 3); 78 79 while (bb.remaining() > 0) { 80 if (unsigned) { 81 sb.append(bb.get() & 0xffff); 82 } else { 83 sb.append(bb.get()); 84 } 85 sb.append(','); 86 sb.append('\n'); 87 } 88 89 return sb.toString(); 90 } 91 formatBytes(byte[] data, boolean unsigned)92 private static String formatBytes(byte[] data, boolean unsigned) { 93 ByteBuffer bb = ByteBuffer.wrap(data); 94 95 StringBuilder sb = new StringBuilder(bb.capacity() * 3); 96 97 while (bb.remaining() > 0) { 98 if (unsigned) { 99 sb.append(bb.get() & 0xff); 100 } else { 101 sb.append(bb.get()); 102 } 103 104 sb.append(','); 105 sb.append('\n'); 106 } 107 108 return sb.toString(); 109 } 110 } 111