GLES20Canvas.java revision 162a0217563f4665da6eb183dfce0fef740f641f
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.view; 18 19import android.graphics.Bitmap; 20import android.graphics.Canvas; 21import android.graphics.ColorFilter; 22import android.graphics.DrawFilter; 23import android.graphics.Matrix; 24import android.graphics.Paint; 25import android.graphics.Path; 26import android.graphics.Picture; 27import android.graphics.PorterDuff; 28import android.graphics.Rect; 29import android.graphics.RectF; 30import android.graphics.Region; 31import android.graphics.Shader; 32import android.graphics.SurfaceTexture; 33import android.graphics.TemporaryBuffer; 34import android.text.GraphicsOperations; 35import android.text.SpannableString; 36import android.text.SpannedString; 37import android.text.TextUtils; 38 39/** 40 * An implementation of Canvas on top of OpenGL ES 2.0. 41 */ 42class GLES20Canvas extends HardwareCanvas { 43 // Must match modifiers used in the JNI layer 44 private static final int MODIFIER_NONE = 0; 45 private static final int MODIFIER_SHADOW = 1; 46 private static final int MODIFIER_SHADER = 2; 47 private static final int MODIFIER_COLOR_FILTER = 4; 48 49 private final boolean mOpaque; 50 private int mRenderer; 51 52 // The native renderer will be destroyed when this object dies. 53 // DO NOT overwrite this reference once it is set. 54 @SuppressWarnings("unused") 55 private CanvasFinalizer mFinalizer; 56 57 private int mWidth; 58 private int mHeight; 59 60 private final float[] mPoint = new float[2]; 61 private final float[] mLine = new float[4]; 62 63 private final Rect mClipBounds = new Rect(); 64 65 private DrawFilter mFilter; 66 67 /////////////////////////////////////////////////////////////////////////// 68 // JNI 69 /////////////////////////////////////////////////////////////////////////// 70 71 private static native boolean nIsAvailable(); 72 private static boolean sIsAvailable = nIsAvailable(); 73 74 static boolean isAvailable() { 75 return sIsAvailable; 76 } 77 78 /////////////////////////////////////////////////////////////////////////// 79 // Constructors 80 /////////////////////////////////////////////////////////////////////////// 81 82 /** 83 * Creates a canvas to render directly on screen. 84 */ 85 GLES20Canvas(boolean translucent) { 86 this(false, translucent); 87 } 88 89 /** 90 * Creates a canvas to render into an FBO. 91 */ 92 GLES20Canvas(int layer, boolean translucent) { 93 mOpaque = !translucent; 94 mRenderer = nCreateLayerRenderer(layer); 95 setupFinalizer(); 96 } 97 98 protected GLES20Canvas(boolean record, boolean translucent) { 99 mOpaque = !translucent; 100 101 if (record) { 102 mRenderer = nCreateDisplayListRenderer(); 103 } else { 104 mRenderer = nCreateRenderer(); 105 } 106 107 setupFinalizer(); 108 } 109 110 private void setupFinalizer() { 111 if (mRenderer == 0) { 112 throw new IllegalStateException("Could not create GLES20Canvas renderer"); 113 } else { 114 mFinalizer = new CanvasFinalizer(mRenderer); 115 } 116 } 117 118 protected void resetDisplayListRenderer() { 119 nResetDisplayListRenderer(mRenderer); 120 } 121 122 private static native int nCreateRenderer(); 123 private static native int nCreateLayerRenderer(int layer); 124 private static native int nCreateDisplayListRenderer(); 125 private static native void nResetDisplayListRenderer(int renderer); 126 private static native void nDestroyRenderer(int renderer); 127 128 private static final class CanvasFinalizer { 129 private final int mRenderer; 130 131 public CanvasFinalizer(int renderer) { 132 mRenderer = renderer; 133 } 134 135 @Override 136 protected void finalize() throws Throwable { 137 try { 138 nDestroyRenderer(mRenderer); 139 } finally { 140 super.finalize(); 141 } 142 } 143 } 144 145 /////////////////////////////////////////////////////////////////////////// 146 // Hardware layers 147 /////////////////////////////////////////////////////////////////////////// 148 149 static native int nCreateTextureLayer(boolean opaque, int[] layerInfo); 150 static native int nCreateLayer(int width, int height, boolean isOpaque, int[] layerInfo); 151 static native void nResizeLayer(int layerId, int width, int height, int[] layerInfo); 152 static native void nUpdateTextureLayer(int layerId, int width, int height, boolean opaque, 153 SurfaceTexture surface); 154 static native void nDestroyLayer(int layerId); 155 static native void nDestroyLayerDeferred(int layerId); 156 static native boolean nCopyLayer(int layerId, int bitmap); 157 158 /////////////////////////////////////////////////////////////////////////// 159 // Canvas management 160 /////////////////////////////////////////////////////////////////////////// 161 162 @Override 163 public boolean isOpaque() { 164 return mOpaque; 165 } 166 167 @Override 168 public int getWidth() { 169 return mWidth; 170 } 171 172 @Override 173 public int getHeight() { 174 return mHeight; 175 } 176 177 @Override 178 public int getMaximumBitmapWidth() { 179 return nGetMaximumTextureWidth(); 180 } 181 182 @Override 183 public int getMaximumBitmapHeight() { 184 return nGetMaximumTextureHeight(); 185 } 186 187 private static native int nGetMaximumTextureWidth(); 188 private static native int nGetMaximumTextureHeight(); 189 190 /////////////////////////////////////////////////////////////////////////// 191 // Setup 192 /////////////////////////////////////////////////////////////////////////// 193 194 @Override 195 public void setViewport(int width, int height) { 196 mWidth = width; 197 mHeight = height; 198 199 nSetViewport(mRenderer, width, height); 200 } 201 202 private static native void nSetViewport(int renderer, int width, int height); 203 204 /** 205 * Preserves the back buffer of the current surface after a buffer swap. 206 * Calling this method sets the EGL_SWAP_BEHAVIOR attribute of the current 207 * surface to EGL_BUFFER_PRESERVED. Calling this method requires an EGL 208 * config that supports EGL_SWAP_BEHAVIOR_PRESERVED_BIT. 209 * 210 * @return True if the swap behavior was successfully changed, 211 * false otherwise. 212 * 213 * @hide 214 */ 215 public static boolean preserveBackBuffer() { 216 return nPreserveBackBuffer(); 217 } 218 219 private static native boolean nPreserveBackBuffer(); 220 221 /** 222 * Indicates whether the current surface preserves its back buffer 223 * after a buffer swap. 224 * 225 * @return True, if the surface's EGL_SWAP_BEHAVIOR is EGL_BUFFER_PRESERVED, 226 * false otherwise 227 * 228 * @hide 229 */ 230 public static boolean isBackBufferPreserved() { 231 return nIsBackBufferPreserved(); 232 } 233 234 private static native boolean nIsBackBufferPreserved(); 235 236 /** 237 * Disables v-sync. For performance testing only. 238 * 239 * @hide 240 */ 241 public static void disableVsync() { 242 nDisableVsync(); 243 } 244 245 private static native void nDisableVsync(); 246 247 @Override 248 void onPreDraw(Rect dirty) { 249 if (dirty != null) { 250 nPrepareDirty(mRenderer, dirty.left, dirty.top, dirty.right, dirty.bottom, mOpaque); 251 } else { 252 nPrepare(mRenderer, mOpaque); 253 } 254 } 255 256 private static native void nPrepare(int renderer, boolean opaque); 257 private static native void nPrepareDirty(int renderer, int left, int top, int right, int bottom, 258 boolean opaque); 259 260 @Override 261 void onPostDraw() { 262 nFinish(mRenderer); 263 } 264 265 private static native void nFinish(int renderer); 266 267 @Override 268 public boolean callDrawGLFunction(int drawGLFunction) { 269 return nCallDrawGLFunction(mRenderer, drawGLFunction); 270 } 271 272 private static native boolean nCallDrawGLFunction(int renderer, int drawGLFunction); 273 274 275 /////////////////////////////////////////////////////////////////////////// 276 // Memory 277 /////////////////////////////////////////////////////////////////////////// 278 279 /** 280 * @see #flushCaches(int) 281 */ 282 public static final int FLUSH_CACHES_MODERATE = 0; 283 284 /** 285 * @see #flushCaches(int) 286 */ 287 public static final int FLUSH_CACHES_FULL = 1; 288 289 /** 290 * Flush caches to reclaim as much memory as possible. The amount of memory 291 * to reclaim is indicate by the level parameter. 292 * 293 * The level can be one of {@link #FLUSH_CACHES_MODERATE} or 294 * {@link #FLUSH_CACHES_FULL}. 295 * 296 * @param level Hint about the amount of memory to reclaim 297 * 298 * @hide 299 */ 300 public static void flushCaches(int level) { 301 nFlushCaches(level); 302 } 303 304 private static native void nFlushCaches(int level); 305 306 /////////////////////////////////////////////////////////////////////////// 307 // Display list 308 /////////////////////////////////////////////////////////////////////////// 309 310 int getDisplayList(int displayList) { 311 return nGetDisplayList(mRenderer, displayList); 312 } 313 314 private static native int nGetDisplayList(int renderer, int displayList); 315 316 static void destroyDisplayList(int displayList) { 317 nDestroyDisplayList(displayList); 318 } 319 320 private static native void nDestroyDisplayList(int displayList); 321 322 @Override 323 public boolean drawDisplayList(DisplayList displayList, int width, int height, Rect dirty) { 324 return nDrawDisplayList(mRenderer, 325 ((GLES20DisplayList) displayList).getNativeDisplayList(), width, height, dirty); 326 } 327 328 private static native boolean nDrawDisplayList(int renderer, int displayList, 329 int width, int height, Rect dirty); 330 331 @Override 332 void outputDisplayList(DisplayList displayList) { 333 nOutputDisplayList(mRenderer, ((GLES20DisplayList) displayList).getNativeDisplayList()); 334 } 335 336 private static native void nOutputDisplayList(int renderer, int displayList); 337 338 /////////////////////////////////////////////////////////////////////////// 339 // Hardware layer 340 /////////////////////////////////////////////////////////////////////////// 341 342 void drawHardwareLayer(HardwareLayer layer, float x, float y, Paint paint) { 343 final GLES20Layer glLayer = (GLES20Layer) layer; 344 int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE; 345 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 346 nDrawLayer(mRenderer, glLayer.getLayer(), x, y, nativePaint); 347 if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier); 348 } 349 350 private static native void nDrawLayer(int renderer, int layer, float x, float y, int paint); 351 352 void interrupt() { 353 nInterrupt(mRenderer); 354 } 355 356 void resume() { 357 nResume(mRenderer); 358 } 359 360 private static native void nInterrupt(int renderer); 361 private static native void nResume(int renderer); 362 363 /////////////////////////////////////////////////////////////////////////// 364 // Clipping 365 /////////////////////////////////////////////////////////////////////////// 366 367 @Override 368 public boolean clipPath(Path path) { 369 throw new UnsupportedOperationException(); 370 } 371 372 @Override 373 public boolean clipPath(Path path, Region.Op op) { 374 throw new UnsupportedOperationException(); 375 } 376 377 @Override 378 public boolean clipRect(float left, float top, float right, float bottom) { 379 return nClipRect(mRenderer, left, top, right, bottom, Region.Op.INTERSECT.nativeInt); 380 } 381 382 private static native boolean nClipRect(int renderer, float left, float top, 383 float right, float bottom, int op); 384 385 @Override 386 public boolean clipRect(float left, float top, float right, float bottom, Region.Op op) { 387 return nClipRect(mRenderer, left, top, right, bottom, op.nativeInt); 388 } 389 390 @Override 391 public boolean clipRect(int left, int top, int right, int bottom) { 392 return nClipRect(mRenderer, left, top, right, bottom, Region.Op.INTERSECT.nativeInt); 393 } 394 395 private static native boolean nClipRect(int renderer, int left, int top, int right, int bottom, 396 int op); 397 398 @Override 399 public boolean clipRect(Rect rect) { 400 return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, 401 Region.Op.INTERSECT.nativeInt); 402 } 403 404 @Override 405 public boolean clipRect(Rect rect, Region.Op op) { 406 return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, op.nativeInt); 407 } 408 409 @Override 410 public boolean clipRect(RectF rect) { 411 return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, 412 Region.Op.INTERSECT.nativeInt); 413 } 414 415 @Override 416 public boolean clipRect(RectF rect, Region.Op op) { 417 return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, op.nativeInt); 418 } 419 420 @Override 421 public boolean clipRegion(Region region) { 422 throw new UnsupportedOperationException(); 423 } 424 425 @Override 426 public boolean clipRegion(Region region, Region.Op op) { 427 throw new UnsupportedOperationException(); 428 } 429 430 @Override 431 public boolean getClipBounds(Rect bounds) { 432 return nGetClipBounds(mRenderer, bounds); 433 } 434 435 private static native boolean nGetClipBounds(int renderer, Rect bounds); 436 437 @Override 438 public boolean quickReject(float left, float top, float right, float bottom, EdgeType type) { 439 return nQuickReject(mRenderer, left, top, right, bottom, type.nativeInt); 440 } 441 442 private static native boolean nQuickReject(int renderer, float left, float top, 443 float right, float bottom, int edge); 444 445 @Override 446 public boolean quickReject(Path path, EdgeType type) { 447 throw new UnsupportedOperationException(); 448 } 449 450 @Override 451 public boolean quickReject(RectF rect, EdgeType type) { 452 return quickReject(rect.left, rect.top, rect.right, rect.bottom, type); 453 } 454 455 /////////////////////////////////////////////////////////////////////////// 456 // Transformations 457 /////////////////////////////////////////////////////////////////////////// 458 459 @Override 460 public void translate(float dx, float dy) { 461 if (dx != 0.0f || dy != 0.0f) nTranslate(mRenderer, dx, dy); 462 } 463 464 private static native void nTranslate(int renderer, float dx, float dy); 465 466 @Override 467 public void skew(float sx, float sy) { 468 nSkew(mRenderer, sx, sy); 469 } 470 471 private static native void nSkew(int renderer, float sx, float sy); 472 473 @Override 474 public void rotate(float degrees) { 475 nRotate(mRenderer, degrees); 476 } 477 478 private static native void nRotate(int renderer, float degrees); 479 480 @Override 481 public void scale(float sx, float sy) { 482 nScale(mRenderer, sx, sy); 483 } 484 485 private static native void nScale(int renderer, float sx, float sy); 486 487 @Override 488 public void setMatrix(Matrix matrix) { 489 nSetMatrix(mRenderer, matrix.native_instance); 490 } 491 492 private static native void nSetMatrix(int renderer, int matrix); 493 494 @Override 495 public int getNativeMatrix() { 496 return nGetMatrix(mRenderer); 497 } 498 499 private static native int nGetMatrix(int renderer); 500 501 @Override 502 public void getMatrix(Matrix matrix) { 503 nGetMatrix(mRenderer, matrix.native_instance); 504 } 505 506 private static native void nGetMatrix(int renderer, int matrix); 507 508 @Override 509 public void concat(Matrix matrix) { 510 nConcatMatrix(mRenderer, matrix.native_instance); 511 } 512 513 private static native void nConcatMatrix(int renderer, int matrix); 514 515 /////////////////////////////////////////////////////////////////////////// 516 // State management 517 /////////////////////////////////////////////////////////////////////////// 518 519 @Override 520 public int save() { 521 return nSave(mRenderer, Canvas.CLIP_SAVE_FLAG | Canvas.MATRIX_SAVE_FLAG); 522 } 523 524 @Override 525 public int save(int saveFlags) { 526 return nSave(mRenderer, saveFlags); 527 } 528 529 private static native int nSave(int renderer, int flags); 530 531 @Override 532 public int saveLayer(RectF bounds, Paint paint, int saveFlags) { 533 return saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, paint, saveFlags); 534 } 535 536 @Override 537 public int saveLayer(float left, float top, float right, float bottom, Paint paint, 538 int saveFlags) { 539 if (left < right && top < bottom) { 540 int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE; 541 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 542 int count = nSaveLayer(mRenderer, left, top, right, bottom, nativePaint, saveFlags); 543 if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier); 544 return count; 545 } 546 return save(saveFlags); 547 } 548 549 private static native int nSaveLayer(int renderer, float left, float top, 550 float right, float bottom, int paint, int saveFlags); 551 552 @Override 553 public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags) { 554 return saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom, 555 alpha, saveFlags); 556 } 557 558 @Override 559 public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, 560 int saveFlags) { 561 if (left < right && top < bottom) { 562 return nSaveLayerAlpha(mRenderer, left, top, right, bottom, alpha, saveFlags); 563 } 564 return save(saveFlags); 565 } 566 567 private static native int nSaveLayerAlpha(int renderer, float left, float top, float right, 568 float bottom, int alpha, int saveFlags); 569 570 @Override 571 public void restore() { 572 nRestore(mRenderer); 573 } 574 575 private static native void nRestore(int renderer); 576 577 @Override 578 public void restoreToCount(int saveCount) { 579 nRestoreToCount(mRenderer, saveCount); 580 } 581 582 private static native void nRestoreToCount(int renderer, int saveCount); 583 584 @Override 585 public int getSaveCount() { 586 return nGetSaveCount(mRenderer); 587 } 588 589 private static native int nGetSaveCount(int renderer); 590 591 /////////////////////////////////////////////////////////////////////////// 592 // Filtering 593 /////////////////////////////////////////////////////////////////////////// 594 595 @Override 596 public void setDrawFilter(DrawFilter filter) { 597 mFilter = filter; 598 } 599 600 @Override 601 public DrawFilter getDrawFilter() { 602 return mFilter; 603 } 604 605 /////////////////////////////////////////////////////////////////////////// 606 // Drawing 607 /////////////////////////////////////////////////////////////////////////// 608 609 @Override 610 public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, 611 Paint paint) { 612 int modifiers = setupModifiers(paint); 613 nDrawArc(mRenderer, oval.left, oval.top, oval.right, oval.bottom, startAngle, sweepAngle, 614 useCenter, paint.mNativePaint); 615 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 616 } 617 618 private static native void nDrawArc(int renderer, float left, float top, 619 float right, float bottom, float startAngle, float sweepAngle, 620 boolean useCenter, int paint); 621 622 @Override 623 public void drawARGB(int a, int r, int g, int b) { 624 drawColor((a & 0xFF) << 24 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF)); 625 } 626 627 @Override 628 public void drawPatch(Bitmap bitmap, byte[] chunks, RectF dst, Paint paint) { 629 // Shaders are ignored when drawing patches 630 int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE; 631 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 632 nDrawPatch(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, chunks, 633 dst.left, dst.top, dst.right, dst.bottom, nativePaint); 634 if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier); 635 } 636 637 private static native void nDrawPatch(int renderer, int bitmap, byte[] buffer, byte[] chunks, 638 float left, float top, float right, float bottom, int paint); 639 640 @Override 641 public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) { 642 // Shaders are ignored when drawing bitmaps 643 int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE; 644 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 645 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, nativePaint); 646 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 647 } 648 649 private static native void nDrawBitmap( 650 int renderer, int bitmap, byte[] buffer, float left, float top, int paint); 651 652 @Override 653 public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) { 654 // Shaders are ignored when drawing bitmaps 655 int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE; 656 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 657 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, 658 matrix.native_instance, nativePaint); 659 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 660 } 661 662 private static native void nDrawBitmap(int renderer, int bitmap, byte[] buff, 663 int matrix, int paint); 664 665 @Override 666 public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) { 667 // Shaders are ignored when drawing bitmaps 668 int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE; 669 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 670 671 int left, top, right, bottom; 672 if (src == null) { 673 left = top = 0; 674 right = bitmap.getWidth(); 675 bottom = bitmap.getHeight(); 676 } else { 677 left = src.left; 678 right = src.right; 679 top = src.top; 680 bottom = src.bottom; 681 } 682 683 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, right, bottom, 684 dst.left, dst.top, dst.right, dst.bottom, nativePaint); 685 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 686 } 687 688 @Override 689 public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) { 690 // Shaders are ignored when drawing bitmaps 691 int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE; 692 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 693 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, src.left, src.top, src.right, 694 src.bottom, dst.left, dst.top, dst.right, dst.bottom, nativePaint); 695 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 696 } 697 698 private static native void nDrawBitmap(int renderer, int bitmap, byte[] buffer, 699 float srcLeft, float srcTop, float srcRight, float srcBottom, 700 float left, float top, float right, float bottom, int paint); 701 702 @Override 703 public void drawBitmap(int[] colors, int offset, int stride, float x, float y, 704 int width, int height, boolean hasAlpha, Paint paint) { 705 // Shaders are ignored when drawing bitmaps 706 int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE; 707 final Bitmap.Config config = hasAlpha ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; 708 final Bitmap b = Bitmap.createBitmap(colors, offset, stride, width, height, config); 709 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 710 nDrawBitmap(mRenderer, b.mNativeBitmap, b.mBuffer, x, y, nativePaint); 711 b.recycle(); 712 if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier); 713 } 714 715 @Override 716 public void drawBitmap(int[] colors, int offset, int stride, int x, int y, 717 int width, int height, boolean hasAlpha, Paint paint) { 718 // Shaders are ignored when drawing bitmaps 719 drawBitmap(colors, offset, stride, (float) x, (float) y, width, height, hasAlpha, paint); 720 } 721 722 @Override 723 public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, 724 int vertOffset, int[] colors, int colorOffset, Paint paint) { 725 if (meshWidth < 0 || meshHeight < 0 || vertOffset < 0 || colorOffset < 0) { 726 throw new ArrayIndexOutOfBoundsException(); 727 } 728 729 if (meshWidth == 0 || meshHeight == 0) { 730 return; 731 } 732 733 final int count = (meshWidth + 1) * (meshHeight + 1); 734 checkRange(verts.length, vertOffset, count * 2); 735 736 // TODO: Colors are ignored for now 737 colors = null; 738 colorOffset = 0; 739 740 int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE; 741 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 742 nDrawBitmapMesh(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, meshWidth, meshHeight, 743 verts, vertOffset, colors, colorOffset, nativePaint); 744 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 745 } 746 747 private static native void nDrawBitmapMesh(int renderer, int bitmap, byte[] buffer, 748 int meshWidth, int meshHeight, float[] verts, int vertOffset, 749 int[] colors, int colorOffset, int paint); 750 751 @Override 752 public void drawCircle(float cx, float cy, float radius, Paint paint) { 753 int modifiers = setupModifiers(paint); 754 nDrawCircle(mRenderer, cx, cy, radius, paint.mNativePaint); 755 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 756 } 757 758 private static native void nDrawCircle(int renderer, float cx, float cy, 759 float radius, int paint); 760 761 @Override 762 public void drawColor(int color) { 763 drawColor(color, PorterDuff.Mode.SRC_OVER); 764 } 765 766 @Override 767 public void drawColor(int color, PorterDuff.Mode mode) { 768 nDrawColor(mRenderer, color, mode.nativeInt); 769 } 770 771 private static native void nDrawColor(int renderer, int color, int mode); 772 773 @Override 774 public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) { 775 mLine[0] = startX; 776 mLine[1] = startY; 777 mLine[2] = stopX; 778 mLine[3] = stopY; 779 drawLines(mLine, 0, 4, paint); 780 } 781 782 @Override 783 public void drawLines(float[] pts, int offset, int count, Paint paint) { 784 if ((offset | count) < 0 || offset + count > pts.length) { 785 throw new IllegalArgumentException("The lines array must contain 4 elements per line."); 786 } 787 int modifiers = setupModifiers(paint); 788 nDrawLines(mRenderer, pts, offset, count, paint.mNativePaint); 789 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 790 } 791 792 private static native void nDrawLines(int renderer, float[] points, 793 int offset, int count, int paint); 794 795 @Override 796 public void drawLines(float[] pts, Paint paint) { 797 drawLines(pts, 0, pts.length, paint); 798 } 799 800 @Override 801 public void drawOval(RectF oval, Paint paint) { 802 int modifiers = setupModifiers(paint); 803 nDrawOval(mRenderer, oval.left, oval.top, oval.right, oval.bottom, paint.mNativePaint); 804 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 805 } 806 807 private static native void nDrawOval(int renderer, float left, float top, 808 float right, float bottom, int paint); 809 810 @Override 811 public void drawPaint(Paint paint) { 812 final Rect r = mClipBounds; 813 nGetClipBounds(mRenderer, r); 814 drawRect(r.left, r.top, r.right, r.bottom, paint); 815 } 816 817 @Override 818 public void drawPath(Path path, Paint paint) { 819 int modifiers = setupModifiers(paint); 820 if (path.isSimplePath) { 821 if (path.rects != null) { 822 nDrawRects(mRenderer, path.rects.mNativeRegion, paint.mNativePaint); 823 } 824 } else { 825 nDrawPath(mRenderer, path.mNativePath, paint.mNativePaint); 826 } 827 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 828 } 829 830 private static native void nDrawPath(int renderer, int path, int paint); 831 private static native void nDrawRects(int renderer, int region, int paint); 832 833 @Override 834 public void drawPicture(Picture picture) { 835 throw new UnsupportedOperationException(); 836 } 837 838 @Override 839 public void drawPicture(Picture picture, Rect dst) { 840 throw new UnsupportedOperationException(); 841 } 842 843 @Override 844 public void drawPicture(Picture picture, RectF dst) { 845 throw new UnsupportedOperationException(); 846 } 847 848 @Override 849 public void drawPoint(float x, float y, Paint paint) { 850 mPoint[0] = x; 851 mPoint[1] = y; 852 drawPoints(mPoint, 0, 2, paint); 853 } 854 855 @Override 856 public void drawPoints(float[] pts, Paint paint) { 857 drawPoints(pts, 0, pts.length, paint); 858 } 859 860 @Override 861 public void drawPoints(float[] pts, int offset, int count, Paint paint) { 862 int modifiers = setupModifiers(paint); 863 nDrawPoints(mRenderer, pts, offset, count, paint.mNativePaint); 864 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 865 } 866 867 private static native void nDrawPoints(int renderer, float[] points, 868 int offset, int count, int paint); 869 870 @Override 871 public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) { 872 // TODO: Implement 873 } 874 875 @Override 876 public void drawPosText(String text, float[] pos, Paint paint) { 877 // TODO: Implement 878 } 879 880 @Override 881 public void drawRect(float left, float top, float right, float bottom, Paint paint) { 882 int modifiers = setupModifiers(paint); 883 nDrawRect(mRenderer, left, top, right, bottom, paint.mNativePaint); 884 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 885 } 886 887 private static native void nDrawRect(int renderer, float left, float top, 888 float right, float bottom, int paint); 889 890 @Override 891 public void drawRect(Rect r, Paint paint) { 892 drawRect(r.left, r.top, r.right, r.bottom, paint); 893 } 894 895 @Override 896 public void drawRect(RectF r, Paint paint) { 897 drawRect(r.left, r.top, r.right, r.bottom, paint); 898 } 899 900 @Override 901 public void drawRGB(int r, int g, int b) { 902 drawColor(0xFF000000 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF)); 903 } 904 905 @Override 906 public void drawRoundRect(RectF rect, float rx, float ry, Paint paint) { 907 int modifiers = setupModifiers(paint); 908 nDrawRoundRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, 909 rx, ry, paint.mNativePaint); 910 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 911 } 912 913 private static native void nDrawRoundRect(int renderer, float left, float top, 914 float right, float bottom, float rx, float y, int paint); 915 916 @Override 917 public void drawText(char[] text, int index, int count, float x, float y, Paint paint) { 918 if ((index | count | (index + count) | (text.length - index - count)) < 0) { 919 throw new IndexOutOfBoundsException(); 920 } 921 922 int modifiers = setupModifiers(paint); 923 try { 924 nDrawText(mRenderer, text, index, count, x, y, paint.mBidiFlags, paint.mNativePaint); 925 } finally { 926 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 927 } 928 } 929 930 private static native void nDrawText(int renderer, char[] text, int index, int count, 931 float x, float y, int bidiFlags, int paint); 932 933 @Override 934 public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) { 935 int modifiers = setupModifiers(paint); 936 try { 937 if (text instanceof String || text instanceof SpannedString || 938 text instanceof SpannableString) { 939 nDrawText(mRenderer, text.toString(), start, end, x, y, paint.mBidiFlags, 940 paint.mNativePaint); 941 } else if (text instanceof GraphicsOperations) { 942 ((GraphicsOperations) text).drawText(this, start, end, x, y, 943 paint); 944 } else { 945 char[] buf = TemporaryBuffer.obtain(end - start); 946 TextUtils.getChars(text, start, end, buf, 0); 947 nDrawText(mRenderer, buf, 0, end - start, x, y, 948 paint.mBidiFlags, paint.mNativePaint); 949 TemporaryBuffer.recycle(buf); 950 } 951 } finally { 952 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 953 } 954 } 955 956 @Override 957 public void drawText(String text, int start, int end, float x, float y, Paint paint) { 958 if ((start | end | (end - start) | (text.length() - end)) < 0) { 959 throw new IndexOutOfBoundsException(); 960 } 961 962 int modifiers = setupModifiers(paint); 963 try { 964 nDrawText(mRenderer, text, start, end, x, y, paint.mBidiFlags, paint.mNativePaint); 965 } finally { 966 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 967 } 968 } 969 970 private static native void nDrawText(int renderer, String text, int start, int end, 971 float x, float y, int bidiFlags, int paint); 972 973 @Override 974 public void drawText(String text, float x, float y, Paint paint) { 975 int modifiers = setupModifiers(paint); 976 try { 977 nDrawText(mRenderer, text, 0, text.length(), x, y, paint.mBidiFlags, 978 paint.mNativePaint); 979 } finally { 980 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 981 } 982 } 983 984 @Override 985 public void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, 986 float vOffset, Paint paint) { 987 // TODO: Implement 988 } 989 990 @Override 991 public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) { 992 // TODO: Implement 993 } 994 995 @Override 996 public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount, 997 float x, float y, int dir, Paint paint) { 998 if ((index | count | text.length - index - count) < 0) { 999 throw new IndexOutOfBoundsException(); 1000 } 1001 if (dir != DIRECTION_LTR && dir != DIRECTION_RTL) { 1002 throw new IllegalArgumentException("Unknown direction: " + dir); 1003 } 1004 1005 int modifiers = setupModifiers(paint); 1006 try { 1007 nDrawTextRun(mRenderer, text, index, count, contextIndex, contextCount, x, y, dir, 1008 paint.mNativePaint); 1009 } finally { 1010 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 1011 } 1012 } 1013 1014 private static native void nDrawTextRun(int renderer, char[] text, int index, int count, 1015 int contextIndex, int contextCount, float x, float y, int dir, int nativePaint); 1016 1017 @Override 1018 public void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd, 1019 float x, float y, int dir, Paint paint) { 1020 if ((start | end | end - start | text.length() - end) < 0) { 1021 throw new IndexOutOfBoundsException(); 1022 } 1023 1024 int modifiers = setupModifiers(paint); 1025 try { 1026 int flags = dir == 0 ? 0 : 1; 1027 if (text instanceof String || text instanceof SpannedString || 1028 text instanceof SpannableString) { 1029 nDrawTextRun(mRenderer, text.toString(), start, end, contextStart, 1030 contextEnd, x, y, flags, paint.mNativePaint); 1031 } else if (text instanceof GraphicsOperations) { 1032 ((GraphicsOperations) text).drawTextRun(this, start, end, 1033 contextStart, contextEnd, x, y, flags, paint); 1034 } else { 1035 int contextLen = contextEnd - contextStart; 1036 int len = end - start; 1037 char[] buf = TemporaryBuffer.obtain(contextLen); 1038 TextUtils.getChars(text, contextStart, contextEnd, buf, 0); 1039 nDrawTextRun(mRenderer, buf, start - contextStart, len, 0, contextLen, 1040 x, y, flags, paint.mNativePaint); 1041 TemporaryBuffer.recycle(buf); 1042 } 1043 } finally { 1044 if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers); 1045 } 1046 } 1047 1048 private static native void nDrawTextRun(int renderer, String text, int start, int end, 1049 int contextStart, int contextEnd, float x, float y, int flags, int nativePaint); 1050 1051 @Override 1052 public void drawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset, 1053 float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices, 1054 int indexOffset, int indexCount, Paint paint) { 1055 // TODO: Implement 1056 } 1057 1058 private int setupModifiers(Bitmap b, Paint paint) { 1059 if (b.getConfig() == Bitmap.Config.ALPHA_8) { 1060 return setupModifiers(paint); 1061 } 1062 1063 final ColorFilter filter = paint.getColorFilter(); 1064 if (filter != null) { 1065 nSetupColorFilter(mRenderer, filter.nativeColorFilter); 1066 return MODIFIER_COLOR_FILTER; 1067 } 1068 1069 return MODIFIER_NONE; 1070 } 1071 1072 private int setupModifiers(Paint paint) { 1073 int modifiers = MODIFIER_NONE; 1074 1075 if (paint.hasShadow) { 1076 nSetupShadow(mRenderer, paint.shadowRadius, paint.shadowDx, paint.shadowDy, 1077 paint.shadowColor); 1078 modifiers |= MODIFIER_SHADOW; 1079 } 1080 1081 final Shader shader = paint.getShader(); 1082 if (shader != null) { 1083 nSetupShader(mRenderer, shader.native_shader); 1084 modifiers |= MODIFIER_SHADER; 1085 } 1086 1087 final ColorFilter filter = paint.getColorFilter(); 1088 if (filter != null) { 1089 nSetupColorFilter(mRenderer, filter.nativeColorFilter); 1090 modifiers |= MODIFIER_COLOR_FILTER; 1091 } 1092 1093 return modifiers; 1094 } 1095 1096 private int setupColorFilter(Paint paint) { 1097 final ColorFilter filter = paint.getColorFilter(); 1098 if (filter != null) { 1099 nSetupColorFilter(mRenderer, filter.nativeColorFilter); 1100 return MODIFIER_COLOR_FILTER; 1101 } 1102 return MODIFIER_NONE; 1103 } 1104 1105 private static native void nSetupShader(int renderer, int shader); 1106 private static native void nSetupColorFilter(int renderer, int colorFilter); 1107 private static native void nSetupShadow(int renderer, float radius, 1108 float dx, float dy, int color); 1109 1110 private static native void nResetModifiers(int renderer, int modifiers); 1111} 1112