ColorFilter_Delegate.java revision 1049f474bcc0a76ef911ff4f60c586a2c7d3f5b0
1/*
2 * Copyright (C) 2010 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
17package android.graphics;
18
19import com.android.layoutlib.bridge.impl.DelegateManager;
20import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
21
22import java.awt.Graphics2D;
23
24import libcore.util.NativeAllocationRegistry_Delegate;
25
26/**
27 * Delegate implementing the native methods of android.graphics.ColorFilter
28 *
29 * Through the layoutlib_create tool, the original native methods of ColorFilter have been replaced
30 * by calls to methods of the same name in this delegate class.
31 *
32 * This class behaves like the original native implementation, but in Java, keeping previously
33 * native data into its own objects and mapping them to int that are sent back and forth between
34 * it and the original ColorFilter class.
35 *
36 * This also serve as a base class for all ColorFilter delegate classes.
37 *
38 * @see DelegateManager
39 *
40 */
41public abstract class ColorFilter_Delegate {
42
43    // ---- delegate manager ----
44    protected static final DelegateManager<ColorFilter_Delegate> sManager =
45            new DelegateManager<ColorFilter_Delegate>(ColorFilter_Delegate.class);
46    private static long sFinalizer = -1;
47
48    // ---- delegate helper data ----
49
50    // ---- delegate data ----
51
52    // ---- Public Helper methods ----
53
54    public static ColorFilter_Delegate getDelegate(long nativeShader) {
55        return sManager.getDelegate(nativeShader);
56    }
57
58    public abstract String getSupportMessage();
59
60    public boolean isSupported() {
61        return false;
62    }
63
64    public void applyFilter(Graphics2D g, int width, int height) {
65        // This should never be called directly. If supported, the sub class should override this.
66        assert false;
67    }
68
69    // ---- native methods ----
70
71    @LayoutlibDelegate
72    /*package*/ static long nativeGetFinalizer() {
73        synchronized (ColorFilter_Delegate.class) {
74            if (sFinalizer == -1) {
75                sFinalizer = NativeAllocationRegistry_Delegate.createFinalizer(
76                        sManager::removeJavaReferenceFor);
77            }
78        }
79        return sFinalizer;
80    }
81
82    // ---- Private delegate/helper methods ----
83}
84