GLES20Canvas.java revision ce0537b80087a6225273040a987414b1dd081aa0
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 drawBitmap(Bitmap bitmap, float left, float top, Paint paint) { 364 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 365 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, left, top, nativePaint, 366 bitmap.getDensity(), mDensity, mScreenDensity); 367 } 368 369 @Override 370 public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) { 371 final float width = bitmap.getWidth(); 372 final float height = bitmap.getHeight(); 373 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 374 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, 0.0f, 0.0f, width, height, 375 0.0f, 0.0f, width, height, matrix.native_instance, nativePaint, 376 bitmap.getDensity(), mDensity, mScreenDensity); 377 } 378 379 @Override 380 public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) { 381 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 382 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, src.left, src.top, src.right, src.bottom, 383 dst.left, dst.top, dst.right, dst.bottom, 0, nativePaint, 384 bitmap.getDensity(), mDensity, mScreenDensity); 385 } 386 387 @Override 388 public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) { 389 final int nativePaint = paint == null ? 0 : paint.mNativePaint; 390 nDrawBitmap(mRenderer, bitmap.mNativeBitmap, src.left, src.top, src.right, src.bottom, 391 dst.left, dst.top, dst.right, dst.bottom, 0, nativePaint, 392 bitmap.getDensity(), mDensity, mScreenDensity); 393 } 394 395 private native void nDrawBitmap(int renderer, int bitmap, float left, float top, int paint, 396 int bitmapDensity, int canvasDensity, int screenDensity); 397 398 private native void nDrawBitmap(int renderer, int bitmap, 399 float srcLeft, float srcTop, float srcRight, float srcBottom, 400 float left, float top, float right, float bottom, int matrix, int paint, 401 int bitmapDensity, int canvasDensity, int screenDensity); 402 403 @Override 404 public void drawBitmap(int[] colors, int offset, int stride, float x, float y, 405 int width, int height, boolean hasAlpha, Paint paint) { 406 407 // TODO: Implement 408 } 409 410 @Override 411 public void drawBitmap(int[] colors, int offset, int stride, int x, int y, 412 int width, int height, boolean hasAlpha, Paint paint) { 413 drawBitmap(colors, offset, stride, (float) x, (float) y, width, height, hasAlpha, paint); 414 } 415 416 @Override 417 public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, 418 int vertOffset, int[] colors, int colorOffset, Paint paint) { 419 throw new UnsupportedOperationException(); 420 } 421 422 @Override 423 public void drawCircle(float cx, float cy, float radius, Paint paint) { 424 throw new UnsupportedOperationException(); 425 } 426 427 @Override 428 public void drawColor(int color) { 429 drawColor(color, PorterDuff.Mode.SRC_OVER); 430 } 431 432 @Override 433 public void drawColor(int color, PorterDuff.Mode mode) { 434 nDrawColor(mRenderer, color, mode.nativeInt); 435 } 436 437 private native void nDrawColor(int renderer, int color, int mode); 438 439 @Override 440 public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) { 441 mLine[0] = startX; 442 mLine[1] = startY; 443 mLine[2] = stopX; 444 mLine[3] = stopY; 445 drawLines(mLine, 0, 1, paint); 446 } 447 448 @Override 449 public void drawLines(float[] pts, int offset, int count, Paint paint) { 450 // TODO: Implement 451 } 452 453 @Override 454 public void drawLines(float[] pts, Paint paint) { 455 drawLines(pts, 0, pts.length / 4, paint); 456 } 457 458 @Override 459 public void drawOval(RectF oval, Paint paint) { 460 throw new UnsupportedOperationException(); 461 } 462 463 @Override 464 public void drawPaint(Paint paint) { 465 // TODO: Implement 466 } 467 468 @Override 469 public void drawPath(Path path, Paint paint) { 470 throw new UnsupportedOperationException(); 471 } 472 473 @Override 474 public void drawPicture(Picture picture) { 475 throw new UnsupportedOperationException(); 476 } 477 478 @Override 479 public void drawPicture(Picture picture, Rect dst) { 480 throw new UnsupportedOperationException(); 481 } 482 483 @Override 484 public void drawPicture(Picture picture, RectF dst) { 485 throw new UnsupportedOperationException(); 486 } 487 488 @Override 489 public void drawPoint(float x, float y, Paint paint) { 490 mPoint[0] = x; 491 mPoint[1] = y; 492 drawPoints(mPoint, 0, 1, paint); 493 } 494 495 @Override 496 public void drawPoints(float[] pts, int offset, int count, Paint paint) { 497 // TODO: Implement 498 } 499 500 @Override 501 public void drawPoints(float[] pts, Paint paint) { 502 drawPoints(pts, 0, pts.length / 2, paint); 503 } 504 505 @Override 506 public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) { 507 throw new UnsupportedOperationException(); 508 } 509 510 @Override 511 public void drawPosText(String text, float[] pos, Paint paint) { 512 throw new UnsupportedOperationException(); 513 } 514 515 @Override 516 public void drawRect(float left, float top, float right, float bottom, Paint paint) { 517 nDrawRect(mRenderer, left, top, right, bottom, paint.mNativePaint); 518 } 519 520 private native void nDrawRect(int renderer, float left, float top, float right, float bottom, 521 int paint); 522 523 @Override 524 public void drawRect(Rect r, Paint paint) { 525 drawRect(r.left, r.top, r.right, r.bottom, paint); 526 } 527 528 @Override 529 public void drawRect(RectF r, Paint paint) { 530 drawRect(r.left, r.top, r.right, r.bottom, paint); 531 } 532 533 @Override 534 public void drawRGB(int r, int g, int b) { 535 drawColor(0xFF000000 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF)); 536 } 537 538 @Override 539 public void drawRoundRect(RectF rect, float rx, float ry, Paint paint) { 540 throw new UnsupportedOperationException(); 541 } 542 543 @Override 544 public void drawText(char[] text, int index, int count, float x, float y, Paint paint) { 545 // TODO: Implement 546 } 547 548 @Override 549 public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) { 550 // TODO: Implement 551 } 552 553 @Override 554 public void drawText(String text, int start, int end, float x, float y, Paint paint) { 555 // TODO: Implement 556 } 557 558 @Override 559 public void drawText(String text, float x, float y, Paint paint) { 560 drawText(text, 0, text.length(), x, y, paint); 561 } 562 563 @Override 564 public void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, 565 float vOffset, Paint paint) { 566 throw new UnsupportedOperationException(); 567 } 568 569 @Override 570 public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) { 571 throw new UnsupportedOperationException(); 572 } 573 574 @Override 575 public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount, 576 float x, float y, int dir, Paint paint) { 577 throw new UnsupportedOperationException(); 578 } 579 580 @Override 581 public void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd, 582 float x, float y, int dir, Paint paint) { 583 throw new UnsupportedOperationException(); 584 } 585 586 @Override 587 public void drawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset, 588 float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices, 589 int indexOffset, int indexCount, Paint paint) { 590 throw new UnsupportedOperationException(); 591 } 592} 593