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
17package android.graphics;
18
19import 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 */
25public class BitmapShader extends Shader {
26    /**
27     * Prevent garbage collection.
28     * @hide
29     */
30    @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
31    public Bitmap mBitmap;
32
33    private int mTileX;
34    private int mTileY;
35
36    /**
37     * Call this to create a new shader that will draw with a bitmap.
38     *
39     * @param bitmap The bitmap to use inside the shader
40     * @param tileX The tiling mode for x to draw the bitmap in.
41     * @param tileY The tiling mode for y to draw the bitmap in.
42     */
43    public BitmapShader(@NonNull Bitmap bitmap, @NonNull TileMode tileX, @NonNull TileMode tileY) {
44        this(bitmap, tileX.nativeInt, tileY.nativeInt);
45    }
46
47    private BitmapShader(Bitmap bitmap, int tileX, int tileY) {
48        if (bitmap == null) {
49            throw new IllegalArgumentException("Bitmap must be non-null");
50        }
51        if (bitmap == mBitmap && tileX == mTileX && tileY == mTileY) {
52            return;
53        }
54        mBitmap = bitmap;
55        mTileX = tileX;
56        mTileY = tileY;
57    }
58
59    @Override
60    long createNativeInstance(long nativeMatrix) {
61        return nativeCreate(nativeMatrix, mBitmap, mTileX, mTileY);
62    }
63
64    /**
65     * @hide
66     */
67    @Override
68    protected Shader copy() {
69        final BitmapShader copy = new BitmapShader(mBitmap, mTileX, mTileY);
70        copyLocalMatrix(copy);
71        return copy;
72    }
73
74    private static native long nativeCreate(long nativeMatrix, Bitmap bitmap,
75            int shaderTileModeX, int shaderTileModeY);
76}
77