summaryrefslogtreecommitdiffstats
path: root/core/java/android/view/GLES20DisplayList.java
blob: bc3bce0cfdafdb3dab80a6c41d5d02a8e71d7b5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.view;

import android.graphics.Bitmap;
import android.graphics.Matrix;

import java.util.ArrayList;

/**
 * An implementation of display list for OpenGL ES 2.0.
 */
class GLES20DisplayList extends DisplayList {
    // These lists ensure that any Bitmaps recorded by a DisplayList are kept alive as long
    // as the DisplayList is alive.  The Bitmaps are populated by the GLES20RecordingCanvas.
    final ArrayList<Bitmap> mBitmaps = new ArrayList<Bitmap>(5);

    private GLES20RecordingCanvas mCanvas;
    private boolean mValid;

    // Used for debugging
    private final String mName;

    // The native display list will be destroyed when this object dies.
    // DO NOT overwrite this reference once it is set.
    private DisplayListFinalizer mFinalizer;

    GLES20DisplayList(String name) {
        mName = name;
    }

    int getNativeDisplayList() {
        if (!mValid || mFinalizer == null) {
            throw new IllegalStateException("The display list is not valid.");
        }
        return mFinalizer.mNativeDisplayList;
    }

    @Override
    public HardwareCanvas start() {
        if (mCanvas != null) {
            throw new IllegalStateException("Recording has already started");
        }

        mValid = false;
        mCanvas = GLES20RecordingCanvas.obtain(this);
        mCanvas.start();
        return mCanvas;
    }

    @Override
    public void invalidate() {
        if (mCanvas != null) {
            mCanvas.recycle();
            mCanvas = null;
        }
        mValid = false;
    }

    @Override
    public boolean isValid() {
        return mValid;
    }

    @Override
    public void end() {
        if (mCanvas != null) {
            if (mFinalizer != null) {
                mCanvas.end(mFinalizer.mNativeDisplayList);
            } else {
                mFinalizer = new DisplayListFinalizer(mCanvas.end(0));
                GLES20Canvas.setDisplayListName(mFinalizer.mNativeDisplayList, mName);
            }
            mCanvas.recycle();
            mCanvas = null;
            mValid = true;
        }
    }

    @Override
    public int getSize() {
        if (mFinalizer == null) return 0;
        return GLES20Canvas.getDisplayListSize(mFinalizer.mNativeDisplayList);
    }

    ///////////////////////////////////////////////////////////////////////////
    // Native View Properties
    ///////////////////////////////////////////////////////////////////////////

