1/*
2 * Copyright (C) 2016 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 com.android.server.wm;
18
19import java.io.FileDescriptor;
20import java.io.FileOutputStream;
21import java.io.DataOutputStream;
22
23import android.util.Slog;
24import android.os.Debug;
25
26// Counterpart to remote surface trace for events which are not tied to a particular surface.
27class RemoteEventTrace {
28    private static final String TAG = "RemoteEventTrace";
29
30    // We terminate all our messages with a recognizable marker, to avoid issues
31    // with partial reads (which ADB makes impossible to avoid).
32    static final byte[] sigil = {(byte)0xfc, (byte)0xfc, (byte)0xfc, (byte)0xfc};
33
34    private final WindowManagerService mService;
35    private final DataOutputStream mOut;
36
37    RemoteEventTrace(WindowManagerService service, FileDescriptor fd) {
38        mService = service;
39        mOut = new DataOutputStream(new FileOutputStream(fd, false));
40    }
41
42    void openSurfaceTransaction() {
43        try {
44            mOut.writeUTF("OpenTransaction");
45            writeSigil();
46        } catch (Exception e) {
47            logException(e);
48            mService.disableSurfaceTrace();
49        }
50    }
51
52    void closeSurfaceTransaction() {
53        try {
54            mOut.writeUTF("CloseTransaction");
55            writeSigil();
56        } catch (Exception e) {
57            logException(e);
58            mService.disableSurfaceTrace();
59        }
60    }
61
62    private void writeSigil() throws Exception {
63        mOut.write(RemoteEventTrace.sigil, 0, 4);
64    }
65
66    static void logException(Exception e) {
67        Slog.i(TAG, "Exception writing to SurfaceTrace (client vanished?): " + e.toString());
68    }
69}
70