Canvas_Delegate.java revision ef44aead46faf1bbc4120825cd6e457ac8ddba1b
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.graphics;
18
19import com.android.layoutlib.api.ILayoutLog;
20import com.android.layoutlib.bridge.DelegateManager;
21
22import java.awt.Graphics2D;
23import java.awt.image.BufferedImage;
24import java.util.Stack;
25
26/**
27 * Delegate implementing the native methods of android.graphics.Canvas
28 *
29 * Through the layoutlib_create tool, the original native methods of Canvas have been replaced
30 * by calls to methods of the same name in this delegate class.
31 *
32 * This class behaves like the original native implementation, but in Java, keeping previously
33 * native data into its own objects and mapping them to int that are sent back and forth between
34 * it and the original Canvas class.
35 *
36 * @see DelegateManager
37 *
38 */
39public class Canvas_Delegate {
40
41    // ---- delegate manager ----
42    private static final DelegateManager<Canvas_Delegate> sManager =
43            new DelegateManager<Canvas_Delegate>();
44
45    // ---- delegate helper data ----
46
47    // ---- delegate data ----
48    private BufferedImage mBufferedImage;
49    private final Stack<Graphics2D> mGraphicsStack = new Stack<Graphics2D>();
50    private ILayoutLog mLogger;
51
52    // ---- Public Helper methods ----
53
54    /**
55     * Returns the native delegate associated to a given {@link Canvas} object.
56     */
57    public static Canvas_Delegate getDelegate(Canvas canvas) {
58        return sManager.getDelegate(canvas.mNativeCanvas);
59    }
60
61    /**
62     * Returns the native delegate associated to a given an int referencing a {@link Canvas} object.
63     */
64    public static Canvas_Delegate getDelegate(int native_canvas) {
65        return sManager.getDelegate(native_canvas);
66    }
67
68    /**
69     * Sets the layoutlib logger into the canvas.
70     * @param logger
71     */
72    public void setLogger(ILayoutLog logger) {
73        mLogger = logger;
74    }
75
76    /**
77     * Returns the current {@link Graphics2D} used to draw.
78     */
79    public Graphics2D getGraphics2d() {
80        return mGraphicsStack.peek();
81    }
82
83    /**
84     * Disposes of the {@link Graphics2D} stack.
85     */
86    public void dispose() {
87
88    }
89
90    // ---- native methods ----
91
92    /*package*/ static boolean isOpaque(Canvas thisCanvas) {
93        // FIXME
94        throw new UnsupportedOperationException();
95    }
96
97    /*package*/ static int getWidth(Canvas thisCanvas) {
98        // FIXME
99        throw new UnsupportedOperationException();
100    }
101
102    /*package*/ static int getHeight(Canvas thisCanvas) {
103        // FIXME
104        throw new UnsupportedOperationException();
105    }
106
107    /*package*/ static void translate(Canvas thisCanvas, float dx, float dy) {
108        // FIXME
109        throw new UnsupportedOperationException();
110    }
111
112    /*package*/ static void rotate(Canvas thisCanvas, float degrees) {
113        // FIXME
114        throw new UnsupportedOperationException();
115    }
116
117    /*package*/ static void scale(Canvas thisCanvas, float sx, float sy) {
118        // FIXME
119        throw new UnsupportedOperationException();
120    }
121
122    /*package*/ static void skew(Canvas thisCanvas, float sx, float sy) {
123        // FIXME
124        throw new UnsupportedOperationException();
125    }
126
127    /*package*/ static boolean clipRect(Canvas thisCanvas, RectF rect) {
128        // FIXME
129        throw new UnsupportedOperationException();
130    }
131
132    /*package*/ static boolean clipRect(Canvas thisCanvas, Rect rect) {
133        // FIXME
134        throw new UnsupportedOperationException();
135    }
136
137    /*package*/ static boolean clipRect(Canvas thisCanvas, float left, float top, float right,
138            float bottom) {
139        // FIXME
140        throw new UnsupportedOperationException();
141    }
142
143    /*package*/ static boolean clipRect(Canvas thisCanvas, int left, int top, int right,
144            int bottom) {
145        // FIXME
146        throw new UnsupportedOperationException();
147    }
148
149    /*package*/ static int save(Canvas thisCanvas) {
150        // FIXME
151        throw new UnsupportedOperationException();
152    }
153
154    /*package*/ static int save(Canvas thisCanvas, int saveFlags) {
155        // FIXME
156        throw new UnsupportedOperationException();
157    }
158
159    /*package*/ static void restore(Canvas thisCanvas) {
160        // FIXME
161        throw new UnsupportedOperationException();
162    }
163
164    /*package*/ static int getSaveCount(Canvas thisCanvas) {
165        // FIXME
166        throw new UnsupportedOperationException();
167    }
168
169    /*package*/ static void restoreToCount(Canvas thisCanvas, int saveCount) {
170        // FIXME
171        throw new UnsupportedOperationException();
172    }
173
174    /*package*/ static void drawPoints(Canvas thisCanvas, float[] pts, int offset, int count,
175            Paint paint) {
176        // FIXME
177        throw new UnsupportedOperationException();
178    }
179
180    /*package*/ static void drawPoint(Canvas thisCanvas, float x, float y, Paint paint) {
181        // FIXME
182        throw new UnsupportedOperationException();
183    }
184
185    /*package*/ static void drawLines(Canvas thisCanvas, float[] pts, int offset, int count,
186            Paint paint) {
187        // FIXME
188        throw new UnsupportedOperationException();
189    }
190
191    /*package*/ static void freeCaches() {
192        // FIXME
193        throw new UnsupportedOperationException();
194    }
195
196    /*package*/ static int initRaster(int nativeBitmapOrZero) {
197        if (nativeBitmapOrZero > 0) {
198            // get the Bitmap from the int
199            Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(nativeBitmapOrZero);
200
201            // create a new Canvas_Delegate with the given bitmap and return its new native int.
202            Canvas_Delegate newDelegate = new Canvas_Delegate(bitmapDelegate.getImage());
203
204            return sManager.addDelegate(newDelegate);
205        } else {
206            // create a new Canvas_Delegate and return its new native int.
207            Canvas_Delegate newDelegate = new Canvas_Delegate();
208
209            return sManager.addDelegate(newDelegate);
210        }
211    }
212
213    /*package*/ static void native_setBitmap(int nativeCanvas, int bitmap) {
214        // get the delegate from the native int.
215        Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas);
216        if (canvasDelegate == null) {
217            assert false;
218            return;
219        }
220
221        // get the delegate from the native int.
222        Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(bitmap);
223        if (bitmapDelegate == null) {
224            assert false;
225            return;
226        }
227
228        canvasDelegate.setBitmap(bitmapDelegate.getImage());
229    }
230
231    /*package*/ static int native_saveLayer(int nativeCanvas, RectF bounds,
232                                               int paint, int layerFlags) {
233        // FIXME
234        throw new UnsupportedOperationException();
235    }
236
237    /*package*/ static int native_saveLayer(int nativeCanvas, float l,
238                                               float t, float r, float b,
239                                               int paint, int layerFlags) {
240        // FIXME
241        throw new UnsupportedOperationException();
242    }
243
244    /*package*/ static int native_saveLayerAlpha(int nativeCanvas,
245                                                    RectF bounds, int alpha,
246                                                    int layerFlags) {
247        // FIXME
248        throw new UnsupportedOperationException();
249    }
250
251    /*package*/ static int native_saveLayerAlpha(int nativeCanvas, float l,
252                                                    float t, float r, float b,
253                                                    int alpha, int layerFlags) {
254        // FIXME
255        throw new UnsupportedOperationException();
256    }
257
258
259    /*package*/ static void native_concat(int nCanvas, int nMatrix) {
260        // FIXME
261        throw new UnsupportedOperationException();
262    }
263
264    /*package*/ static void native_setMatrix(int nCanvas, int nMatrix) {
265        // FIXME
266        throw new UnsupportedOperationException();
267    }
268
269    /*package*/ static boolean native_clipRect(int nCanvas,
270                                                  float left, float top,
271                                                  float right, float bottom,
272                                                  int regionOp) {
273        // FIXME
274        throw new UnsupportedOperationException();
275    }
276
277    /*package*/ static boolean native_clipPath(int nativeCanvas,
278                                                  int nativePath,
279                                                  int regionOp) {
280        // FIXME
281        throw new UnsupportedOperationException();
282    }
283
284    /*package*/ static boolean native_clipRegion(int nativeCanvas,
285                                                    int nativeRegion,
286                                                    int regionOp) {
287        // FIXME
288        throw new UnsupportedOperationException();
289    }
290
291    /*package*/ static void nativeSetDrawFilter(int nativeCanvas,
292                                                   int nativeFilter) {
293        // FIXME
294        throw new UnsupportedOperationException();
295    }
296
297    /*package*/ static boolean native_getClipBounds(int nativeCanvas,
298                                                       Rect bounds) {
299        // FIXME
300        throw new UnsupportedOperationException();
301    }
302
303    /*package*/ static void native_getCTM(int canvas, int matrix) {
304        // FIXME
305        throw new UnsupportedOperationException();
306    }
307
308    /*package*/ static boolean native_quickReject(int nativeCanvas,
309                                                     RectF rect,
310                                                     int native_edgeType) {
311        // FIXME
312        throw new UnsupportedOperationException();
313    }
314
315    /*package*/ static boolean native_quickReject(int nativeCanvas,
316                                                     int path,
317                                                     int native_edgeType) {
318        // FIXME
319        throw new UnsupportedOperationException();
320    }
321
322    /*package*/ static boolean native_quickReject(int nativeCanvas,
323                                                     float left, float top,
324                                                     float right, float bottom,
325                                                     int native_edgeType) {
326        // FIXME
327        throw new UnsupportedOperationException();
328    }
329
330    /*package*/ static void native_drawRGB(int nativeCanvas, int r, int g,
331                                              int b) {
332        // FIXME
333        throw new UnsupportedOperationException();
334    }
335
336    /*package*/ static void native_drawARGB(int nativeCanvas, int a, int r,
337                                               int g, int b) {
338        // FIXME
339        throw new UnsupportedOperationException();
340    }
341
342    /*package*/ static void native_drawColor(int nativeCanvas, int color) {
343        // FIXME
344        throw new UnsupportedOperationException();
345    }
346
347    /*package*/ static void native_drawColor(int nativeCanvas, int color,
348                                                int mode) {
349        // FIXME
350        throw new UnsupportedOperationException();
351    }
352
353    /*package*/ static void native_drawPaint(int nativeCanvas, int paint) {
354        // FIXME
355        throw new UnsupportedOperationException();
356    }
357
358    /*package*/ static void native_drawLine(int nativeCanvas, float startX,
359                                               float startY, float stopX,
360                                               float stopY, int paint) {
361        // FIXME
362        throw new UnsupportedOperationException();
363    }
364
365    /*package*/ static void native_drawRect(int nativeCanvas, RectF rect,
366                                               int paint) {
367        // FIXME
368        throw new UnsupportedOperationException();
369    }
370
371    /*package*/ static void native_drawRect(int nativeCanvas, float left,
372                                               float top, float right,
373                                               float bottom, int paint) {
374        // FIXME
375        throw new UnsupportedOperationException();
376    }
377
378    /*package*/ static void native_drawOval(int nativeCanvas, RectF oval,
379                                               int paint) {
380        // FIXME
381        throw new UnsupportedOperationException();
382    }
383
384    /*package*/ static void native_drawCircle(int nativeCanvas, float cx,
385                                                 float cy, float radius,
386                                                 int paint) {
387        // FIXME
388        throw new UnsupportedOperationException();
389    }
390
391    /*package*/ static void native_drawArc(int nativeCanvas, RectF oval,
392                                              float startAngle, float sweep,
393                                              boolean useCenter, int paint) {
394        // FIXME
395        throw new UnsupportedOperationException();
396    }
397
398    /*package*/ static void native_drawRoundRect(int nativeCanvas,
399                                                    RectF rect, float rx,
400                                                    float ry, int paint) {
401        // FIXME
402        throw new UnsupportedOperationException();
403    }
404
405    /*package*/ static void native_drawPath(int nativeCanvas, int path,
406                                               int paint) {
407        // FIXME
408        throw new UnsupportedOperationException();
409    }
410
411    /*package*/ static void native_drawBitmap(Canvas thisCanvas, int nativeCanvas, int bitmap,
412                                                 float left, float top,
413                                                 int nativePaintOrZero,
414                                                 int canvasDensity,
415                                                 int screenDensity,
416                                                 int bitmapDensity) {
417        // FIXME
418        throw new UnsupportedOperationException();
419    }
420
421    /*package*/ static void native_drawBitmap(Canvas thisCanvas, int nativeCanvas, int bitmap,
422                                                 Rect src, RectF dst,
423                                                 int nativePaintOrZero,
424                                                 int screenDensity,
425                                                 int bitmapDensity) {
426        // FIXME
427        throw new UnsupportedOperationException();
428    }
429
430    /*package*/ static void native_drawBitmap(int nativeCanvas, int bitmap,
431                                                 Rect src, Rect dst,
432                                                 int nativePaintOrZero,
433                                                 int screenDensity,
434                                                 int bitmapDensity) {
435        // FIXME
436        throw new UnsupportedOperationException();
437    }
438
439    /*package*/ static void native_drawBitmap(int nativeCanvas, int[] colors,
440                                                int offset, int stride, float x,
441                                                 float y, int width, int height,
442                                                 boolean hasAlpha,
443                                                 int nativePaintOrZero) {
444        // FIXME
445        throw new UnsupportedOperationException();
446    }
447
448    /*package*/ static void nativeDrawBitmapMatrix(int nCanvas, int nBitmap,
449                                                      int nMatrix, int nPaint) {
450        // FIXME
451        throw new UnsupportedOperationException();
452    }
453
454    /*package*/ static void nativeDrawBitmapMesh(int nCanvas, int nBitmap,
455                                                    int meshWidth, int meshHeight,
456                                                    float[] verts, int vertOffset,
457                                                    int[] colors, int colorOffset, int nPaint) {
458        // FIXME
459        throw new UnsupportedOperationException();
460    }
461
462    /*package*/ static void nativeDrawVertices(int nCanvas, int mode, int n,
463                   float[] verts, int vertOffset, float[] texs, int texOffset,
464                   int[] colors, int colorOffset, short[] indices,
465                   int indexOffset, int indexCount, int nPaint) {
466        // FIXME
467        throw new UnsupportedOperationException();
468    }
469
470
471    /*package*/ static void native_drawText(int nativeCanvas, char[] text,
472                                               int index, int count, float x,
473                                               float y, int flags, int paint) {
474        // FIXME
475        throw new UnsupportedOperationException();
476    }
477
478    /*package*/ static void native_drawText(int nativeCanvas, String text,
479                                               int start, int end, float x,
480                                               float y, int flags, int paint) {
481        // FIXME
482        throw new UnsupportedOperationException();
483    }
484
485
486    /*package*/ static void native_drawTextRun(int nativeCanvas, String text,
487            int start, int end, int contextStart, int contextEnd,
488            float x, float y, int flags, int paint) {
489        // FIXME
490        throw new UnsupportedOperationException();
491    }
492
493
494    /*package*/ static void native_drawTextRun(int nativeCanvas, char[] text,
495            int start, int count, int contextStart, int contextCount,
496            float x, float y, int flags, int paint) {
497        // FIXME
498        throw new UnsupportedOperationException();
499    }
500
501
502    /*package*/ static void native_drawPosText(int nativeCanvas,
503                                                  char[] text, int index,
504                                                  int count, float[] pos,
505                                                  int paint) {
506        // FIXME
507        throw new UnsupportedOperationException();
508    }
509
510    /*package*/ static void native_drawPosText(int nativeCanvas,
511                                                  String text, float[] pos,
512                                                  int paint) {
513        // FIXME
514        throw new UnsupportedOperationException();
515    }
516
517    /*package*/ static void native_drawTextOnPath(int nativeCanvas,
518                                                     char[] text, int index,
519                                                     int count, int path,
520                                                     float hOffset,
521                                                     float vOffset, int bidiFlags,
522                                                     int paint) {
523        // FIXME
524        throw new UnsupportedOperationException();
525    }
526
527    /*package*/ static void native_drawTextOnPath(int nativeCanvas,
528                                                     String text, int path,
529                                                     float hOffset,
530                                                     float vOffset,
531                                                     int flags, int paint) {
532        // FIXME
533        throw new UnsupportedOperationException();
534    }
535
536    /*package*/ static void native_drawPicture(int nativeCanvas,
537                                                  int nativePicture) {
538        // FIXME
539        throw new UnsupportedOperationException();
540    }
541
542    /*package*/ static void finalizer(int nativeCanvas) {
543        sManager.removeDelegate(nativeCanvas);
544    }
545
546    // ---- Private delegate/helper methods ----
547
548    private Canvas_Delegate(BufferedImage image) {
549        setBitmap(image);
550    }
551
552    private Canvas_Delegate() {
553    }
554
555    private void setBitmap(BufferedImage image) {
556        mBufferedImage = image;
557        mGraphicsStack.push(mBufferedImage.createGraphics());
558    }
559}
560