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