BasePaintThread.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.traceviewer.painter;
00002 
00003 import java.util.ArrayList;
00004 import java.util.List;
00005 import java.util.Queue;
00006 import java.util.concurrent.Callable;
00007 import java.util.concurrent.atomic.AtomicInteger;
00008 
00009 import org.eclipse.core.runtime.Assert;
00010 import org.eclipse.swt.graphics.Color;
00011 import org.eclipse.swt.graphics.GC;
00012 import org.eclipse.swt.graphics.Device;
00013 import edu.rice.cs.hpc.traceviewer.data.db.BaseDataVisualization;
00014 import edu.rice.cs.hpc.traceviewer.data.db.TimelineDataSet;
00015 import edu.rice.cs.hpc.traceviewer.painter.ImagePosition;
00016 import edu.rice.cs.hpc.traceviewer.spaceTimeData.SpaceTimeDataController;
00017 
00018 /*****************************************************************
00019  *
00020  * Abstract base class thread to paint a canvas that can be either 
00021  *  detail canvas or depth canvas.
00022  * 
00023  * The class has a future variable List<ImagePosition> which is
00024  *
00025  *****************************************************************/
00026 public abstract class BasePaintThread implements Callable<List<ImagePosition>> {
00027 
00028     final protected Device device;
00029     final protected int width;
00030 
00031     final private Queue<TimelineDataSet> list;
00032     final private List<ImagePosition> listOfImages;
00033     final private int numberOfTotalLines;
00034     
00035     final protected SpaceTimeDataController stData;
00036     final private AtomicInteger timelineDone;
00037     
00038     /****
00039      * constructor of the class, requiring a queue of list of data (per line) to be
00040      * visualized on a set of images. The queue can be thread-safe (in case of multithreaded)
00041      * or unsafe (case of single threaded). 
00042      * <p>
00043      * To retrieve the list of images, the caller needs to call the method get() from
00044      * {@link java.util.concurrent.Callable} 
00045      * 
00046      * @param list : the queue of TimelineDataSet data. Use a thread-safe queue for multi-threads
00047      * @param numberOfTotalLines : number of total images or lines
00048      * @param device : the display device used to create images. Cannot be null
00049      * @param width : the width of the view
00050      */
00051     public BasePaintThread( SpaceTimeDataController stData, Queue<TimelineDataSet> list, 
00052             int numberOfTotalLines, AtomicInteger paintDone,
00053             Device device, int width) {
00054         
00055         Assert.isNotNull(device);
00056         Assert.isNotNull(list);
00057         
00058         this.list = list;
00059         this.numberOfTotalLines = numberOfTotalLines;
00060         this.timelineDone = paintDone;
00061         
00062         this.device = device;
00063         this.stData = stData;
00064         
00065         this.width = width;
00066         listOfImages = new ArrayList<ImagePosition>(list.size());
00067     }
00068     
00069     @Override
00070     /*
00071      * (non-Javadoc)
00072      * @see java.util.concurrent.Callable#call()
00073      */
00074     public List<ImagePosition> call() throws Exception {
00075 
00076         while( ! list.isEmpty()                               // while there are tasks to do 
00077                 ||                                            // or  
00078                 numberOfTotalLines>getNumberOfCreatedData()   // the data collection threads have not finished 
00079                 ||                                            // or 
00080                 timelineDone.get()>0 )                        // the data preparation threads haven't finished the job
00081         {
00082             // ------------------------------------------------------------------
00083             // get the task to do from the list and compute the height and the position
00084             // if the list is empty, it means the data collection threads haven't finished 
00085             //  their work yet. It's better to wait and sleep a bit 
00086             // ------------------------------------------------------------------
00087 
00088             TimelineDataSet setDataToPaint = list.poll();
00089             if (setDataToPaint == null) {
00090                 Thread.sleep(40);
00091                 continue;
00092             }
00093             final int height = setDataToPaint.getHeight();
00094             final int position = setDataToPaint.getLineNumber();
00095             
00096             // ------------------------------------------------------------------
00097             // initialize the painting, the derived class has to create image ready
00098             // ------------------------------------------------------------------
00099             initPaint(device, width, height);
00100 
00101             // ------------------------------------------------------------------
00102             // a line can contains many trace data (one trace data equals one rectangle)
00103             // we just assume here that each trace data is different and each 
00104             //  has different color
00105             // ------------------------------------------------------------------
00106             for(BaseDataVisualization data : setDataToPaint.getList()) 
00107             {
00108                 // ------------------------------------------------------------------
00109                 // paint the image
00110                 // ------------------------------------------------------------------
00111                 paint(position, data, height);
00112             }
00113             // ------------------------------------------------------------------
00114             // finalize phase
00115             // ------------------------------------------------------------------
00116             final ImagePosition imgPos = finalizePaint(position);
00117             
00118             listOfImages.add(imgPos);
00119         }
00120 
00121         return listOfImages;
00122     }
00123     
00124     /*****
00125      * Abstract method to initialize the paint. 
00126      * The derived class can use this method to create images and GC before painting it
00127      * 
00128      * @param device : device to create the image
00129      * @param width : the width of the image
00130      * @param height : the height of the image
00131      */
00132     abstract protected void initPaint(Device device, int width, int height);
00133     
00134     /*****
00135      * the actual method to paint a trace image
00136      * 
00137      * @param position : the rank or the position line number of the image
00138      * @param data : the data to be painted
00139      * @param height : the height of the image
00140      */
00141     abstract protected void paint(int position, BaseDataVisualization data, int height);
00142     
00143     /********
00144      * Finalizing the image. 
00145      * 
00146      * @param linenum : the position of the line number of the image
00147      * @return
00148      */
00149     abstract protected ImagePosition finalizePaint(int linenum);
00150     
00151     abstract protected int getNumberOfCreatedData();
00152     
00153     /***
00154      * basic method to paint on a gc
00155      * 
00156      * @param gc
00157      * @param p_start
00158      * @param p_end
00159      * @param height
00160      * @param color
00161      */
00162     protected void paint(GC gc, int p_start, int p_end, int height, Color color) {
00163         
00164         int width = p_end - p_start;
00165         if (width <= 0)
00166             return;
00167         
00168         gc.setBackground(color);
00169         gc.fillRectangle(p_start, 0, width, height);
00170     }
00171 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1