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 import android.compat.annotation.UnsupportedAppUsage;
21 
22 /**
23  * Shader used to draw a bitmap as a texture. The bitmap can be repeated or
24  * mirrored by setting the tiling mode.
25  */
26 public class BitmapShader extends Shader {
27     /**
28      * Prevent garbage collection.
29      * @hide
30      */
31     @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
32     @UnsupportedAppUsage
33     public Bitmap mBitmap;
34 
35     @UnsupportedAppUsage
36     private int mTileX;
37     @UnsupportedAppUsage
38     private int mTileY;
39 
40     /**
41      * Call this to create a new shader that will draw with a bitmap.
42      *
43      * @param bitmap The bitmap to use inside the shader
44      * @param tileX The tiling mode for x to draw the bitmap in.
45      * @param tileY The tiling mode for y to draw the bitmap in.
46      */
BitmapShader(@onNull Bitmap bitmap, @NonNull TileMode tileX, @NonNull TileMode tileY)47     public BitmapShader(@NonNull Bitmap bitmap, @NonNull TileMode tileX, @NonNull TileMode tileY) {
48         this(bitmap, tileX.nativeInt, tileY.nativeInt);
49     }
50 
BitmapShader(Bitmap bitmap, int tileX, int tileY)51     private BitmapShader(Bitmap bitmap, int tileX, int tileY) {
52         if (bitmap == null) {
53             throw new IllegalArgumentException("Bitmap must be non-null");
54         }
55         if (bitmap == mBitmap && tileX == mTileX && tileY == mTileY) {
56             return;
57         }
58         mBitmap = bitmap;
59         mTileX = tileX;
60         mTileY = tileY;
61     }
62 
63     @Override
createNativeInstance(long nativeMatrix)64     long createNativeInstance(long nativeMatrix) {
65         return nativeCreate(nativeMatrix, mBitmap.getNativeInstance(), mTileX, mTileY);
66     }
67 
nativeCreate(long nativeMatrix, long bitmapHandle, int shaderTileModeX, int shaderTileModeY)68     private static native long nativeCreate(long nativeMatrix, long bitmapHandle,
69             int shaderTileModeX, int shaderTileModeY);
70 }
71