Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2009 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.cooliris.media;
     18 
     19 import java.util.ArrayList;
     20 import java.util.HashMap;
     21 
     22 
     23 // CR: comment.
     24 public final class DisplayList {
     25     private DirectLinkedList<DisplayItem> mAnimatables = new DirectLinkedList<DisplayItem>();
     26     private HashMap<MediaItem, DisplayItem> mDisplayMap = new HashMap<MediaItem, DisplayItem>(1024);
     27     private ArrayList<DisplayItem> mItems = new ArrayList<DisplayItem>(1024);
     28 
     29     public DisplayItem get(MediaItem item) {
     30         HashMap<MediaItem, DisplayItem> displayMap = mDisplayMap;
     31         DisplayItem displayItem = displayMap.get(item);
     32         if (displayItem == null) {
     33             displayItem = new DisplayItem(item);
     34             displayMap.put(item, displayItem);
     35             mItems.add(displayItem);
     36         }
     37         return displayItem;
     38     }
     39 
     40     public void setPositionAndStackIndex(DisplayItem item, Vector3f position, int stackId, boolean performTransition) {
     41         item.set(position, stackId, performTransition);
     42         if (!performTransition) {
     43             item.commit();
     44         } else {
     45             markIfDirty(item);
     46         }
     47     }
     48 
     49     public void setHasFocus(DisplayItem item, boolean hasFocus, boolean pushDown) {
     50         boolean currentHasFocus = item.getHasFocus();
     51         if (currentHasFocus != hasFocus) {
     52             item.setHasFocus(hasFocus, pushDown);
     53             markIfDirty(item);
     54         }
     55     }
     56 
     57     public final void setOffset(DisplayItem item, boolean useOffset, boolean pushDown, float span, float dx1, float dy1, float dx2, float dy2) {
     58         item.setOffset(useOffset, pushDown, span, dx1, dy1, dx2, dy2);
     59         markIfDirty(item);
     60     }
     61 
     62     public final void setSingleOffset(DisplayItem item, boolean useOffset, boolean pushAway, float x, float y, float z, float spreadValue) {
     63         item.setSingleOffset(useOffset, pushAway, x, y, z, spreadValue);
     64         markIfDirty(item);
     65     }
     66 
     67     public ArrayList<DisplayItem> getAllDisplayItems() {
     68         return mItems;
     69     }
     70 
     71     public void update(float timeElapsed) {
     72         final DirectLinkedList<DisplayItem> animatables = mAnimatables;
     73         synchronized (animatables) {
     74             DirectLinkedList.Entry<DisplayItem> entry = animatables.getHead();
     75             while (entry != null) {
     76                 DisplayItem item = entry.value;
     77                 item.update(timeElapsed);
     78                 if (!item.isAnimating()) {
     79                     entry = animatables.remove(entry);
     80                 } else {
     81                     entry = entry.next;
     82                 }
     83             }
     84         }
     85     }
     86 
     87     public int getNumAnimatables() {
     88         return mAnimatables.size();
     89     }
     90 
     91     public void setAlive(DisplayItem item, boolean alive) {
     92         item.mAlive = alive;
     93         if (alive && item.isAnimating()) {
     94             final DirectLinkedList.Entry<DisplayItem> entry = item.getAnimatablesEntry();
     95             if (!entry.inserted) {
     96                 mAnimatables.add(entry);
     97             }
     98         }
     99     }
    100 
    101     public void commit(DisplayItem item) {
    102         item.commit();
    103         final DirectLinkedList<DisplayItem> animatables = mAnimatables;
    104         synchronized (animatables) {
    105             animatables.remove(item.getAnimatablesEntry());
    106         }
    107     }
    108 
    109     public void addToAnimatables(DisplayItem item) {
    110         final DirectLinkedList.Entry<DisplayItem> entry = item.getAnimatablesEntry();
    111         if (!entry.inserted) {
    112             final DirectLinkedList<DisplayItem> animatables = mAnimatables;
    113             synchronized (animatables) {
    114                 animatables.add(entry);
    115             }
    116         }
    117     }
    118 
    119     private void markIfDirty(DisplayItem item) {
    120         if (item.isAnimating()) {
    121             addToAnimatables(item);
    122         }
    123     }
    124 
    125     public void clear() {
    126         mDisplayMap.clear();
    127         synchronized (mItems) {
    128             mItems.clear();
    129         }
    130     }
    131 
    132     public void clearExcept(DisplayItem[] displayItems) {
    133         HashMap<MediaItem, DisplayItem> displayMap = mDisplayMap;
    134         displayMap.clear();
    135         synchronized (mItems) {
    136             mItems.clear();
    137             int numItems = displayItems.length;
    138             for (int i = 0; i < numItems; ++i) {
    139                 DisplayItem displayItem = displayItems[i];
    140                 if (displayItem != null) {
    141                     displayMap.put(displayItem.mItemRef, displayItem);
    142                     mItems.add(displayItem);
    143                 }
    144             }
    145         }
    146     }
    147 }
    148