Romain Guy | e4d0112 | 2010-06-16 18:44:05 -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.view; |
| 18 | |
| 19 | import android.graphics.Bitmap; |
| 20 | import android.graphics.Canvas; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 21 | import android.graphics.ColorFilter; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 22 | import android.graphics.DrawFilter; |
| 23 | import android.graphics.Matrix; |
| 24 | import android.graphics.Paint; |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 25 | import android.graphics.PaintFlagsDrawFilter; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 26 | import android.graphics.Path; |
| 27 | import android.graphics.Picture; |
| 28 | import android.graphics.PorterDuff; |
| 29 | import android.graphics.Rect; |
| 30 | import android.graphics.RectF; |
| 31 | import android.graphics.Region; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 32 | import android.graphics.Shader; |
Romain Guy | e5e0c50 | 2011-06-15 15:18:31 -0700 | [diff] [blame] | 33 | import android.graphics.SurfaceTexture; |
Romain Guy | a1db574 | 2010-07-20 13:09:13 -0700 | [diff] [blame] | 34 | import android.graphics.TemporaryBuffer; |
| 35 | import android.text.GraphicsOperations; |
| 36 | import android.text.SpannableString; |
| 37 | import android.text.SpannedString; |
| 38 | import android.text.TextUtils; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 39 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 40 | /** |
| 41 | * An implementation of Canvas on top of OpenGL ES 2.0. |
| 42 | */ |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 43 | class GLES20Canvas extends HardwareCanvas { |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 44 | // Must match modifiers used in the JNI layer |
| 45 | private static final int MODIFIER_NONE = 0; |
| 46 | private static final int MODIFIER_SHADOW = 1; |
| 47 | private static final int MODIFIER_SHADER = 2; |
| 48 | private static final int MODIFIER_COLOR_FILTER = 4; |
| 49 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 50 | private final boolean mOpaque; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 51 | private int mRenderer; |
Patrick Dubroy | f890fab | 2010-12-19 16:47:17 -0800 | [diff] [blame] | 52 | |
| 53 | // The native renderer will be destroyed when this object dies. |
| 54 | // DO NOT overwrite this reference once it is set. |
Romain Guy | eea6069 | 2011-07-26 20:35:55 -0700 | [diff] [blame] | 55 | @SuppressWarnings({"unused", "FieldCanBeLocal"}) |
Patrick Dubroy | f890fab | 2010-12-19 16:47:17 -0800 | [diff] [blame] | 56 | private CanvasFinalizer mFinalizer; |
| 57 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 58 | private int mWidth; |
| 59 | private int mHeight; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 60 | |
| 61 | private final float[] mPoint = new float[2]; |
| 62 | private final float[] mLine = new float[4]; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 63 | |
| 64 | private final Rect mClipBounds = new Rect(); |
Romain Guy | 7677d8f | 2012-01-17 11:22:42 -0800 | [diff] [blame] | 65 | private final RectF mPathBounds = new RectF(); |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 66 | |
| 67 | private DrawFilter mFilter; |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 68 | |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 69 | /////////////////////////////////////////////////////////////////////////// |
| 70 | // JNI |
| 71 | /////////////////////////////////////////////////////////////////////////// |
| 72 | |
| 73 | private static native boolean nIsAvailable(); |
| 74 | private static boolean sIsAvailable = nIsAvailable(); |
| 75 | |
| 76 | static boolean isAvailable() { |
| 77 | return sIsAvailable; |
| 78 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 79 | |
| 80 | /////////////////////////////////////////////////////////////////////////// |
| 81 | // Constructors |
| 82 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 83 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 84 | /** |
| 85 | * Creates a canvas to render directly on screen. |
| 86 | */ |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 87 | GLES20Canvas(boolean translucent) { |
| 88 | this(false, translucent); |
| 89 | } |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Creates a canvas to render into an FBO. |
| 93 | */ |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 94 | GLES20Canvas(int layer, boolean translucent) { |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 95 | mOpaque = !translucent; |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 96 | mRenderer = nCreateLayerRenderer(layer); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 97 | setupFinalizer(); |
| 98 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 99 | |
Patrick Dubroy | f890fab | 2010-12-19 16:47:17 -0800 | [diff] [blame] | 100 | protected GLES20Canvas(boolean record, boolean translucent) { |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 101 | mOpaque = !translucent; |
| 102 | |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 103 | if (record) { |
Jeff Brown | 162a021 | 2011-07-21 17:02:54 -0700 | [diff] [blame] | 104 | mRenderer = nCreateDisplayListRenderer(); |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 105 | } else { |
| 106 | mRenderer = nCreateRenderer(); |
| 107 | } |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 108 | |
| 109 | setupFinalizer(); |
| 110 | } |
| 111 | |
| 112 | private void setupFinalizer() { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 113 | if (mRenderer == 0) { |
| 114 | throw new IllegalStateException("Could not create GLES20Canvas renderer"); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 115 | } else { |
Jeff Brown | 162a021 | 2011-07-21 17:02:54 -0700 | [diff] [blame] | 116 | mFinalizer = new CanvasFinalizer(mRenderer); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 117 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 118 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 119 | |
Jeff Brown | 162a021 | 2011-07-21 17:02:54 -0700 | [diff] [blame] | 120 | protected void resetDisplayListRenderer() { |
| 121 | nResetDisplayListRenderer(mRenderer); |
| 122 | } |
| 123 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 124 | private static native int nCreateRenderer(); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 125 | private static native int nCreateLayerRenderer(int layer); |
Jeff Brown | 162a021 | 2011-07-21 17:02:54 -0700 | [diff] [blame] | 126 | private static native int nCreateDisplayListRenderer(); |
| 127 | private static native void nResetDisplayListRenderer(int renderer); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 128 | private static native void nDestroyRenderer(int renderer); |
| 129 | |
Jeff Brown | 162a021 | 2011-07-21 17:02:54 -0700 | [diff] [blame] | 130 | private static final class CanvasFinalizer { |
| 131 | private final int mRenderer; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 132 | |
Jeff Brown | 162a021 | 2011-07-21 17:02:54 -0700 | [diff] [blame] | 133 | public CanvasFinalizer(int renderer) { |
Patrick Dubroy | f890fab | 2010-12-19 16:47:17 -0800 | [diff] [blame] | 134 | mRenderer = renderer; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
Patrick Dubroy | f890fab | 2010-12-19 16:47:17 -0800 | [diff] [blame] | 138 | protected void finalize() throws Throwable { |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 139 | try { |
Jeff Brown | 162a021 | 2011-07-21 17:02:54 -0700 | [diff] [blame] | 140 | nDestroyRenderer(mRenderer); |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 141 | } finally { |
| 142 | super.finalize(); |
| 143 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 146 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 147 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 148 | // Hardware layers |
| 149 | /////////////////////////////////////////////////////////////////////////// |
| 150 | |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 151 | static native int nCreateTextureLayer(boolean opaque, int[] layerInfo); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 152 | static native int nCreateLayer(int width, int height, boolean isOpaque, int[] layerInfo); |
| 153 | static native void nResizeLayer(int layerId, int width, int height, int[] layerInfo); |
Romain Guy | 846a533 | 2012-07-11 17:44:57 -0700 | [diff] [blame] | 154 | static native void nSetOpaqueLayer(int layerId, boolean isOpaque); |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 155 | static native void nUpdateTextureLayer(int layerId, int width, int height, boolean opaque, |
| 156 | SurfaceTexture surface); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 157 | static native void nSetTextureLayerTransform(int layerId, int matrix); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 158 | static native void nDestroyLayer(int layerId); |
| 159 | static native void nDestroyLayerDeferred(int layerId); |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 160 | static native void nUpdateRenderLayer(int layerId, int renderer, int displayList, |
| 161 | int left, int top, int right, int bottom); |
Romain Guy | 77a8116 | 2011-06-14 16:45:55 -0700 | [diff] [blame] | 162 | static native boolean nCopyLayer(int layerId, int bitmap); |
Romain Guy | 57066eb | 2011-01-12 12:53:32 -0800 | [diff] [blame] | 163 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 164 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 165 | // Canvas management |
| 166 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 167 | |
| 168 | @Override |
| 169 | public boolean isOpaque() { |
| 170 | return mOpaque; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public int getWidth() { |
| 175 | return mWidth; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public int getHeight() { |
| 180 | return mHeight; |
| 181 | } |
| 182 | |
Romain Guy | f61970fc | 2011-07-07 14:10:06 -0700 | [diff] [blame] | 183 | @Override |
| 184 | public int getMaximumBitmapWidth() { |
| 185 | return nGetMaximumTextureWidth(); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public int getMaximumBitmapHeight() { |
| 190 | return nGetMaximumTextureHeight(); |
| 191 | } |
| 192 | |
| 193 | private static native int nGetMaximumTextureWidth(); |
Romain Guy | 530041d | 2012-01-25 18:56:29 -0800 | [diff] [blame] | 194 | private static native int nGetMaximumTextureHeight(); |
Romain Guy | f61970fc | 2011-07-07 14:10:06 -0700 | [diff] [blame] | 195 | |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 196 | /** |
| 197 | * Returns the native OpenGLRenderer object. |
| 198 | */ |
| 199 | int getRenderer() { |
| 200 | return mRenderer; |
| 201 | } |
| 202 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 203 | /////////////////////////////////////////////////////////////////////////// |
| 204 | // Setup |
| 205 | /////////////////////////////////////////////////////////////////////////// |
| 206 | |
| 207 | @Override |
| 208 | public void setViewport(int width, int height) { |
| 209 | mWidth = width; |
| 210 | mHeight = height; |
| 211 | |
| 212 | nSetViewport(mRenderer, width, height); |
| 213 | } |
| 214 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 215 | private static native void nSetViewport(int renderer, int width, int height); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 216 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 217 | @Override |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 218 | public int onPreDraw(Rect dirty) { |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 219 | if (dirty != null) { |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 220 | return nPrepareDirty(mRenderer, dirty.left, dirty.top, dirty.right, dirty.bottom, |
| 221 | mOpaque); |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 222 | } else { |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 223 | return nPrepare(mRenderer, mOpaque); |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 227 | private static native int nPrepare(int renderer, boolean opaque); |
| 228 | private static native int nPrepareDirty(int renderer, int left, int top, int right, int bottom, |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 229 | boolean opaque); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 230 | |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 231 | @Override |
Gilles Debunne | b35ab7b | 2011-12-05 15:54:00 -0800 | [diff] [blame] | 232 | public void onPostDraw() { |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 233 | nFinish(mRenderer); |
| 234 | } |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 235 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 236 | private static native void nFinish(int renderer); |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 237 | |
Romain Guy | 530041d | 2012-01-25 18:56:29 -0800 | [diff] [blame] | 238 | /** |
| 239 | * Returns the size of the stencil buffer required by the underlying |
| 240 | * implementation. |
| 241 | * |
| 242 | * @return The minimum number of bits the stencil buffer must. Always >= 0. |
| 243 | * |
| 244 | * @hide |
| 245 | */ |
| 246 | public static int getStencilSize() { |
| 247 | return nGetStencilSize(); |
| 248 | } |
| 249 | |
| 250 | private static native int nGetStencilSize(); |
| 251 | |
| 252 | /////////////////////////////////////////////////////////////////////////// |
| 253 | // Functor |
| 254 | /////////////////////////////////////////////////////////////////////////// |
| 255 | |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 256 | @Override |
Romain Guy | 6554943 | 2012-03-26 16:45:05 -0700 | [diff] [blame] | 257 | public int callDrawGLFunction(int drawGLFunction) { |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 258 | return nCallDrawGLFunction(mRenderer, drawGLFunction); |
| 259 | } |
| 260 | |
Romain Guy | 6554943 | 2012-03-26 16:45:05 -0700 | [diff] [blame] | 261 | private static native int nCallDrawGLFunction(int renderer, int drawGLFunction); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 262 | |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 263 | @Override |
| 264 | public int invokeFunctors(Rect dirty) { |
| 265 | return nInvokeFunctors(mRenderer, dirty); |
| 266 | } |
| 267 | |
| 268 | private static native int nInvokeFunctors(int renderer, Rect dirty); |
| 269 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 270 | @Override |
| 271 | public void detachFunctor(int functor) { |
| 272 | nDetachFunctor(mRenderer, functor); |
| 273 | } |
| 274 | |
| 275 | private static native void nDetachFunctor(int renderer, int functor); |
| 276 | |
| 277 | @Override |
| 278 | public void attachFunctor(int functor) { |
| 279 | nAttachFunctor(mRenderer, functor); |
| 280 | } |
| 281 | |
| 282 | private static native void nAttachFunctor(int renderer, int functor); |
| 283 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 284 | /////////////////////////////////////////////////////////////////////////// |
| 285 | // Memory |
| 286 | /////////////////////////////////////////////////////////////////////////// |
| 287 | |
| 288 | /** |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 289 | * Must match Caches::FlushMode values |
| 290 | * |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 291 | * @see #flushCaches(int) |
| 292 | */ |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 293 | public static final int FLUSH_CACHES_LAYERS = 0; |
| 294 | |
| 295 | /** |
| 296 | * Must match Caches::FlushMode values |
| 297 | * |
| 298 | * @see #flushCaches(int) |
| 299 | */ |
| 300 | public static final int FLUSH_CACHES_MODERATE = 1; |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 301 | |
| 302 | /** |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 303 | * Must match Caches::FlushMode values |
| 304 | * |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 305 | * @see #flushCaches(int) |
| 306 | */ |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 307 | public static final int FLUSH_CACHES_FULL = 2; |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 308 | |
| 309 | /** |
| 310 | * Flush caches to reclaim as much memory as possible. The amount of memory |
| 311 | * to reclaim is indicate by the level parameter. |
| 312 | * |
| 313 | * The level can be one of {@link #FLUSH_CACHES_MODERATE} or |
| 314 | * {@link #FLUSH_CACHES_FULL}. |
| 315 | * |
| 316 | * @param level Hint about the amount of memory to reclaim |
| 317 | * |
| 318 | * @hide |
| 319 | */ |
| 320 | public static void flushCaches(int level) { |
| 321 | nFlushCaches(level); |
| 322 | } |
| 323 | |
| 324 | private static native void nFlushCaches(int level); |
| 325 | |
Romain Guy | 8ff6b9e | 2011-11-09 20:10:18 -0800 | [diff] [blame] | 326 | /** |
| 327 | * Release all resources associated with the underlying caches. This should |
| 328 | * only be called after a full flushCaches(). |
| 329 | * |
| 330 | * @hide |
| 331 | */ |
| 332 | public static void terminateCaches() { |
| 333 | nTerminateCaches(); |
| 334 | } |
| 335 | |
| 336 | private static native void nTerminateCaches(); |
| 337 | |
| 338 | /** |
| 339 | * @hide |
| 340 | */ |
| 341 | public static void initCaches() { |
| 342 | nInitCaches(); |
| 343 | } |
| 344 | |
| 345 | private static native void nInitCaches(); |
| 346 | |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 347 | /////////////////////////////////////////////////////////////////////////// |
| 348 | // Display list |
| 349 | /////////////////////////////////////////////////////////////////////////// |
| 350 | |
Jeff Brown | 162a021 | 2011-07-21 17:02:54 -0700 | [diff] [blame] | 351 | int getDisplayList(int displayList) { |
| 352 | return nGetDisplayList(mRenderer, displayList); |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Jeff Brown | 162a021 | 2011-07-21 17:02:54 -0700 | [diff] [blame] | 355 | private static native int nGetDisplayList(int renderer, int displayList); |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 356 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 357 | static void destroyDisplayList(int displayList) { |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 358 | nDestroyDisplayList(displayList); |
| 359 | } |
| 360 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 361 | private static native void nDestroyDisplayList(int displayList); |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 362 | |
Romain Guy | 65b345f | 2011-07-27 18:51:50 -0700 | [diff] [blame] | 363 | static int getDisplayListSize(int displayList) { |
| 364 | return nGetDisplayListSize(displayList); |
| 365 | } |
| 366 | |
| 367 | private static native int nGetDisplayListSize(int displayList); |
| 368 | |
Romain Guy | 13631f3 | 2012-01-30 17:41:55 -0800 | [diff] [blame] | 369 | static void setDisplayListName(int displayList, String name) { |
| 370 | nSetDisplayListName(displayList, name); |
| 371 | } |
| 372 | |
| 373 | private static native void nSetDisplayListName(int displayList, String name); |
| 374 | |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 375 | @Override |
Chet Haase | 1271e2c | 2012-04-20 09:54:27 -0700 | [diff] [blame] | 376 | public int drawDisplayList(DisplayList displayList, Rect dirty, int flags) { |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 377 | return nDrawDisplayList(mRenderer, ((GLES20DisplayList) displayList).getNativeDisplayList(), |
Chet Haase | 1271e2c | 2012-04-20 09:54:27 -0700 | [diff] [blame] | 378 | dirty, flags); |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Romain Guy | 6554943 | 2012-03-26 16:45:05 -0700 | [diff] [blame] | 381 | private static native int nDrawDisplayList(int renderer, int displayList, |
Chet Haase | 1271e2c | 2012-04-20 09:54:27 -0700 | [diff] [blame] | 382 | Rect dirty, int flags); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 383 | |
Chet Haase | ed30fd8 | 2011-04-22 16:18:45 -0700 | [diff] [blame] | 384 | @Override |
| 385 | void outputDisplayList(DisplayList displayList) { |
Jeff Brown | 162a021 | 2011-07-21 17:02:54 -0700 | [diff] [blame] | 386 | nOutputDisplayList(mRenderer, ((GLES20DisplayList) displayList).getNativeDisplayList()); |
Chet Haase | ed30fd8 | 2011-04-22 16:18:45 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | private static native void nOutputDisplayList(int renderer, int displayList); |
| 390 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 391 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 392 | // Hardware layer |
| 393 | /////////////////////////////////////////////////////////////////////////// |
| 394 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 395 | void drawHardwareLayer(HardwareLayer layer, float x, float y, Paint paint) { |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 396 | final GLES20Layer glLayer = (GLES20Layer) layer; |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 397 | int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE; |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 398 | try { |
| 399 | final int nativePaint = paint == null ? 0 : paint.mNativePaint; |
| 400 | nDrawLayer(mRenderer, glLayer.getLayer(), x, y, nativePaint); |
| 401 | } finally { |
| 402 | if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier); |
| 403 | } |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 406 | private static native void nDrawLayer(int renderer, int layer, float x, float y, int paint); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 407 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 408 | void interrupt() { |
| 409 | nInterrupt(mRenderer); |
| 410 | } |
| 411 | |
| 412 | void resume() { |
| 413 | nResume(mRenderer); |
| 414 | } |
| 415 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 416 | private static native void nInterrupt(int renderer); |
| 417 | private static native void nResume(int renderer); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 418 | |
| 419 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 420 | // Clipping |
| 421 | /////////////////////////////////////////////////////////////////////////// |
| 422 | |
| 423 | @Override |
| 424 | public boolean clipPath(Path path) { |
Romain Guy | 7677d8f | 2012-01-17 11:22:42 -0800 | [diff] [blame] | 425 | // TODO: Implement |
| 426 | path.computeBounds(mPathBounds, true); |
| 427 | return nClipRect(mRenderer, mPathBounds.left, mPathBounds.top, |
| 428 | mPathBounds.right, mPathBounds.bottom, Region.Op.INTERSECT.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public boolean clipPath(Path path, Region.Op op) { |
Romain Guy | 7677d8f | 2012-01-17 11:22:42 -0800 | [diff] [blame] | 433 | // TODO: Implement |
| 434 | path.computeBounds(mPathBounds, true); |
| 435 | return nClipRect(mRenderer, mPathBounds.left, mPathBounds.top, |
| 436 | mPathBounds.right, mPathBounds.bottom, op.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public boolean clipRect(float left, float top, float right, float bottom) { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 441 | return nClipRect(mRenderer, left, top, right, bottom, Region.Op.INTERSECT.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 442 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 443 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 444 | private static native boolean nClipRect(int renderer, float left, float top, |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 445 | float right, float bottom, int op); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 446 | |
| 447 | @Override |
| 448 | public boolean clipRect(float left, float top, float right, float bottom, Region.Op op) { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 449 | return nClipRect(mRenderer, left, top, right, bottom, op.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | @Override |
| 453 | public boolean clipRect(int left, int top, int right, int bottom) { |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 454 | return nClipRect(mRenderer, left, top, right, bottom, Region.Op.INTERSECT.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 455 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 456 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 457 | private static native boolean nClipRect(int renderer, int left, int top, int right, int bottom, |
| 458 | int op); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 459 | |
| 460 | @Override |
| 461 | public boolean clipRect(Rect rect) { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 462 | return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, |
| 463 | Region.Op.INTERSECT.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | @Override |
| 467 | public boolean clipRect(Rect rect, Region.Op op) { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 468 | return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, op.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public boolean clipRect(RectF rect) { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 473 | return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, |
| 474 | Region.Op.INTERSECT.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | @Override |
| 478 | public boolean clipRect(RectF rect, Region.Op op) { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 479 | return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, op.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | @Override |
| 483 | public boolean clipRegion(Region region) { |
Romain Guy | 7677d8f | 2012-01-17 11:22:42 -0800 | [diff] [blame] | 484 | // TODO: Implement |
| 485 | region.getBounds(mClipBounds); |
| 486 | return nClipRect(mRenderer, mClipBounds.left, mClipBounds.top, |
| 487 | mClipBounds.right, mClipBounds.bottom, Region.Op.INTERSECT.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | @Override |
| 491 | public boolean clipRegion(Region region, Region.Op op) { |
Romain Guy | 7677d8f | 2012-01-17 11:22:42 -0800 | [diff] [blame] | 492 | // TODO: Implement |
| 493 | region.getBounds(mClipBounds); |
| 494 | return nClipRect(mRenderer, mClipBounds.left, mClipBounds.top, |
| 495 | mClipBounds.right, mClipBounds.bottom, op.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public boolean getClipBounds(Rect bounds) { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 500 | return nGetClipBounds(mRenderer, bounds); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 503 | private static native boolean nGetClipBounds(int renderer, Rect bounds); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 504 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 505 | @Override |
| 506 | public boolean quickReject(float left, float top, float right, float bottom, EdgeType type) { |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 507 | return nQuickReject(mRenderer, left, top, right, bottom, type.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 508 | } |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 509 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 510 | private static native boolean nQuickReject(int renderer, float left, float top, |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 511 | float right, float bottom, int edge); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 512 | |
| 513 | @Override |
| 514 | public boolean quickReject(Path path, EdgeType type) { |
Romain Guy | 7677d8f | 2012-01-17 11:22:42 -0800 | [diff] [blame] | 515 | path.computeBounds(mPathBounds, true); |
| 516 | return nQuickReject(mRenderer, mPathBounds.left, mPathBounds.top, |
| 517 | mPathBounds.right, mPathBounds.bottom, type.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public boolean quickReject(RectF rect, EdgeType type) { |
Romain Guy | 7677d8f | 2012-01-17 11:22:42 -0800 | [diff] [blame] | 522 | return nQuickReject(mRenderer, rect.left, rect.top, rect.right, rect.bottom, type.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /////////////////////////////////////////////////////////////////////////// |
| 526 | // Transformations |
| 527 | /////////////////////////////////////////////////////////////////////////// |
| 528 | |
| 529 | @Override |
| 530 | public void translate(float dx, float dy) { |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 531 | if (dx != 0.0f || dy != 0.0f) nTranslate(mRenderer, dx, dy); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 532 | } |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 533 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 534 | private static native void nTranslate(int renderer, float dx, float dy); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 535 | |
| 536 | @Override |
| 537 | public void skew(float sx, float sy) { |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 538 | nSkew(mRenderer, sx, sy); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 541 | private static native void nSkew(int renderer, float sx, float sy); |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 542 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 543 | @Override |
| 544 | public void rotate(float degrees) { |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 545 | nRotate(mRenderer, degrees); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 546 | } |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 547 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 548 | private static native void nRotate(int renderer, float degrees); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 549 | |
| 550 | @Override |
| 551 | public void scale(float sx, float sy) { |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 552 | nScale(mRenderer, sx, sy); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 553 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 554 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 555 | private static native void nScale(int renderer, float sx, float sy); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 556 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 557 | @Override |
| 558 | public void setMatrix(Matrix matrix) { |
Romain Guy | e707859 | 2011-10-28 14:32:20 -0700 | [diff] [blame] | 559 | nSetMatrix(mRenderer, matrix == null ? 0 : matrix.native_instance); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 560 | } |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 561 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 562 | private static native void nSetMatrix(int renderer, int matrix); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 563 | |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 564 | @SuppressWarnings("deprecation") |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 565 | @Override |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 566 | public void getMatrix(Matrix matrix) { |
| 567 | nGetMatrix(mRenderer, matrix.native_instance); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 568 | } |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 569 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 570 | private static native void nGetMatrix(int renderer, int matrix); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 571 | |
| 572 | @Override |
| 573 | public void concat(Matrix matrix) { |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 574 | nConcatMatrix(mRenderer, matrix.native_instance); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 577 | private static native void nConcatMatrix(int renderer, int matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 578 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 579 | /////////////////////////////////////////////////////////////////////////// |
| 580 | // State management |
| 581 | /////////////////////////////////////////////////////////////////////////// |
| 582 | |
| 583 | @Override |
| 584 | public int save() { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 585 | return nSave(mRenderer, Canvas.CLIP_SAVE_FLAG | Canvas.MATRIX_SAVE_FLAG); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 586 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 587 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 588 | @Override |
| 589 | public int save(int saveFlags) { |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 590 | return nSave(mRenderer, saveFlags); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 593 | private static native int nSave(int renderer, int flags); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 594 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 595 | @Override |
| 596 | public int saveLayer(RectF bounds, Paint paint, int saveFlags) { |
Romain Guy | 189887e | 2011-08-25 11:45:13 -0700 | [diff] [blame] | 597 | if (bounds != null) { |
| 598 | return saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, paint, saveFlags); |
| 599 | } |
| 600 | |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 601 | int count; |
Romain Guy | 189887e | 2011-08-25 11:45:13 -0700 | [diff] [blame] | 602 | int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE; |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 603 | try { |
| 604 | final int nativePaint = paint == null ? 0 : paint.mNativePaint; |
| 605 | count = nSaveLayer(mRenderer, nativePaint, saveFlags); |
| 606 | } finally { |
| 607 | if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier); |
| 608 | } |
Romain Guy | 189887e | 2011-08-25 11:45:13 -0700 | [diff] [blame] | 609 | return count; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Romain Guy | 189887e | 2011-08-25 11:45:13 -0700 | [diff] [blame] | 612 | private static native int nSaveLayer(int renderer, int paint, int saveFlags); |
| 613 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 614 | @Override |
| 615 | public int saveLayer(float left, float top, float right, float bottom, Paint paint, |
| 616 | int saveFlags) { |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 617 | if (left < right && top < bottom) { |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 618 | int count; |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 619 | int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE; |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 620 | try { |
| 621 | final int nativePaint = paint == null ? 0 : paint.mNativePaint; |
| 622 | count = nSaveLayer(mRenderer, left, top, right, bottom, nativePaint, saveFlags); |
| 623 | } finally { |
| 624 | if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier); |
| 625 | } |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 626 | return count; |
| 627 | } |
| 628 | return save(saveFlags); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 631 | private static native int nSaveLayer(int renderer, float left, float top, |
| 632 | float right, float bottom, int paint, int saveFlags); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 633 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 634 | @Override |
| 635 | public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags) { |
Romain Guy | 189887e | 2011-08-25 11:45:13 -0700 | [diff] [blame] | 636 | if (bounds != null) { |
| 637 | return saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom, |
| 638 | alpha, saveFlags); |
| 639 | } |
| 640 | return nSaveLayerAlpha(mRenderer, alpha, saveFlags); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Romain Guy | 189887e | 2011-08-25 11:45:13 -0700 | [diff] [blame] | 643 | private static native int nSaveLayerAlpha(int renderer, int alpha, int saveFlags); |
| 644 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 645 | @Override |
| 646 | public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, |
| 647 | int saveFlags) { |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 648 | if (left < right && top < bottom) { |
| 649 | return nSaveLayerAlpha(mRenderer, left, top, right, bottom, alpha, saveFlags); |
| 650 | } |
| 651 | return save(saveFlags); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 654 | private static native int nSaveLayerAlpha(int renderer, float left, float top, float right, |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 655 | float bottom, int alpha, int saveFlags); |
| 656 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 657 | @Override |
| 658 | public void restore() { |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 659 | nRestore(mRenderer); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 660 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 661 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 662 | private static native void nRestore(int renderer); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 663 | |
| 664 | @Override |
| 665 | public void restoreToCount(int saveCount) { |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 666 | nRestoreToCount(mRenderer, saveCount); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 669 | private static native void nRestoreToCount(int renderer, int saveCount); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 670 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 671 | @Override |
| 672 | public int getSaveCount() { |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 673 | return nGetSaveCount(mRenderer); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 674 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 675 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 676 | private static native int nGetSaveCount(int renderer); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 677 | |
| 678 | /////////////////////////////////////////////////////////////////////////// |
| 679 | // Filtering |
| 680 | /////////////////////////////////////////////////////////////////////////// |
| 681 | |
| 682 | @Override |
| 683 | public void setDrawFilter(DrawFilter filter) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 684 | mFilter = filter; |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 685 | if (filter == null) { |
| 686 | nResetPaintFilter(mRenderer); |
| 687 | } else if (filter instanceof PaintFlagsDrawFilter) { |
| 688 | PaintFlagsDrawFilter flagsFilter = (PaintFlagsDrawFilter) filter; |
| 689 | nSetupPaintFilter(mRenderer, flagsFilter.clearBits, flagsFilter.setBits); |
| 690 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 693 | private static native void nResetPaintFilter(int renderer); |
| 694 | private static native void nSetupPaintFilter(int renderer, int clearBits, int setBits); |
| 695 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 696 | @Override |
| 697 | public DrawFilter getDrawFilter() { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 698 | return mFilter; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | /////////////////////////////////////////////////////////////////////////// |
| 702 | // Drawing |
| 703 | /////////////////////////////////////////////////////////////////////////// |
| 704 | |
| 705 | @Override |
| 706 | public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, |
| 707 | Paint paint) { |
Romain Guy | 765dcf3 | 2012-02-27 13:28:22 -0800 | [diff] [blame] | 708 | int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER); |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 709 | try { |
| 710 | nDrawArc(mRenderer, oval.left, oval.top, oval.right, oval.bottom, |
| 711 | startAngle, sweepAngle, useCenter, paint.mNativePaint); |
| 712 | } finally { |
| 713 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
| 714 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 717 | private static native void nDrawArc(int renderer, float left, float top, |
| 718 | float right, float bottom, float startAngle, float sweepAngle, |
| 719 | boolean useCenter, int paint); |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 720 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 721 | @Override |
| 722 | public void drawARGB(int a, int r, int g, int b) { |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 723 | drawColor((a & 0xFF) << 24 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF)); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | @Override |
Romain Guy | deba785 | 2010-07-07 17:54:48 -0700 | [diff] [blame] | 727 | public void drawPatch(Bitmap bitmap, byte[] chunks, RectF dst, Paint paint) { |
Romain Guy | bc52cce | 2012-02-07 19:40:19 -0800 | [diff] [blame] | 728 | if (bitmap.isRecycled()) throw new IllegalArgumentException("Cannot draw recycled bitmaps"); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 729 | // Shaders are ignored when drawing patches |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 730 | int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE; |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 731 | try { |
| 732 | final int nativePaint = paint == null ? 0 : paint.mNativePaint; |
| 733 | nDrawPatch(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, chunks, |
| 734 | dst.left, dst.top, dst.right, dst.bottom, nativePaint); |
| 735 | } finally { |
| 736 | if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier); |
| 737 | } |
Romain Guy | deba785 | 2010-07-07 17:54:48 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 740 | private static native void nDrawPatch(int renderer, int bitmap, byte[] buffer, byte[] chunks, |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 741 | float left, float top, float right, float bottom, int paint); |
Romain Guy | deba785 | 2010-07-07 17:54:48 -0700 | [diff] [blame] | 742 | |
| 743 | @Override |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 744 | public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) { |
Romain Guy | bc52cce | 2012-02-07 19:40:19 -0800 | [diff] [blame] | 745 | if (bitmap.isRecycled()) throw new IllegalArgumentException("Cannot draw recycled bitmaps"); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 746 | // Shaders are ignored when drawing bitmaps |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 747 | int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE; |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 748 | try { |
| 749 | final int nativePaint = paint == null ? 0 : paint.mNativePaint; |
| 750 | nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, nativePaint); |
| 751 | } finally { |
| 752 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
| 753 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 756 | private static native void nDrawBitmap( |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 757 | int renderer, int bitmap, byte[] buffer, float left, float top, int paint); |
Romain Guy | dbd77cd | 2010-07-09 10:36:05 -0700 | [diff] [blame] | 758 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 759 | @Override |
| 760 | public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) { |
Romain Guy | bc52cce | 2012-02-07 19:40:19 -0800 | [diff] [blame] | 761 | if (bitmap.isRecycled()) throw new IllegalArgumentException("Cannot draw recycled bitmaps"); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 762 | // Shaders are ignored when drawing bitmaps |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 763 | int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE; |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 764 | try { |
| 765 | final int nativePaint = paint == null ? 0 : paint.mNativePaint; |
| 766 | nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, |
| 767 | matrix.native_instance, nativePaint); |
| 768 | } finally { |
| 769 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
| 770 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 773 | private static native void nDrawBitmap(int renderer, int bitmap, byte[] buff, |
| 774 | int matrix, int paint); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 775 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 776 | @Override |
| 777 | public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) { |
Romain Guy | bc52cce | 2012-02-07 19:40:19 -0800 | [diff] [blame] | 778 | if (bitmap.isRecycled()) throw new IllegalArgumentException("Cannot draw recycled bitmaps"); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 779 | // Shaders are ignored when drawing bitmaps |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 780 | int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE; |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 781 | try { |
| 782 | final int nativePaint = paint == null ? 0 : paint.mNativePaint; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 783 | |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 784 | int left, top, right, bottom; |
| 785 | if (src == null) { |
| 786 | left = top = 0; |
| 787 | right = bitmap.getWidth(); |
| 788 | bottom = bitmap.getHeight(); |
| 789 | } else { |
| 790 | left = src.left; |
| 791 | right = src.right; |
| 792 | top = src.top; |
| 793 | bottom = src.bottom; |
| 794 | } |
| 795 | |
| 796 | nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, right, bottom, |
| 797 | dst.left, dst.top, dst.right, dst.bottom, nativePaint); |
| 798 | } finally { |
| 799 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 800 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | @Override |
| 804 | public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) { |
Romain Guy | bc52cce | 2012-02-07 19:40:19 -0800 | [diff] [blame] | 805 | if (bitmap.isRecycled()) throw new IllegalArgumentException("Cannot draw recycled bitmaps"); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 806 | // Shaders are ignored when drawing bitmaps |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 807 | int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE; |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 808 | try { |
| 809 | final int nativePaint = paint == null ? 0 : paint.mNativePaint; |
| 810 | |
| 811 | float left, top, right, bottom; |
| 812 | if (src == null) { |
| 813 | left = top = 0; |
| 814 | right = bitmap.getWidth(); |
| 815 | bottom = bitmap.getHeight(); |
| 816 | } else { |
| 817 | left = src.left; |
| 818 | right = src.right; |
| 819 | top = src.top; |
| 820 | bottom = src.bottom; |
| 821 | } |
| 822 | |
| 823 | nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, right, bottom, |
| 824 | dst.left, dst.top, dst.right, dst.bottom, nativePaint); |
| 825 | } finally { |
| 826 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
Romain Guy | ff98fa5 | 2011-11-28 09:35:09 -0800 | [diff] [blame] | 827 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 830 | private static native void nDrawBitmap(int renderer, int bitmap, byte[] buffer, |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 831 | float srcLeft, float srcTop, float srcRight, float srcBottom, |
Romain Guy | dbd77cd | 2010-07-09 10:36:05 -0700 | [diff] [blame] | 832 | float left, float top, float right, float bottom, int paint); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 833 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 834 | @Override |
| 835 | public void drawBitmap(int[] colors, int offset, int stride, float x, float y, |
| 836 | int width, int height, boolean hasAlpha, Paint paint) { |
Romain Guy | e651cc6 | 2012-05-14 19:44:40 -0700 | [diff] [blame] | 837 | if (width < 0) { |
| 838 | throw new IllegalArgumentException("width must be >= 0"); |
| 839 | } |
| 840 | |
| 841 | if (height < 0) { |
| 842 | throw new IllegalArgumentException("height must be >= 0"); |
| 843 | } |
| 844 | |
| 845 | if (Math.abs(stride) < width) { |
| 846 | throw new IllegalArgumentException("abs(stride) must be >= width"); |
| 847 | } |
| 848 | |
| 849 | int lastScanline = offset + (height - 1) * stride; |
| 850 | int length = colors.length; |
| 851 | |
| 852 | if (offset < 0 || (offset + width > length) || lastScanline < 0 || |
| 853 | (lastScanline + width > length)) { |
| 854 | throw new ArrayIndexOutOfBoundsException(); |
| 855 | } |
| 856 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 857 | // Shaders are ignored when drawing bitmaps |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 858 | int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE; |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 859 | try { |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 860 | final int nativePaint = paint == null ? 0 : paint.mNativePaint; |
Romain Guy | e651cc6 | 2012-05-14 19:44:40 -0700 | [diff] [blame] | 861 | nDrawBitmap(mRenderer, colors, offset, stride, x, y, |
| 862 | width, height, hasAlpha, nativePaint); |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 863 | } finally { |
| 864 | if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier); |
| 865 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Romain Guy | e651cc6 | 2012-05-14 19:44:40 -0700 | [diff] [blame] | 868 | private static native void nDrawBitmap(int renderer, int[] colors, int offset, int stride, |
| 869 | float x, float y, int width, int height, boolean hasAlpha, int nativePaint); |
| 870 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 871 | @Override |
| 872 | public void drawBitmap(int[] colors, int offset, int stride, int x, int y, |
| 873 | int width, int height, boolean hasAlpha, Paint paint) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 874 | // Shaders are ignored when drawing bitmaps |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 875 | drawBitmap(colors, offset, stride, (float) x, (float) y, width, height, hasAlpha, paint); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | @Override |
| 879 | public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, |
| 880 | int vertOffset, int[] colors, int colorOffset, Paint paint) { |
Romain Guy | bc52cce | 2012-02-07 19:40:19 -0800 | [diff] [blame] | 881 | if (bitmap.isRecycled()) throw new IllegalArgumentException("Cannot draw recycled bitmaps"); |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 882 | if (meshWidth < 0 || meshHeight < 0 || vertOffset < 0 || colorOffset < 0) { |
| 883 | throw new ArrayIndexOutOfBoundsException(); |
| 884 | } |
| 885 | |
| 886 | if (meshWidth == 0 || meshHeight == 0) { |
| 887 | return; |
| 888 | } |
| 889 | |
| 890 | final int count = (meshWidth + 1) * (meshHeight + 1); |
| 891 | checkRange(verts.length, vertOffset, count * 2); |
| 892 | |
| 893 | // TODO: Colors are ignored for now |
| 894 | colors = null; |
| 895 | colorOffset = 0; |
| 896 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 897 | int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE; |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 898 | try { |
| 899 | final int nativePaint = paint == null ? 0 : paint.mNativePaint; |
| 900 | nDrawBitmapMesh(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, meshWidth, meshHeight, |
| 901 | verts, vertOffset, colors, colorOffset, nativePaint); |
| 902 | } finally { |
| 903 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
| 904 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 907 | private static native void nDrawBitmapMesh(int renderer, int bitmap, byte[] buffer, |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 908 | int meshWidth, int meshHeight, float[] verts, int vertOffset, |
| 909 | int[] colors, int colorOffset, int paint); |
| 910 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 911 | @Override |
| 912 | public void drawCircle(float cx, float cy, float radius, Paint paint) { |
Romain Guy | 765dcf3 | 2012-02-27 13:28:22 -0800 | [diff] [blame] | 913 | int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER); |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 914 | try { |
| 915 | nDrawCircle(mRenderer, cx, cy, radius, paint.mNativePaint); |
| 916 | } finally { |
| 917 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
| 918 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 921 | private static native void nDrawCircle(int renderer, float cx, float cy, |
| 922 | float radius, int paint); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 923 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 924 | @Override |
| 925 | public void drawColor(int color) { |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 926 | drawColor(color, PorterDuff.Mode.SRC_OVER); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | @Override |
| 930 | public void drawColor(int color, PorterDuff.Mode mode) { |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 931 | nDrawColor(mRenderer, color, mode.nativeInt); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 932 | } |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 933 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 934 | private static native void nDrawColor(int renderer, int color, int mode); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 935 | |
| 936 | @Override |
| 937 | public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 938 | mLine[0] = startX; |
| 939 | mLine[1] = startY; |
| 940 | mLine[2] = stopX; |
| 941 | mLine[3] = stopY; |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 942 | drawLines(mLine, 0, 4, paint); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | @Override |
| 946 | public void drawLines(float[] pts, int offset, int count, Paint paint) { |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 947 | if ((offset | count) < 0 || offset + count > pts.length) { |
| 948 | throw new IllegalArgumentException("The lines array must contain 4 elements per line."); |
| 949 | } |
Romain Guy | 765dcf3 | 2012-02-27 13:28:22 -0800 | [diff] [blame] | 950 | int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER); |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 951 | try { |
| 952 | nDrawLines(mRenderer, pts, offset, count, paint.mNativePaint); |
| 953 | } finally { |
| 954 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
| 955 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 958 | private static native void nDrawLines(int renderer, float[] points, |
| 959 | int offset, int count, int paint); |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 960 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 961 | @Override |
| 962 | public void drawLines(float[] pts, Paint paint) { |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 963 | drawLines(pts, 0, pts.length, paint); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | @Override |
| 967 | public void drawOval(RectF oval, Paint paint) { |
Romain Guy | 765dcf3 | 2012-02-27 13:28:22 -0800 | [diff] [blame] | 968 | int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER); |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 969 | try { |
| 970 | nDrawOval(mRenderer, oval.left, oval.top, oval.right, oval.bottom, paint.mNativePaint); |
| 971 | } finally { |
| 972 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
| 973 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 976 | private static native void nDrawOval(int renderer, float left, float top, |
| 977 | float right, float bottom, int paint); |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 978 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 979 | @Override |
| 980 | public void drawPaint(Paint paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 981 | final Rect r = mClipBounds; |
| 982 | nGetClipBounds(mRenderer, r); |
| 983 | drawRect(r.left, r.top, r.right, r.bottom, paint); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | @Override |
| 987 | public void drawPath(Path path, Paint paint) { |
Romain Guy | 765dcf3 | 2012-02-27 13:28:22 -0800 | [diff] [blame] | 988 | int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER); |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 989 | try { |
| 990 | if (path.isSimplePath) { |
| 991 | if (path.rects != null) { |
| 992 | nDrawRects(mRenderer, path.rects.mNativeRegion, paint.mNativePaint); |
| 993 | } |
| 994 | } else { |
| 995 | nDrawPath(mRenderer, path.mNativePath, paint.mNativePaint); |
Romain Guy | a48a1a8 | 2010-08-10 14:59:15 -0700 | [diff] [blame] | 996 | } |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 997 | } finally { |
| 998 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
Romain Guy | a48a1a8 | 2010-08-10 14:59:15 -0700 | [diff] [blame] | 999 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1002 | private static native void nDrawPath(int renderer, int path, int paint); |
| 1003 | private static native void nDrawRects(int renderer, int region, int paint); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 1004 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1005 | @Override |
| 1006 | public void drawPicture(Picture picture) { |
Romain Guy | f9d9c06 | 2012-01-19 16:53:41 -0800 | [diff] [blame] | 1007 | if (picture.createdFromStream) { |
| 1008 | return; |
| 1009 | } |
| 1010 | |
Romain Guy | 75582e8 | 2012-01-18 18:13:35 -0800 | [diff] [blame] | 1011 | picture.endRecording(); |
| 1012 | // TODO: Implement rendering |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | @Override |
| 1016 | public void drawPicture(Picture picture, Rect dst) { |
Romain Guy | f9d9c06 | 2012-01-19 16:53:41 -0800 | [diff] [blame] | 1017 | if (picture.createdFromStream) { |
| 1018 | return; |
| 1019 | } |
| 1020 | |
Romain Guy | 75582e8 | 2012-01-18 18:13:35 -0800 | [diff] [blame] | 1021 | save(); |
| 1022 | translate(dst.left, dst.top); |
| 1023 | if (picture.getWidth() > 0 && picture.getHeight() > 0) { |
| 1024 | scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight()); |
| 1025 | } |
| 1026 | drawPicture(picture); |
| 1027 | restore(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | @Override |
| 1031 | public void drawPicture(Picture picture, RectF dst) { |
Romain Guy | f9d9c06 | 2012-01-19 16:53:41 -0800 | [diff] [blame] | 1032 | if (picture.createdFromStream) { |
| 1033 | return; |
| 1034 | } |
| 1035 | |
Romain Guy | 75582e8 | 2012-01-18 18:13:35 -0800 | [diff] [blame] | 1036 | save(); |
| 1037 | translate(dst.left, dst.top); |
| 1038 | if (picture.getWidth() > 0 && picture.getHeight() > 0) { |
| 1039 | scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight()); |
| 1040 | } |
| 1041 | drawPicture(picture); |
| 1042 | restore(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | @Override |
| 1046 | public void drawPoint(float x, float y, Paint paint) { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 1047 | mPoint[0] = x; |
| 1048 | mPoint[1] = y; |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1049 | drawPoints(mPoint, 0, 2, paint); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | @Override |
| 1053 | public void drawPoints(float[] pts, Paint paint) { |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1054 | drawPoints(pts, 0, pts.length, paint); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | @Override |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1058 | public void drawPoints(float[] pts, int offset, int count, Paint paint) { |
Romain Guy | 765dcf3 | 2012-02-27 13:28:22 -0800 | [diff] [blame] | 1059 | int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER); |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 1060 | try { |
| 1061 | nDrawPoints(mRenderer, pts, offset, count, paint.mNativePaint); |
| 1062 | } finally { |
| 1063 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
| 1064 | } |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | private static native void nDrawPoints(int renderer, float[] points, |
| 1068 | int offset, int count, int paint); |
| 1069 | |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 1070 | @SuppressWarnings("deprecation") |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1071 | @Override |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1072 | public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) { |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 1073 | if (index < 0 || index + count > text.length || count * 2 > pos.length) { |
| 1074 | throw new IndexOutOfBoundsException(); |
| 1075 | } |
| 1076 | |
| 1077 | int modifiers = setupModifiers(paint); |
| 1078 | try { |
| 1079 | nDrawPosText(mRenderer, text, index, count, pos, paint.mNativePaint); |
| 1080 | } finally { |
| 1081 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
| 1082 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 1085 | private static native void nDrawPosText(int renderer, char[] text, int index, int count, |
| 1086 | float[] pos, int paint); |
| 1087 | |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 1088 | @SuppressWarnings("deprecation") |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1089 | @Override |
| 1090 | public void drawPosText(String text, float[] pos, Paint paint) { |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 1091 | if (text.length() * 2 > pos.length) { |
| 1092 | throw new ArrayIndexOutOfBoundsException(); |
| 1093 | } |
| 1094 | |
| 1095 | int modifiers = setupModifiers(paint); |
| 1096 | try { |
| 1097 | nDrawPosText(mRenderer, text, 0, text.length(), pos, paint.mNativePaint); |
| 1098 | } finally { |
| 1099 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
| 1100 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 1103 | private static native void nDrawPosText(int renderer, String text, int start, int end, |
| 1104 | float[] pos, int paint); |
| 1105 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1106 | @Override |
| 1107 | public void drawRect(float left, float top, float right, float bottom, Paint paint) { |
Romain Guy | 765dcf3 | 2012-02-27 13:28:22 -0800 | [diff] [blame] | 1108 | if (left == right || top == bottom) return; |
| 1109 | int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER); |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 1110 | try { |
| 1111 | nDrawRect(mRenderer, left, top, right, bottom, paint.mNativePaint); |
| 1112 | } finally { |
| 1113 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
| 1114 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1117 | private static native void nDrawRect(int renderer, float left, float top, |
| 1118 | float right, float bottom, int paint); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 1119 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1120 | @Override |
| 1121 | public void drawRect(Rect r, Paint paint) { |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 1122 | drawRect(r.left, r.top, r.right, r.bottom, paint); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | @Override |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 1126 | public void drawRect(RectF r, Paint paint) { |
| 1127 | drawRect(r.left, r.top, r.right, r.bottom, paint); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | @Override |
| 1131 | public void drawRGB(int r, int g, int b) { |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 1132 | drawColor(0xFF000000 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF)); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | @Override |
| 1136 | public void drawRoundRect(RectF rect, float rx, float ry, Paint paint) { |
Romain Guy | 765dcf3 | 2012-02-27 13:28:22 -0800 | [diff] [blame] | 1137 | int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER); |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 1138 | try { |
| 1139 | nDrawRoundRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, |
| 1140 | rx, ry, paint.mNativePaint); |
| 1141 | } finally { |
| 1142 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
| 1143 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1146 | private static native void nDrawRoundRect(int renderer, float left, float top, |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 1147 | float right, float bottom, float rx, float y, int paint); |
| 1148 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1149 | @Override |
| 1150 | public void drawText(char[] text, int index, int count, float x, float y, Paint paint) { |
Romain Guy | a1db574 | 2010-07-20 13:09:13 -0700 | [diff] [blame] | 1151 | if ((index | count | (index + count) | (text.length - index - count)) < 0) { |
| 1152 | throw new IndexOutOfBoundsException(); |
| 1153 | } |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1154 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1155 | int modifiers = setupModifiers(paint); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1156 | try { |
| 1157 | nDrawText(mRenderer, text, index, count, x, y, paint.mBidiFlags, paint.mNativePaint); |
| 1158 | } finally { |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1159 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1160 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1161 | } |
Romain Guy | a1db574 | 2010-07-20 13:09:13 -0700 | [diff] [blame] | 1162 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1163 | private static native void nDrawText(int renderer, char[] text, int index, int count, |
| 1164 | float x, float y, int bidiFlags, int paint); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1165 | |
| 1166 | @Override |
| 1167 | public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) { |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1168 | int modifiers = setupModifiers(paint); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1169 | try { |
| 1170 | if (text instanceof String || text instanceof SpannedString || |
| 1171 | text instanceof SpannableString) { |
| 1172 | nDrawText(mRenderer, text.toString(), start, end, x, y, paint.mBidiFlags, |
| 1173 | paint.mNativePaint); |
| 1174 | } else if (text instanceof GraphicsOperations) { |
| 1175 | ((GraphicsOperations) text).drawText(this, start, end, x, y, |
| 1176 | paint); |
| 1177 | } else { |
| 1178 | char[] buf = TemporaryBuffer.obtain(end - start); |
| 1179 | TextUtils.getChars(text, start, end, buf, 0); |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1180 | nDrawText(mRenderer, buf, 0, end - start, x, y, |
| 1181 | paint.mBidiFlags, paint.mNativePaint); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1182 | TemporaryBuffer.recycle(buf); |
| 1183 | } |
| 1184 | } finally { |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1185 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
Romain Guy | a1db574 | 2010-07-20 13:09:13 -0700 | [diff] [blame] | 1186 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | @Override |
| 1190 | public void drawText(String text, int start, int end, float x, float y, Paint paint) { |
Romain Guy | a1db574 | 2010-07-20 13:09:13 -0700 | [diff] [blame] | 1191 | if ((start | end | (end - start) | (text.length() - end)) < 0) { |
| 1192 | throw new IndexOutOfBoundsException(); |
| 1193 | } |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1194 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1195 | int modifiers = setupModifiers(paint); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1196 | try { |
| 1197 | nDrawText(mRenderer, text, start, end, x, y, paint.mBidiFlags, paint.mNativePaint); |
| 1198 | } finally { |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1199 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1200 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1203 | private static native void nDrawText(int renderer, String text, int start, int end, |
| 1204 | float x, float y, int bidiFlags, int paint); |
Romain Guy | a1db574 | 2010-07-20 13:09:13 -0700 | [diff] [blame] | 1205 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1206 | @Override |
| 1207 | public void drawText(String text, float x, float y, Paint paint) { |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1208 | int modifiers = setupModifiers(paint); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1209 | try { |
| 1210 | nDrawText(mRenderer, text, 0, text.length(), x, y, paint.mBidiFlags, |
| 1211 | paint.mNativePaint); |
| 1212 | } finally { |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1213 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1214 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | @Override |
| 1218 | public void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, |
| 1219 | float vOffset, Paint paint) { |
Romain Guy | 325740f | 2012-02-24 16:48:34 -0800 | [diff] [blame] | 1220 | if (index < 0 || index + count > text.length) { |
| 1221 | throw new ArrayIndexOutOfBoundsException(); |
| 1222 | } |
| 1223 | |
| 1224 | int modifiers = setupModifiers(paint); |
| 1225 | try { |
| 1226 | nDrawTextOnPath(mRenderer, text, index, count, path.mNativePath, hOffset, vOffset, |
| 1227 | paint.mBidiFlags, paint.mNativePaint); |
| 1228 | } finally { |
| 1229 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
| 1230 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
Romain Guy | 325740f | 2012-02-24 16:48:34 -0800 | [diff] [blame] | 1233 | private static native void nDrawTextOnPath(int renderer, char[] text, int index, int count, |
| 1234 | int path, float hOffset, float vOffset, int bidiFlags, int nativePaint); |
| 1235 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1236 | @Override |
| 1237 | public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) { |
Romain Guy | 325740f | 2012-02-24 16:48:34 -0800 | [diff] [blame] | 1238 | if (text.length() == 0) return; |
| 1239 | |
| 1240 | int modifiers = setupModifiers(paint); |
| 1241 | try { |
| 1242 | nDrawTextOnPath(mRenderer, text, 0, text.length(), path.mNativePath, hOffset, vOffset, |
| 1243 | paint.mBidiFlags, paint.mNativePaint); |
| 1244 | } finally { |
| 1245 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
| 1246 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
Romain Guy | 325740f | 2012-02-24 16:48:34 -0800 | [diff] [blame] | 1249 | private static native void nDrawTextOnPath(int renderer, String text, int start, int end, |
| 1250 | int path, float hOffset, float vOffset, int bidiFlags, int nativePaint); |
| 1251 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1252 | @Override |
| 1253 | public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount, |
| 1254 | float x, float y, int dir, Paint paint) { |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1255 | if ((index | count | text.length - index - count) < 0) { |
| 1256 | throw new IndexOutOfBoundsException(); |
| 1257 | } |
| 1258 | if (dir != DIRECTION_LTR && dir != DIRECTION_RTL) { |
| 1259 | throw new IllegalArgumentException("Unknown direction: " + dir); |
| 1260 | } |
| 1261 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1262 | int modifiers = setupModifiers(paint); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1263 | try { |
| 1264 | nDrawTextRun(mRenderer, text, index, count, contextIndex, contextCount, x, y, dir, |
| 1265 | paint.mNativePaint); |
| 1266 | } finally { |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1267 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1268 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1271 | private static native void nDrawTextRun(int renderer, char[] text, int index, int count, |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1272 | int contextIndex, int contextCount, float x, float y, int dir, int nativePaint); |
| 1273 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1274 | @Override |
| 1275 | public void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd, |
| 1276 | float x, float y, int dir, Paint paint) { |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1277 | if ((start | end | end - start | text.length() - end) < 0) { |
| 1278 | throw new IndexOutOfBoundsException(); |
| 1279 | } |
| 1280 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1281 | int modifiers = setupModifiers(paint); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1282 | try { |
| 1283 | int flags = dir == 0 ? 0 : 1; |
| 1284 | if (text instanceof String || text instanceof SpannedString || |
| 1285 | text instanceof SpannableString) { |
| 1286 | nDrawTextRun(mRenderer, text.toString(), start, end, contextStart, |
| 1287 | contextEnd, x, y, flags, paint.mNativePaint); |
| 1288 | } else if (text instanceof GraphicsOperations) { |
| 1289 | ((GraphicsOperations) text).drawTextRun(this, start, end, |
| 1290 | contextStart, contextEnd, x, y, flags, paint); |
| 1291 | } else { |
| 1292 | int contextLen = contextEnd - contextStart; |
| 1293 | int len = end - start; |
| 1294 | char[] buf = TemporaryBuffer.obtain(contextLen); |
| 1295 | TextUtils.getChars(text, contextStart, contextEnd, buf, 0); |
| 1296 | nDrawTextRun(mRenderer, buf, start - contextStart, len, 0, contextLen, |
| 1297 | x, y, flags, paint.mNativePaint); |
| 1298 | TemporaryBuffer.recycle(buf); |
| 1299 | } |
| 1300 | } finally { |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1301 | if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1302 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1305 | private static native void nDrawTextRun(int renderer, String text, int start, int end, |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 1306 | int contextStart, int contextEnd, float x, float y, int flags, int nativePaint); |
| 1307 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1308 | @Override |
| 1309 | public void drawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset, |
| 1310 | float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices, |
| 1311 | int indexOffset, int indexCount, Paint paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1312 | // TODO: Implement |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1313 | } |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1314 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1315 | private int setupModifiers(Bitmap b, Paint paint) { |
Romain Guy | 445c83c | 2012-02-16 16:43:07 -0800 | [diff] [blame] | 1316 | if (b.getConfig() != Bitmap.Config.ALPHA_8) { |
| 1317 | final ColorFilter filter = paint.getColorFilter(); |
| 1318 | if (filter != null) { |
| 1319 | nSetupColorFilter(mRenderer, filter.nativeColorFilter); |
| 1320 | return MODIFIER_COLOR_FILTER; |
| 1321 | } |
| 1322 | |
| 1323 | return MODIFIER_NONE; |
| 1324 | } else { |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1325 | return setupModifiers(paint); |
| 1326 | } |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | private int setupModifiers(Paint paint) { |
| 1330 | int modifiers = MODIFIER_NONE; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1331 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 1332 | if (paint.hasShadow) { |
| 1333 | nSetupShadow(mRenderer, paint.shadowRadius, paint.shadowDx, paint.shadowDy, |
| 1334 | paint.shadowColor); |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1335 | modifiers |= MODIFIER_SHADOW; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1338 | final Shader shader = paint.getShader(); |
| 1339 | if (shader != null) { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1340 | nSetupShader(mRenderer, shader.native_shader); |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1341 | modifiers |= MODIFIER_SHADER; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | final ColorFilter filter = paint.getColorFilter(); |
| 1345 | if (filter != null) { |
| 1346 | nSetupColorFilter(mRenderer, filter.nativeColorFilter); |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1347 | modifiers |= MODIFIER_COLOR_FILTER; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1348 | } |
| 1349 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1350 | return modifiers; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1351 | } |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 1352 | |
Romain Guy | 765dcf3 | 2012-02-27 13:28:22 -0800 | [diff] [blame] | 1353 | private int setupModifiers(Paint paint, int flags) { |
| 1354 | int modifiers = MODIFIER_NONE; |
| 1355 | |
| 1356 | if (paint.hasShadow && (flags & MODIFIER_SHADOW) != 0) { |
| 1357 | nSetupShadow(mRenderer, paint.shadowRadius, paint.shadowDx, paint.shadowDy, |
| 1358 | paint.shadowColor); |
| 1359 | modifiers |= MODIFIER_SHADOW; |
| 1360 | } |
| 1361 | |
| 1362 | final Shader shader = paint.getShader(); |
| 1363 | if (shader != null && (flags & MODIFIER_SHADER) != 0) { |
| 1364 | nSetupShader(mRenderer, shader.native_shader); |
| 1365 | modifiers |= MODIFIER_SHADER; |
| 1366 | } |
| 1367 | |
| 1368 | final ColorFilter filter = paint.getColorFilter(); |
| 1369 | if (filter != null && (flags & MODIFIER_COLOR_FILTER) != 0) { |
| 1370 | nSetupColorFilter(mRenderer, filter.nativeColorFilter); |
| 1371 | modifiers |= MODIFIER_COLOR_FILTER; |
| 1372 | } |
| 1373 | |
| 1374 | return modifiers; |
| 1375 | } |
| 1376 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1377 | private int setupColorFilter(Paint paint) { |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1378 | final ColorFilter filter = paint.getColorFilter(); |
| 1379 | if (filter != null) { |
| 1380 | nSetupColorFilter(mRenderer, filter.nativeColorFilter); |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1381 | return MODIFIER_COLOR_FILTER; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1382 | } |
Romain Guy | 5621527 | 2011-03-23 16:55:38 -0700 | [diff] [blame] | 1383 | return MODIFIER_NONE; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1384 | } |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1385 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 1386 | private static native void nSetupShader(int renderer, int shader); |
| 1387 | private static native void nSetupColorFilter(int renderer, int colorFilter); |
| 1388 | private static native void nSetupShadow(int renderer, float radius, |
| 1389 | float dx, float dy, int color); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 1390 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1391 | private static native void nResetModifiers(int renderer, int modifiers); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1392 | } |