1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.ddm;
import android.opengl.GLUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewDebug;
import android.view.ViewRootImpl;
import android.view.WindowManagerGlobal;
import org.apache.harmony.dalvik.ddmc.Chunk;
import org.apache.harmony.dalvik.ddmc.ChunkHandler;
import org.apache.harmony.dalvik.ddmc.DdmServer;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
/**
* Handle various requests related to profiling / debugging of the view system.
* Support for these features are advertised via {@link DdmHandleHello}.
*/
public class DdmHandleViewDebug extends ChunkHandler {
/** Enable/Disable tracing of OpenGL calls. */
public static final int CHUNK_VUGL = type("VUGL");
/** List {@link ViewRootImpl}'s of this process. */
private static final int CHUNK_VULW = type("VULW");
/** Operation on view root, first parameter in packet should be one of VURT_* constants */
private static final int CHUNK_VURT = type("VURT");
/** Dump view hierarchy. */
private static final int VURT_DUMP_HIERARCHY = 1;
/** Capture View Layers. */
private static final int VURT_CAPTURE_LAYERS = 2;
/**
* Generic View Operation, first parameter in the packet should be one of the
* VUOP_* constants below.
*/
private static final int CHUNK_VUOP = type("VUOP");
/** Capture View. */
private static final int VUOP_CAPTURE_VIEW = 1;
/** Obtain the Display List corresponding to the view. */
private static final int VUOP_DUMP_DISPLAYLIST = 2;
/** Invalidate View. */
private static final int VUOP_INVALIDATE_VIEW = 3;
/** Re-layout given view. */
private static final int VUOP_LAYOUT_VIEW = 4;
/** Profile a view. */
private static final int VUOP_PROFILE_VIEW = 5;
/** Error code indicating operation specified in chunk is invalid. */
private static final int ERR_INVALID_OP = -1;
/** Error code indicating that the parameters are invalid. */
private static final int ERR_INVALID_PARAM = -2;
private static final DdmHandleViewDebug sInstance = new DdmHandleViewDebug();
/** singleton, do not instantiate. */
private DdmHandleViewDebug() {}
public static void register() {
DdmServer.registerHandler(CHUNK_VUGL, sInstance);
DdmServer.registerHandler(CHUNK_VULW, sInstance);
DdmServer.registerHandler(CHUNK_VURT, sInstance);
DdmServer.registerHandler(CHUNK_VUOP, sInstance);
}
@Override
public void connected() {
}
@Override
public void disconnected() {
}
@Override
public Chunk handleChunk(Chunk request) {
int type = request.type;
if (type == CHUNK_VUGL) {
return handleOpenGlTrace(request);
} else if (type == CHUNK_VULW) {
return listWindows();
}
ByteBuffer in = wrapChunk(request);
int op = in.getInt();
View rootView = getRootView(in);
if (rootView == null) {
return createFailChunk(ERR_INVALID_PARAM, "Invalid View Root");
}
if (type == CHUNK_VURT) {
if (op == VURT_DUMP_HIERARCHY)
return dumpHierarchy(rootView, in);
else if (op == VURT_CAPTURE_LAYERS)
return captureLayers(rootView);
else
return createFailChunk(ERR_INVALID_OP, "Unknown view root operation: " + op);
}
final View targetView = getTargetView(rootView, in);
if (targetView == null) {
return createFailChunk(ERR_INVALID_PARAM, "Invalid target view");
}
if (type == CHUNK_VUOP) {
switch (op) {
case VUOP_CAPTURE_VIEW:
return captureView(rootView, targetView);
case VUOP_DUMP_DISPLAYLIST:
return dumpDisplayLists(rootView, targetView);
case VUOP_INVALIDATE_VIEW:
return invalidateView(rootView, targetView);
case VUOP_LAYOUT_VIEW:
return layoutView(rootView, targetView);
case VUOP_PROFILE_VIEW:
return profileView(rootView, targetView);
default:
return createFailChunk(ERR_INVALID_OP, "Unknown view operation: " + op);
}
} else {
throw new RuntimeException("Unknown packet " + ChunkHandler.name(type));
}
}
private Chunk handleOpenGlTrace(Chunk request) {
ByteBuffer in = wrapChunk(request);
GLUtils.setTracingLevel(in.getInt());
return null; // empty response
}
/** Returns the list of windows owned by this client. */
private Chunk listWindows() {
String[] windowNames = WindowManagerGlobal.getInstance().getViewRootNames();
int responseLength = 4; // # of windows
for (String name : windowNames) {
responseLength += 4; // length of next window name
responseLength += name.length() * 2; // window name
}
ByteBuffer out = ByteBuffer.allocate(responseLength);
out.order(ChunkHandler.CHUNK_ORDER);
out.putInt(windowNames.length);
for (String name : windowNames) {
out.putInt(name.length());
putString(out, name);
}
return new Chunk(CHUNK_VULW, out);
}
private View getRootView(ByteBuffer in) {
try {
int viewRootNameLength = in.getInt();
String viewRootName = getString(in, viewRootNameLength);
return WindowManagerGlobal.getInstance().getRootView(viewRootName);
} catch (BufferUnderflowException e) {
return null;
}
}
private View getTargetView(View root, ByteBuffer in) {
int viewLength;
String viewName;
try {
viewLength = in.getInt();
viewName = getString(in, viewLength);
} catch (BufferUnderflowException e) {
return null;
}
return ViewDebug.findView(root, viewName);
}
/**
* Returns the view hierarchy and/or view properties starting at the provided view.
* Based on the input options, the return data may include:
* - just the view hierarchy
* - view hierarchy & the properties for each of the views
* - just the view properties for a specific view.
* TODO: Currently this only returns views starting at the root, need to fix so that
* it can return properties of any view.
*/
private Chunk dumpHierarchy(View rootView, ByteBuffer in) {
boolean skipChildren = in.getInt() > 0;
boolean includeProperties = in.getInt() > 0;
ByteArrayOutputStream b = new ByteArrayOutputStream(1024);
try {
ViewDebug.dump(rootView, skipChildren, includeProperties, b);
} catch (IOException e) {
return createFailChunk(1, "Unexpected error while obtaining view hierarchy: "
+ e.getMessage());
}
byte[] data = b.toByteArray();
return new Chunk(CHUNK_VURT, data, 0, data.length);
}
/** Returns a buffer with region details & bitmap of every single view. */
private Chunk captureLayers(View rootView) {
ByteArrayOutputStream b = new ByteArrayOutputStream(1024);
DataOutputStream dos = new DataOutputStream(b);
try {
ViewDebug.captureLayers(rootView, dos);
} catch (IOException e) {
return createFailChunk(1, "Unexpected error while obtaining view hierarchy: "
+ e.getMessage());
} finally {
try {
dos.close();
} catch (IOException e) {
// ignore
}
}
byte[] data = b.toByteArray();
return new Chunk(CHUNK_VURT, data, 0, data.length);
}
private Chunk captureView(View rootView, View targetView) {
ByteArrayOutputStream b = new ByteArrayOutputStream(1024);
try {
ViewDebug.capture(rootView, b, targetView);
} catch (IOException e) {
return createFailChunk(1, "Unexpected error while capturing view: "
+ e.getMessage());
}
byte[] data = b.toByteArray();
return new Chunk(CHUNK_VUOP, data, 0, data.length);
}
/** Returns the display lists corresponding to the provided view. */
private Chunk dumpDisplayLists(final View rootView, final View targetView) {
rootView.post(new Runnable() {
@Override
public void run() {
ViewDebug.outputDisplayList(rootView, targetView);
}
});
return null;
}
/** Invalidates provided view. */
private Chunk invalidateView(final View rootView, final View targetView) {
targetView.postInvalidate();
return null;
}
/** Lays out provided view. */
private Chunk layoutView(View rootView, final View targetView) {
rootView.post(new Runnable() {
@Override
public void run() {
targetView.requestLayout();
}
});
return null;
}
/** Profiles provided view. */
private Chunk profileView(View rootView, final View targetView) {
ByteArrayOutputStream b = new ByteArrayOutputStream(32 * 1024);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(b), 32 * 1024);
try {
ViewDebug.profileViewAndChildren(targetView, bw);
} catch (IOException e) {
return createFailChunk(1, "Unexpected error while profiling view: " + e.getMessage());
} finally {
try {
bw.close();
} catch (IOException e) {
// ignore
}
}
byte[] data = b.toByteArray();
return new Chunk(CHUNK_VUOP, data, 0, data.length);
}
}
|