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