    @Override
    public void setCaching(boolean caching) {
        try {
            nSetCaching(getNativeDisplayList(), caching);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setClipChildren(boolean clipChildren) {
        try {
            nSetClipChildren(getNativeDisplayList(), clipChildren);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setStaticMatrix(Matrix matrix) {
        try {
            nSetStaticMatrix(getNativeDisplayList(), matrix.native_instance);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setAnimationMatrix(Matrix matrix) {
        try {
            nSetAnimationMatrix(getNativeDisplayList(), matrix.native_instance);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setAlpha(float alpha) {
        try {
            nSetAlpha(getNativeDisplayList(), alpha);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setTranslationX(float translationX) {
        try {
            nSetTranslationX(getNativeDisplayList(), translationX);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setTranslationY(float translationY) {
        try {
            nSetTranslationY(getNativeDisplayList(), translationY);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setRotation(float rotation) {
        try {
            nSetRotation(getNativeDisplayList(), rotation);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setRotationX(float rotationX) {
        try {
            nSetRotationX(getNativeDisplayList(), rotationX);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setRotationY(float rotationY) {
        try {
            nSetRotationY(getNativeDisplayList(), rotationY);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setScaleX(float scaleX) {
        try {
            nSetScaleX(getNativeDisplayList(), scaleX);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setScaleY(float scaleY) {
        try {
            nSetScaleY(getNativeDisplayList(), scaleY);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setTransformationInfo(float alpha, float translationX, float translationY,
            float rotation, float rotationX, float rotationY, float scaleX, float scaleY) {
        try {
            nSetTransformationInfo(getNativeDisplayList(), alpha, translationX, translationY,
                    rotation, rotationX, rotationY, scaleX, scaleY);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setPivotX(float pivotX) {
        try {
            nSetPivotX(getNativeDisplayList(), pivotX);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setPivotY(float pivotY) {
        try {
            nSetPivotY(getNativeDisplayList(), pivotY);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setCameraDistance(float distance) {
        try {
            nSetCameraDistance(getNativeDisplayList(), distance);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setLeft(int left) {
        try {
            nSetLeft(getNativeDisplayList(), left);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setTop(int top) {
        try {
            nSetTop(getNativeDisplayList(), top);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setRight(int right) {
        try {
            nSetRight(getNativeDisplayList(), right);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setBottom(int bottom) {
        try {
            nSetBottom(getNativeDisplayList(), bottom);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setLeftTop(int left, int top) {
        try {
            nSetLeftTop(getNativeDisplayList(), left, top);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void setLeftTopRightBottom(int left, int top, int right, int bottom) {
        try {
            nSetLeftTopRightBottom(getNativeDisplayList(), left, top, right, bottom);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void offsetLeftRight(int offset) {
        try {
            nOffsetLeftRight(getNativeDisplayList(), offset);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    @Override
    public void offsetTopBottom(int offset) {
        try {
            nOffsetTopBottom(getNativeDisplayList(), offset);
        } catch (IllegalStateException e) {
            // invalid DisplayList okay: we'll set current values the next time we render to it
        }
    }

    private static native void nOffsetTopBottom(int displayList, int offset);
    private static native void nOffsetLeftRight(int displayList, int offset);
    private static native void nSetLeftTopRightBottom(int displayList, int left, int top,
            int right, int bottom);
    private static native void nSetLeftTop(int displayList, int left, int top);
    private static native void nSetBottom(int displayList, int bottom);
    private static native void nSetRight(int displayList, int right);
    private static native void nSetTop(int displayList, int top);
    private static native void nSetLeft(int displayList, int left);
    private static native void nSetCameraDistance(int displayList, float distance);
    private static native void nSetPivotY(int displayList, float pivotY);
    private static native void nSetPivotX(int displayList, float pivotX);
    private static native void nSetCaching(int displayList, boolean caching);
    private static native void nSetClipChildren(int displayList, boolean clipChildren);
    private static native void nSetApplicationScale(int displayList, float scale);
    private static native void nSetAlpha(int displayList, float alpha);
    private static native void nSetTranslationX(int displayList, float translationX);
    private static native void nSetTranslationY(int displayList, float translationY);
    private static native void nSetRotation(int displayList, float rotation);
    private static native void nSetRotationX(int displayList, float rotationX);
    private static native void nSetRotationY(int displayList, float rotationY);
    private static native void nSetScaleX(int displayList, float scaleX);
    private static native void nSetScaleY(int displayList, float scaleY);
    private static native void nSetTransformationInfo(int displayList, float alpha,
            float translationX, float translationY, float rotation, float rotationX,
            float rotationY, float scaleX, float scaleY);
    private static native void nSetStaticMatrix(int displayList, int nativeMatrix);
    private static native void nSetAnimationMatrix(int displayList, int animationMatrix);


    ///////////////////////////////////////////////////////////////////////////
    // Finalization
    ///////////////////////////////////////////////////////////////////////////

    private static class DisplayListFinalizer {
        final int mNativeDisplayList;

        public DisplayListFinalizer(int nativeDisplayList) {
            mNativeDisplayList = nativeDisplayList;
        }

        @Override
        protected void finalize() throws Throwable {
            try {
                GLES20Canvas.destroyDisplayList(mNativeDisplayList);
            } finally {
                super.finalize();
            }
        }
    }
}