GLES20Canvas.java revision 4b0959d8db20c08ab1fed37f397b303af229162b
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.CanvasProperty; 22import android.graphics.DrawFilter; 23import android.graphics.Matrix; 24import android.graphics.NinePatch; 25import android.graphics.Paint; 26import android.graphics.PaintFlagsDrawFilter; 27import android.graphics.Path; 28import android.graphics.Picture; 29import android.graphics.PorterDuff; 30import android.graphics.Rect; 31import android.graphics.RectF; 32import android.graphics.Region; 33import android.graphics.Shader; 34import android.graphics.TemporaryBuffer; 35import android.text.GraphicsOperations; 36import android.text.SpannableString; 37import android.text.SpannedString; 38import android.text.TextUtils; 39 40/** 41 * An implementation of Canvas on top of OpenGL ES 2.0. 42 */ 43class GLES20Canvas extends HardwareCanvas { 44 private final boolean mOpaque; 45 protected long mRenderer; 46 47 // The native renderer will be destroyed when this object dies. 48 // DO NOT overwrite this reference once it is set. 49 @SuppressWarnings({"unused", "FieldCanBeLocal"}) 50 private CanvasFinalizer mFinalizer; 51 52 private int mWidth; 53 private int mHeight; 54 55 private float[] mPoint; 56 private float[] mLine; 57 58 private Rect mClipBounds; 59 private RectF mPathBounds; 60 61 private DrawFilter mFilter; 62 63 /////////////////////////////////////////////////////////////////////////// 64 // JNI 65 /////////////////////////////////////////////////////////////////////////// 66 67 private static native boolean nIsAvailable(); 68 private static boolean sIsAvailable = nIsAvailable(); 69 70 static boolean isAvailable() { 71 return sIsAvailable; 72 } 73 74 /////////////////////////////////////////////////////////////////////////// 75 // Constructors 76 /////////////////////////////////////////////////////////////////////////// 77 78 // TODO: Merge with GLES20RecordingCanvas 79 protected GLES20Canvas() { 80 mOpaque = false; 81 mRenderer = nCreateDisplayListRenderer(); 82 setupFinalizer(); 83 } 84 85 private void setupFinalizer() { 86 if (mRenderer == 0) { 87 throw new IllegalStateException("Could not create GLES20Canvas renderer"); 88 } else { 89 mFinalizer = new CanvasFinalizer(mRenderer); 90 } 91 } 92 93 private static native long nCreateDisplayListRenderer(); 94 private static native void nResetDisplayListRenderer(long renderer); 95 private static native void nDestroyRenderer(long renderer); 96 97 private static final class CanvasFinalizer { 98 private final long mRenderer; 99 100 public CanvasFinalizer(long renderer) { 101 mRenderer = renderer; 102 } 103 104 @Override 105 protected void finalize() throws Throwable { 106 try { 107 nDestroyRenderer(mRenderer); 108 } finally { 109 super.finalize(); 110 } 111 } 112 } 113 114 public static void setProperty(String name, String value) { 115 nSetProperty(name, value); 116 } 117 118 private static native void nSetProperty(String name, String value); 119 120 /////////////////////////////////////////////////////////////////////////// 121 // Canvas management 122 /////////////////////////////////////////////////////////////////////////// 123 124 @Override 125 public boolean isOpaque() { 126 return mOpaque; 127 } 128 129 @Override 130 public int getWidth() { 131 return mWidth; 132 } 133 134 @Override 135 public int getHeight() { 136 return mHeight; 137 } 138 139 @Override 140 public int getMaximumBitmapWidth() { 141 return nGetMaximumTextureWidth(); 142 } 143 144 @Override 145 public int getMaximumBitmapHeight() { 146 return nGetMaximumTextureHeight(); 147 } 148 149 private static native int nGetMaximumTextureWidth(); 150 private static native int nGetMaximumTextureHeight(); 151 152 /** 153 * Returns the native OpenGLRenderer object. 154 */ 155 long getRenderer() { 156 return mRenderer; 157 } 158 159 /////////////////////////////////////////////////////////////////////////// 160 // Setup 161 /////////////////////////////////////////////////////////////////////////// 162 163 @Override 164 public void setViewport(int width, int height) { 165 mWidth = width; 166 mHeight = height; 167 168 nSetViewport(mRenderer, width, height); 169 } 170 171 private static native void nSetViewport(long renderer, int width, int height); 172 173 @Override 174 public int onPreDraw(Rect dirty) { 175 if (dirty != null) { 176 return nPrepareDirty(mRenderer, dirty.left, dirty.top, dirty.right, dirty.bottom, 177 mOpaque); 178 } else { 179 return nPrepare(mRenderer, mOpaque); 180 } 181 } 182 183 private static native int nPrepare(long renderer, boolean opaque); 184 private static native int nPrepareDirty(long renderer, int left, int top, int right, int bottom, 185 boolean opaque); 186 187 @Override 188 public void onPostDraw() { 189 nFinish(mRenderer); 190 } 191 192 private static native void nFinish(long renderer); 193 194 /////////////////////////////////////////////////////////////////////////// 195 // Functor 196 /////////////////////////////////////////////////////////////////////////// 197 198 @Override 199 public int callDrawGLFunction(long drawGLFunction) { 200 return nCallDrawGLFunction(mRenderer, drawGLFunction); 201 } 202 203 private static native int nCallDrawGLFunction(long renderer, long drawGLFunction); 204 205 /////////////////////////////////////////////////////////////////////////// 206 // Memory 207 /////////////////////////////////////////////////////////////////////////// 208 209 /** 210 * Must match Caches::FlushMode values 211 * 212 * @see #flushCaches(int) 213 */ 214 static final int FLUSH_CACHES_LAYERS = 0; 215 216 /** 217 * Must match Caches::FlushMode values 218 * 219 * @see #flushCaches(int) 220 */ 221 static final int FLUSH_CACHES_MODERATE = 1; 222 223 /** 224 * Must match Caches::FlushMode values 225 * 226 * @see #flushCaches(int) 227 */ 228 static final int FLUSH_CACHES_FULL = 2; 229 230 /////////////////////////////////////////////////////////////////////////// 231 // Display list 232 /////////////////////////////////////////////////////////////////////////// 233 234 protected static native long nFinishRecording(long renderer); 235 236 @Override 237 public int drawDisplayList(RenderNode displayList, Rect dirty, int flags) { 238 return nDrawDisplayList(mRenderer, displayList.getNativeDisplayList(), 239 dirty, flags); 240 } 241 242 private static native int nDrawDisplayList(long renderer, long displayList, 243 Rect dirty, int flags); 244 245 /////////////////////////////////////////////////////////////////////////// 246 // Hardware layer 247 /////////////////////////////////////////////////////////////////////////// 248 249 void drawHardwareLayer(HardwareLayer layer, float x, float y, Paint paint) { 250 layer.setLayerPaint(paint); 251 nDrawLayer(mRenderer, layer.getLayer(), x, y); 252 } 253 254 private static native void nDrawLayer(long renderer, long layer, float x, float y); 255 256 /////////////////////////////////////////////////////////////////////////// 257 // Support 258 /////////////////////////////////////////////////////////////////////////// 259 260 private Rect getInternalClipBounds() { 261 if (mClipBounds == null) mClipBounds = new Rect(); 262 return mClipBounds; 263 } 264 265 266 private RectF getPathBounds() { 267 if (mPathBounds == null) mPathBounds = new RectF(); 268 return mPathBounds; 269 } 270 271 private float[] getPointStorage() { 272 if (mPoint == null) mPoint = new float[2]; 273 return mPoint; 274 } 275 276 private float[] getLineStorage() { 277 if (mLine == null) mLine = new float[4]; 278 return mLine; 279 } 280 281 /////////////////////////////////////////////////////////////////////////// 282 // Clipping 283 /////////////////////////////////////////////////////////////////////////// 284 285 @Override 286 public boolean clipPath(Path path) { 287 return nClipPath(mRenderer, path.mNativePath, Region.Op.INTERSECT.nativeInt); 288 } 289 290 @Override 291 public boolean clipPath(Path path, Region.Op op) { 292 return nClipPath(mRenderer, path.mNativePath, op.nativeInt); 293 } 294 295 private static native boolean nClipPath(long renderer, long path, int op); 296 297 @Override 298 public boolean clipRect(float left, float top, float right, float bottom) { 299 return nClipRect(mRenderer, left, top, right, bottom, Region.Op.INTERSECT.nativeInt); 300 } 301 302 private static native boolean nClipRect(long renderer, float left, float top, 303 float right, float bottom, int op); 304 305 @Override 306 public boolean clipRect(float left, float top, float right, float bottom, Region.Op op) { 307 return nClipRect(mRenderer, left, top, right, bottom, op.nativeInt); 308 } 309 310 @Override 311 public boolean clipRect(int left, int top, int right, int bottom) { 312 return nClipRect(mRenderer, left, top, right, bottom, Region.Op.INTERSECT.nativeInt); 313 } 314 315 private static native boolean nClipRect(long renderer, int left, int top, 316 int right, int bottom, int op); 317 318 @Override 319 public boolean clipRect(Rect rect) { 320 return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, 321 Region.Op.INTERSECT.nativeInt); 322 } 323 324 @Override 325 public boolean clipRect(Rect rect, Region.Op op) { 326 return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, op.nativeInt); 327 } 328 329 @Override 330 public boolean clipRect(RectF rect) { 331 return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, 332 Region.Op.INTERSECT.nativeInt); 333 } 334 335 @Override 336 public boolean clipRect(RectF rect, Region.Op op) { 337 return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, op.nativeInt); 338 } 339 340 @Override 341 public boolean clipRegion(Region region) { 342 return nClipRegion(mRenderer, region.mNativeRegion, Region.Op.INTERSECT.nativeInt); 343 } 344 345 @Override 346 public boolean clipRegion(Region region, Region.Op op) { 347 return nClipRegion(mRenderer, region.mNativeRegion, op.nativeInt); 348 } 349 350 private static native boolean nClipRegion(long renderer, long region, int op); 351 352 @Override 353 public boolean getClipBounds(Rect bounds) { 354 return nGetClipBounds(mRenderer, bounds); 355 } 356 357 private static native boolean nGetClipBounds(long renderer, Rect bounds); 358 359 @Override 360 public boolean quickReject(float left, float top, float right, float bottom, EdgeType type) { 361 return nQuickReject(mRenderer, left, top, right, bottom); 362 } 363 364 private static native boolean nQuickReject(long renderer, float left, float top, 365 float right, float bottom); 366 367 @Override 368 public boolean quickReject(Path path, EdgeType type) { 369 RectF pathBounds = getPathBounds(); 370 path.computeBounds(pathBounds, true); 371 return nQuickReject(mRenderer, pathBounds.left, pathBounds.top, 372 pathBounds.right, pathBounds.bottom); 373 } 374 375 @Override 376 public boolean quickReject(RectF rect, EdgeType type) { 377 return nQuickReject(mRenderer, rect.left, rect.top, rect.right, rect.bottom); 378 } 379 380 /////////////////////////////////////////////////////////////////////////// 381 // Transformations 382 /////////////////////////////////////////////////////////////////////////// 383 384 @Override 385 public void translate(float dx, float dy) { 386 if (dx != 0.0f || dy != 0.0f) nTranslate(mRenderer, dx, dy); 387 } 388 389 private static native void nTranslate(long renderer, float dx, float dy); 390 391 @Override 392 public void skew(float sx, float sy) { 393 nSkew(mRenderer, sx, sy); 394 } 395 396 private static native void nSkew(long renderer, float sx, float sy); 397 398 @Override 399 public void rotate(float degrees) { 400 nRotate(mRenderer, degrees); 401 } 402 403 private static native void nRotate(long renderer, float degrees); 404 405 @Override 406 public void scale(float sx, float sy) { 407 nScale(mRenderer, sx, sy); 408 } 409 410 private static native void nScale(long renderer, float sx, float sy); 411 412 @Override 413 public void setMatrix(Matrix matrix) { 414 nSetMatrix(mRenderer, matrix == null ? 0 : matrix.native_instance); 415 } 416 417 private static native void nSetMatrix(long renderer, long matrix); 418 419 @SuppressWarnings("deprecation") 420 @Override 421 public void getMatrix(Matrix matrix) { 422 nGetMatrix(mRenderer, matrix.native_instance); 423 } 424 425 private static native void nGetMatrix(long renderer, long matrix); 426 427 @Override 428 public void concat(Matrix matrix) { 429 if (matrix != null) nConcatMatrix(mRenderer, matrix.native_instance); 430 } 431 432 private static native void nConcatMatrix(long renderer, long matrix); 433 434 /////////////////////////////////////////////////////////////////////////// 435 // State management 436 /////////////////////////////////////////////////////////////////////////// 437 438 @Override 439 public int save() { 440 return nSave(mRenderer, Canvas.CLIP_SAVE_FLAG | Canvas.MATRIX_SAVE_FLAG); 441 } 442 443 @Override 444 public int save(int saveFlags) { 445 return nSave(mRenderer, saveFlags); 446 } 447 448 private static native int nSave(long renderer, int flags); 449 450 @Override 451 public int saveLayer(RectF bounds, Paint paint, int saveFlags) { 452 if (bounds != null) { 453 return saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, paint, saveFlags); 454 } 455 456 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 457 return nSaveLayer(mRenderer, nativePaint, saveFlags); 458 } 459 460 private static native int nSaveLayer(long renderer, long paint, int saveFlags); 461 462 @Override 463 public int saveLayer(float left, float top, float right, float bottom, Paint paint, 464 int saveFlags) { 465 if (left < right && top < bottom) { 466 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 467 return nSaveLayer(mRenderer, left, top, right, bottom, nativePaint, saveFlags); 468 } 469 return save(saveFlags); 470 } 471 472 private static native int nSaveLayer(long renderer, float left, float top, 473 float right, float bottom, long paint, int saveFlags); 474 475 @Override 476 public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags) { 477 if (bounds != null) { 478 return saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom, 479 alpha, saveFlags); 480 } 481 return nSaveLayerAlpha(mRenderer, alpha, saveFlags); 482 } 483 484 private static native int nSaveLayerAlpha(long renderer, int alpha, int saveFlags); 485 486 @Override 487 public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, 488 int saveFlags) { 489 if (left < right && top < bottom) { 490 return nSaveLayerAlpha(mRenderer, left, top, right, bottom, alpha, saveFlags); 491 } 492 return save(saveFlags); 493 } 494 495 private static native int nSaveLayerAlpha(long renderer, float left, float top, float right, 496 float bottom, int alpha, int saveFlags); 497 498 @Override 499 public void restore() { 500 nRestore(mRenderer); 501 } 502 503 private static native void nRestore(long renderer); 504 505 @Override 506 public void restoreToCount(int saveCount) { 507 nRestoreToCount(mRenderer, saveCount); 508 } 509 510 private static native void nRestoreToCount(long renderer, int saveCount); 511 512 @Override 513 public int getSaveCount() { 514 return nGetSaveCount(mRenderer); 515 } 516 517 private static native int nGetSaveCount(long renderer); 518 519 /////////////////////////////////////////////////////////////////////////// 520 // Filtering 521 /////////////////////////////////////////////////////////////////////////// 522 523 @Override 524 public void setDrawFilter(DrawFilter filter) { 525 mFilter = filter; 526 if (filter == null) { 527 nResetPaintFilter(mRenderer); 528 } else if (filter instanceof PaintFlagsDrawFilter) { 529 PaintFlagsDrawFilter flagsFilter = (PaintFlagsDrawFilter) filter; 530 nSetupPaintFilter(mRenderer, flagsFilter.clearBits, flagsFilter.setBits); 531 } 532 } 533 534 private static native void nResetPaintFilter(long renderer); 535 private static native void nSetupPaintFilter(long renderer, int clearBits, int setBits); 536 537 @Override 538 public DrawFilter getDrawFilter() { 539 return mFilter; 540 } 541 542 /////////////////////////////////////////////////////////////////////////// 543 // Drawing 544 /////////////////////////////////////////////////////////////////////////// 545 546 @Override 547 public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, 548 Paint paint) { 549 nDrawArc(mRenderer, oval.left, oval.top, oval.right, oval.bottom, 550 startAngle, sweepAngle, useCenter, paint.mNativePaint); 551 } 552 553 private static native void nDrawArc(long renderer, float left, float top, 554 float right, float bottom, float startAngle, float sweepAngle, 555 boolean useCenter, long paint); 556 557 @Override 558 public void drawARGB(int a, int r, int g, int b) { 559 drawColor((a & 0xFF) << 24 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF)); 560 } 561 562 @Override 563 public void drawPatch(NinePatch patch, Rect dst, Paint paint) { 564 Bitmap bitmap = patch.getBitmap(); 565 throwIfCannotDraw(bitmap); 566 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 567 nDrawPatch(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, patch.mNativeChunk, 568 dst.left, dst.top, dst.right, dst.bottom, nativePaint); 569 } 570 571 @Override 572 public void drawPatch(NinePatch patch, RectF dst, Paint paint) { 573 Bitmap bitmap = patch.getBitmap(); 574 throwIfCannotDraw(bitmap); 575 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 576 nDrawPatch(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, patch.mNativeChunk, 577 dst.left, dst.top, dst.right, dst.bottom, nativePaint); 578 } 579 580 private static native void nDrawPatch(long renderer, long bitmap, byte[] buffer, long chunk, 581 float left, float top, float right, float bottom, long paint); 582 583 @Override 584 public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) { 585 throwIfCannotDraw(bitmap); 586 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 587 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, nativePaint); 588 } 589 590 private static native void nDrawBitmap(long renderer, long bitmap, byte[] buffer, 591 float left, float top, long paint); 592 593 @Override 594 public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) { 595 throwIfCannotDraw(bitmap); 596 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 597 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, 598 matrix.native_instance, nativePaint); 599 } 600 601 private static native void nDrawBitmap(long renderer, long bitmap, byte[] buffer, 602 long matrix, long paint); 603 604 @Override 605 public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) { 606 throwIfCannotDraw(bitmap); 607 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 608 609 int left, top, right, bottom; 610 if (src == null) { 611 left = top = 0; 612 right = bitmap.getWidth(); 613 bottom = bitmap.getHeight(); 614 } else { 615 left = src.left; 616 right = src.right; 617 top = src.top; 618 bottom = src.bottom; 619 } 620 621 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, right, bottom, 622 dst.left, dst.top, dst.right, dst.bottom, nativePaint); 623 } 624 625 @Override 626 public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) { 627 throwIfCannotDraw(bitmap); 628 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 629 630 float left, top, right, bottom; 631 if (src == null) { 632 left = top = 0; 633 right = bitmap.getWidth(); 634 bottom = bitmap.getHeight(); 635 } else { 636 left = src.left; 637 right = src.right; 638 top = src.top; 639 bottom = src.bottom; 640 } 641 642 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, right, bottom, 643 dst.left, dst.top, dst.right, dst.bottom, nativePaint); 644 } 645 646 private static native void nDrawBitmap(long renderer, long bitmap, byte[] buffer, 647 float srcLeft, float srcTop, float srcRight, float srcBottom, 648 float left, float top, float right, float bottom, long paint); 649 650 @Override 651 public void drawBitmap(int[] colors, int offset, int stride, float x, float y, 652 int width, int height, boolean hasAlpha, Paint paint) { 653 if (width < 0) { 654 throw new IllegalArgumentException("width must be >= 0"); 655 } 656 657 if (height < 0) { 658 throw new IllegalArgumentException("height must be >= 0"); 659 } 660 661 if (Math.abs(stride) < width) { 662 throw new IllegalArgumentException("abs(stride) must be >= width"); 663 } 664 665 int lastScanline = offset + (height - 1) * stride; 666 int length = colors.length; 667 668 if (offset < 0 || (offset + width > length) || lastScanline < 0 || 669 (lastScanline + width > length)) { 670 throw new ArrayIndexOutOfBoundsException(); 671 } 672 673 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 674 nDrawBitmap(mRenderer, colors, offset, stride, x, y, 675 width, height, hasAlpha, nativePaint); 676 } 677 678 private static native void nDrawBitmap(long renderer, int[] colors, int offset, int stride, 679 float x, float y, int width, int height, boolean hasAlpha, long nativePaint); 680 681 @Override 682 public void drawBitmap(int[] colors, int offset, int stride, int x, int y, 683 int width, int height, boolean hasAlpha, Paint paint) { 684 drawBitmap(colors, offset, stride, (float) x, (float) y, width, height, hasAlpha, paint); 685 } 686 687 @Override 688 public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, 689 int vertOffset, int[] colors, int colorOffset, Paint paint) { 690 throwIfCannotDraw(bitmap); 691 if (meshWidth < 0 || meshHeight < 0 || vertOffset < 0 || colorOffset < 0) { 692 throw new ArrayIndexOutOfBoundsException(); 693 } 694 695 if (meshWidth == 0 || meshHeight == 0) { 696 return; 697 } 698 699 final int count = (meshWidth + 1) * (meshHeight + 1); 700 checkRange(verts.length, vertOffset, count * 2); 701 702 if (colors != null) { 703 checkRange(colors.length, colorOffset, count); 704 } 705 706 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 707 nDrawBitmapMesh(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, meshWidth, meshHeight, 708 verts, vertOffset, colors, colorOffset, nativePaint); 709 } 710 711 private static native void nDrawBitmapMesh(long renderer, long bitmap, byte[] buffer, 712 int meshWidth, int meshHeight, float[] verts, int vertOffset, 713 int[] colors, int colorOffset, long paint); 714 715 @Override 716 public void drawCircle(float cx, float cy, float radius, Paint paint) { 717 nDrawCircle(mRenderer, cx, cy, radius, paint.mNativePaint); 718 } 719 720 private static native void nDrawCircle(long renderer, float cx, float cy, 721 float radius, long paint); 722 723 @Override 724 public void drawCircle(CanvasProperty<Float> cx, CanvasProperty<Float> cy, 725 CanvasProperty<Float> radius, CanvasProperty<Paint> paint) { 726 nDrawCircle(mRenderer, cx.getNativeContainer(), cy.getNativeContainer(), 727 radius.getNativeContainer(), paint.getNativeContainer()); 728 } 729 730 private static native void nDrawCircle(long renderer, long propCx, 731 long propCy, long propRadius, long propPaint); 732 733 @Override 734 public void drawColor(int color) { 735 drawColor(color, PorterDuff.Mode.SRC_OVER); 736 } 737 738 @Override 739 public void drawColor(int color, PorterDuff.Mode mode) { 740 nDrawColor(mRenderer, color, mode.nativeInt); 741 } 742 743 private static native void nDrawColor(long renderer, int color, int mode); 744 745 @Override 746 public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) { 747 float[] line = getLineStorage(); 748 line[0] = startX; 749 line[1] = startY; 750 line[2] = stopX; 751 line[3] = stopY; 752 drawLines(line, 0, 4, paint); 753 } 754 755 @Override 756 public void drawLines(float[] pts, int offset, int count, Paint paint) { 757 if (count < 4) return; 758 759 if ((offset | count) < 0 || offset + count > pts.length) { 760 throw new IllegalArgumentException("The lines array must contain 4 elements per line."); 761 } 762 nDrawLines(mRenderer, pts, offset, count, paint.mNativePaint); 763 } 764 765 private static native void nDrawLines(long renderer, float[] points, 766 int offset, int count, long paint); 767 768 @Override 769 public void drawLines(float[] pts, Paint paint) { 770 drawLines(pts, 0, pts.length, paint); 771 } 772 773 @Override 774 public void drawOval(RectF oval, Paint paint) { 775 nDrawOval(mRenderer, oval.left, oval.top, oval.right, oval.bottom, paint.mNativePaint); 776 } 777 778 private static native void nDrawOval(long renderer, float left, float top, 779 float right, float bottom, long paint); 780 781 @Override 782 public void drawPaint(Paint paint) { 783 final Rect r = getInternalClipBounds(); 784 nGetClipBounds(mRenderer, r); 785 drawRect(r.left, r.top, r.right, r.bottom, paint); 786 } 787 788 @Override 789 public void drawPath(Path path, Paint paint) { 790 if (path.isSimplePath) { 791 if (path.rects != null) { 792 nDrawRects(mRenderer, path.rects.mNativeRegion, paint.mNativePaint); 793 } 794 } else { 795 nDrawPath(mRenderer, path.mNativePath, paint.mNativePaint); 796 } 797 } 798 799 private static native void nDrawPath(long renderer, long path, long paint); 800 private static native void nDrawRects(long renderer, long region, long paint); 801 802 @Override 803 public void drawPicture(Picture picture) { 804 picture.endRecording(); 805 // TODO: Implement rendering 806 } 807 808 @Override 809 public void drawPicture(Picture picture, Rect dst) { 810 save(); 811 translate(dst.left, dst.top); 812 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 813 scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight()); 814 } 815 drawPicture(picture); 816 restore(); 817 } 818 819 @Override 820 public void drawPicture(Picture picture, RectF dst) { 821 save(); 822 translate(dst.left, dst.top); 823 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 824 scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight()); 825 } 826 drawPicture(picture); 827 restore(); 828 } 829 830 @Override 831 public void drawPoint(float x, float y, Paint paint) { 832 float[] point = getPointStorage(); 833 point[0] = x; 834 point[1] = y; 835 drawPoints(point, 0, 2, paint); 836 } 837 838 @Override 839 public void drawPoints(float[] pts, Paint paint) { 840 drawPoints(pts, 0, pts.length, paint); 841 } 842 843 @Override 844 public void drawPoints(float[] pts, int offset, int count, Paint paint) { 845 if (count < 2) return; 846 847 nDrawPoints(mRenderer, pts, offset, count, paint.mNativePaint); 848 } 849 850 private static native void nDrawPoints(long renderer, float[] points, 851 int offset, int count, long paint); 852 853 @SuppressWarnings("deprecation") 854 @Override 855 public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) { 856 if (index < 0 || index + count > text.length || count * 2 > pos.length) { 857 throw new IndexOutOfBoundsException(); 858 } 859 860 nDrawPosText(mRenderer, text, index, count, pos, paint.mNativePaint); 861 } 862 863 private static native void nDrawPosText(long renderer, char[] text, int index, int count, 864 float[] pos, long paint); 865 866 @SuppressWarnings("deprecation") 867 @Override 868 public void drawPosText(String text, float[] pos, Paint paint) { 869 if (text.length() * 2 > pos.length) { 870 throw new ArrayIndexOutOfBoundsException(); 871 } 872 873 nDrawPosText(mRenderer, text, 0, text.length(), pos, paint.mNativePaint); 874 } 875 876 private static native void nDrawPosText(long renderer, String text, int start, int end, 877 float[] pos, long paint); 878 879 @Override 880 public void drawRect(float left, float top, float right, float bottom, Paint paint) { 881 if (left == right || top == bottom) return; 882 nDrawRect(mRenderer, left, top, right, bottom, paint.mNativePaint); 883 } 884 885 private static native void nDrawRect(long renderer, float left, float top, 886 float right, float bottom, long paint); 887 888 @Override 889 public void drawRect(Rect r, Paint paint) { 890 drawRect(r.left, r.top, r.right, r.bottom, paint); 891 } 892 893 @Override 894 public void drawRect(RectF r, Paint paint) { 895 drawRect(r.left, r.top, r.right, r.bottom, paint); 896 } 897 898 @Override 899 public void drawRGB(int r, int g, int b) { 900 drawColor(0xFF000000 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF)); 901 } 902 903 @Override 904 public void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, 905 Paint paint) { 906 nDrawRoundRect(mRenderer, left, top, right, bottom, rx, ry, paint.mNativePaint); 907 } 908 909 private static native void nDrawRoundRect(long renderer, float left, float top, 910 float right, float bottom, float rx, float y, long paint); 911 912 @Override 913 public void drawText(char[] text, int index, int count, float x, float y, Paint paint) { 914 if ((index | count | (index + count) | (text.length - index - count)) < 0) { 915 throw new IndexOutOfBoundsException(); 916 } 917 918 nDrawText(mRenderer, text, index, count, x, y, 919 paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface); 920 } 921 922 private static native void nDrawText(long renderer, char[] text, int index, int count, 923 float x, float y, int bidiFlags, long paint, long typeface); 924 925 @Override 926 public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) { 927 if (text instanceof String || text instanceof SpannedString || 928 text instanceof SpannableString) { 929 nDrawText(mRenderer, text.toString(), start, end, x, y, paint.mBidiFlags, 930 paint.mNativePaint, paint.mNativeTypeface); 931 } else if (text instanceof GraphicsOperations) { 932 ((GraphicsOperations) text).drawText(this, start, end, x, y, paint); 933 } else { 934 char[] buf = TemporaryBuffer.obtain(end - start); 935 TextUtils.getChars(text, start, end, buf, 0); 936 nDrawText(mRenderer, buf, 0, end - start, x, y, 937 paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface); 938 TemporaryBuffer.recycle(buf); 939 } 940 } 941 942 @Override 943 public void drawText(String text, int start, int end, float x, float y, Paint paint) { 944 if ((start | end | (end - start) | (text.length() - end)) < 0) { 945 throw new IndexOutOfBoundsException(); 946 } 947 948 nDrawText(mRenderer, text, start, end, x, y, 949 paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface); 950 } 951 952 private static native void nDrawText(long renderer, String text, int start, int end, 953 float x, float y, int bidiFlags, long paint, long typeface); 954 955 @Override 956 public void drawText(String text, float x, float y, Paint paint) { 957 nDrawText(mRenderer, text, 0, text.length(), x, y, 958 paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface); 959 } 960 961 @Override 962 public void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, 963 float vOffset, Paint paint) { 964 if (index < 0 || index + count > text.length) { 965 throw new ArrayIndexOutOfBoundsException(); 966 } 967 968 nDrawTextOnPath(mRenderer, text, index, count, path.mNativePath, hOffset, vOffset, 969 paint.mBidiFlags, paint.mNativePaint); 970 } 971 972 private static native void nDrawTextOnPath(long renderer, char[] text, int index, int count, 973 long path, float hOffset, float vOffset, int bidiFlags, long nativePaint); 974 975 @Override 976 public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) { 977 if (text.length() == 0) return; 978 979 nDrawTextOnPath(mRenderer, text, 0, text.length(), path.mNativePath, hOffset, vOffset, 980 paint.mBidiFlags, paint.mNativePaint); 981 } 982 983 private static native void nDrawTextOnPath(long renderer, String text, int start, int end, 984 long path, float hOffset, float vOffset, int bidiFlags, long nativePaint); 985 986 @Override 987 public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount, 988 float x, float y, int dir, Paint paint) { 989 if ((index | count | text.length - index - count) < 0) { 990 throw new IndexOutOfBoundsException(); 991 } 992 if (dir != DIRECTION_LTR && dir != DIRECTION_RTL) { 993 throw new IllegalArgumentException("Unknown direction: " + dir); 994 } 995 996 nDrawTextRun(mRenderer, text, index, count, contextIndex, contextCount, x, y, dir, 997 paint.mNativePaint, paint.mNativeTypeface); 998 } 999 1000 private static native void nDrawTextRun(long renderer, char[] text, int index, int count, 1001 int contextIndex, int contextCount, float x, float y, int dir, long nativePaint, long nativeTypeface); 1002 1003 @Override 1004 public void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd, 1005 float x, float y, int dir, Paint paint) { 1006 if ((start | end | end - start | text.length() - end) < 0) { 1007 throw new IndexOutOfBoundsException(); 1008 } 1009 1010 int flags = dir == 0 ? 0 : 1; 1011 if (text instanceof String || text instanceof SpannedString || 1012 text instanceof SpannableString) { 1013 nDrawTextRun(mRenderer, text.toString(), start, end, contextStart, 1014 contextEnd, x, y, flags, paint.mNativePaint, paint.mNativeTypeface); 1015 } else if (text instanceof GraphicsOperations) { 1016 ((GraphicsOperations) text).drawTextRun(this, start, end, 1017 contextStart, contextEnd, x, y, flags, paint); 1018 } else { 1019 int contextLen = contextEnd - contextStart; 1020 int len = end - start; 1021 char[] buf = TemporaryBuffer.obtain(contextLen); 1022 TextUtils.getChars(text, contextStart, contextEnd, buf, 0); 1023 nDrawTextRun(mRenderer, buf, start - contextStart, len, 0, contextLen, 1024 x, y, flags, paint.mNativePaint, paint.mNativeTypeface); 1025 TemporaryBuffer.recycle(buf); 1026 } 1027 } 1028 1029 private static native void nDrawTextRun(long renderer, String text, int start, int end, 1030 int contextStart, int contextEnd, float x, float y, int flags, long nativePaint, long nativeTypeface); 1031 1032 @Override 1033 public void drawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset, 1034 float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices, 1035 int indexOffset, int indexCount, Paint paint) { 1036 // TODO: Implement 1037 } 1038} 1039