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