Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.gecko.toolbar;
7 import android.graphics.Bitmap;
8 import android.graphics.Canvas;
9 import android.graphics.Paint;
10 import android.graphics.Path;
11 import android.graphics.PorterDuff.Mode;
12 import android.graphics.PorterDuffXfermode;
13 import android.graphics.Shader;
14 import android.os.Build;
16 class CanvasDelegate {
17 Paint mPaint;
18 PorterDuffXfermode mMode;
19 DrawManager mDrawManager;
21 // DrawManager would do a default draw of the background.
22 static interface DrawManager {
23 public void defaultDraw(Canvas cavas);
24 }
26 CanvasDelegate(DrawManager drawManager, Mode mode) {
27 mDrawManager = drawManager;
29 // DST_IN masks, DST_OUT clips.
30 mMode = new PorterDuffXfermode(mode);
32 mPaint = new Paint();
33 mPaint.setAntiAlias(true);
34 mPaint.setColor(0xFFFF0000);
35 mPaint.setStrokeWidth(0.0f);
36 }
38 void draw(Canvas canvas, Path path, int width, int height) {
39 // Save the canvas. All PorterDuff operations should be done in a offscreen bitmap.
40 int count = canvas.saveLayer(0, 0, width, height, null,
41 Canvas.MATRIX_SAVE_FLAG |
42 Canvas.CLIP_SAVE_FLAG |
43 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
44 Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
45 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
47 // Do a default draw.
48 mDrawManager.defaultDraw(canvas);
50 if (path != null && !path.isEmpty()) {
51 // ICS added double-buffering, which made it easier for drawing the Path directly over the DST.
52 // In pre-ICS, drawPath() doesn't seem to use ARGB_8888 mode for performance, hence transparency is not preserved.
53 if (Build.VERSION.SDK_INT >= 14) {
54 mPaint.setXfermode(mMode);
55 canvas.drawPath(path, mPaint);
56 } else {
57 // Allocate a bitmap and draw the masking/clipping path.
58 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
59 (new Canvas(bitmap)).drawPath(path, mPaint);
61 mPaint.setXfermode(mMode);
62 canvas.drawBitmap(bitmap, 0, 0, mPaint);
63 bitmap.recycle();
65 mPaint.setXfermode(null);
66 }
67 }
69 // Restore the canvas.
70 canvas.restoreToCount(count);
71 }
73 void setShader(Shader shader) {
74 mPaint.setShader(shader);
75 }
76 }