GLES20Canvas.java revision deba785f122a47915756ffd991f5540d952cf937
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.DrawFilter; 22import android.graphics.Matrix; 23import android.graphics.Paint; 24import android.graphics.Path; 25import android.graphics.Picture; 26import android.graphics.PorterDuff; 27import android.graphics.Rect; 28import android.graphics.RectF; 29import android.graphics.Region; 30 31import javax.microedition.khronos.opengles.GL; 32 33/** 34 * An implementation of Canvas on top of OpenGL ES 2.0. 35 */ 36@SuppressWarnings({"deprecation"}) 37class GLES20Canvas extends Canvas { 38 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) 39 private final GL mGl; 40 private final boolean mOpaque; 41 private final int mRenderer; 42 43 private int mWidth; 44 private int mHeight; 45 46 private final float[] mPoint = new float[2]; 47 private final float[] mLine = new float[4]; 48 49 /////////////////////////////////////////////////////////////////////////// 50 // Constructors 51 /////////////////////////////////////////////////////////////////////////// 52 53 GLES20Canvas(GL gl, boolean translucent) { 54 mGl = gl; 55 mOpaque = !translucent; 56 57 mRenderer = nCreateRenderer(); 58 } 59 60 private native int nCreateRenderer(); 61 62 @Override 63 protected void finalize() throws Throwable { 64 try { 65 super.finalize(); 66 } finally { 67 nDestroyRenderer(mRenderer); 68 } 69 } 70 71 private native void nDestroyRenderer(int renderer); 72 73 /////////////////////////////////////////////////////////////////////////// 74 // Canvas management 75 /////////////////////////////////////////////////////////////////////////// 76 77 @Override 78 public boolean isHardwareAccelerated() { 79 return true; 80 } 81 82 @Override 83 public GL getGL() { 84 throw new UnsupportedOperationException(); 85 } 86 87 @Override 88 public void setBitmap(Bitmap bitmap) { 89 throw new UnsupportedOperationException(); 90 } 91 92 @Override 93 public boolean isOpaque() { 94 return mOpaque; 95 } 96 97 @Override 98 public int getWidth() { 99 return mWidth; 100 } 101 102 @Override 103 public int getHeight() { 104 return mHeight; 105 } 106 107 /////////////////////////////////////////////////////////////////////////// 108 // Setup 109 /////////////////////////////////////////////////////////////////////////// 110 111 @Override 112 public void setViewport(int width, int height) { 113 mWidth = width; 114 mHeight = height; 115 116 nSetViewport(mRenderer, width, height); 117 } 118 119 private native void nSetViewport(int renderer, int width, int height); 120 121 void onPreDraw() { 122 nPrepare(mRenderer); 123 } 124 125 private native void nPrepare(int renderer); 126 127 /////////////////////////////////////////////////////////////////////////// 128 // Clipping 129 /////////////////////////////////////////////////////////////////////////// 130 131 @Override 132 public boolean clipPath(Path path) { 133 throw new UnsupportedOperationException(); 134 } 135 136 @Override 137 public boolean clipPath(Path path, Region.Op op) { 138 throw new UnsupportedOperationException(); 139 } 140 141 @Override 142 public boolean clipRect(float left, float top, float right, float bottom) { 143 return nClipRect(mRenderer, left, top, right, bottom); 144 } 145 146 private native boolean nClipRect(int renderer, float left, float top, float right, float bottom); 147 148 @Override 149 public boolean clipRect(float left, float top, float right, float bottom, Region.Op op) { 150 throw new UnsupportedOperationException(); 151 } 152 153 @Override 154 public boolean clipRect(int left, int top, int right, int bottom) { 155 return nClipRect(mRenderer, left, top, right, bottom); 156 } 157 158 private native boolean nClipRect(int renderer, int left, int top, int right, int bottom); 159 160 @Override 161 public boolean clipRect(Rect rect) { 162 return clipRect(rect.left, rect.top, rect.right, rect.bottom); 163 } 164 165 @Override 166 public boolean clipRect(Rect rect, Region.Op op) { 167 throw new UnsupportedOperationException(); 168 } 169 170 @Override 171 public boolean clipRect(RectF rect) { 172 return clipRect(rect.left, rect.top, rect.right, rect.bottom); 173 } 174 175 @Override 176 public boolean clipRect(RectF rect, Region.Op op) { 177 throw new UnsupportedOperationException(); 178 } 179 180 @Override 181 public boolean clipRegion(Region region) { 182 throw new UnsupportedOperationException(); 183 } 184 185 @Override 186 public boolean clipRegion(Region region, Region.Op op) { 187 throw new UnsupportedOperationException(); 188 } 189 190 @Override 191 public boolean getClipBounds(Rect bounds) { 192 return nGetClipBounds(mRenderer, bounds); 193 } 194 195 private native boolean nGetClipBounds(int renderer, Rect bounds); 196 197 @Override 198 public boolean quickReject(float left, float top, float right, float bottom, EdgeType type) { 199 return nQuickReject(mRenderer, left, top, right, bottom, type.nativeInt); 200 } 201 202 private native boolean nQuickReject(int renderer, float left, float top, 203 float right, float bottom, int edge); 204 205 @Override 206 public boolean quickReject(Path path, EdgeType type) { 207 throw new UnsupportedOperationException(); 208 } 209 210 @Override 211 public boolean quickReject(RectF rect, EdgeType type) { 212 return quickReject(rect.left, rect.top, rect.right, rect.bottom, type); 213 } 214 215 /////////////////////////////////////////////////////////////////////////// 216 // Transformations 217 /////////////////////////////////////////////////////////////////////////// 218 219 @Override 220 public void translate(float dx, float dy) { 221 nTranslate(mRenderer, dx, dy); 222 } 223 224 private native void nTranslate(int renderer, float dx, float dy); 225 226 @Override 227 public void skew(float sx, float sy) { 228 throw new UnsupportedOperationException(); 229 } 230 231 @Override 232 public void rotate(float degrees) { 233 nRotate(mRenderer, degrees); 234 } 235 236 private native void nRotate(int renderer, float degrees); 237 238 @Override 239 public void scale(float sx, float sy) { 240 nScale(mRenderer, sx, sy); 241 } 242 243 private native void nScale(int renderer, float sx, float sy); 244 245 @Override 246 public void setMatrix(Matrix matrix) { 247 nSetMatrix(mRenderer, matrix.native_instance); 248 } 249 250 private native void nSetMatrix(int renderer, int matrix); 251 252 @Override 253 public void getMatrix(Matrix matrix) { 254 nGetMatrix(mRenderer, matrix.native_instance); 255 } 256 257 private native void nGetMatrix(int renderer, int matrix); 258 259 @Override 260 public void concat(Matrix matrix) { 261 nConcatMatrix(mRenderer, matrix.native_instance); 262 } 263 264 private native void nConcatMatrix(int renderer, int matrix); 265 266 /////////////////////////////////////////////////////////////////////////// 267 // State management 268 /////////////////////////////////////////////////////////////////////////// 269 270 @Override 271 public int save() { 272 return nSave(mRenderer, 0); 273 } 274 275 @Override 276 public int save(int saveFlags) { 277 return nSave(mRenderer, saveFlags); 278 } 279 280 private native int nSave(int renderer, int flags); 281 282 @Override 283 public int saveLayer(RectF bounds, Paint paint, int saveFlags) { 284 return saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, paint, saveFlags); 285 } 286 287 @Override 288 public int saveLayer(float left, float top, float right, float bottom, Paint paint, 289 int saveFlags) { 290 int nativePaint = paint == null ? 0 : paint.mNativePaint; 291 return nSaveLayer(mRenderer, left, top, right, bottom, nativePaint, saveFlags); 292 } 293 294 private native int nSaveLayer(int renderer, float left, float top, float right, float bottom, 295 int paint, int saveFlags); 296 297 @Override 298 public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags) { 299 return saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom, 300 alpha, saveFlags); 301 } 302 303 @Override 304 public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, 305 int saveFlags) { 306 return nSaveLayerAlpha(mRenderer, left, top, right, bottom, alpha, saveFlags); 307 } 308 309 private native int nSaveLayerAlpha(int renderer, float left, float top, float right, 310 float bottom, int alpha, int saveFlags); 311 312 @Override 313 public void restore() { 314 nRestore(mRenderer); 315 } 316 317 private native void nRestore(int renderer); 318 319 @Override 320 public void restoreToCount(int saveCount) { 321 nRestoreToCount(mRenderer, saveCount); 322 } 323 324 private native void nRestoreToCount(int renderer, int saveCount); 325 326 @Override 327 public int getSaveCount() { 328 return nGetSaveCount(mRenderer); 329 } 330 331 private native int nGetSaveCount(int renderer); 332 333 /////////////////////////////////////////////////////////////////////////// 334 // Filtering 335 /////////////////////////////////////////////////////////////////////////// 336 337 @Override 338 public void setDrawFilter(DrawFilter filter) { 339 throw new UnsupportedOperationException(); 340 } 341 342 @Override 343 public DrawFilter getDrawFilter() { 344 throw new UnsupportedOperationException(); 345 } 346 347 /////////////////////////////////////////////////////////////////////////// 348 // Drawing 349 /////////////////////////////////////////////////////////////////////////// 350 351 @Override 352 public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, 353 Paint paint) { 354 throw new UnsupportedOperationException(); 355 } 356 357 @Override 358 public void drawARGB(int a, int r, int g, int b) { 359 drawColor((a & 0xFF) << 24 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF)); 360 } 361 362 @Override 363 public void drawPatch(Bitmap bitmap, byte[] chunks, RectF dst, Paint paint) { 364 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 365 nDrawPatch(mRenderer, bitmap.mNativeBitmap, chunks, dst.left, dst.top, dst.right, 366 dst.bottom, nativePaint, bitmap.getDensity(), mDensity, mScreenDensity); 367 } 368 369 private native void nDrawPatch(int renderer, int bitmap, byte[] chunks, float left, float top, 370 float right, float bottom, int paint, int bitmapDensity, int canvasDensity, int screenDensity); 371 372 @Override 373 public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) { 374 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 375 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, left, top, nativePaint, 376 bitmap.getDensity(), mDensity, mScreenDensity); 377 } 378 379 @Override 380 public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) { 381 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 382 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, matrix.native_instance, nativePaint, 383 bitmap.getDensity(), mDensity, mScreenDensity); 384 } 385 386 private native void nDrawBitmap(int renderer, int bitmap, int matrix, int paint, 387 int bitmapDensity, int canvasDensity, int screenDensity); 388 389 @Override 390 public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) { 391 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 392 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, src.left, src.top, src.right, src.bottom, 393 dst.left, dst.top, dst.right, dst.bottom, nativePaint, 394 bitmap.getDensity(), mDensity, mScreenDensity); 395 } 396 397 @Override 398 public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) { 399 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 400 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, src.left, src.top, src.right, src.bottom, 401 dst.left, dst.top, dst.right, dst.bottom, nativePaint, 402 bitmap.getDensity(), mDensity, mScreenDensity); 403 } 404 405 private native void nDrawBitmap(int renderer, int bitmap, float left, float top, int paint, 406 int bitmapDensity, int canvasDensity, int screenDensity); 407 408 private native void nDrawBitmap(int renderer, int bitmap, 409 float srcLeft, float srcTop, float srcRight, float srcBottom, 410 float left, float top, float right, float bottom, int paint, 411 int bitmapDensity, int canvasDensity, int screenDensity); 412 413 @Override 414 public void drawBitmap(int[] colors, int offset, int stride, float x, float y, 415 int width, int height, boolean hasAlpha, Paint paint) { 416 // TODO: Implement 417 } 418 419 @Override 420 public void drawBitmap(int[] colors, int offset, int stride, int x, int y, 421 int width, int height, boolean hasAlpha, Paint paint) { 422 drawBitmap(colors, offset, stride, (float) x, (float) y, width, height, hasAlpha, paint); 423 } 424 425 @Override 426 public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, 427 int vertOffset, int[] colors, int colorOffset, Paint paint) { 428 throw new UnsupportedOperationException(); 429 } 430 431 @Override 432 public void drawCircle(float cx, float cy, float radius, Paint paint) { 433 throw new UnsupportedOperationException(); 434 } 435 436 @Override 437 public void drawColor(int color) { 438 drawColor(color, PorterDuff.Mode.SRC_OVER); 439 } 440 441 @Override 442 public void drawColor(int color, PorterDuff.Mode mode) { 443 nDrawColor(mRenderer, color, mode.nativeInt); 444 } 445 446 private native void nDrawColor(int renderer, int color, int mode); 447 448 @Override 449 public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) { 450 mLine[0] = startX; 451 mLine[1] = startY; 452 mLine[2] = stopX; 453 mLine[3] = stopY; 454 drawLines(mLine, 0, 1, paint); 455 } 456 457 @Override 458 public void drawLines(float[] pts, int offset, int count, Paint paint) { 459 // TODO: Implement 460 } 461 462 @Override 463 public void drawLines(float[] pts, Paint paint) { 464 drawLines(pts, 0, pts.length / 4, paint); 465 } 466 467 @Override 468 public void drawOval(RectF oval, Paint paint) { 469 throw new UnsupportedOperationException(); 470 } 471 472 @Override 473 public void drawPaint(Paint paint) { 474 // TODO: Implement 475 } 476 477 @Override 478 public void drawPath(Path path, Paint paint) { 479 throw new UnsupportedOperationException(); 480 } 481 482 @Override 483 public void drawPicture(Picture picture) { 484 throw new UnsupportedOperationException(); 485 } 486 487 @Override 488 public void drawPicture(Picture picture, Rect dst) { 489 throw new UnsupportedOperationException(); 490 } 491 492 @Override 493 public void drawPicture(Picture picture, RectF dst) { 494 throw new UnsupportedOperationException(); 495 } 496 497 @Override 498 public void drawPoint(float x, float y, Paint paint) { 499 mPoint[0] = x; 500 mPoint[1] = y; 501 drawPoints(mPoint, 0, 1, paint); 502 } 503 504 @Override 505 public void drawPoints(float[] pts, int offset, int count, Paint paint) { 506 // TODO: Implement 507 } 508 509 @Override 510 public void drawPoints(float[] pts, Paint paint) { 511 drawPoints(pts, 0, pts.length / 2, paint); 512 } 513 514 @Override 515 public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) { 516 throw new UnsupportedOperationException(); 517 } 518 519 @Override 520 public void drawPosText(String text, float[] pos, Paint paint) { 521 throw new UnsupportedOperationException(); 522 } 523 524 @Override 525 public void drawRect(float left, float top, float right, float bottom, Paint paint) { 526 nDrawRect(mRenderer, left, top, right, bottom, paint.mNativePaint); 527 } 528 529 private native void nDrawRect(int renderer, float left, float top, float right, float bottom, 530 int paint); 531 532 @Override 533 public void drawRect(Rect r, Paint paint) { 534 drawRect(r.left, r.top, r.right, r.bottom, paint); 535 } 536 537 @Override 538 public void drawRect(RectF r, Paint paint) { 539 drawRect(r.left, r.top, r.right, r.bottom, paint); 540 } 541 542 @Override 543 public void drawRGB(int r, int g, int b) { 544 drawColor(0xFF000000 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF)); 545 } 546 547 @Override 548 public void drawRoundRect(RectF rect, float rx, float ry, Paint paint) { 549 throw new UnsupportedOperationException(); 550 } 551 552 @Override 553 public void drawText(char[] text, int index, int count, float x, float y, Paint paint) { 554 // TODO: Implement 555 } 556 557 @Override 558 public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) { 559 // TODO: Implement 560 } 561 562 @Override 563 public void drawText(String text, int start, int end, float x, float y, Paint paint) { 564 // TODO: Implement 565 } 566 567 @Override 568 public void drawText(String text, float x, float y, Paint paint) { 569 drawText(text, 0, text.length(), x, y, paint); 570 } 571 572 @Override 573 public void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, 574 float vOffset, Paint paint) { 575 throw new UnsupportedOperationException(); 576 } 577 578 @Override 579 public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) { 580 throw new UnsupportedOperationException(); 581 } 582 583 @Override 584 public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount, 585 float x, float y, int dir, Paint paint) { 586 throw new UnsupportedOperationException(); 587 } 588 589 @Override 590 public void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd, 591 float x, float y, int dir, Paint paint) { 592 throw new UnsupportedOperationException(); 593 } 594 595 @Override 596 public void drawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset, 597 float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices, 598 int indexOffset, int indexCount, Paint paint) { 599 throw new UnsupportedOperationException(); 600 } 601} 602