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    boolean copyInto(Bitmap bitmap) {
57        return GLES20Canvas.nCopyLayer(mLayer, bitmap.mNativeBitmap);
58    }
59
60    @Override
61    void destroy() {
62        if (mFinalizer != null) {
63            mFinalizer.destroy();
64            mFinalizer = null;
65        }
66        mLayer = 0;
67    }
68
69    @Override
70    void clearStorage() {
71        if (mLayer != 0) GLES20Canvas.nClearLayerTexture(mLayer);
72    }
73
74    static class Finalizer {
75        private int mLayerId;
76
77        public Finalizer(int layerId) {
78            mLayerId = layerId;
79        }
80
81        @Override
82        protected void finalize() throws Throwable {
83            try {
84                if (mLayerId != 0) {
85                    GLES20Canvas.nDestroyLayerDeferred(mLayerId);
86                }
87            } finally {
88                super.finalize();
89            }
90        }
91
92        void destroy() {
93            GLES20Canvas.nDestroyLayer(mLayerId);
94            mLayerId = 0;
95        }
96    }
97}
98