Home | History | Annotate | Download | only in model
      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 package com.android.ide.eclipse.gltrace.model;
     18 
     19 import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage;
     20 import com.android.ide.eclipse.gltrace.ProtoBufUtils;
     21 import com.android.ide.eclipse.gltrace.TraceFileInfo;
     22 import com.android.ide.eclipse.gltrace.TraceFileReader;
     23 
     24 import org.eclipse.swt.graphics.Image;
     25 import org.eclipse.swt.widgets.Display;
     26 
     27 import java.io.File;
     28 import java.io.FileNotFoundException;
     29 import java.io.IOException;
     30 import java.io.RandomAccessFile;
     31 import java.util.Collections;
     32 import java.util.List;
     33 
     34 /** GLTrace is the in memory model of a OpenGL trace file. */
     35 public class GLTrace {
     36     private static final TraceFileReader sTraceFileReader = new TraceFileReader();
     37 
     38     /** Information regarding the trace file. */
     39     private final TraceFileInfo mTraceFileInfo;
     40 
     41     /** List of frames in the trace. */
     42     private final List<GLFrame> mGLFrames;
     43 
     44     /** List of GL Calls comprising the trace. */
     45     private final List<GLCall> mGLCalls;
     46 
     47     /** List of context ids used by the application. */
     48     private List<Integer> mContextIds;
     49 
     50     public GLTrace(TraceFileInfo traceFileInfo, List<GLFrame> glFrames, List<GLCall> glCalls,
     51             List<Integer> contextIds) {
     52         mTraceFileInfo = traceFileInfo;
     53         mGLFrames = glFrames;
     54         mGLCalls = glCalls;
     55         mContextIds = contextIds;
     56     }
     57 
     58     public List<GLFrame> getFrames() {
     59         return mGLFrames;
     60     }
     61 
     62     public GLFrame getFrame(int i) {
     63         return mGLFrames.get(i);
     64     }
     65 
     66     public List<GLCall> getGLCalls() {
     67         return mGLCalls;
     68     }
     69 
     70     public List<GLCall> getGLCallsForFrame(int frameIndex) {
     71         if (frameIndex >= mGLFrames.size()) {
     72             return Collections.emptyList();
     73         }
     74 
     75         GLFrame frame = mGLFrames.get(frameIndex);
     76         return mGLCalls.subList(frame.getStartIndex(), frame.getEndIndex());
     77     }
     78 
     79     public Image getImage(GLCall c) {
     80         if (!c.hasFb()) {
     81             return null;
     82         }
     83 
     84         if (isTraceFileModified()) {
     85             return null;
     86         }
     87 
     88         RandomAccessFile file;
     89         try {
     90             file = new RandomAccessFile(mTraceFileInfo.getPath(), "r"); //$NON-NLS-1$
     91         } catch (FileNotFoundException e1) {
     92             return null;
     93         }
     94 
     95         GLMessage m = null;
     96         try {
     97             m = sTraceFileReader.getMessageAtOffset(file, c.getOffsetInTraceFile());
     98         } catch (Exception e) {
     99             return null;
    100         } finally {
    101             try {
    102                 file.close();
    103             } catch (IOException e) {
    104                 // ignore exception while closing file
    105             }
    106         }
    107 
    108         return ProtoBufUtils.getImage(Display.getCurrent(), m);
    109     }
    110 
    111     private boolean isTraceFileModified() {
    112         File f = new File(mTraceFileInfo.getPath());
    113         return f.length() != mTraceFileInfo.getSize()
    114                 || f.lastModified() != mTraceFileInfo.getLastModificationTime();
    115     }
    116 
    117     public List<Integer> getContexts() {
    118         return mContextIds;
    119     }
    120 }
    121