aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/model/GLTrace.java
blob: 766764466a2c56f5581ae858cee791ea24c295df (plain)
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
/*
 * Copyright (C) 2011 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 com.android.ide.eclipse.gltrace.model;

import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage;
import com.android.ide.eclipse.gltrace.ProtoBufUtils;
import com.android.ide.eclipse.gltrace.TraceFileInfo;
import com.android.ide.eclipse.gltrace.TraceFileReader;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Collections;
import java.util.List;

/** GLTrace is the in memory model of a OpenGL trace file. */
public class GLTrace {
    private static final TraceFileReader sTraceFileReader = new TraceFileReader();

    /** Information regarding the trace file. */
    private final TraceFileInfo mTraceFileInfo;

    /** List of frames in the trace. */
    private final List<GLFrame> mGLFrames;

    /** List of GL Calls comprising the trace. */
    private final List<GLCall> mGLCalls;

    /** List of context ids used by the application. */
    private List<Integer> mContextIds;

    public GLTrace(TraceFileInfo traceFileInfo, List<GLFrame> glFrames, List<GLCall> glCalls,
            List<Integer> contextIds) {
        mTraceFileInfo = traceFileInfo;
        mGLFrames = glFrames;
        mGLCalls = glCalls;
        mContextIds = contextIds;
    }

    public List<GLFrame> getFrames() {
        return mGLFrames;
    }

    public GLFrame getFrame(int i) {
        return mGLFrames.get(i);
    }

    public List<GLCall> getGLCalls() {
        return mGLCalls;
    }

    public List<GLCall> getGLCallsForFrame(int frameIndex) {
        if (frameIndex >= mGLFrames.size()) {
            return Collections.emptyList();
        }

        GLFrame frame = mGLFrames.get(frameIndex);
        return mGLCalls.subList(frame.getStartIndex(), frame.getEndIndex());
    }

    public Image getImage(GLCall c) {
        if (!c.hasFb()) {
            return null;
        }

        if (isTraceFileModified()) {
            return null;
        }

        RandomAccessFile file;
        try {
            file = new RandomAccessFile(mTraceFileInfo.getPath(), "r"); //$NON-NLS-1$
        } catch (FileNotFoundException e1) {
            return null;
        }

        GLMessage m = null;
        try {
            m = sTraceFileReader.getMessageAtOffset(file, c.getOffsetInTraceFile());
        } catch (Exception e) {
            return null;
        } finally {
            try {
                file.close();
            } catch (IOException e) {
                // ignore exception while closing file
            }
        }

        return ProtoBufUtils.getImage(Display.getCurrent(), m);
    }

    private boolean isTraceFileModified() {
        File f = new File(mTraceFileInfo.getPath());
        return f.length() != mTraceFileInfo.getSize()
                || f.lastModified() != mTraceFileInfo.getLastModificationTime();
    }

    public List<Integer> getContexts() {
        return mContextIds;
    }
}