• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.state.transforms;
18 
19 import com.android.ide.eclipse.gltrace.state.GLCompositeProperty;
20 import com.android.ide.eclipse.gltrace.state.GLListProperty;
21 import com.android.ide.eclipse.gltrace.state.GLSparseArrayProperty;
22 import com.android.ide.eclipse.gltrace.state.GLStateType;
23 import com.android.ide.eclipse.gltrace.state.IGLProperty;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 /**
29  * GLPropertyAccessor's can be used to extract a certain property from the provided
30  * OpenGL State hierarchy.
31  */
32 public class GLPropertyAccessor implements IGLPropertyAccessor {
33     private final int mContextId;
34     private final List<GLPropertyExtractor> mExtractors;
35 
GLPropertyAccessor(int contextId, List<GLPropertyExtractor> extractors)36     private GLPropertyAccessor(int contextId, List<GLPropertyExtractor> extractors) {
37         mContextId = contextId;
38         mExtractors = extractors;
39     }
40 
41     @Override
getProperty(IGLProperty state)42     public IGLProperty getProperty(IGLProperty state) {
43         IGLProperty root = ((GLListProperty) state).get(mContextId);
44 
45         for (GLPropertyExtractor e : mExtractors) {
46             IGLProperty successor = e.getProperty(root);
47             if (successor == null) {
48                 root = null;
49                 break;
50             }
51             root = successor;
52         }
53 
54         return root;
55     }
56 
57     /**
58      * Factory method used to construct a {@link GLPropertyAccessor}.
59      * @param contextId id of affected context
60      * @param accessors list of accessor's to be used to navigate the property hierarchy. The
61      *                  accessors are either Integers or {@link GLStateType} objects. Integers
62      *                  are assumed to be indexes in a {@link GLListProperty} or
63      *                  {@link GLSparseArrayProperty}, and the GLStateType enum objects are
64      *                  used to query {@link GLCompositeProperty}'s.
65      */
makeAccessor(int contextId, Object...accessors)66     public static IGLPropertyAccessor makeAccessor(int contextId, Object...accessors) {
67         List<GLPropertyExtractor> extractors = new ArrayList<GLPropertyExtractor>();
68 
69         for (Object accessor : accessors) {
70             if (accessor instanceof GLStateType) {
71                 extractors.add(new GLNamePropertyExtractor((GLStateType) accessor));
72             } else if (accessor instanceof Integer) {
73                 extractors.add(new GLIndexPropertyExtractor((Integer) accessor));
74             } else {
75                 throw new IllegalArgumentException("Unknown property (" + accessor
76                         + ") used to access members of IGLProperty");
77             }
78         }
79 
80         return new GLPropertyAccessor(contextId, extractors);
81     }
82 
83     private interface GLPropertyExtractor {
getProperty(IGLProperty p)84         IGLProperty getProperty(IGLProperty p);
85     }
86 
87     /** Extract properties by name. */
88     private static class GLNamePropertyExtractor implements GLPropertyExtractor {
89         private final GLStateType mType;
90 
GLNamePropertyExtractor(GLStateType type)91         public GLNamePropertyExtractor(GLStateType type) {
92             mType = type;
93         }
94 
95         @Override
getProperty(IGLProperty p)96         public IGLProperty getProperty(IGLProperty p) {
97             if (p instanceof GLCompositeProperty) {
98                 return ((GLCompositeProperty) p).getProperty(mType);
99             }
100 
101             return null;
102         }
103     }
104 
105     /** Extract properties by index. */
106     private static class GLIndexPropertyExtractor implements GLPropertyExtractor {
107         private final int mIndex;
108 
GLIndexPropertyExtractor(int index)109         public GLIndexPropertyExtractor(int index) {
110             mIndex = index;
111         }
112 
113         @Override
getProperty(IGLProperty p)114         public IGLProperty getProperty(IGLProperty p) {
115             if (p instanceof GLListProperty && mIndex >= 0) {
116                 return ((GLListProperty) p).get(mIndex);
117             }
118             if (p instanceof GLSparseArrayProperty) {
119                 return ((GLSparseArrayProperty) p).getProperty(mIndex);
120             }
121             return null;
122         }
123     }
124 
125     @Override
getPath()126     public String getPath() {
127         StringBuilder sb = new StringBuilder(mExtractors.size() * 10);
128         for (GLPropertyExtractor e: mExtractors) {
129             if (e instanceof GLNamePropertyExtractor) {
130                 sb.append(((GLNamePropertyExtractor) e).mType);
131             } else {
132                 sb.append(((GLIndexPropertyExtractor) e).mIndex);
133             }
134             sb.append('/');
135         }
136 
137         return sb.toString();
138     }
139 }
140