Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 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 | |
| 17 | package android.graphics; |
| 18 | |
Xavier Ducrohet | 918aaa5 | 2011-01-13 10:59:34 -0800 | [diff] [blame] | 19 | import com.android.ide.common.rendering.api.LayoutLog; |
Xavier Ducrohet | 168677c | 2010-12-06 10:11:44 -0800 | [diff] [blame] | 20 | import com.android.layoutlib.bridge.Bridge; |
Xavier Ducrohet | c2e9651 | 2010-11-09 18:25:03 -0800 | [diff] [blame] | 21 | import com.android.layoutlib.bridge.impl.DelegateManager; |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 22 | import com.android.layoutlib.bridge.impl.GcSnapshot; |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 23 | import com.android.tools.layoutlib.annotations.LayoutlibDelegate; |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 24 | |
Xavier Ducrohet | b6e53f4 | 2011-01-07 18:37:49 -0800 | [diff] [blame] | 25 | import android.graphics.Bitmap.Config; |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 26 | import android.text.TextUtils; |
| 27 | |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 28 | import java.awt.Color; |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 29 | import java.awt.Composite; |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 30 | import java.awt.Graphics2D; |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 31 | import java.awt.Rectangle; |
| 32 | import java.awt.RenderingHints; |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 33 | import java.awt.Shape; |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 34 | import java.awt.geom.AffineTransform; |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 35 | import java.awt.geom.Arc2D; |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 36 | import java.awt.image.BufferedImage; |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 37 | |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 38 | |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Delegate implementing the native methods of android.graphics.Canvas |
| 41 | * |
| 42 | * Through the layoutlib_create tool, the original native methods of Canvas have been replaced |
| 43 | * by calls to methods of the same name in this delegate class. |
| 44 | * |
| 45 | * This class behaves like the original native implementation, but in Java, keeping previously |
| 46 | * native data into its own objects and mapping them to int that are sent back and forth between |
| 47 | * it and the original Canvas class. |
| 48 | * |
| 49 | * @see DelegateManager |
| 50 | * |
| 51 | */ |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 52 | public final class Canvas_Delegate { |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 53 | |
| 54 | // ---- delegate manager ---- |
| 55 | private static final DelegateManager<Canvas_Delegate> sManager = |
Xavier Ducrohet | f0a5343 | 2011-02-23 16:51:08 -0800 | [diff] [blame] | 56 | new DelegateManager<Canvas_Delegate>(Canvas_Delegate.class); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 57 | |
| 58 | // ---- delegate helper data ---- |
| 59 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 60 | private final static boolean[] sBoolOut = new boolean[1]; |
| 61 | |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 62 | // ---- delegate data ---- |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 63 | private Bitmap_Delegate mBitmap; |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 64 | private GcSnapshot mSnapshot; |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 65 | |
Xavier Ducrohet | cc4977d | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 66 | private DrawFilter_Delegate mDrawFilter = null; |
Xavier Ducrohet | d43909c | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 67 | |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 68 | // ---- Public Helper methods ---- |
| 69 | |
| 70 | /** |
| 71 | * Returns the native delegate associated to a given {@link Canvas} object. |
| 72 | */ |
| 73 | public static Canvas_Delegate getDelegate(Canvas canvas) { |
| 74 | return sManager.getDelegate(canvas.mNativeCanvas); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the native delegate associated to a given an int referencing a {@link Canvas} object. |
| 79 | */ |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 80 | public static Canvas_Delegate getDelegate(long native_canvas) { |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 81 | return sManager.getDelegate(native_canvas); |
| 82 | } |
| 83 | |
| 84 | /** |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 85 | * Returns the current {@link Graphics2D} used to draw. |
| 86 | */ |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 87 | public GcSnapshot getSnapshot() { |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 88 | return mSnapshot; |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Xavier Ducrohet | d43909c | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 91 | /** |
| 92 | * Returns the {@link DrawFilter} delegate or null if none have been set. |
| 93 | * |
| 94 | * @return the delegate or null. |
| 95 | */ |
| 96 | public DrawFilter_Delegate getDrawFilter() { |
Xavier Ducrohet | cc4977d | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 97 | return mDrawFilter; |
Xavier Ducrohet | d43909c | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 100 | // ---- native methods ---- |
| 101 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 102 | @LayoutlibDelegate |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 103 | /*package*/ static boolean isOpaque(Canvas thisCanvas) { |
Xavier Ducrohet | b6e53f4 | 2011-01-07 18:37:49 -0800 | [diff] [blame] | 104 | // get the delegate from the native int. |
| 105 | Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas); |
| 106 | if (canvasDelegate == null) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | return canvasDelegate.mBitmap.getConfig() == Config.RGB_565; |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 113 | @LayoutlibDelegate |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 114 | /*package*/ static int getWidth(Canvas thisCanvas) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 115 | // get the delegate from the native int. |
| 116 | Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas); |
| 117 | if (canvasDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 121 | return canvasDelegate.mBitmap.getImage().getWidth(); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 124 | @LayoutlibDelegate |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 125 | /*package*/ static int getHeight(Canvas thisCanvas) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 126 | // get the delegate from the native int. |
| 127 | Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas); |
| 128 | if (canvasDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 132 | return canvasDelegate.mBitmap.getImage().getHeight(); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 135 | @LayoutlibDelegate |
| 136 | /*package*/ static void translate(Canvas thisCanvas, float dx, float dy) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 137 | // get the delegate from the native int. |
| 138 | Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas); |
| 139 | if (canvasDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 140 | return; |
| 141 | } |
| 142 | |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 143 | canvasDelegate.getSnapshot().translate(dx, dy); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 146 | @LayoutlibDelegate |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 147 | /*package*/ static void rotate(Canvas thisCanvas, float degrees) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 148 | // get the delegate from the native int. |
| 149 | Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas); |
| 150 | if (canvasDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 154 | canvasDelegate.getSnapshot().rotate(Math.toRadians(degrees)); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 157 | @LayoutlibDelegate |
| 158 | /*package*/ static void scale(Canvas thisCanvas, float sx, float sy) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 159 | // get the delegate from the native int. |
| 160 | Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas); |
| 161 | if (canvasDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 165 | canvasDelegate.getSnapshot().scale(sx, sy); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 168 | @LayoutlibDelegate |
| 169 | /*package*/ static void skew(Canvas thisCanvas, float kx, float ky) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 170 | // get the delegate from the native int. |
| 171 | Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas); |
| 172 | if (canvasDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | |
| 176 | // get the current top graphics2D object. |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 177 | GcSnapshot g = canvasDelegate.getSnapshot(); |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 178 | |
| 179 | // get its current matrix |
| 180 | AffineTransform currentTx = g.getTransform(); |
| 181 | // get the AffineTransform for the given skew. |
| 182 | float[] mtx = Matrix_Delegate.getSkew(kx, ky); |
| 183 | AffineTransform matrixTx = Matrix_Delegate.getAffineTransform(mtx); |
| 184 | |
| 185 | // combine them so that the given matrix is applied after. |
| 186 | currentTx.preConcatenate(matrixTx); |
| 187 | |
| 188 | // give it to the graphics2D as a new matrix replacing all previous transform |
| 189 | g.setTransform(currentTx); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 192 | @LayoutlibDelegate |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 193 | /*package*/ static boolean clipRect(Canvas thisCanvas, RectF rect) { |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 194 | return clipRect(thisCanvas, rect.left, rect.top, rect.right, rect.bottom); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 197 | @LayoutlibDelegate |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 198 | /*package*/ static boolean clipRect(Canvas thisCanvas, Rect rect) { |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 199 | return clipRect(thisCanvas, (float) rect.left, (float) rect.top, |
| 200 | (float) rect.right, (float) rect.bottom); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 203 | @LayoutlibDelegate |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 204 | /*package*/ static boolean clipRect(Canvas thisCanvas, float left, float top, float right, |
| 205 | float bottom) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 206 | // get the delegate from the native int. |
| 207 | Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas); |
| 208 | if (canvasDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | |
Xavier Ducrohet | 8da36314 | 2010-12-13 16:42:01 -0800 | [diff] [blame] | 212 | return canvasDelegate.clipRect(left, top, right, bottom, Region.Op.INTERSECT.nativeInt); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 215 | @LayoutlibDelegate |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 216 | /*package*/ static boolean clipRect(Canvas thisCanvas, int left, int top, int right, |
| 217 | int bottom) { |
| 218 | |
| 219 | return clipRect(thisCanvas, (float) left, (float) top, (float) right, (float) bottom); |
| 220 | } |
| 221 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 222 | @LayoutlibDelegate |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 223 | /*package*/ static int save(Canvas thisCanvas) { |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 224 | return save(thisCanvas, Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG); |
| 225 | } |
| 226 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 227 | @LayoutlibDelegate |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 228 | /*package*/ static int save(Canvas thisCanvas, int saveFlags) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 229 | // get the delegate from the native int. |
| 230 | Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas); |
| 231 | if (canvasDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 235 | return canvasDelegate.save(saveFlags); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 238 | @LayoutlibDelegate |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 239 | /*package*/ static void restore(Canvas thisCanvas) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 240 | // get the delegate from the native int. |
| 241 | Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas); |
| 242 | if (canvasDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 246 | canvasDelegate.restore(); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 249 | @LayoutlibDelegate |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 250 | /*package*/ static int getSaveCount(Canvas thisCanvas) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 251 | // get the delegate from the native int. |
| 252 | Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas); |
| 253 | if (canvasDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 257 | return canvasDelegate.getSnapshot().size(); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 260 | @LayoutlibDelegate |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 261 | /*package*/ static void restoreToCount(Canvas thisCanvas, int saveCount) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 262 | // get the delegate from the native int. |
| 263 | Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas); |
| 264 | if (canvasDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 268 | canvasDelegate.restoreTo(saveCount); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 271 | @LayoutlibDelegate |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 272 | /*package*/ static void drawPoints(Canvas thisCanvas, float[] pts, int offset, int count, |
| 273 | Paint paint) { |
| 274 | // FIXME |
Xavier Ducrohet | 8a80a85 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 275 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, |
| 276 | "Canvas.drawPoint is not supported.", null, null /*data*/); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 279 | @LayoutlibDelegate |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 280 | /*package*/ static void drawPoint(Canvas thisCanvas, float x, float y, Paint paint) { |
| 281 | // FIXME |
Xavier Ducrohet | 8a80a85 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 282 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, |
| 283 | "Canvas.drawPoint is not supported.", null, null /*data*/); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 286 | @LayoutlibDelegate |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 287 | /*package*/ static void drawLines(Canvas thisCanvas, |
| 288 | final float[] pts, final int offset, final int count, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 289 | Paint paint) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 290 | draw(thisCanvas.mNativeCanvas, paint.mNativePaint, false /*compositeOnly*/, |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 291 | false /*forceSrcMode*/, new GcSnapshot.Drawable() { |
Xavier Ducrohet | 46d43cc | 2012-02-02 15:44:50 -0800 | [diff] [blame] | 292 | @Override |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 293 | public void draw(Graphics2D graphics, Paint_Delegate paintDelegate) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 294 | for (int i = 0 ; i < count ; i += 4) { |
| 295 | graphics.drawLine((int)pts[i + offset], (int)pts[i + offset + 1], |
| 296 | (int)pts[i + offset + 2], (int)pts[i + offset + 3]); |
| 297 | } |
| 298 | } |
| 299 | }); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 302 | @LayoutlibDelegate |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 303 | /*package*/ static void freeCaches() { |
Xavier Ducrohet | b44b43b | 2010-12-23 10:22:14 -0800 | [diff] [blame] | 304 | // nothing to be done here. |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 307 | @LayoutlibDelegate |
Xavier Ducrohet | f18c68b | 2012-05-08 17:14:18 -0700 | [diff] [blame] | 308 | /*package*/ static void freeTextLayoutCaches() { |
| 309 | // nothing to be done here yet. |
| 310 | } |
| 311 | |
| 312 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 313 | /*package*/ static long initRaster(long nativeBitmapOrZero) { |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 314 | if (nativeBitmapOrZero > 0) { |
| 315 | // get the Bitmap from the int |
| 316 | Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(nativeBitmapOrZero); |
| 317 | |
| 318 | // create a new Canvas_Delegate with the given bitmap and return its new native int. |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 319 | Canvas_Delegate newDelegate = new Canvas_Delegate(bitmapDelegate); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 320 | |
Xavier Ducrohet | cc4977d | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 321 | return sManager.addNewDelegate(newDelegate); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 322 | } |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 323 | |
| 324 | // create a new Canvas_Delegate and return its new native int. |
| 325 | Canvas_Delegate newDelegate = new Canvas_Delegate(); |
| 326 | |
| 327 | return sManager.addNewDelegate(newDelegate); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 330 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 331 | /*package*/ static void copyNativeCanvasState(long srcCanvas, long dstCanvas) { |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 332 | // get the delegate from the native int. |
Deepanshu Gupta | 279c00e | 2013-05-23 15:20:04 -0700 | [diff] [blame] | 333 | Canvas_Delegate srcCanvasDelegate = sManager.getDelegate(srcCanvas); |
| 334 | if (srcCanvasDelegate == null) { |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | |
| 338 | // get the delegate from the native int. |
Deepanshu Gupta | 279c00e | 2013-05-23 15:20:04 -0700 | [diff] [blame] | 339 | Canvas_Delegate dstCanvasDelegate = sManager.getDelegate(dstCanvas); |
| 340 | if (dstCanvasDelegate == null) { |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 341 | return; |
| 342 | } |
Deepanshu Gupta | 279c00e | 2013-05-23 15:20:04 -0700 | [diff] [blame] | 343 | // TODO: actually copy the canvas state. |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 346 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 347 | /*package*/ static long native_saveLayer(long nativeCanvas, RectF bounds, |
| 348 | long paint, int layerFlags) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 349 | // get the delegate from the native int. |
| 350 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas); |
| 351 | if (canvasDelegate == null) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | Paint_Delegate paintDelegate = Paint_Delegate.getDelegate(paint); |
| 356 | if (paintDelegate == null) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | return canvasDelegate.saveLayer(bounds, paintDelegate, layerFlags); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 363 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 364 | /*package*/ static long native_saveLayer(long nativeCanvas, float l, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 365 | float t, float r, float b, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 366 | long paint, int layerFlags) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 367 | // get the delegate from the native int. |
| 368 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas); |
| 369 | if (canvasDelegate == null) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | Paint_Delegate paintDelegate = Paint_Delegate.getDelegate(paint); |
| 374 | if (paintDelegate == null) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | return canvasDelegate.saveLayer(new RectF(l, t, r, b), |
| 379 | paintDelegate, layerFlags); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 382 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 383 | /*package*/ static long native_saveLayerAlpha(long nativeCanvas, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 384 | RectF bounds, int alpha, |
| 385 | int layerFlags) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 386 | // get the delegate from the native int. |
| 387 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas); |
| 388 | if (canvasDelegate == null) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | return canvasDelegate.saveLayerAlpha(bounds, alpha, layerFlags); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 395 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 396 | /*package*/ static long native_saveLayerAlpha(long nativeCanvas, float l, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 397 | float t, float r, float b, |
| 398 | int alpha, int layerFlags) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 399 | // get the delegate from the native int. |
| 400 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas); |
| 401 | if (canvasDelegate == null) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | return canvasDelegate.saveLayerAlpha(new RectF(l, t, r, b), alpha, layerFlags); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 409 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 410 | /*package*/ static void native_concat(long nCanvas, long nMatrix) { |
Xavier Ducrohet | 2eea6fa | 2010-11-24 11:26:40 -0800 | [diff] [blame] | 411 | // get the delegate from the native int. |
| 412 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nCanvas); |
| 413 | if (canvasDelegate == null) { |
Xavier Ducrohet | 2eea6fa | 2010-11-24 11:26:40 -0800 | [diff] [blame] | 414 | return; |
| 415 | } |
| 416 | |
| 417 | Matrix_Delegate matrixDelegate = Matrix_Delegate.getDelegate(nMatrix); |
| 418 | if (matrixDelegate == null) { |
Xavier Ducrohet | 2eea6fa | 2010-11-24 11:26:40 -0800 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | |
| 422 | // get the current top graphics2D object. |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 423 | GcSnapshot snapshot = canvasDelegate.getSnapshot(); |
Xavier Ducrohet | 2eea6fa | 2010-11-24 11:26:40 -0800 | [diff] [blame] | 424 | |
| 425 | // get its current matrix |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 426 | AffineTransform currentTx = snapshot.getTransform(); |
Xavier Ducrohet | 2eea6fa | 2010-11-24 11:26:40 -0800 | [diff] [blame] | 427 | // get the AffineTransform of the given matrix |
| 428 | AffineTransform matrixTx = matrixDelegate.getAffineTransform(); |
| 429 | |
| 430 | // combine them so that the given matrix is applied after. |
Xavier Ducrohet | fb93ce9 | 2011-06-03 19:38:03 -0700 | [diff] [blame] | 431 | currentTx.concatenate(matrixTx); |
Xavier Ducrohet | 2eea6fa | 2010-11-24 11:26:40 -0800 | [diff] [blame] | 432 | |
| 433 | // give it to the graphics2D as a new matrix replacing all previous transform |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 434 | snapshot.setTransform(currentTx); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 437 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 438 | /*package*/ static void native_setMatrix(long nCanvas, long nMatrix) { |
Xavier Ducrohet | 2eea6fa | 2010-11-24 11:26:40 -0800 | [diff] [blame] | 439 | // get the delegate from the native int. |
| 440 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nCanvas); |
| 441 | if (canvasDelegate == null) { |
Xavier Ducrohet | d43909c | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 442 | return; |
Xavier Ducrohet | 2eea6fa | 2010-11-24 11:26:40 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | Matrix_Delegate matrixDelegate = Matrix_Delegate.getDelegate(nMatrix); |
| 446 | if (matrixDelegate == null) { |
Xavier Ducrohet | d43909c | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 447 | return; |
Xavier Ducrohet | 2eea6fa | 2010-11-24 11:26:40 -0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // get the current top graphics2D object. |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 451 | GcSnapshot snapshot = canvasDelegate.getSnapshot(); |
Xavier Ducrohet | 2eea6fa | 2010-11-24 11:26:40 -0800 | [diff] [blame] | 452 | |
| 453 | // get the AffineTransform of the given matrix |
| 454 | AffineTransform matrixTx = matrixDelegate.getAffineTransform(); |
| 455 | |
| 456 | // give it to the graphics2D as a new matrix replacing all previous transform |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 457 | snapshot.setTransform(matrixTx); |
Xavier Ducrohet | 2eea6fa | 2010-11-24 11:26:40 -0800 | [diff] [blame] | 458 | |
Xavier Ducrohet | 168677c | 2010-12-06 10:11:44 -0800 | [diff] [blame] | 459 | if (matrixDelegate.hasPerspective()) { |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 460 | assert false; |
Xavier Ducrohet | 918aaa5 | 2011-01-13 10:59:34 -0800 | [diff] [blame] | 461 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_AFFINE, |
Xavier Ducrohet | 168677c | 2010-12-06 10:11:44 -0800 | [diff] [blame] | 462 | "android.graphics.Canvas#setMatrix(android.graphics.Matrix) only " + |
Xavier Ducrohet | 51a7e54 | 2011-01-14 16:40:43 -0800 | [diff] [blame] | 463 | "supports affine transformations.", null, null /*data*/); |
Xavier Ducrohet | 168677c | 2010-12-06 10:11:44 -0800 | [diff] [blame] | 464 | } |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 467 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 468 | /*package*/ static boolean native_clipRect(long nCanvas, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 469 | float left, float top, |
| 470 | float right, float bottom, |
| 471 | int regionOp) { |
Xavier Ducrohet | 8da36314 | 2010-12-13 16:42:01 -0800 | [diff] [blame] | 472 | |
| 473 | // get the delegate from the native int. |
| 474 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nCanvas); |
| 475 | if (canvasDelegate == null) { |
Xavier Ducrohet | d43909c | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 476 | return false; |
Xavier Ducrohet | 8da36314 | 2010-12-13 16:42:01 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 479 | return canvasDelegate.clipRect(left, top, right, bottom, regionOp); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 482 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 483 | /*package*/ static boolean native_clipPath(long nativeCanvas, |
| 484 | long nativePath, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 485 | int regionOp) { |
Xavier Ducrohet | b44b43b | 2010-12-23 10:22:14 -0800 | [diff] [blame] | 486 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas); |
| 487 | if (canvasDelegate == null) { |
| 488 | return true; |
| 489 | } |
| 490 | |
| 491 | Path_Delegate pathDelegate = Path_Delegate.getDelegate(nativePath); |
| 492 | if (pathDelegate == null) { |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | return canvasDelegate.mSnapshot.clip(pathDelegate.getJavaShape(), regionOp); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 499 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 500 | /*package*/ static boolean native_clipRegion(long nativeCanvas, |
| 501 | long nativeRegion, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 502 | int regionOp) { |
Xavier Ducrohet | b44b43b | 2010-12-23 10:22:14 -0800 | [diff] [blame] | 503 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas); |
| 504 | if (canvasDelegate == null) { |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | Region_Delegate region = Region_Delegate.getDelegate(nativeRegion); |
| 509 | if (region == null) { |
| 510 | return true; |
| 511 | } |
| 512 | |
| 513 | return canvasDelegate.mSnapshot.clip(region.getJavaArea(), regionOp); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 516 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 517 | /*package*/ static void nativeSetDrawFilter(long nativeCanvas, long nativeFilter) { |
Xavier Ducrohet | d43909c | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 518 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas); |
| 519 | if (canvasDelegate == null) { |
| 520 | return; |
| 521 | } |
| 522 | |
Xavier Ducrohet | cc4977d | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 523 | canvasDelegate.mDrawFilter = DrawFilter_Delegate.getDelegate(nativeFilter); |
Xavier Ducrohet | d43909c | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 524 | |
Xavier Ducrohet | cc4977d | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 525 | if (canvasDelegate.mDrawFilter != null && |
| 526 | canvasDelegate.mDrawFilter.isSupported() == false) { |
Xavier Ducrohet | 918aaa5 | 2011-01-13 10:59:34 -0800 | [diff] [blame] | 527 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_DRAWFILTER, |
Xavier Ducrohet | cc4977d | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 528 | canvasDelegate.mDrawFilter.getSupportMessage(), null, null /*data*/); |
Xavier Ducrohet | d43909c | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 529 | } |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 532 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 533 | /*package*/ static boolean native_getClipBounds(long nativeCanvas, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 534 | Rect bounds) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 535 | // get the delegate from the native int. |
| 536 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas); |
| 537 | if (canvasDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 538 | return false; |
| 539 | } |
| 540 | |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 541 | Rectangle rect = canvasDelegate.getSnapshot().getClip().getBounds(); |
Xavier Ducrohet | a7cac5e0 | 2011-01-06 15:50:42 -0800 | [diff] [blame] | 542 | if (rect != null && rect.isEmpty() == false) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 543 | bounds.left = rect.x; |
| 544 | bounds.top = rect.y; |
| 545 | bounds.right = rect.x + rect.width; |
| 546 | bounds.bottom = rect.y + rect.height; |
| 547 | return true; |
| 548 | } |
Xavier Ducrohet | a7cac5e0 | 2011-01-06 15:50:42 -0800 | [diff] [blame] | 549 | |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 550 | return false; |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 553 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 554 | /*package*/ static void native_getCTM(long canvas, long matrix) { |
Xavier Ducrohet | b6e53f4 | 2011-01-07 18:37:49 -0800 | [diff] [blame] | 555 | // get the delegate from the native int. |
| 556 | Canvas_Delegate canvasDelegate = sManager.getDelegate(canvas); |
| 557 | if (canvasDelegate == null) { |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | Matrix_Delegate matrixDelegate = Matrix_Delegate.getDelegate(matrix); |
| 562 | if (matrixDelegate == null) { |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | AffineTransform transform = canvasDelegate.getSnapshot().getTransform(); |
| 567 | matrixDelegate.set(Matrix_Delegate.makeValues(transform)); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 570 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 571 | /*package*/ static boolean native_quickReject(long nativeCanvas, |
Deepanshu Gupta | 279c00e | 2013-05-23 15:20:04 -0700 | [diff] [blame] | 572 | RectF rect) { |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 573 | // FIXME properly implement quickReject |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 574 | return false; |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 577 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 578 | /*package*/ static boolean native_quickReject(long nativeCanvas, |
| 579 | long path) { |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 580 | // FIXME properly implement quickReject |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 581 | return false; |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 584 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 585 | /*package*/ static boolean native_quickReject(long nativeCanvas, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 586 | float left, float top, |
Deepanshu Gupta | 279c00e | 2013-05-23 15:20:04 -0700 | [diff] [blame] | 587 | float right, float bottom) { |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 588 | // FIXME properly implement quickReject |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 589 | return false; |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 592 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 593 | /*package*/ static void native_drawRGB(long nativeCanvas, int r, int g, int b) { |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 594 | native_drawColor(nativeCanvas, 0xFF000000 | r << 16 | (g&0xFF) << 8 | (b&0xFF), |
| 595 | PorterDuff.Mode.SRC_OVER.nativeInt); |
| 596 | |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 599 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 600 | /*package*/ static void native_drawARGB(long nativeCanvas, int a, int r, int g, int b) { |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 601 | native_drawColor(nativeCanvas, a << 24 | (r&0xFF) << 16 | (g&0xFF) << 8 | (b&0xFF), |
| 602 | PorterDuff.Mode.SRC_OVER.nativeInt); |
| 603 | } |
| 604 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 605 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 606 | /*package*/ static void native_drawColor(long nativeCanvas, int color) { |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 607 | native_drawColor(nativeCanvas, color, PorterDuff.Mode.SRC_OVER.nativeInt); |
| 608 | } |
| 609 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 610 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 611 | /*package*/ static void native_drawColor(long nativeCanvas, final int color, final int mode) { |
Xavier Ducrohet | abff653 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 612 | // get the delegate from the native int. |
| 613 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas); |
| 614 | if (canvasDelegate == null) { |
Xavier Ducrohet | abff653 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 615 | return; |
| 616 | } |
| 617 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 618 | final int w = canvasDelegate.mBitmap.getImage().getWidth(); |
| 619 | final int h = canvasDelegate.mBitmap.getImage().getHeight(); |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 620 | draw(nativeCanvas, new GcSnapshot.Drawable() { |
Xavier Ducrohet | abff653 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 621 | |
Xavier Ducrohet | 46d43cc | 2012-02-02 15:44:50 -0800 | [diff] [blame] | 622 | @Override |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 623 | public void draw(Graphics2D graphics, Paint_Delegate paint) { |
| 624 | // reset its transform just in case |
| 625 | graphics.setTransform(new AffineTransform()); |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 626 | |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 627 | // set the color |
| 628 | graphics.setColor(new Color(color, true /*alpha*/)); |
| 629 | |
| 630 | Composite composite = PorterDuffXfermode_Delegate.getComposite( |
| 631 | PorterDuffXfermode_Delegate.getPorterDuffMode(mode), 0xFF); |
| 632 | if (composite != null) { |
| 633 | graphics.setComposite(composite); |
| 634 | } |
| 635 | |
| 636 | graphics.fillRect(0, 0, w, h); |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 637 | } |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 638 | }); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 641 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 642 | /*package*/ static void native_drawPaint(long nativeCanvas, long paint) { |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 643 | // FIXME |
Xavier Ducrohet | 8a80a85 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 644 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, |
| 645 | "Canvas.drawPaint is not supported.", null, null /*data*/); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 648 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 649 | /*package*/ static void native_drawLine(long nativeCanvas, |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 650 | final float startX, final float startY, final float stopX, final float stopY, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 651 | long paint) { |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 652 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 653 | draw(nativeCanvas, paint, false /*compositeOnly*/, false /*forceSrcMode*/, |
| 654 | new GcSnapshot.Drawable() { |
Xavier Ducrohet | 46d43cc | 2012-02-02 15:44:50 -0800 | [diff] [blame] | 655 | @Override |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 656 | public void draw(Graphics2D graphics, Paint_Delegate paintDelegate) { |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 657 | graphics.drawLine((int)startX, (int)startY, (int)stopX, (int)stopY); |
| 658 | } |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 659 | }); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 662 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 663 | /*package*/ static void native_drawRect(long nativeCanvas, RectF rect, long paint) { |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 664 | native_drawRect(nativeCanvas, rect.left, rect.top, rect.right, rect.bottom, paint); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 667 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 668 | /*package*/ static void native_drawRect(long nativeCanvas, |
| 669 | final float left, final float top, final float right, final float bottom, long paint) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 670 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 671 | draw(nativeCanvas, paint, false /*compositeOnly*/, false /*forceSrcMode*/, |
| 672 | new GcSnapshot.Drawable() { |
Xavier Ducrohet | 46d43cc | 2012-02-02 15:44:50 -0800 | [diff] [blame] | 673 | @Override |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 674 | public void draw(Graphics2D graphics, Paint_Delegate paintDelegate) { |
| 675 | int style = paintDelegate.getStyle(); |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 676 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 677 | // draw |
| 678 | if (style == Paint.Style.FILL.nativeInt || |
| 679 | style == Paint.Style.FILL_AND_STROKE.nativeInt) { |
| 680 | graphics.fillRect((int)left, (int)top, |
| 681 | (int)(right-left), (int)(bottom-top)); |
| 682 | } |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 683 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 684 | if (style == Paint.Style.STROKE.nativeInt || |
| 685 | style == Paint.Style.FILL_AND_STROKE.nativeInt) { |
| 686 | graphics.drawRect((int)left, (int)top, |
| 687 | (int)(right-left), (int)(bottom-top)); |
| 688 | } |
| 689 | } |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 690 | }); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 693 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 694 | /*package*/ static void native_drawOval(long nativeCanvas, final RectF oval, long paint) { |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 695 | if (oval.right > oval.left && oval.bottom > oval.top) { |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 696 | draw(nativeCanvas, paint, false /*compositeOnly*/, false /*forceSrcMode*/, |
| 697 | new GcSnapshot.Drawable() { |
Xavier Ducrohet | 46d43cc | 2012-02-02 15:44:50 -0800 | [diff] [blame] | 698 | @Override |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 699 | public void draw(Graphics2D graphics, Paint_Delegate paintDelegate) { |
| 700 | int style = paintDelegate.getStyle(); |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 701 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 702 | // draw |
| 703 | if (style == Paint.Style.FILL.nativeInt || |
| 704 | style == Paint.Style.FILL_AND_STROKE.nativeInt) { |
| 705 | graphics.fillOval((int)oval.left, (int)oval.top, |
| 706 | (int)oval.width(), (int)oval.height()); |
| 707 | } |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 708 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 709 | if (style == Paint.Style.STROKE.nativeInt || |
| 710 | style == Paint.Style.FILL_AND_STROKE.nativeInt) { |
| 711 | graphics.drawOval((int)oval.left, (int)oval.top, |
| 712 | (int)oval.width(), (int)oval.height()); |
| 713 | } |
| 714 | } |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 715 | }); |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 716 | } |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 719 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 720 | /*package*/ static void native_drawCircle(long nativeCanvas, |
| 721 | float cx, float cy, float radius, long paint) { |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 722 | native_drawOval(nativeCanvas, |
Xavier Ducrohet | 5bf2802 | 2012-01-18 17:08:53 -0800 | [diff] [blame] | 723 | new RectF(cx - radius, cy - radius, cx + radius, cy + radius), |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 724 | paint); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 727 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 728 | /*package*/ static void native_drawArc(long nativeCanvas, |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 729 | final RectF oval, final float startAngle, final float sweep, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 730 | final boolean useCenter, long paint) { |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 731 | if (oval.right > oval.left && oval.bottom > oval.top) { |
| 732 | draw(nativeCanvas, paint, false /*compositeOnly*/, false /*forceSrcMode*/, |
| 733 | new GcSnapshot.Drawable() { |
Xavier Ducrohet | 46d43cc | 2012-02-02 15:44:50 -0800 | [diff] [blame] | 734 | @Override |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 735 | public void draw(Graphics2D graphics, Paint_Delegate paintDelegate) { |
| 736 | int style = paintDelegate.getStyle(); |
| 737 | |
| 738 | Arc2D.Float arc = new Arc2D.Float( |
| 739 | oval.left, oval.top, oval.width(), oval.height(), |
| 740 | -startAngle, -sweep, |
| 741 | useCenter ? Arc2D.PIE : Arc2D.OPEN); |
| 742 | |
| 743 | // draw |
| 744 | if (style == Paint.Style.FILL.nativeInt || |
| 745 | style == Paint.Style.FILL_AND_STROKE.nativeInt) { |
| 746 | graphics.fill(arc); |
| 747 | } |
| 748 | |
| 749 | if (style == Paint.Style.STROKE.nativeInt || |
| 750 | style == Paint.Style.FILL_AND_STROKE.nativeInt) { |
| 751 | graphics.draw(arc); |
| 752 | } |
| 753 | } |
| 754 | }); |
| 755 | } |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 758 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 759 | /*package*/ static void native_drawRoundRect(long nativeCanvas, |
| 760 | final RectF rect, final float rx, final float ry, long paint) { |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 761 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 762 | draw(nativeCanvas, paint, false /*compositeOnly*/, false /*forceSrcMode*/, |
| 763 | new GcSnapshot.Drawable() { |
Xavier Ducrohet | 46d43cc | 2012-02-02 15:44:50 -0800 | [diff] [blame] | 764 | @Override |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 765 | public void draw(Graphics2D graphics, Paint_Delegate paintDelegate) { |
| 766 | int style = paintDelegate.getStyle(); |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 767 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 768 | // draw |
| 769 | if (style == Paint.Style.FILL.nativeInt || |
| 770 | style == Paint.Style.FILL_AND_STROKE.nativeInt) { |
| 771 | graphics.fillRoundRect( |
| 772 | (int)rect.left, (int)rect.top, |
| 773 | (int)rect.width(), (int)rect.height(), |
| 774 | (int)rx, (int)ry); |
| 775 | } |
Xavier Ducrohet | 251d2e9 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 776 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 777 | if (style == Paint.Style.STROKE.nativeInt || |
| 778 | style == Paint.Style.FILL_AND_STROKE.nativeInt) { |
| 779 | graphics.drawRoundRect( |
| 780 | (int)rect.left, (int)rect.top, |
| 781 | (int)rect.width(), (int)rect.height(), |
| 782 | (int)rx, (int)ry); |
| 783 | } |
| 784 | } |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 785 | }); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 788 | @LayoutlibDelegate |
Narayan Kamath | 4058200 | 2014-01-29 15:05:03 +0000 | [diff] [blame] | 789 | /*package*/ static void native_drawPath(long nativeCanvas, long path, long paint) { |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 790 | final Path_Delegate pathDelegate = Path_Delegate.getDelegate(path); |
| 791 | if (pathDelegate == null) { |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 792 | return; |
| 793 | } |
| 794 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 795 | draw(nativeCanvas, paint, false /*compositeOnly*/, false /*forceSrcMode*/, |
| 796 | new GcSnapshot.Drawable() { |
Xavier Ducrohet | 46d43cc | 2012-02-02 15:44:50 -0800 | [diff] [blame] | 797 | @Override |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 798 | public void draw(Graphics2D graphics, Paint_Delegate paintDelegate) { |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 799 | Shape shape = pathDelegate.getJavaShape(); |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 800 | int style = paintDelegate.getStyle(); |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 801 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 802 | if (style == Paint.Style.FILL.nativeInt || |
| 803 | style == Paint.Style.FILL_AND_STROKE.nativeInt) { |
| 804 | graphics.fill(shape); |
| 805 | } |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 806 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 807 | if (style == Paint.Style.STROKE.nativeInt || |
| 808 | style == Paint.Style.FILL_AND_STROKE.nativeInt) { |
| 809 | graphics.draw(shape); |
| 810 | } |
| 811 | } |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 812 | }); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 815 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 816 | /*package*/ static void native_drawBitmap(Canvas thisCanvas, long nativeCanvas, long bitmap, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 817 | float left, float top, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 818 | long nativePaintOrZero, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 819 | int canvasDensity, |
| 820 | int screenDensity, |
| 821 | int bitmapDensity) { |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 822 | // get the delegate from the native int. |
| 823 | Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(bitmap); |
| 824 | if (bitmapDelegate == null) { |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 825 | return; |
| 826 | } |
| 827 | |
| 828 | BufferedImage image = bitmapDelegate.getImage(); |
| 829 | float right = left + image.getWidth(); |
| 830 | float bottom = top + image.getHeight(); |
| 831 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 832 | drawBitmap(nativeCanvas, bitmapDelegate, nativePaintOrZero, |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 833 | 0, 0, image.getWidth(), image.getHeight(), |
| 834 | (int)left, (int)top, (int)right, (int)bottom); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 837 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 838 | /*package*/ static void native_drawBitmap(Canvas thisCanvas, long nativeCanvas, long bitmap, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 839 | Rect src, RectF dst, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 840 | long nativePaintOrZero, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 841 | int screenDensity, |
| 842 | int bitmapDensity) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 843 | // get the delegate from the native int. |
| 844 | Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(bitmap); |
| 845 | if (bitmapDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 846 | return; |
| 847 | } |
| 848 | |
| 849 | BufferedImage image = bitmapDelegate.getImage(); |
| 850 | |
| 851 | if (src == null) { |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 852 | drawBitmap(nativeCanvas, bitmapDelegate, nativePaintOrZero, |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 853 | 0, 0, image.getWidth(), image.getHeight(), |
| 854 | (int)dst.left, (int)dst.top, (int)dst.right, (int)dst.bottom); |
| 855 | } else { |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 856 | drawBitmap(nativeCanvas, bitmapDelegate, nativePaintOrZero, |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 857 | src.left, src.top, src.width(), src.height(), |
| 858 | (int)dst.left, (int)dst.top, (int)dst.right, (int)dst.bottom); |
| 859 | } |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 862 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 863 | /*package*/ static void native_drawBitmap(long nativeCanvas, long bitmap, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 864 | Rect src, Rect dst, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 865 | long nativePaintOrZero, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 866 | int screenDensity, |
| 867 | int bitmapDensity) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 868 | // get the delegate from the native int. |
| 869 | Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(bitmap); |
| 870 | if (bitmapDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 871 | return; |
| 872 | } |
| 873 | |
| 874 | BufferedImage image = bitmapDelegate.getImage(); |
| 875 | |
| 876 | if (src == null) { |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 877 | drawBitmap(nativeCanvas, bitmapDelegate, nativePaintOrZero, |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 878 | 0, 0, image.getWidth(), image.getHeight(), |
| 879 | dst.left, dst.top, dst.right, dst.bottom); |
| 880 | } else { |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 881 | drawBitmap(nativeCanvas, bitmapDelegate, nativePaintOrZero, |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 882 | src.left, src.top, src.width(), src.height(), |
| 883 | dst.left, dst.top, dst.right, dst.bottom); |
| 884 | } |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 887 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 888 | /*package*/ static void native_drawBitmap(long nativeCanvas, int[] colors, |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 889 | int offset, int stride, final float x, |
| 890 | final float y, int width, int height, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 891 | boolean hasAlpha, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 892 | long nativePaintOrZero) { |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 893 | |
| 894 | // create a temp BufferedImage containing the content. |
| 895 | final BufferedImage image = new BufferedImage(width, height, |
| 896 | hasAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB); |
| 897 | image.setRGB(0, 0, width, height, colors, offset, stride); |
| 898 | |
| 899 | draw(nativeCanvas, nativePaintOrZero, true /*compositeOnly*/, false /*forceSrcMode*/, |
| 900 | new GcSnapshot.Drawable() { |
Xavier Ducrohet | 46d43cc | 2012-02-02 15:44:50 -0800 | [diff] [blame] | 901 | @Override |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 902 | public void draw(Graphics2D graphics, Paint_Delegate paint) { |
| 903 | if (paint != null && paint.isFilterBitmap()) { |
| 904 | graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, |
| 905 | RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
| 906 | } |
| 907 | |
| 908 | graphics.drawImage(image, (int) x, (int) y, null); |
| 909 | } |
| 910 | }); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 911 | } |
| 912 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 913 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 914 | /*package*/ static void nativeDrawBitmapMatrix(long nCanvas, long nBitmap, |
| 915 | long nMatrix, long nPaint) { |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 916 | // get the delegate from the native int. |
| 917 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nCanvas); |
| 918 | if (canvasDelegate == null) { |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 919 | return; |
| 920 | } |
| 921 | |
Xavier Ducrohet | d43909c | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 922 | // get the delegate from the native int, which can be null |
| 923 | Paint_Delegate paintDelegate = Paint_Delegate.getDelegate(nPaint); |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 924 | |
| 925 | // get the delegate from the native int. |
| 926 | Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(nBitmap); |
| 927 | if (bitmapDelegate == null) { |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 928 | return; |
| 929 | } |
| 930 | |
| 931 | final BufferedImage image = getImageToDraw(bitmapDelegate, paintDelegate, sBoolOut); |
| 932 | |
| 933 | Matrix_Delegate matrixDelegate = Matrix_Delegate.getDelegate(nMatrix); |
| 934 | if (matrixDelegate == null) { |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 935 | return; |
| 936 | } |
| 937 | |
| 938 | final AffineTransform mtx = matrixDelegate.getAffineTransform(); |
| 939 | |
| 940 | canvasDelegate.getSnapshot().draw(new GcSnapshot.Drawable() { |
Xavier Ducrohet | 46d43cc | 2012-02-02 15:44:50 -0800 | [diff] [blame] | 941 | @Override |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 942 | public void draw(Graphics2D graphics, Paint_Delegate paint) { |
| 943 | if (paint != null && paint.isFilterBitmap()) { |
| 944 | graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, |
| 945 | RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
| 946 | } |
| 947 | |
| 948 | //FIXME add support for canvas, screen and bitmap densities. |
| 949 | graphics.drawImage(image, mtx, null); |
| 950 | } |
| 951 | }, paintDelegate, true /*compositeOnly*/, false /*forceSrcMode*/); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 954 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 955 | /*package*/ static void nativeDrawBitmapMesh(long nCanvas, long nBitmap, |
Xavier Ducrohet | 8a80a85 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 956 | int meshWidth, int meshHeight, float[] verts, int vertOffset, int[] colors, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 957 | int colorOffset, long nPaint) { |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 958 | // FIXME |
Xavier Ducrohet | 8a80a85 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 959 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, |
| 960 | "Canvas.drawBitmapMesh is not supported.", null, null /*data*/); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 963 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 964 | /*package*/ static void nativeDrawVertices(long nCanvas, int mode, int n, |
Xavier Ducrohet | 8a80a85 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 965 | float[] verts, int vertOffset, |
| 966 | float[] texs, int texOffset, |
| 967 | int[] colors, int colorOffset, |
| 968 | short[] indices, int indexOffset, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 969 | int indexCount, long nPaint) { |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 970 | // FIXME |
Xavier Ducrohet | 8a80a85 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 971 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, |
| 972 | "Canvas.drawVertices is not supported.", null, null /*data*/); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 973 | } |
| 974 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 975 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 976 | /*package*/ static void native_drawText(long nativeCanvas, |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 977 | final char[] text, final int index, final int count, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 978 | final float startX, final float startY, final int flags, long paint) { |
Deepanshu Gupta | 0d9c922 | 2013-07-12 11:38:05 -0700 | [diff] [blame] | 979 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 980 | draw(nativeCanvas, paint, false /*compositeOnly*/, false /*forceSrcMode*/, |
| 981 | new GcSnapshot.Drawable() { |
Xavier Ducrohet | 46d43cc | 2012-02-02 15:44:50 -0800 | [diff] [blame] | 982 | @Override |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 983 | public void draw(Graphics2D graphics, Paint_Delegate paintDelegate) { |
Xavier Ducrohet | c6083f2 | 2011-02-23 09:53:56 -0800 | [diff] [blame] | 984 | // WARNING: the logic in this method is similar to Paint_Delegate.measureText. |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 985 | // Any change to this method should be reflected in Paint.measureText |
| 986 | // Paint.TextAlign indicates how the text is positioned relative to X. |
| 987 | // LEFT is the default and there's nothing to do. |
| 988 | float x = startX; |
Deepanshu Gupta | 0d9c922 | 2013-07-12 11:38:05 -0700 | [diff] [blame] | 989 | int limit = index + count; |
| 990 | boolean isRtl = flags == Canvas.DIRECTION_RTL; |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 991 | if (paintDelegate.getTextAlign() != Paint.Align.LEFT.nativeInt) { |
Deepanshu Gupta | 5ad7c18 | 2014-01-07 11:58:44 -0800 | [diff] [blame] | 992 | RectF bounds = paintDelegate.measureText(text, index, count, isRtl); |
| 993 | float m = bounds.right - bounds.left; |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 994 | if (paintDelegate.getTextAlign() == Paint.Align.CENTER.nativeInt) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 995 | x -= m / 2; |
Xavier Ducrohet | 9d76581 | 2011-03-14 15:12:21 -0700 | [diff] [blame] | 996 | } else if (paintDelegate.getTextAlign() == Paint.Align.RIGHT.nativeInt) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 997 | x -= m; |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 998 | } |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 999 | } |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1000 | |
Deepanshu Gupta | 0d9c922 | 2013-07-12 11:38:05 -0700 | [diff] [blame] | 1001 | new BidiRenderer(graphics, paintDelegate, text).renderText( |
| 1002 | index, limit, isRtl, null, 0, true, x, startY); |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1003 | } |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1004 | }); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1007 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1008 | /*package*/ static void native_drawText(long nativeCanvas, String text, |
| 1009 | int start, int end, float x, float y, final int flags, long paint) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1010 | int count = end - start; |
| 1011 | char[] buffer = TemporaryBuffer.obtain(count); |
| 1012 | TextUtils.getChars(text, start, end, buffer, 0); |
| 1013 | |
| 1014 | native_drawText(nativeCanvas, buffer, 0, count, x, y, flags, paint); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1017 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1018 | /*package*/ static void native_drawTextRun(long nativeCanvas, String text, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1019 | int start, int end, int contextStart, int contextEnd, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1020 | float x, float y, int flags, long paint) { |
Xavier Ducrohet | e0c763f | 2010-11-04 18:04:27 -0700 | [diff] [blame] | 1021 | int count = end - start; |
| 1022 | char[] buffer = TemporaryBuffer.obtain(count); |
| 1023 | TextUtils.getChars(text, start, end, buffer, 0); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1024 | |
Xavier Ducrohet | a4510a7 | 2011-02-24 18:56:34 -0800 | [diff] [blame] | 1025 | native_drawText(nativeCanvas, buffer, 0, count, x, y, flags, paint); |
Xavier Ducrohet | e0c763f | 2010-11-04 18:04:27 -0700 | [diff] [blame] | 1026 | } |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1027 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1028 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1029 | /*package*/ static void native_drawTextRun(long nativeCanvas, char[] text, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1030 | int start, int count, int contextStart, int contextCount, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1031 | float x, float y, int flags, long paint) { |
Xavier Ducrohet | bbefc86 | 2011-01-13 18:02:08 -0800 | [diff] [blame] | 1032 | native_drawText(nativeCanvas, text, start, count, x, y, flags, paint); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1035 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1036 | /*package*/ static void native_drawPosText(long nativeCanvas, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1037 | char[] text, int index, |
| 1038 | int count, float[] pos, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1039 | long paint) { |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1040 | // FIXME |
Xavier Ducrohet | 8a80a85 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 1041 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, |
| 1042 | "Canvas.drawPosText is not supported.", null, null /*data*/); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1045 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1046 | /*package*/ static void native_drawPosText(long nativeCanvas, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1047 | String text, float[] pos, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1048 | long paint) { |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1049 | // FIXME |
Xavier Ducrohet | 8a80a85 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 1050 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, |
| 1051 | "Canvas.drawPosText is not supported.", null, null /*data*/); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1054 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1055 | /*package*/ static void native_drawTextOnPath(long nativeCanvas, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1056 | char[] text, int index, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1057 | int count, long path, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1058 | float hOffset, |
| 1059 | float vOffset, int bidiFlags, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1060 | long paint) { |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1061 | // FIXME |
Xavier Ducrohet | 8a80a85 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 1062 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, |
| 1063 | "Canvas.drawTextOnPath is not supported.", null, null /*data*/); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1066 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1067 | /*package*/ static void native_drawTextOnPath(long nativeCanvas, |
| 1068 | String text, long path, |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1069 | float hOffset, |
| 1070 | float vOffset, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1071 | int flags, long paint) { |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1072 | // FIXME |
Xavier Ducrohet | 8a80a85 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 1073 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, |
| 1074 | "Canvas.drawTextOnPath is not supported.", null, null /*data*/); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
Xavier Ducrohet | 9a4fe29 | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1077 | @LayoutlibDelegate |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1078 | /*package*/ static void finalizer(long nativeCanvas) { |
Xavier Ducrohet | 9eb6d41 | 2010-12-01 12:28:43 -0800 | [diff] [blame] | 1079 | // get the delegate from the native int so that it can be disposed. |
| 1080 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas); |
| 1081 | if (canvasDelegate == null) { |
Xavier Ducrohet | 9eb6d41 | 2010-12-01 12:28:43 -0800 | [diff] [blame] | 1082 | return; |
| 1083 | } |
| 1084 | |
| 1085 | canvasDelegate.dispose(); |
| 1086 | |
| 1087 | // remove it from the manager. |
Xavier Ducrohet | cc4977d | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 1088 | sManager.removeJavaReferenceFor(nativeCanvas); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | // ---- Private delegate/helper methods ---- |
| 1092 | |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 1093 | /** |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 1094 | * Executes a {@link GcSnapshot.Drawable} with a given canvas and paint. |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1095 | * <p>Note that the drawable may actually be executed several times if there are |
| 1096 | * layers involved (see {@link #saveLayer(RectF, int, int)}. |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 1097 | */ |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1098 | private static void draw(long nCanvas, long nPaint, boolean compositeOnly, boolean forceSrcMode, |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1099 | GcSnapshot.Drawable drawable) { |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 1100 | // get the delegate from the native int. |
| 1101 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nCanvas); |
| 1102 | if (canvasDelegate == null) { |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 1103 | return; |
| 1104 | } |
| 1105 | |
Xavier Ducrohet | d43909c | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 1106 | // get the paint which can be null if nPaint is 0; |
| 1107 | Paint_Delegate paintDelegate = Paint_Delegate.getDelegate(nPaint); |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1108 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 1109 | canvasDelegate.getSnapshot().draw(drawable, paintDelegate, compositeOnly, forceSrcMode); |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /** |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 1113 | * Executes a {@link GcSnapshot.Drawable} with a given canvas. No paint object will be provided |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1114 | * to {@link GcSnapshot.Drawable#draw(Graphics2D, Paint_Delegate)}. |
| 1115 | * <p>Note that the drawable may actually be executed several times if there are |
| 1116 | * layers involved (see {@link #saveLayer(RectF, int, int)}. |
| 1117 | */ |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1118 | private static void draw(long nCanvas, GcSnapshot.Drawable drawable) { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1119 | // get the delegate from the native int. |
| 1120 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nCanvas); |
| 1121 | if (canvasDelegate == null) { |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 1122 | return; |
| 1123 | } |
| 1124 | |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1125 | canvasDelegate.mSnapshot.draw(drawable); |
Xavier Ducrohet | d348b6e | 2010-12-20 08:22:47 -0800 | [diff] [blame] | 1126 | } |
| 1127 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 1128 | private Canvas_Delegate(Bitmap_Delegate bitmap) { |
| 1129 | mSnapshot = GcSnapshot.createDefaultSnapshot(mBitmap = bitmap); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | private Canvas_Delegate() { |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1133 | mSnapshot = GcSnapshot.createDefaultSnapshot(null /*image*/); |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Xavier Ducrohet | 9eb6d41 | 2010-12-01 12:28:43 -0800 | [diff] [blame] | 1136 | /** |
| 1137 | * Disposes of the {@link Graphics2D} stack. |
| 1138 | */ |
| 1139 | private void dispose() { |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 1140 | mSnapshot.dispose(); |
Xavier Ducrohet | 9eb6d41 | 2010-12-01 12:28:43 -0800 | [diff] [blame] | 1141 | } |
| 1142 | |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 1143 | private int save(int saveFlags) { |
| 1144 | // get the current save count |
| 1145 | int count = mSnapshot.size(); |
| 1146 | |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1147 | mSnapshot = mSnapshot.save(saveFlags); |
| 1148 | |
| 1149 | // return the old save count |
| 1150 | return count; |
| 1151 | } |
| 1152 | |
| 1153 | private int saveLayerAlpha(RectF rect, int alpha, int saveFlags) { |
| 1154 | Paint_Delegate paint = new Paint_Delegate(); |
| 1155 | paint.setAlpha(alpha); |
| 1156 | return saveLayer(rect, paint, saveFlags); |
| 1157 | } |
| 1158 | |
| 1159 | private int saveLayer(RectF rect, Paint_Delegate paint, int saveFlags) { |
| 1160 | // get the current save count |
| 1161 | int count = mSnapshot.size(); |
| 1162 | |
| 1163 | mSnapshot = mSnapshot.saveLayer(rect, paint, saveFlags); |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 1164 | |
| 1165 | // return the old save count |
| 1166 | return count; |
| 1167 | } |
| 1168 | |
| 1169 | /** |
| 1170 | * Restores the {@link GcSnapshot} to <var>saveCount</var> |
| 1171 | * @param saveCount the saveCount |
| 1172 | */ |
| 1173 | private void restoreTo(int saveCount) { |
| 1174 | mSnapshot = mSnapshot.restoreTo(saveCount); |
| 1175 | } |
| 1176 | |
| 1177 | /** |
| 1178 | * Restores the {@link GcSnapshot} to <var>saveCount</var> |
| 1179 | * @param saveCount the saveCount |
| 1180 | */ |
| 1181 | private void restore() { |
| 1182 | mSnapshot = mSnapshot.restore(); |
| 1183 | } |
| 1184 | |
| 1185 | private boolean clipRect(float left, float top, float right, float bottom, int regionOp) { |
| 1186 | return mSnapshot.clipRect(left, top, right, bottom, regionOp); |
Xavier Ducrohet | 8da36314 | 2010-12-13 16:42:01 -0800 | [diff] [blame] | 1187 | } |
| 1188 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 1189 | private void setBitmap(Bitmap_Delegate bitmap) { |
| 1190 | mBitmap = bitmap; |
Xavier Ducrohet | cfdc784 | 2010-12-14 18:05:22 -0800 | [diff] [blame] | 1191 | assert mSnapshot.size() == 1; |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 1192 | mSnapshot.setBitmap(mBitmap); |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1195 | private static void drawBitmap( |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1196 | long nativeCanvas, |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 1197 | Bitmap_Delegate bitmap, |
Narayan Kamath | 8415143 | 2014-01-27 14:24:16 +0000 | [diff] [blame] | 1198 | long nativePaintOrZero, |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1199 | final int sleft, final int stop, final int sright, final int sbottom, |
| 1200 | final int dleft, final int dtop, final int dright, final int dbottom) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1201 | // get the delegate from the native int. |
| 1202 | Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas); |
| 1203 | if (canvasDelegate == null) { |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1204 | return; |
| 1205 | } |
| 1206 | |
Xavier Ducrohet | d43909c | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 1207 | // get the paint, which could be null if the int is 0 |
| 1208 | Paint_Delegate paintDelegate = Paint_Delegate.getDelegate(nativePaintOrZero); |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1209 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 1210 | final BufferedImage image = getImageToDraw(bitmap, paintDelegate, sBoolOut); |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1211 | |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 1212 | draw(nativeCanvas, nativePaintOrZero, true /*compositeOnly*/, sBoolOut[0], |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1213 | new GcSnapshot.Drawable() { |
Xavier Ducrohet | 46d43cc | 2012-02-02 15:44:50 -0800 | [diff] [blame] | 1214 | @Override |
Xavier Ducrohet | d38e776 | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1215 | public void draw(Graphics2D graphics, Paint_Delegate paint) { |
| 1216 | if (paint != null && paint.isFilterBitmap()) { |
| 1217 | graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, |
| 1218 | RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
| 1219 | } |
| 1220 | |
| 1221 | //FIXME add support for canvas, screen and bitmap densities. |
| 1222 | graphics.drawImage(image, dleft, dtop, dright, dbottom, |
| 1223 | sleft, stop, sright, sbottom, null); |
| 1224 | } |
| 1225 | }); |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1226 | } |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 1227 | |
| 1228 | |
| 1229 | /** |
| 1230 | * Returns a BufferedImage ready for drawing, based on the bitmap and paint delegate. |
| 1231 | * The image returns, through a 1-size boolean array, whether the drawing code should |
| 1232 | * use a SRC composite no matter what the paint says. |
| 1233 | * |
| 1234 | * @param bitmap the bitmap |
| 1235 | * @param paint the paint that will be used to draw |
| 1236 | * @param forceSrcMode whether the composite will have to be SRC |
| 1237 | * @return the image to draw |
| 1238 | */ |
| 1239 | private static BufferedImage getImageToDraw(Bitmap_Delegate bitmap, Paint_Delegate paint, |
| 1240 | boolean[] forceSrcMode) { |
| 1241 | BufferedImage image = bitmap.getImage(); |
| 1242 | forceSrcMode[0] = false; |
| 1243 | |
| 1244 | // if the bitmap config is alpha_8, then we erase all color value from it |
| 1245 | // before drawing it. |
| 1246 | if (bitmap.getConfig() == Bitmap.Config.ALPHA_8) { |
| 1247 | fixAlpha8Bitmap(image); |
| 1248 | } else if (bitmap.hasAlpha() == false) { |
| 1249 | // hasAlpha is merely a rendering hint. There can in fact be alpha values |
| 1250 | // in the bitmap but it should be ignored at drawing time. |
| 1251 | // There is two ways to do this: |
| 1252 | // - override the composite to be SRC. This can only be used if the composite |
| 1253 | // was going to be SRC or SRC_OVER in the first place |
| 1254 | // - Create a different bitmap to draw in which all the alpha channel values is set |
| 1255 | // to 0xFF. |
| 1256 | if (paint != null) { |
Xavier Ducrohet | d43909c | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 1257 | Xfermode_Delegate xfermodeDelegate = paint.getXfermode(); |
| 1258 | if (xfermodeDelegate instanceof PorterDuffXfermode_Delegate) { |
| 1259 | PorterDuff.Mode mode = |
| 1260 | ((PorterDuffXfermode_Delegate)xfermodeDelegate).getMode(); |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 1261 | |
Xavier Ducrohet | d43909c | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 1262 | forceSrcMode[0] = mode == PorterDuff.Mode.SRC_OVER || |
| 1263 | mode == PorterDuff.Mode.SRC; |
Xavier Ducrohet | b1da1af | 2010-12-22 10:13:23 -0800 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | // if we can't force SRC mode, then create a temp bitmap of TYPE_RGB |
| 1268 | if (forceSrcMode[0] == false) { |
| 1269 | image = Bitmap_Delegate.createCopy(image, BufferedImage.TYPE_INT_RGB, 0xFF); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | return image; |
| 1274 | } |
| 1275 | |
| 1276 | private static void fixAlpha8Bitmap(final BufferedImage image) { |
| 1277 | int w = image.getWidth(); |
| 1278 | int h = image.getHeight(); |
| 1279 | int[] argb = new int[w * h]; |
| 1280 | image.getRGB(0, 0, image.getWidth(), image.getHeight(), argb, 0, image.getWidth()); |
| 1281 | |
| 1282 | final int length = argb.length; |
| 1283 | for (int i = 0 ; i < length; i++) { |
| 1284 | argb[i] &= 0xFF000000; |
| 1285 | } |
| 1286 | image.setRGB(0, 0, w, h, argb, 0, w); |
| 1287 | } |
Xavier Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1288 | } |
Xavier Ducrohet | 5802dea | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1289 | |