1 /* 2 * Copyright (C) 2007 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 17 package android.graphics; 18 19 import android.annotation.NonNull; 20 21 /** 22 * Shader used to draw a bitmap as a texture. The bitmap can be repeated or 23 * mirrored by setting the tiling mode. 24 */ 25 public class BitmapShader extends Shader { 26 /** 27 * Prevent garbage collection. 28 */ 29 /*package*/ Bitmap mBitmap; 30 31 private int mTileX; 32 private int mTileY; 33 34 /* 35 * This is cache of the last value from the Paint of bitmap-filtering. 36 * In the future, BitmapShaders will carry their own (expanded) data for this 37 * (e.g. including mipmap options, or bicubic weights) 38 * 39 * When that happens, this bool will become those extended values, and we will 40 * need to track whether this Shader was created with those new constructors, 41 * or from the current "legacy" constructor, which (for compatibility) will 42 * still need to know the Paint's setting. 43 * 44 * When the filter Paint setting is finally gone, we will be able to remove 45 * the filterFromPaint parameter currently being passed to createNativeInstance() 46 * and shouldDiscardNativeInstance(), as shaders will always know their filter 47 * settings. 48 */ 49 private boolean mFilterFromPaint; 50 51 /** 52 * Call this to create a new shader that will draw with a bitmap. 53 * 54 * @param bitmap The bitmap to use inside the shader 55 * @param tileX The tiling mode for x to draw the bitmap in. 56 * @param tileY The tiling mode for y to draw the bitmap in. 57 */ BitmapShader(@onNull Bitmap bitmap, @NonNull TileMode tileX, @NonNull TileMode tileY)58 public BitmapShader(@NonNull Bitmap bitmap, @NonNull TileMode tileX, @NonNull TileMode tileY) { 59 this(bitmap, tileX.nativeInt, tileY.nativeInt); 60 } 61 BitmapShader(Bitmap bitmap, int tileX, int tileY)62 private BitmapShader(Bitmap bitmap, int tileX, int tileY) { 63 if (bitmap == null) { 64 throw new IllegalArgumentException("Bitmap must be non-null"); 65 } 66 mBitmap = bitmap; 67 mTileX = tileX; 68 mTileY = tileY; 69 mFilterFromPaint = false; 70 } 71 72 /** @hide */ 73 @Override createNativeInstance(long nativeMatrix, boolean filterFromPaint)74 protected long createNativeInstance(long nativeMatrix, boolean filterFromPaint) { 75 mFilterFromPaint = filterFromPaint; 76 return nativeCreate(nativeMatrix, mBitmap.getNativeInstance(), mTileX, mTileY, 77 mFilterFromPaint); 78 } 79 80 /** @hide */ 81 @Override shouldDiscardNativeInstance(boolean filterFromPaint)82 protected boolean shouldDiscardNativeInstance(boolean filterFromPaint) { 83 return mFilterFromPaint != filterFromPaint; 84 } 85 nativeCreate(long nativeMatrix, long bitmapHandle, int shaderTileModeX, int shaderTileModeY, boolean filter)86 private static native long nativeCreate(long nativeMatrix, long bitmapHandle, 87 int shaderTileModeX, int shaderTileModeY, boolean filter); 88 } 89 90