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
18package android.view;
19
20import android.graphics.Bitmap;
21import android.graphics.Paint;
22
23/**
24 * An OpenGL ES 2.0 implementation of {@link HardwareLayer}.
25 */
26abstract class GLES20Layer extends HardwareLayer {
27    int mLayer;
28    Finalizer mFinalizer;
29
30    GLES20Layer() {
31    }
32
33    GLES20Layer(int width, int height, boolean opaque) {
34        super(width, height, opaque);
35    }
36
37    /**
38     * Returns the native layer object used to render this layer.
39     *
40     * @return A pointer to the native layer object, or 0 if the object is NULL
41     */
42    public int getLayer() {
43        return mLayer;
44    }
45
46    @Override
47    void setLayerPaint(Paint paint) {
48        if (paint != null) {
49            GLES20Canvas.nSetLayerPaint(mLayer, paint.mNativePaint);
50            GLES20Canvas.nSetLayerColorFilter(mLayer, paint.getColorFilter() != null ?
51                    paint.getColorFilter().nativeColorFilter : 0);
52        }
53    }
54
55    @Override
56    public boolean copyInto(Bitmap bitmap) {
57        return GLES20Canvas.nCopyLayer(mLayer, bitmap.mNativeBitmap);
58    }
59
60    @Override
61    public void destroy() {
62        if (mDisplayList != null) {
63            mDisplayList.reset();
64        }
65        if (mFinalizer != null) {
66            mFinalizer.destroy();
67            mFinalizer = null;
68        }
69        mLayer = 0;
70    }
71
72    @Override
73    void clearStorage() {
74        if (mLayer != 0) GLES20Canvas.nClearLayerTexture(mLayer);
75    }
76
77    static class Finalizer {
78        private int mLayerId;
79
80        public Finalizer(int layerId) {
81            mLayerId = layerId;
82        }
83
84        @Override
85        protected void finalize() throws Throwable {
86            try {
87                if (mLayerId != 0) {
88                    GLES20Canvas.nDestroyLayerDeferred(mLayerId);
89                }
90            } finally {
91                super.finalize();
92            }
93        }
94
95        void destroy() {
96            GLES20Canvas.nDestroyLayer(mLayerId);
97            mLayerId = 0;
98        }
99    }
100}
101