blob: 65919ad57e3da36e348517aaffd9883fc4d97ed5 [file] [log] [blame]
Xavier Ducrohet251d2e92010-11-01 22:02:08 -07001/*
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
Xavier Ducrohetc2e96512010-11-09 18:25:03 -080019import com.android.layoutlib.bridge.impl.DelegateManager;
Xavier Ducrohet9a4fe292011-02-09 17:17:49 -080020import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
Xavier Ducrohet251d2e92010-11-01 22:02:08 -070021
Xavier Ducrohetd348b6e2010-12-20 08:22:47 -080022import android.graphics.Shader.TileMode;
Xavier Ducrohetd9c64362010-12-14 14:40:41 -080023
Xavier Ducrohet251d2e92010-11-01 22:02:08 -070024/**
25 * Delegate implementing the native methods of android.graphics.Shader
26 *
27 * Through the layoutlib_create tool, the original native methods of Shader have been replaced
28 * by calls to methods of the same name in this delegate class.
29 *
30 * This class behaves like the original native implementation, but in Java, keeping previously
31 * native data into its own objects and mapping them to int that are sent back and forth between
32 * it and the original Shader class.
33 *
34 * This also serve as a base class for all Shader delegate classes.
35 *
36 * @see DelegateManager
37 *
38 */
39public abstract class Shader_Delegate {
40
41 // ---- delegate manager ----
42 protected static final DelegateManager<Shader_Delegate> sManager =
Xavier Ducrohetf0a53432011-02-23 16:51:08 -080043 new DelegateManager<Shader_Delegate>(Shader_Delegate.class);
Xavier Ducrohet251d2e92010-11-01 22:02:08 -070044
45 // ---- delegate helper data ----
46
47 // ---- delegate data ----
Xavier Ducrohetcc4977d2011-02-22 11:54:37 -080048 private Matrix_Delegate mLocalMatrix = null;
Xavier Ducrohet251d2e92010-11-01 22:02:08 -070049
50 // ---- Public Helper methods ----
51
52 public static Shader_Delegate getDelegate(int nativeShader) {
53 return sManager.getDelegate(nativeShader);
54 }
55
Xavier Ducrohetd348b6e2010-12-20 08:22:47 -080056 /**
57 * Returns the {@link TileMode} matching the given int.
58 * @param tileMode the tile mode int value
59 * @return the TileMode enum.
60 */
61 public static TileMode getTileMode(int tileMode) {
62 for (TileMode tm : TileMode.values()) {
63 if (tm.nativeInt == tileMode) {
64 return tm;
65 }
66 }
67
68 assert false;
69 return TileMode.CLAMP;
70 }
71
Xavier Ducrohet251d2e92010-11-01 22:02:08 -070072 public abstract java.awt.Paint getJavaPaint();
Xavier Ducrohetd348b6e2010-12-20 08:22:47 -080073 public abstract boolean isSupported();
74 public abstract String getSupportMessage();
Xavier Ducrohet251d2e92010-11-01 22:02:08 -070075
Xavier Ducrohet515a08b2011-04-06 17:03:31 -070076 public boolean isValid() {
77 if (mLocalMatrix != null && mLocalMatrix.getAffineTransform().getDeterminant() == 0) {
78 return false;
79 }
80
81 return true;
82 }
83
Xavier Ducrohet251d2e92010-11-01 22:02:08 -070084 // ---- native methods ----
85
Xavier Ducrohet9a4fe292011-02-09 17:17:49 -080086 @LayoutlibDelegate
Xavier Ducrohet251d2e92010-11-01 22:02:08 -070087 /*package*/ static void nativeDestructor(int native_shader, int native_skiaShader) {
Xavier Ducrohetcc4977d2011-02-22 11:54:37 -080088 sManager.removeJavaReferenceFor(native_shader);
Xavier Ducrohet251d2e92010-11-01 22:02:08 -070089 }
90
Xavier Ducrohet9a4fe292011-02-09 17:17:49 -080091 @LayoutlibDelegate
Xavier Ducrohet251d2e92010-11-01 22:02:08 -070092 /*package*/ static void nativeSetLocalMatrix(int native_shader, int native_skiaShader,
93 int matrix_instance) {
94 // get the delegate from the native int.
95 Shader_Delegate shaderDelegate = sManager.getDelegate(native_shader);
96 if (shaderDelegate == null) {
Xavier Ducrohet251d2e92010-11-01 22:02:08 -070097 return;
98 }
99
Xavier Ducrohetcc4977d2011-02-22 11:54:37 -0800100 shaderDelegate.mLocalMatrix = Matrix_Delegate.getDelegate(matrix_instance);
Xavier Ducrohet251d2e92010-11-01 22:02:08 -0700101 }
102
103 // ---- Private delegate/helper methods ----
104
Xavier Ducrohetd348b6e2010-12-20 08:22:47 -0800105 protected java.awt.geom.AffineTransform getLocalMatrix() {
Xavier Ducrohetcc4977d2011-02-22 11:54:37 -0800106 if (mLocalMatrix != null) {
107 return mLocalMatrix.getAffineTransform();
Xavier Ducrohetd9c64362010-12-14 14:40:41 -0800108 }
109
Xavier Ducrohetd348b6e2010-12-20 08:22:47 -0800110 return new java.awt.geom.AffineTransform();
Xavier Ducrohetd9c64362010-12-14 14:40:41 -0800111 }
Xavier Ducrohet251d2e92010-11-01 22:02:08 -0700112}