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