GLES20Canvas.java revision cce47eb580d666ead1f6095d1e3b65233592bbaa
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, 172 int width, int height); 173 174 @Override 175 public void setHighContrastText(boolean highContrastText) { 176 nSetHighContrastText(mRenderer, highContrastText); 177 } 178 179 private static native void nSetHighContrastText(long renderer, boolean highContrastText); 180 181 @Override 182 public int onPreDraw(Rect dirty) { 183 if (dirty != null) { 184 return nPrepareDirty(mRenderer, dirty.left, dirty.top, dirty.right, dirty.bottom, 185 mOpaque); 186 } else { 187 return nPrepare(mRenderer, mOpaque); 188 } 189 } 190 191 private static native int nPrepare(long renderer, boolean opaque); 192 private static native int nPrepareDirty(long renderer, int left, int top, int right, int bottom, 193 boolean opaque); 194 195 @Override 196 public void onPostDraw() { 197 nFinish(mRenderer); 198 } 199 200 private static native void nFinish(long renderer); 201 202 /////////////////////////////////////////////////////////////////////////// 203 // Functor 204 /////////////////////////////////////////////////////////////////////////// 205 206 @Override 207 public int callDrawGLFunction(long drawGLFunction) { 208 return nCallDrawGLFunction(mRenderer, drawGLFunction); 209 } 210 211 private static native int nCallDrawGLFunction(long renderer, long drawGLFunction); 212 213 /////////////////////////////////////////////////////////////////////////// 214 // Display list 215 /////////////////////////////////////////////////////////////////////////// 216 217 protected static native long nFinishRecording(long renderer); 218 219 @Override 220 public int drawRenderNode(RenderNode renderNode, Rect dirty, int flags) { 221 return nDrawRenderNode(mRenderer, renderNode.getNativeDisplayList(), dirty, flags); 222 } 223 224 private static native int nDrawRenderNode(long renderer, long renderNode, 225 Rect dirty, int flags); 226 227 /////////////////////////////////////////////////////////////////////////// 228 // Hardware layer 229 /////////////////////////////////////////////////////////////////////////// 230 231 void drawHardwareLayer(HardwareLayer layer, float x, float y, Paint paint) { 232 layer.setLayerPaint(paint); 233 nDrawLayer(mRenderer, layer.getLayer(), x, y); 234 } 235 236 private static native void nDrawLayer(long renderer, long layer, float x, float y); 237 238 /////////////////////////////////////////////////////////////////////////// 239 // Support 240 /////////////////////////////////////////////////////////////////////////// 241 242 private Rect getInternalClipBounds() { 243 if (mClipBounds == null) mClipBounds = new Rect(); 244 return mClipBounds; 245 } 246 247 248 private RectF getPathBounds() { 249 if (mPathBounds == null) mPathBounds = new RectF(); 250 return mPathBounds; 251 } 252 253 private float[] getPointStorage() { 254 if (mPoint == null) mPoint = new float[2]; 255 return mPoint; 256 } 257 258 private float[] getLineStorage() { 259 if (mLine == null) mLine = new float[4]; 260 return mLine; 261 } 262 263 /////////////////////////////////////////////////////////////////////////// 264 // Clipping 265 /////////////////////////////////////////////////////////////////////////// 266 267 @Override 268 public boolean clipPath(Path path) { 269 return nClipPath(mRenderer, path.mNativePath, Region.Op.INTERSECT.nativeInt); 270 } 271 272 @Override 273 public boolean clipPath(Path path, Region.Op op) { 274 return nClipPath(mRenderer, path.mNativePath, op.nativeInt); 275 } 276 277 private static native boolean nClipPath(long renderer, long path, int op); 278 279 @Override 280 public boolean clipRect(float left, float top, float right, float bottom) { 281 return nClipRect(mRenderer, left, top, right, bottom, Region.Op.INTERSECT.nativeInt); 282 } 283 284 private static native boolean nClipRect(long renderer, float left, float top, 285 float right, float bottom, int op); 286 287 @Override 288 public boolean clipRect(float left, float top, float right, float bottom, Region.Op op) { 289 return nClipRect(mRenderer, left, top, right, bottom, op.nativeInt); 290 } 291 292 @Override 293 public boolean clipRect(int left, int top, int right, int bottom) { 294 return nClipRect(mRenderer, left, top, right, bottom, Region.Op.INTERSECT.nativeInt); 295 } 296 297 private static native boolean nClipRect(long renderer, int left, int top, 298 int right, int bottom, int op); 299 300 @Override 301 public boolean clipRect(Rect rect) { 302 return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, 303 Region.Op.INTERSECT.nativeInt); 304 } 305 306 @Override 307 public boolean clipRect(Rect rect, Region.Op op) { 308 return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, op.nativeInt); 309 } 310 311 @Override 312 public boolean clipRect(RectF rect) { 313 return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, 314 Region.Op.INTERSECT.nativeInt); 315 } 316 317 @Override 318 public boolean clipRect(RectF rect, Region.Op op) { 319 return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, op.nativeInt); 320 } 321 322 @Override 323 public boolean clipRegion(Region region) { 324 return nClipRegion(mRenderer, region.mNativeRegion, Region.Op.INTERSECT.nativeInt); 325 } 326 327 @Override 328 public boolean clipRegion(Region region, Region.Op op) { 329 return nClipRegion(mRenderer, region.mNativeRegion, op.nativeInt); 330 } 331 332 private static native boolean nClipRegion(long renderer, long region, int op); 333 334 @Override 335 public boolean getClipBounds(Rect bounds) { 336 return nGetClipBounds(mRenderer, bounds); 337 } 338 339 private static native boolean nGetClipBounds(long renderer, Rect bounds); 340 341 @Override 342 public boolean quickReject(float left, float top, float right, float bottom, EdgeType type) { 343 return nQuickReject(mRenderer, left, top, right, bottom); 344 } 345 346 private static native boolean nQuickReject(long renderer, float left, float top, 347 float right, float bottom); 348 349 @Override 350 public boolean quickReject(Path path, EdgeType type) { 351 RectF pathBounds = getPathBounds(); 352 path.computeBounds(pathBounds, true); 353 return nQuickReject(mRenderer, pathBounds.left, pathBounds.top, 354 pathBounds.right, pathBounds.bottom); 355 } 356 357 @Override 358 public boolean quickReject(RectF rect, EdgeType type) { 359 return nQuickReject(mRenderer, rect.left, rect.top, rect.right, rect.bottom); 360 } 361 362 /////////////////////////////////////////////////////////////////////////// 363 // Transformations 364 /////////////////////////////////////////////////////////////////////////// 365 366 @Override 367 public void translate(float dx, float dy) { 368 if (dx != 0.0f || dy != 0.0f) nTranslate(mRenderer, dx, dy); 369 } 370 371 private static native void nTranslate(long renderer, float dx, float dy); 372 373 @Override 374 public void skew(float sx, float sy) { 375 nSkew(mRenderer, sx, sy); 376 } 377 378 private static native void nSkew(long renderer, float sx, float sy); 379 380 @Override 381 public void rotate(float degrees) { 382 nRotate(mRenderer, degrees); 383 } 384 385 private static native void nRotate(long renderer, float degrees); 386 387 @Override 388 public void scale(float sx, float sy) { 389 // TODO: remove 390 if (sx > 1000000 || sy > 1000000) throw new IllegalArgumentException("invalid scales passed " + sx + ", " + sy); 391 392 nScale(mRenderer, sx, sy); 393 } 394 395 private static native void nScale(long renderer, float sx, float sy); 396 397 @Override 398 public void setMatrix(Matrix matrix) { 399 nSetMatrix(mRenderer, matrix == null ? 0 : matrix.native_instance); 400 } 401 402 private static native void nSetMatrix(long renderer, long matrix); 403 404 @SuppressWarnings("deprecation") 405 @Override 406 public void getMatrix(Matrix matrix) { 407 nGetMatrix(mRenderer, matrix.native_instance); 408 } 409 410 private static native void nGetMatrix(long renderer, long matrix); 411 412 @Override 413 public void concat(Matrix matrix) { 414 if (matrix != null) nConcatMatrix(mRenderer, matrix.native_instance); 415 } 416 417 private static native void nConcatMatrix(long renderer, long matrix); 418 419 /////////////////////////////////////////////////////////////////////////// 420 // State management 421 /////////////////////////////////////////////////////////////////////////// 422 423 @Override 424 public int save() { 425 return nSave(mRenderer, Canvas.CLIP_SAVE_FLAG | Canvas.MATRIX_SAVE_FLAG); 426 } 427 428 @Override 429 public int save(int saveFlags) { 430 return nSave(mRenderer, saveFlags); 431 } 432 433 private static native int nSave(long renderer, int flags); 434 435 @Override 436 public int saveLayer(RectF bounds, Paint paint, int saveFlags) { 437 if (bounds != null) { 438 return saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, paint, saveFlags); 439 } 440 441 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 442 return nSaveLayer(mRenderer, nativePaint, saveFlags); 443 } 444 445 private static native int nSaveLayer(long renderer, long paint, int saveFlags); 446 447 @Override 448 public int saveLayer(float left, float top, float right, float bottom, Paint paint, 449 int saveFlags) { 450 if (left < right && top < bottom) { 451 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 452 return nSaveLayer(mRenderer, left, top, right, bottom, nativePaint, saveFlags); 453 } 454 return save(saveFlags); 455 } 456 457 private static native int nSaveLayer(long renderer, float left, float top, 458 float right, float bottom, long paint, int saveFlags); 459 460 @Override 461 public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags) { 462 if (bounds != null) { 463 return saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom, 464 alpha, saveFlags); 465 } 466 return nSaveLayerAlpha(mRenderer, alpha, saveFlags); 467 } 468 469 private static native int nSaveLayerAlpha(long renderer, int alpha, int saveFlags); 470 471 @Override 472 public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, 473 int saveFlags) { 474 if (left < right && top < bottom) { 475 return nSaveLayerAlpha(mRenderer, left, top, right, bottom, alpha, saveFlags); 476 } 477 return save(saveFlags); 478 } 479 480 private static native int nSaveLayerAlpha(long renderer, float left, float top, float right, 481 float bottom, int alpha, int saveFlags); 482 483 @Override 484 public void restore() { 485 nRestore(mRenderer); 486 } 487 488 private static native void nRestore(long renderer); 489 490 @Override 491 public void restoreToCount(int saveCount) { 492 nRestoreToCount(mRenderer, saveCount); 493 } 494 495 private static native void nRestoreToCount(long renderer, int saveCount); 496 497 @Override 498 public int getSaveCount() { 499 return nGetSaveCount(mRenderer); 500 } 501 502 private static native int nGetSaveCount(long renderer); 503 504 /////////////////////////////////////////////////////////////////////////// 505 // Filtering 506 /////////////////////////////////////////////////////////////////////////// 507 508 @Override 509 public void setDrawFilter(DrawFilter filter) { 510 mFilter = filter; 511 if (filter == null) { 512 nResetPaintFilter(mRenderer); 513 } else if (filter instanceof PaintFlagsDrawFilter) { 514 PaintFlagsDrawFilter flagsFilter = (PaintFlagsDrawFilter) filter; 515 nSetupPaintFilter(mRenderer, flagsFilter.clearBits, flagsFilter.setBits); 516 } 517 } 518 519 private static native void nResetPaintFilter(long renderer); 520 private static native void nSetupPaintFilter(long renderer, int clearBits, int setBits); 521 522 @Override 523 public DrawFilter getDrawFilter() { 524 return mFilter; 525 } 526 527 /////////////////////////////////////////////////////////////////////////// 528 // Drawing 529 /////////////////////////////////////////////////////////////////////////// 530 531 @Override 532 public void drawArc(float left, float top, float right, float bottom, 533 float startAngle, float sweepAngle, boolean useCenter, Paint paint) { 534 nDrawArc(mRenderer, left, top, right, bottom, 535 startAngle, sweepAngle, useCenter, paint.mNativePaint); 536 } 537 538 private static native void nDrawArc(long renderer, float left, float top, 539 float right, float bottom, float startAngle, float sweepAngle, 540 boolean useCenter, long paint); 541 542 @Override 543 public void drawARGB(int a, int r, int g, int b) { 544 drawColor((a & 0xFF) << 24 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF)); 545 } 546 547 @Override 548 public void drawPatch(NinePatch patch, Rect dst, Paint paint) { 549 Bitmap bitmap = patch.getBitmap(); 550 throwIfCannotDraw(bitmap); 551 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 552 nDrawPatch(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, patch.mNativeChunk, 553 dst.left, dst.top, dst.right, dst.bottom, nativePaint); 554 } 555 556 @Override 557 public void drawPatch(NinePatch patch, RectF dst, Paint paint) { 558 Bitmap bitmap = patch.getBitmap(); 559 throwIfCannotDraw(bitmap); 560 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 561 nDrawPatch(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, patch.mNativeChunk, 562 dst.left, dst.top, dst.right, dst.bottom, nativePaint); 563 } 564 565 private static native void nDrawPatch(long renderer, long bitmap, byte[] buffer, long chunk, 566 float left, float top, float right, float bottom, long paint); 567 568 @Override 569 public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) { 570 throwIfCannotDraw(bitmap); 571 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 572 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, nativePaint); 573 } 574 575 private static native void nDrawBitmap(long renderer, long bitmap, byte[] buffer, 576 float left, float top, long paint); 577 578 @Override 579 public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) { 580 throwIfCannotDraw(bitmap); 581 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 582 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, 583 matrix.native_instance, nativePaint); 584 } 585 586 private static native void nDrawBitmap(long renderer, long bitmap, byte[] buffer, 587 long matrix, long paint); 588 589 @Override 590 public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) { 591 throwIfCannotDraw(bitmap); 592 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 593 594 int left, top, right, bottom; 595 if (src == null) { 596 left = top = 0; 597 right = bitmap.getWidth(); 598 bottom = bitmap.getHeight(); 599 } else { 600 left = src.left; 601 right = src.right; 602 top = src.top; 603 bottom = src.bottom; 604 } 605 606 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, right, bottom, 607 dst.left, dst.top, dst.right, dst.bottom, nativePaint); 608 } 609 610 @Override 611 public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) { 612 throwIfCannotDraw(bitmap); 613 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 614 615 float left, top, right, bottom; 616 if (src == null) { 617 left = top = 0; 618 right = bitmap.getWidth(); 619 bottom = bitmap.getHeight(); 620 } else { 621 left = src.left; 622 right = src.right; 623 top = src.top; 624 bottom = src.bottom; 625 } 626 627 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, right, bottom, 628 dst.left, dst.top, dst.right, dst.bottom, nativePaint); 629 } 630 631 private static native void nDrawBitmap(long renderer, long bitmap, byte[] buffer, 632 float srcLeft, float srcTop, float srcRight, float srcBottom, 633 float left, float top, float right, float bottom, long paint); 634 635 @Override 636 public void drawBitmap(int[] colors, int offset, int stride, float x, float y, 637 int width, int height, boolean hasAlpha, Paint paint) { 638 if (width < 0) { 639 throw new IllegalArgumentException("width must be >= 0"); 640 } 641 642 if (height < 0) { 643 throw new IllegalArgumentException("height must be >= 0"); 644 } 645 646 if (Math.abs(stride) < width) { 647 throw new IllegalArgumentException("abs(stride) must be >= width"); 648 } 649 650 int lastScanline = offset + (height - 1) * stride; 651 int length = colors.length; 652 653 if (offset < 0 || (offset + width > length) || lastScanline < 0 || 654 (lastScanline + width > length)) { 655 throw new ArrayIndexOutOfBoundsException(); 656 } 657 658 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 659 nDrawBitmap(mRenderer, colors, offset, stride, x, y, 660 width, height, hasAlpha, nativePaint); 661 } 662 663 private static native void nDrawBitmap(long renderer, int[] colors, int offset, int stride, 664 float x, float y, int width, int height, boolean hasAlpha, long nativePaint); 665 666 @Override 667 public void drawBitmap(int[] colors, int offset, int stride, int x, int y, 668 int width, int height, boolean hasAlpha, Paint paint) { 669 drawBitmap(colors, offset, stride, (float) x, (float) y, width, height, hasAlpha, paint); 670 } 671 672 @Override 673 public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, 674 int vertOffset, int[] colors, int colorOffset, Paint paint) { 675 throwIfCannotDraw(bitmap); 676 if (meshWidth < 0 || meshHeight < 0 || vertOffset < 0 || colorOffset < 0) { 677 throw new ArrayIndexOutOfBoundsException(); 678 } 679 680 if (meshWidth == 0 || meshHeight == 0) { 681 return; 682 } 683 684 final int count = (meshWidth + 1) * (meshHeight + 1); 685 checkRange(verts.length, vertOffset, count * 2); 686 687 if (colors != null) { 688 checkRange(colors.length, colorOffset, count); 689 } 690 691 final long nativePaint = paint == null ? 0 : paint.mNativePaint; 692 nDrawBitmapMesh(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, meshWidth, meshHeight, 693 verts, vertOffset, colors, colorOffset, nativePaint); 694 } 695 696 private static native void nDrawBitmapMesh(long renderer, long bitmap, byte[] buffer, 697 int meshWidth, int meshHeight, float[] verts, int vertOffset, 698 int[] colors, int colorOffset, long paint); 699 700 @Override 701 public void drawCircle(float cx, float cy, float radius, Paint paint) { 702 nDrawCircle(mRenderer, cx, cy, radius, paint.mNativePaint); 703 } 704 705 private static native void nDrawCircle(long renderer, float cx, float cy, 706 float radius, long paint); 707 708 @Override 709 public void drawCircle(CanvasProperty<Float> cx, CanvasProperty<Float> cy, 710 CanvasProperty<Float> radius, CanvasProperty<Paint> paint) { 711 nDrawCircle(mRenderer, cx.getNativeContainer(), cy.getNativeContainer(), 712 radius.getNativeContainer(), paint.getNativeContainer()); 713 } 714 715 private static native void nDrawCircle(long renderer, long propCx, 716 long propCy, long propRadius, long propPaint); 717 718 @Override 719 public void drawColor(int color) { 720 drawColor(color, PorterDuff.Mode.SRC_OVER); 721 } 722 723 @Override 724 public void drawColor(int color, PorterDuff.Mode mode) { 725 nDrawColor(mRenderer, color, mode.nativeInt); 726 } 727 728 private static native void nDrawColor(long renderer, int color, int mode); 729 730 @Override 731 public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) { 732 float[] line = getLineStorage(); 733 line[0] = startX; 734 line[1] = startY; 735 line[2] = stopX; 736 line[3] = stopY; 737 drawLines(line, 0, 4, paint); 738 } 739 740 @Override 741 public void drawLines(float[] pts, int offset, int count, Paint paint) { 742 if (count < 4) return; 743 744 if ((offset | count) < 0 || offset + count > pts.length) { 745 throw new IllegalArgumentException("The lines array must contain 4 elements per line."); 746 } 747 nDrawLines(mRenderer, pts, offset, count, paint.mNativePaint); 748 } 749 750 private static native void nDrawLines(long renderer, float[] points, 751 int offset, int count, long paint); 752 753 @Override 754 public void drawLines(float[] pts, Paint paint) { 755 drawLines(pts, 0, pts.length, paint); 756 } 757 758 @Override 759 public void drawOval(float left, float top, float right, float bottom, Paint paint) { 760 nDrawOval(mRenderer, left, top, right, bottom, paint.mNativePaint); 761 } 762 763 private static native void nDrawOval(long renderer, float left, float top, 764 float right, float bottom, long paint); 765 766 @Override 767 public void drawPaint(Paint paint) { 768 final Rect r = getInternalClipBounds(); 769 nGetClipBounds(mRenderer, r); 770 drawRect(r.left, r.top, r.right, r.bottom, paint); 771 } 772 773 @Override 774 public void drawPath(Path path, Paint paint) { 775 if (path.isSimplePath) { 776 if (path.rects != null) { 777 nDrawRects(mRenderer, path.rects.mNativeRegion, paint.mNativePaint); 778 } 779 } else { 780 nDrawPath(mRenderer, path.mNativePath, paint.mNativePaint); 781 } 782 } 783 784 private static native void nDrawPath(long renderer, long path, long paint); 785 private static native void nDrawRects(long renderer, long region, long paint); 786 787 @Override 788 public void drawPicture(Picture picture) { 789 picture.endRecording(); 790 // TODO: Implement rendering 791 } 792 793 @Override 794 public void drawPicture(Picture picture, Rect dst) { 795 save(); 796 translate(dst.left, dst.top); 797 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 798 scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight()); 799 } 800 drawPicture(picture); 801 restore(); 802 } 803 804 @Override 805 public void drawPicture(Picture picture, RectF dst) { 806 save(); 807 translate(dst.left, dst.top); 808 if (picture.getWidth() > 0 && picture.getHeight() > 0) { 809 scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight()); 810 } 811 drawPicture(picture); 812 restore(); 813 } 814 815 @Override 816 public void drawPoint(float x, float y, Paint paint) { 817 float[] point = getPointStorage(); 818 point[0] = x; 819 point[1] = y; 820 drawPoints(point, 0, 2, paint); 821 } 822 823 @Override 824 public void drawPoints(float[] pts, Paint paint) { 825 drawPoints(pts, 0, pts.length, paint); 826 } 827 828 @Override 829 public void drawPoints(float[] pts, int offset, int count, Paint paint) { 830 if (count < 2) return; 831 832 nDrawPoints(mRenderer, pts, offset, count, paint.mNativePaint); 833 } 834 835 private static native void nDrawPoints(long renderer, float[] points, 836 int offset, int count, long paint); 837 838 // Note: drawPosText just uses implementation in Canvas 839 840 @Override 841 public void drawRect(float left, float top, float right, float bottom, Paint paint) { 842 if (left == right || top == bottom) return; 843 nDrawRect(mRenderer, left, top, right, bottom, paint.mNativePaint); 844 } 845 846 private static native void nDrawRect(long renderer, float left, float top, 847 float right, float bottom, long paint); 848 849 @Override 850 public void drawRect(Rect r, Paint paint) { 851 drawRect(r.left, r.top, r.right, r.bottom, paint); 852 } 853 854 @Override 855 public void drawRect(RectF r, Paint paint) { 856 drawRect(r.left, r.top, r.right, r.bottom, paint); 857 } 858 859 @Override 860 public void drawRGB(int r, int g, int b) { 861 drawColor(0xFF000000 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF)); 862 } 863 864 @Override 865 public void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, 866 Paint paint) { 867 nDrawRoundRect(mRenderer, left, top, right, bottom, rx, ry, paint.mNativePaint); 868 } 869 870 private static native void nDrawRoundRect(long renderer, float left, float top, 871 float right, float bottom, float rx, float y, long paint); 872 873 @Override 874 public void drawText(char[] text, int index, int count, float x, float y, Paint paint) { 875 if ((index | count | (index + count) | (text.length - index - count)) < 0) { 876 throw new IndexOutOfBoundsException(); 877 } 878 879 nDrawText(mRenderer, text, index, count, x, y, 880 paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface); 881 } 882 883 private static native void nDrawText(long renderer, char[] text, int index, int count, 884 float x, float y, int bidiFlags, long paint, long typeface); 885 886 @Override 887 public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) { 888 if (text instanceof String || text instanceof SpannedString || 889 text instanceof SpannableString) { 890 nDrawText(mRenderer, text.toString(), start, end, x, y, paint.mBidiFlags, 891 paint.mNativePaint, paint.mNativeTypeface); 892 } else if (text instanceof GraphicsOperations) { 893 ((GraphicsOperations) text).drawText(this, start, end, x, y, paint); 894 } else { 895 char[] buf = TemporaryBuffer.obtain(end - start); 896 TextUtils.getChars(text, start, end, buf, 0); 897 nDrawText(mRenderer, buf, 0, end - start, x, y, 898 paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface); 899 TemporaryBuffer.recycle(buf); 900 } 901 } 902 903 @Override 904 public void drawText(String text, int start, int end, float x, float y, Paint paint) { 905 if ((start | end | (end - start) | (text.length() - end)) < 0) { 906 throw new IndexOutOfBoundsException(); 907 } 908 909 nDrawText(mRenderer, text, start, end, x, y, 910 paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface); 911 } 912 913 private static native void nDrawText(long renderer, String text, int start, int end, 914 float x, float y, int bidiFlags, long paint, long typeface); 915 916 @Override 917 public void drawText(String text, float x, float y, Paint paint) { 918 nDrawText(mRenderer, text, 0, text.length(), x, y, 919 paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface); 920 } 921 922 @Override 923 public void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, 924 float vOffset, Paint paint) { 925 if (index < 0 || index + count > text.length) { 926 throw new ArrayIndexOutOfBoundsException(); 927 } 928 929 nDrawTextOnPath(mRenderer, text, index, count, path.mNativePath, hOffset, vOffset, 930 paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface); 931 } 932 933 private static native void nDrawTextOnPath(long renderer, char[] text, int index, int count, 934 long path, float hOffset, float vOffset, int bidiFlags, long nativePaint, 935 long typeface); 936 937 @Override 938 public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) { 939 if (text.length() == 0) return; 940 941 nDrawTextOnPath(mRenderer, text, 0, text.length(), path.mNativePath, hOffset, vOffset, 942 paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface); 943 } 944 945 private static native void nDrawTextOnPath(long renderer, String text, int start, int end, 946 long path, float hOffset, float vOffset, int bidiFlags, long nativePaint, 947 long typeface); 948 949 @Override 950 public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount, 951 float x, float y, boolean isRtl, Paint paint) { 952 if ((index | count | text.length - index - count) < 0) { 953 throw new IndexOutOfBoundsException(); 954 } 955 956 nDrawTextRun(mRenderer, text, index, count, contextIndex, contextCount, x, y, isRtl, 957 paint.mNativePaint, paint.mNativeTypeface); 958 } 959 960 private static native void nDrawTextRun(long renderer, char[] text, int index, int count, 961 int contextIndex, int contextCount, float x, float y, boolean isRtl, long nativePaint, long nativeTypeface); 962 963 @Override 964 public void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd, 965 float x, float y, boolean isRtl, Paint paint) { 966 if ((start | end | end - start | text.length() - end) < 0) { 967 throw new IndexOutOfBoundsException(); 968 } 969 970 if (text instanceof String || text instanceof SpannedString || 971 text instanceof SpannableString) { 972 nDrawTextRun(mRenderer, text.toString(), start, end, contextStart, 973 contextEnd, x, y, isRtl, paint.mNativePaint, paint.mNativeTypeface); 974 } else if (text instanceof GraphicsOperations) { 975 ((GraphicsOperations) text).drawTextRun(this, start, end, 976 contextStart, contextEnd, x, y, isRtl, paint); 977 } else { 978 int contextLen = contextEnd - contextStart; 979 int len = end - start; 980 char[] buf = TemporaryBuffer.obtain(contextLen); 981 TextUtils.getChars(text, contextStart, contextEnd, buf, 0); 982 nDrawTextRun(mRenderer, buf, start - contextStart, len, 0, contextLen, 983 x, y, isRtl, paint.mNativePaint, paint.mNativeTypeface); 984 TemporaryBuffer.recycle(buf); 985 } 986 } 987 988 private static native void nDrawTextRun(long renderer, String text, int start, int end, 989 int contextStart, int contextEnd, float x, float y, boolean isRtl, long nativePaint, long nativeTypeface); 990 991 @Override 992 public void drawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset, 993 float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices, 994 int indexOffset, int indexCount, Paint paint) { 995 // TODO: Implement 996 } 997} 998