1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Jean-Christian Kouamé - Initial API and implementation
11 * Patrick Tasse - Updates to mipmap feature
12 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.core
.statesystem
.mipmap
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
18 import org
.eclipse
.jdt
.annotation
.NonNull
;
19 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
20 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.TmfStateInterval
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
29 * The mipmap feature base implementation.
31 * @author Jean-Christian Kouamé
32 * @author Patrick Tasse
35 public abstract class TmfMipmapFeature
implements ITmfMipmapFeature
{
37 /** The current state value */
38 protected @NonNull ITmfStateValue currentValue
= TmfStateValue
.nullValue();
39 /** The current start time for the state value */
40 protected long currentStartTime
;
41 /** The list of ongoing state intervals per mipmap level */
42 protected List
<List
<ITmfStateInterval
>> intervals
= new ArrayList
<>();
43 /** The state system used to store the mipmap attributes */
44 protected ITmfStateSystemBuilder ss
;
46 private int mipmapResolution
;
47 private int mipmapQuark
;
48 private List
<Integer
> levelQuarks
= new ArrayList
<>();
54 * The quark of the attribute we want to mipmap
56 * The quark of the mipmap feature attribute
57 * @param mipmapResolution
58 * The resolution that will be used for the mipmap
60 * The state system in which to insert the state changes
62 public TmfMipmapFeature(int baseQuark
, int mipmapQuark
, int mipmapResolution
, ITmfStateSystemBuilder ss
) {
63 this.mipmapQuark
= mipmapQuark
;
64 this.mipmapResolution
= mipmapResolution
;
67 /* store the base attribute quark at level 0 */
68 this.levelQuarks
.add(baseQuark
);
70 /* create the level 0 list */
71 intervals
.add(new ArrayList
<ITmfStateInterval
>(mipmapResolution
));
75 public void updateMipmap(ITmfStateValue value
, long ts
) {
76 /* if the value did not change, ignore it */
77 if (currentValue
.equals(value
)) {
81 /* if the ongoing state value is not null, create and store a state interval */
82 if (!currentValue
.isNull()) {
83 ITmfStateInterval interval
= new TmfStateInterval(currentStartTime
, ts
, getLevelQuark(0), currentValue
);
84 intervals
.get(0).add(interval
);
87 /* if the new value is not null, update the mipmap levels that are full */
88 if (!value
.isNull()) {
90 while (intervals
.get(level
).size() == getMipmapResolution()) {
91 updateMipmapLevel(++level
, ts
);
95 /* store the new value as the ongoing state value */
97 currentStartTime
= ts
;
101 public void updateAndCloseMipmap() {
102 if (!currentValue
.isNull()) {
103 ITmfStateInterval interval
= new TmfStateInterval(currentStartTime
, currentStartTime
, getLevelQuark(0), currentValue
);
104 intervals
.get(0).add(interval
);
106 for (int level
= 1; level
<= getNbLevels(); level
++) {
107 updateMipmapLevel(level
, currentStartTime
);
112 * Compute and update the mipmap level attribute from the lower-level
113 * state interval list
116 * The mipmap level to update
118 * The end timestamp to use for the mipmap interval
120 protected void updateMipmapLevel(int level
, long endTime
) {
122 /* get the lower-level interval list */
123 List
<ITmfStateInterval
> lowerIntervals
= intervals
.get(level
- 1);
124 if (lowerIntervals
.size() == 0) {
128 /* get the start time from the first interval in the lower-level list */
129 long startTime
= lowerIntervals
.get(0).getStartTime();
131 /* compute the mipmap value */
132 ITmfStateValue value
= computeMipmapValue(lowerIntervals
, startTime
, endTime
);
134 /* clear the lower-level list */
135 lowerIntervals
.clear();
137 /* get or create the current-level quark */
138 int levelQuark
= ss
.getQuarkRelativeAndAdd(mipmapQuark
, String
.valueOf(level
));
139 if (!checkLevelExists(level
)) {
140 addLevelQuark(levelQuark
);
141 ss
.updateOngoingState(TmfStateValue
.newValueInt(level
), mipmapQuark
);
142 intervals
.add(new ArrayList
<ITmfStateInterval
>(getMipmapResolution()));
145 /* add new interval to current-level list */
146 ITmfStateInterval interval
= new TmfStateInterval(startTime
, endTime
, levelQuark
, value
);
147 intervals
.get(level
).add(interval
);
149 /* update the current-level attribute */
150 ss
.modifyAttribute(startTime
, value
, levelQuark
);
151 } catch (StateValueTypeException e
) {
153 } catch (AttributeNotFoundException e
) {
155 } catch (TimeRangeException e
) {
161 * Compute the mipmap value from a list of lower-level state intervals
163 * @param lowerIntervals
164 * The list of lower-level state intervals
166 * The start time of the mipmap interval
168 * The end time of the mipmap interval
169 * @return A state value to be stored in the mipmap level attribute
171 protected abstract @NonNull ITmfStateValue
computeMipmapValue(List
<ITmfStateInterval
> lowerIntervals
, long startTime
, long endTime
);
174 * Get the mipmap resolution
176 * @return The mipmap resolution for this feature
178 protected int getMipmapResolution() {
179 return mipmapResolution
;
183 * Get the mipmap feature quark. The state value
184 * of this attribute is the mipmap number of levels.
185 * This is the parent attribute of the mipmap level quarks.
187 * @return The attribute quark for this mipmap feature
189 protected int getMipmapQuark() {
194 * Get the mipmap quark for the specified level.
195 * For level 0 the base attribute quark is returned.
198 * The mipmap level (0 for the base attribute)
199 * @return The attribute quark for this mipmap level
201 protected int getLevelQuark(int level
) {
202 return levelQuarks
.get(level
);
206 * Add a new mipmap level quark.
209 * The attribute quark for the new mipmap level
211 protected void addLevelQuark(int quark
) {
212 levelQuarks
.add(quark
);
216 * Get the mipmap number of levels.
218 * @return The current number of mipmap levels for this feature
219 * (excluding the base attribute)
221 protected int getNbLevels() {
222 return levelQuarks
.size() - 1;
226 * Checks if a mipmap level exists.
229 * The mipmap level to check
230 * @return true if this level exists, false otherwise
232 protected boolean checkLevelExists(int level
) {
233 if (level
>= levelQuarks
.size() || level
< 0) {