1/*
2 * Copyright (C) 2011 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#include "DisplayListLogBuffer.h"
18
19// BUFFER_SIZE size must be one more than a multiple of COMMAND_SIZE to ensure
20// that mStart always points at the next command, not just the next item
21#define COMMAND_SIZE 2
22#define NUM_COMMANDS 50
23#define BUFFER_SIZE ((NUM_COMMANDS * COMMAND_SIZE) + 1)
24
25/**
26 * DisplayListLogBuffer is a utility class which logs the most recent display
27 * list operations in a circular buffer. The log is process-wide, because we
28 * only care about the most recent operations, not the operations on a per-window
29 * basis for a given activity. The purpose of the log is to provide more debugging
30 * information in a bug report, by telling us not just where a process hung (which
31 * generally is just reported as a stack trace at the Java level) or crashed, but
32 * also what happened immediately before that hang or crash. This may help track down
33 * problems in the native rendering code or driver interaction related to the display
34 * list operations that led up to the hang or crash.
35 *
36 * The log is implemented as a circular buffer for both space and performance
37 * reasons - we only care about the last several operations to give us context
38 * leading up to the problem, and we don't want to constantly copy data around or do
39 * additional mallocs to keep the most recent operations logged. Only numbers are
40 * logged to make the operation fast. If and when the log is output, we process this
41 * data into meaningful strings.
42 *
43 * There is an assumption about the format of the command (currently 2 ints: the
44 * opcode and the nesting level). If the type of information logged changes (for example,
45 * we may want to save a timestamp), then the size of the buffer and the way the
46 * information is recorded in writeCommand() should change to suit.
47 */
48
49namespace android {
50
51#ifdef USE_OPENGL_RENDERER
52using namespace uirenderer;
53ANDROID_SINGLETON_STATIC_INSTANCE(DisplayListLogBuffer);
54#endif
55
56namespace uirenderer {
57
58
59DisplayListLogBuffer::DisplayListLogBuffer() {
60    mBufferFirst = (int*) malloc(BUFFER_SIZE * sizeof(int));
61    mStart = mBufferFirst;
62    mBufferLast = mBufferFirst + BUFFER_SIZE - 1;
63    mEnd = mStart;
64}
65
66DisplayListLogBuffer::~DisplayListLogBuffer() {
67    free(mBufferFirst);
68}
69
70/**
71 * Called from DisplayListRenderer to output the current buffer into the
72 * specified FILE. This only happens in a dumpsys/bugreport operation.
73 */
74void DisplayListLogBuffer::outputCommands(FILE *file, const char* opNames[])
75{
76    int *tmpBufferPtr = mStart;
77    while (true) {
78        if (tmpBufferPtr == mEnd) {
79            break;
80        }
81        int level = *tmpBufferPtr++;
82        if (tmpBufferPtr > mBufferLast) {
83            tmpBufferPtr = mBufferFirst;
84        }
85        int op = *tmpBufferPtr++;
86        if (tmpBufferPtr > mBufferLast) {
87            tmpBufferPtr = mBufferFirst;
88        }
89        uint32_t count = (level + 1) * 2;
90        char indent[count + 1];
91        for (uint32_t i = 0; i < count; i++) {
92            indent[i] = ' ';
93        }
94        indent[count] = '\0';
95        fprintf(file, "%s%s\n", indent, opNames[op]);
96    }
97}
98
99void DisplayListLogBuffer::writeCommand(int level, int op) {
100    writeInt(level);
101    writeInt(op);
102}
103
104/**
105 * Store the given value in the buffer and increment/wrap the mEnd
106 * and mStart values as appropriate.
107 */
108void DisplayListLogBuffer::writeInt(int value) {
109    *((int*)mEnd) = value;
110    if (mEnd == mBufferLast) {
111        mEnd = mBufferFirst;
112    } else {
113        mEnd++;
114    }
115    if (mEnd == mStart) {
116        mStart++;
117        if (mStart > mBufferLast) {
118            mStart = mBufferFirst;
119        }
120    }
121}
122
123}; // namespace uirenderer
124}; // namespace android
125