19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.graphics;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
191c2bf03d1082fc6b7eb42cbd163c60c07cf2bcccTor Norbyeimport android.annotation.CheckResult;
2080756e38882720860db52f1fcc21fa1505a02abfTor Norbyeimport android.annotation.ColorInt;
21c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viveretteimport android.annotation.NonNull;
22efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guyimport android.annotation.Nullable;
238242656f495847c50c9ceaea92839dce78218a75Romain Guyimport android.annotation.Size;
245d3501172c9e2329f46bc6bb53050fd2ec7d2305John Reckimport android.annotation.WorkerThread;
251480b67635cdc4c2c5e735741bf30393fd70d738Makoto Onukiimport android.content.res.ResourcesImpl;
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Parcel;
278cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chenimport android.os.Parcelable;
289fd259c9b0878ba52fec86173d76976e70977fcdsergeyvimport android.os.StrictMode;
290df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reckimport android.os.Trace;
302784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackbornimport android.util.DisplayMetrics;
3101a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reckimport android.util.Log;
32716f38177ee1401373ced8d1f665431bb7a57d14Chris Craikimport android.view.DisplayListCanvas;
33716f38177ee1401373ced8d1f665431bb7a57d14Chris Craikimport android.view.RenderNode;
34716f38177ee1401373ced8d1f665431bb7a57d14Chris Craikimport android.view.ThreadedRenderer;
35716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik
36a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winsonimport libcore.util.NativeAllocationRegistry;
37a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson
388cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chenimport java.io.OutputStream;
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.nio.Buffer;
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.nio.ByteBuffer;
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.nio.IntBuffer;
428cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chenimport java.nio.ShortBuffer;
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic final class Bitmap implements Parcelable {
4501a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck    private static final String TAG = "Bitmap";
4601a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Indicates that the bitmap was created for an unknown pixel density.
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see Bitmap#getDensity()
5111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see Bitmap#setDensity(int)
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public static final int DENSITY_NONE = 0;
54f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
55775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler    // Estimated size of the Bitmap native allocation, not including
56775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler    // pixel data.
57775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler    private static final long NATIVE_ALLOCATION_SIZE = 32;
58775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler
59f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    // Convenience for JNI access
60f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    private final long mNativePtr;
61e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final boolean mIsMutable;
631abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik
641abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    /**
6557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * Represents whether the Bitmap's content is requested to be pre-multiplied.
661abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * Note that isPremultiplied() does not directly return this value, because
6757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * isPremultiplied() may never return true for a 565 Bitmap or a bitmap
6857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * without alpha.
691abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
701abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * setPremultiplied() does directly set the value so that setConfig() and
711abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * setPremultiplied() aren't order dependent, despite being setters.
7257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     *
7357ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * The native bitmap's premultiplication state is kept up to date by
7457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * pushing down this preference for every config change.
751abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     */
7657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private boolean mRequestPremultiplied;
778790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III
7847cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    private byte[] mNinePatchChunk; // may be null
7947cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    private NinePatch.InsetStruct mNinePatchInsets; // may be null
801abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    private int mWidth;
811abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    private int mHeight;
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private boolean mRecycled;
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
84efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy    private ColorSpace mColorSpace;
85efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy
86caa08ff5e9ee004634a95776fc72bb769f1286deJohn Reck    /** @hide */
87caa08ff5e9ee004634a95776fc72bb769f1286deJohn Reck    public int mDensity = getDefaultDensity();
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    private static volatile int sDefaultDensity = -1;
9002890fd0f98b3b8d98baf0bda1ea906afd723d8bRomain Guy
911480b67635cdc4c2c5e735741bf30393fd70d738Makoto Onuki    /** @hide Used only when ResourcesImpl.TRACE_FOR_DETAILED_PRELOAD is true. */
921480b67635cdc4c2c5e735741bf30393fd70d738Makoto Onuki    public static volatile int sPreloadTracingNumInstantiatedBitmaps;
931480b67635cdc4c2c5e735741bf30393fd70d738Makoto Onuki
941480b67635cdc4c2c5e735741bf30393fd70d738Makoto Onuki    /** @hide Used only when ResourcesImpl.TRACE_FOR_DETAILED_PRELOAD is true. */
951480b67635cdc4c2c5e735741bf30393fd70d738Makoto Onuki    public static volatile long sPreloadTracingTotalBitmapsSize;
961480b67635cdc4c2c5e735741bf30393fd70d738Makoto Onuki
9796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    /**
9896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * For backwards compatibility, allows the app layer to change the default
9996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * density when running old apps.
10096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * @hide
10196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     */
10296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    public static void setDefaultDensity(int density) {
10396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        sDefaultDensity = density;
10496e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    }
1053849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy
106f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck    @SuppressWarnings("deprecation")
1073849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy    static int getDefaultDensity() {
10896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        if (sDefaultDensity >= 0) {
10996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn            return sDefaultDensity;
11096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        }
11196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        sDefaultDensity = DisplayMetrics.DENSITY_DEVICE;
11296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        return sDefaultDensity;
11396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    }
114dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1169f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * Private constructor that must received an already allocated native bitmap
1179f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * int (pointer).
118ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani     */
119f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck    // called from JNI
1204508218850faedea95371188da587b6734f5f3dasergeyv    Bitmap(long nativeBitmap, int width, int height, int density,
12157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III            boolean isMutable, boolean requestPremultiplied,
12247cd8e921db73e894f94ec4729ade90da50996f5Chris Craik            byte[] ninePatchChunk, NinePatch.InsetStruct ninePatchInsets) {
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (nativeBitmap == 0) {
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("internal error: native bitmap is 0");
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1268cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
1271abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mWidth = width;
1281abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mHeight = height;
1291abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mIsMutable = isMutable;
13057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        mRequestPremultiplied = requestPremultiplied;
1314cd7dbc90f93893f521dce32d8cd25c02a185a24Chris Craik
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNinePatchChunk = ninePatchChunk;
13347cd8e921db73e894f94ec4729ade90da50996f5Chris Craik        mNinePatchInsets = ninePatchInsets;
134de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn        if (density >= 0) {
135de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn            mDensity = density;
136de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn        }
1374cd7dbc90f93893f521dce32d8cd25c02a185a24Chris Craik
138f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        mNativePtr = nativeBitmap;
1394508218850faedea95371188da587b6734f5f3dasergeyv        long nativeSize = NATIVE_ALLOCATION_SIZE + getAllocationByteCount();
140775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        NativeAllocationRegistry registry = new NativeAllocationRegistry(
1410ebdbb4a8470bb77f38c33a42c220ecb8a7adcc6Richard Uhler            Bitmap.class.getClassLoader(), nativeGetNativeFinalizer(), nativeSize);
142775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        registry.registerNativeAllocation(this, nativeBitmap);
1431480b67635cdc4c2c5e735741bf30393fd70d738Makoto Onuki
1441480b67635cdc4c2c5e735741bf30393fd70d738Makoto Onuki        if (ResourcesImpl.TRACE_FOR_DETAILED_PRELOAD) {
1451480b67635cdc4c2c5e735741bf30393fd70d738Makoto Onuki            sPreloadTracingNumInstantiatedBitmaps++;
1461480b67635cdc4c2c5e735741bf30393fd70d738Makoto Onuki            sPreloadTracingTotalBitmapsSize += nativeSize;
1471480b67635cdc4c2c5e735741bf30393fd70d738Makoto Onuki        }
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1514c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger     * Return the pointer to the native object.
152caa08ff5e9ee004634a95776fc72bb769f1286deJohn Reck     * @hide
1534c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger     */
154caa08ff5e9ee004634a95776fc72bb769f1286deJohn Reck    public long getNativeInstance() {
1554c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        return mNativePtr;
1564c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    }
1574c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger
1584c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    /**
1591abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * Native bitmap has been reconfigured, so set premult and cached
1601abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * width/height values
1619f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     */
1629505a6552764461c22ce48f1ac13d025d23e1579Romain Guy    @SuppressWarnings("unused") // called from JNI
16357ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    void reinit(int width, int height, boolean requestPremultiplied) {
1641abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mWidth = width;
1651abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mHeight = height;
16657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        mRequestPremultiplied = requestPremultiplied;
1675acc476878bce8b72c0059e05cbbd5b43ffee5d5Romain Guy        mColorSpace = null;
1689f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    }
1699f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik
1709f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    /**
17111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * <p>Returns the density for this bitmap.</p>
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
17396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * <p>The default density is the same density as the current display,
17496e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * unless the current application does not support different screen
17596e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * densities in which case it is
17696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * {@link android.util.DisplayMetrics#DENSITY_DEFAULT}.  Note that
17796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * compatibility mode is determined by the application that was initially
17896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * loaded into a process -- applications that share the same process should
17996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * all have the same compatibility, or ensure they explicitly set the
18096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * density of their bitmaps appropriately.</p>
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
18296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * @return A scaling factor of the default density or {@link #DENSITY_NONE}
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         if the scaling factor is unknown.
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
18511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see #setDensity(int)
186a53b828635fce8b6b2d3e3377d74d72070056623Dianne Hackborn     * @see android.util.DisplayMetrics#DENSITY_DEFAULT
18711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see android.util.DisplayMetrics#densityDpi
18811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see #DENSITY_NONE
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
19011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getDensity() {
19101a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
19201a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getDensity() on a recycle()'d bitmap! This is undefined behavior!");
19301a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
19411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return mDensity;
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
19811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * <p>Specifies the density for this bitmap.  When the bitmap is
19911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * drawn to a Canvas that also has a density, it will be scaled
20011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * appropriately.</p>
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
20211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @param density The density scaling factor to use with this bitmap or
20311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     *        {@link #DENSITY_NONE} if the density is unknown.
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
20511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see #getDensity()
206a53b828635fce8b6b2d3e3377d74d72070056623Dianne Hackborn     * @see android.util.DisplayMetrics#DENSITY_DEFAULT
20711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see android.util.DisplayMetrics#densityDpi
20811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see #DENSITY_NONE
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
21011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public void setDensity(int density) {
21111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        mDensity = density;
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
213c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
214c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    /**
215c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>Modifies the bitmap to have a specified width, height, and {@link
216c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * Config}, without affecting the underlying allocation backing the bitmap.
217c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * Bitmap pixel data is not re-initialized for the new configuration.</p>
218c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
219c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>This method can be used to avoid allocating a new bitmap, instead
220c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * reusing an existing bitmap's allocation for a new configuration of equal
221c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * or lesser size. If the Bitmap's allocation isn't large enough to support
222c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the new configuration, an IllegalArgumentException will be thrown and the
223c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * bitmap will not be modified.</p>
224c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
225c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>The result of {@link #getByteCount()} will reflect the new configuration,
226c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * while {@link #getAllocationByteCount()} will reflect that of the initial
227c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * configuration.</p>
228c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
22917a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     * <p>Note: This may change this result of hasAlpha(). When converting to 565,
23017a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     * the new bitmap will always be considered opaque. When converting from 565,
23117a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     * the new bitmap will be considered non-opaque, and will respect the value
23217a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     * set by setPremultiplied().</p>
23317a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     *
2346d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * <p>WARNING: This method should NOT be called on a bitmap currently in use
2356d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * by the view system, Canvas, or the AndroidBitmap NDK API. It does not
2366d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * make guarantees about how the underlying pixel buffer is remapped to the
2376d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * new config, just that the allocation is reused. Additionally, the view
2386d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * system does not account for bitmap properties being modifying during use,
2396d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * e.g. while attached to drawables.</p>
2406d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *
2416d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * <p>In order to safely ensure that a Bitmap is no longer in use by the
2426d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * View system it is necessary to wait for a draw pass to occur after
2436d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * invalidate()'ing any view that had previously drawn the Bitmap in the last
2446d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * draw pass due to hardware acceleration's caching of draw commands. As
2456d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * an example, here is how this can be done for an ImageView:
2466d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * <pre class="prettyprint">
2476d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      ImageView myImageView = ...;
2486d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      final Bitmap myBitmap = ...;
2496d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      myImageView.setImageDrawable(null);
2506d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      myImageView.post(new Runnable() {
2516d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *          public void run() {
2526d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *              // myBitmap is now no longer in use by the ImageView
2536d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *              // and can be safely reconfigured.
2546d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *              myBitmap.reconfigure(...);
2556d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *          }
2566d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      });
2576d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * </pre></p>
258c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
259c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setWidth(int)
260c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setHeight(int)
261c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setConfig(Config)
262c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     */
263c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    public void reconfigure(int width, int height, Config config) {
264c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        checkRecycled("Can't call reconfigure() on a recycled bitmap");
265c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        if (width <= 0 || height <= 0) {
266c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik            throw new IllegalArgumentException("width and height must be > 0");
267c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        }
268c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        if (!isMutable()) {
2699c2cde0583d02ba7551877dbe7a5ecaee6defb56Chris Craik            throw new IllegalStateException("only mutable bitmaps may be reconfigured");
270c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        }
271c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
2724508218850faedea95371188da587b6734f5f3dasergeyv        nativeReconfigure(mNativePtr, width, height, config.nativeInt, mRequestPremultiplied);
273c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        mWidth = width;
274c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        mHeight = height;
2755acc476878bce8b72c0059e05cbbd5b43ffee5d5Romain Guy        mColorSpace = null;
276c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    }
277c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
278c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    /**
279c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>Convenience method for calling {@link #reconfigure(int, int, Config)}
280c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * with the current height and config.</p>
281c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
282c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>WARNING: this method should not be used on bitmaps currently used by
283c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the view system, see {@link #reconfigure(int, int, Config)} for more
284c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * details.</p>
285c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
286c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #reconfigure(int, int, Config)
287c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setHeight(int)
288c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setConfig(Config)
289c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     */
290c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    public void setWidth(int width) {
291c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        reconfigure(width, getHeight(), getConfig());
292c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    }
293c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
294c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    /**
295c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>Convenience method for calling {@link #reconfigure(int, int, Config)}
296c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * with the current width and config.</p>
297c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
298c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>WARNING: this method should not be used on bitmaps currently used by
299c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the view system, see {@link #reconfigure(int, int, Config)} for more
300c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * details.</p>
301c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
302c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #reconfigure(int, int, Config)
303c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setWidth(int)
304c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setConfig(Config)
305c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     */
306c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    public void setHeight(int height) {
307c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        reconfigure(getWidth(), height, getConfig());
308c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    }
309c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
310c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    /**
311c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>Convenience method for calling {@link #reconfigure(int, int, Config)}
312c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * with the current height and width.</p>
313c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
314c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>WARNING: this method should not be used on bitmaps currently used by
315c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the view system, see {@link #reconfigure(int, int, Config)} for more
316c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * details.</p>
317c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
318c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #reconfigure(int, int, Config)
319c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setWidth(int)
320c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setHeight(int)
321c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     */
322c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    public void setConfig(Config config) {
323c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        reconfigure(getWidth(), getHeight(), config);
324c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    }
325c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the nine patch chunk.
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param chunk The definition of the nine patch
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setNinePatchChunk(byte[] chunk) {
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNinePatchChunk = chunk;
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3368cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
338a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * Free the native object associated with this bitmap, and clear the
339a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * reference to the pixel data. This will not free the pixel data synchronously;
340a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * it simply allows it to be garbage collected if there are no other references.
341a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * The bitmap is marked as "dead", meaning it will throw an exception if
342a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * getPixels() or setPixels() is called, and will draw nothing. This operation
343a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * cannot be reversed, so it should only be called if you are sure there are no
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * further uses for the bitmap. This is an advanced call, and normally need
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * not be called, since the normal GC process will free up this memory when
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * there are no more references to this bitmap.
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void recycle() {
349775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        if (!mRecycled && mNativePtr != 0) {
350775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler            if (nativeRecycle(mNativePtr)) {
351547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                // return value indicates whether native pixel object was actually recycled.
352547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                // false indicates that it is still in use at the native level and these
353547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                // objects should not be collected now. They will be collected later when the
354547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                // Bitmap itself is collected.
355547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                mNinePatchChunk = null;
356547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase            }
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mRecycled = true;
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns true if this bitmap has been recycled. If so, then it is an error
3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * to try to access its pixels, and the bitmap will not draw.
3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true if the bitmap has been recycled
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final boolean isRecycled() {
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mRecycled;
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3708cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3720bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     * Returns the generation ID of this bitmap. The generation ID changes
3730bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     * whenever the bitmap is modified. This can be used as an efficient way to
3740bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     * check if a bitmap has changed.
375f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
3760bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     * @return The current generation ID for this bitmap.
3770bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     */
3780bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    public int getGenerationId() {
37901a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
38001a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getGenerationId() on a recycle()'d bitmap! This is undefined behavior!");
38101a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
382775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeGenerationId(mNativePtr);
3830bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    }
384f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
3850bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    /**
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This is called by methods that want to throw an exception if the bitmap
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * has already been recycled.
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void checkRecycled(String errorMessage) {
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mRecycled) {
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalStateException(errorMessage);
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3948cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
396980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv     * This is called by methods that want to throw an exception if the bitmap
397980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv     * is {@link Config#HARDWARE}.
398980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv     */
399980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv    private void checkHardware(String errorMessage) {
400980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv        if (getConfig() == Config.HARDWARE) {
401980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv            throw new IllegalStateException(errorMessage);
402980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv        }
403980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv    }
404980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv
405980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv    /**
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Common code for checking that x and y are >= 0
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x x coordinate to ensure is >= 0
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y y coordinate to ensure is >= 0
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static void checkXYSign(int x, int y) {
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (x < 0) {
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("x must be >= 0");
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (y < 0) {
4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("y must be >= 0");
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Common code for checking that width and height are > 0
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width  width to ensure is > 0
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height height to ensure is > 0
4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static void checkWidthHeight(int width, int height) {
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (width <= 0) {
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("width must be > 0");
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (height <= 0) {
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("height must be > 0");
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
435676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy    /**
436676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy     * Possible bitmap configurations. A bitmap configuration describes
437676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy     * how pixels are stored. This affects the quality (color depth) as
438676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy     * well as the ability to display transparent/translucent colors.
439676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy     */
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public enum Config {
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // these native values must match up with the enum in SkBitmap.h
442676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
443676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        /**
444676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * Each pixel is stored as a single translucency (alpha) channel.
445676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This is very useful to efficiently store masks for instance.
446676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * No color information is stored.
447676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * With this configuration, each pixel requires 1 byte of memory.
448676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         */
449b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        ALPHA_8     (1),
450676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
451676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        /**
452676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * Each pixel is stored on 2 bytes and only the RGB channels are
453676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * encoded: red is stored with 5 bits of precision (32 possible
454676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * values), green is stored with 6 bits of precision (64 possible
455676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * values) and blue is stored with 5 bits of precision.
456f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
457676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This configuration can produce slight visual artifacts depending
458676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * on the configuration of the source. For instance, without
459676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * dithering, the result might show a greenish tint. To get better
460676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * results dithering should be applied.
461f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
462676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This configuration may be useful when using opaque bitmaps
463676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * that do not require high color fidelity.
464186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         *
465186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         * <p>Use this formula to pack into 16 bits:</p>
466186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         * <pre class="prettyprint">
467186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         * short color = (R & 0x1f) << 11 | (G & 0x3f) << 5 | (B & 0x1f);
468186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         * </pre>
469676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         */
470b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        RGB_565     (3),
471676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
472676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        /**
473676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * Each pixel is stored on 2 bytes. The three RGB color channels
474676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * and the alpha channel (translucency) are stored with a 4 bits
475676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * precision (16 possible values.)
476f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
477676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This configuration is mostly useful if the application needs
478676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * to store translucency information but also needs to save
479676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * memory.
480f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
481676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * It is recommended to use {@link #ARGB_8888} instead of this
482676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * configuration.
48325ba1c86945a441428194d9ebcabbf31be75a45aRomain Guy         *
484e8222dddaf2e3da14380101e818d4254899e0c0dChet Haase         * Note: as of {@link android.os.Build.VERSION_CODES#KITKAT},
48525ba1c86945a441428194d9ebcabbf31be75a45aRomain Guy         * any bitmap created with this configuration will be created
48625ba1c86945a441428194d9ebcabbf31be75a45aRomain Guy         * using {@link #ARGB_8888} instead.
487f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
488676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * @deprecated Because of the poor quality of this configuration,
489676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         *             it is advised to use {@link #ARGB_8888} instead.
490676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         */
491676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        @Deprecated
492b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        ARGB_4444   (4),
493676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
494676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        /**
495676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * Each pixel is stored on 4 bytes. Each channel (RGB and alpha
496676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * for translucency) is stored with 8 bits of precision (256
497676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * possible values.)
498f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
499676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This configuration is very flexible and offers the best
500676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * quality. It should be used whenever possible.
501186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         *
502186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         * <p>Use this formula to pack into 32 bits:</p>
503186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         * <pre class="prettyprint">
504186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         * int color = (A & 0xff) << 24 | (B & 0xff) << 16 | (G & 0xff) << 8 | (R & 0xff);
505186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         * </pre>
506676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         */
507da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv        ARGB_8888   (5),
508da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv
5099505a6552764461c22ce48f1ac13d025d23e1579Romain Guy        /**
5109505a6552764461c22ce48f1ac13d025d23e1579Romain Guy         * Each pixels is stored on 8 bytes. Each channel (RGB and alpha
5119505a6552764461c22ce48f1ac13d025d23e1579Romain Guy         * for translucency) is stored as a
5129505a6552764461c22ce48f1ac13d025d23e1579Romain Guy         * {@link android.util.Half half-precision floating point value}.
5139505a6552764461c22ce48f1ac13d025d23e1579Romain Guy         *
5149505a6552764461c22ce48f1ac13d025d23e1579Romain Guy         * This configuration is particularly suited for wide-gamut and
5159505a6552764461c22ce48f1ac13d025d23e1579Romain Guy         * HDR content.
516186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         *
517186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         * <p>Use this formula to pack into 64 bits:</p>
518186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         * <pre class="prettyprint">
519186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         * long color = (A & 0xffff) << 48 | (B & 0xffff) << 32 | (G & 0xffff) << 16 | (R & 0xffff);
520186ea1218ee5dcb7650d0759a2af4b61af166cbeDerek Sollenberger         * </pre>
5219505a6552764461c22ce48f1ac13d025d23e1579Romain Guy         */
5229505a6552764461c22ce48f1ac13d025d23e1579Romain Guy        RGBA_F16    (6),
523da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv
524da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv        /**
5259fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv         * Special configuration, when bitmap is stored only in graphic memory.
5269fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv         * Bitmaps in this configuration are always immutable.
5279fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv         *
5289fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv         * It is optimal for cases, when the only operation with the bitmap is to draw it on a
5299fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv         * screen.
530da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv         */
5319505a6552764461c22ce48f1ac13d025d23e1579Romain Guy        HARDWARE    (7);
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
533676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        final int nativeInt;
534676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
535676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        private static Config sConfigs[] = {
5369505a6552764461c22ce48f1ac13d025d23e1579Romain Guy            null, ALPHA_8, null, RGB_565, ARGB_4444, ARGB_8888, RGBA_F16, HARDWARE
537676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        };
538f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Config(int ni) {
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            this.nativeInt = ni;
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5428cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
543676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        static Config nativeToConfig(int ni) {
5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return sConfigs[ni];
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5478cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5493849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Copy the bitmap's pixels into the specified buffer (allocated by the
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * caller). An exception is thrown if the buffer is not large enough to
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * hold all of the pixels (taking into account the number of bytes per
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pixel) or if the Buffer subclass is not one of the support types
5533849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * (ByteBuffer, ShortBuffer, IntBuffer).</p>
5543849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>The content of the bitmap is copied into the buffer as-is. This means
5553849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * that if this bitmap stores its pixels pre-multiplied
5563849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * (see {@link #isPremultiplied()}, the values in the buffer will also be
557ce217faddb4b40c1b3e698944da1951027080427Romain Guy     * pre-multiplied. The pixels remain in the color space of the bitmap.</p>
5581f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * <p>After this method returns, the current position of the buffer is
5591f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * updated: the position is incremented by the number of elements written
5601f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * in the buffer.</p>
561980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv     * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE}
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void copyPixelsToBuffer(Buffer dst) {
564980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv        checkHardware("unable to copyPixelsToBuffer, "
565980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv                + "pixel access is not supported on Config#HARDWARE bitmaps");
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int elements = dst.remaining();
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int shift;
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (dst instanceof ByteBuffer) {
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 0;
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (dst instanceof ShortBuffer) {
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 1;
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (dst instanceof IntBuffer) {
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 2;
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("unsupported Buffer subclass");
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5778cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        long bufferSize = (long)elements << shift;
579f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson        long pixelSize = getByteCount();
5808cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (bufferSize < pixelSize) {
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("Buffer not large enough for pixels");
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5848cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
585775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeCopyPixelsToBuffer(mNativePtr, dst);
5868cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // now update the buffer's position
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int position = dst.position();
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        position += pixelSize >> shift;
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        dst.position(position);
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5943849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Copy the pixels from the buffer, beginning at the current position,
5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * overwriting the bitmap's pixels. The data in the buffer is not changed
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * in any way (unlike setPixels(), which converts from unpremultipled 32bit
597ce217faddb4b40c1b3e698944da1951027080427Romain Guy     * to whatever the bitmap's native format is. The pixels in the source
598ce217faddb4b40c1b3e698944da1951027080427Romain Guy     * buffer are assumed to be in the bitmap's color space.</p>
5991f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * <p>After this method returns, the current position of the buffer is
6001f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * updated: the position is incremented by the number of elements read from
6011f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * the buffer. If you need to read the bitmap from the buffer again you must
6021f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * first rewind the buffer.</p>
603980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv     * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE}
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void copyPixelsFromBuffer(Buffer src) {
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("copyPixelsFromBuffer called on recycled bitmap");
607980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv        checkHardware("unable to copyPixelsFromBuffer, Config#HARDWARE bitmaps are immutable");
6088cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int elements = src.remaining();
6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int shift;
6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (src instanceof ByteBuffer) {
6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 0;
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (src instanceof ShortBuffer) {
6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 1;
6159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (src instanceof IntBuffer) {
6169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 2;
6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
6189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("unsupported Buffer subclass");
6199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6208cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
6213849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy        long bufferBytes = (long) elements << shift;
622f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson        long bitmapBytes = getByteCount();
6238cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (bufferBytes < bitmapBytes) {
6259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("Buffer not large enough for pixels");
6269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6278cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
628775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeCopyPixelsFromBuffer(mNativePtr, src);
62955adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen
63055adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen        // now update the buffer's position
63155adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen        int position = src.position();
63255adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen        position += bitmapBytes >> shift;
63355adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen        src.position(position);
6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6358cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
6369fd259c9b0878ba52fec86173d76976e70977fcdsergeyv    private void noteHardwareBitmapSlowCall() {
6379fd259c9b0878ba52fec86173d76976e70977fcdsergeyv        if (getConfig() == Config.HARDWARE) {
6389fd259c9b0878ba52fec86173d76976e70977fcdsergeyv            StrictMode.noteSlowCall("Warning: attempt to read pixels from hardware "
6399fd259c9b0878ba52fec86173d76976e70977fcdsergeyv                    + "bitmap, which is very slow operation");
6409fd259c9b0878ba52fec86173d76976e70977fcdsergeyv        }
6419fd259c9b0878ba52fec86173d76976e70977fcdsergeyv    }
6429fd259c9b0878ba52fec86173d76976e70977fcdsergeyv
6439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Tries to make a new bitmap based on the dimensions of this bitmap,
6459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * setting the new bitmap's config to the one specified, and then copying
6469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * this bitmap's pixels into the new bitmap. If the conversion is not
64796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * supported, or the allocator fails, then this returns NULL.  The returned
6488242656f495847c50c9ceaea92839dce78218a75Romain Guy     * bitmap has the same density and color space as the original.
6499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
6509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param config    The desired config for the resulting bitmap
6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param isMutable True if the resulting bitmap should be mutable (i.e.
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                  its pixels can be modified)
6539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the new bitmap, or null if the copy could not be made.
65405126d151eb3caa85bd3a039cffb6e37940c3fa4sergeyv     * @throws IllegalArgumentException if config is {@link Config#HARDWARE} and isMutable is true
6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public Bitmap copy(Config config, boolean isMutable) {
6579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't copy a recycled bitmap");
65805126d151eb3caa85bd3a039cffb6e37940c3fa4sergeyv        if (config == Config.HARDWARE && isMutable) {
65905126d151eb3caa85bd3a039cffb6e37940c3fa4sergeyv            throw new IllegalArgumentException("Hardware bitmaps are always immutable");
66005126d151eb3caa85bd3a039cffb6e37940c3fa4sergeyv        }
6619fd259c9b0878ba52fec86173d76976e70977fcdsergeyv        noteHardwareBitmapSlowCall();
662775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        Bitmap b = nativeCopy(mNativePtr, config.nativeInt, isMutable);
66396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        if (b != null) {
66457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III            b.setPremultiplied(mRequestPremultiplied);
66596e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn            b.mDensity = mDensity;
66696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        }
66796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        return b;
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
67002d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick    /**
671721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews     * Creates a new immutable bitmap backed by ashmem which can efficiently
6728242656f495847c50c9ceaea92839dce78218a75Romain Guy     * be passed between processes. The bitmap is assumed to be in the sRGB
6738242656f495847c50c9ceaea92839dce78218a75Romain Guy     * color space.
674721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews     *
675721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews     * @hide
676721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews     */
677721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    public Bitmap createAshmemBitmap() {
678721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews        checkRecycled("Can't copy a recycled bitmap");
6799fd259c9b0878ba52fec86173d76976e70977fcdsergeyv        noteHardwareBitmapSlowCall();
680775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        Bitmap b = nativeCopyAshmem(mNativePtr);
681721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews        if (b != null) {
682721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews            b.setPremultiplied(mRequestPremultiplied);
683721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews            b.mDensity = mDensity;
684721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews        }
685721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews        return b;
686721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    }
687721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews
688721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    /**
689a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson     * Creates a new immutable bitmap backed by ashmem which can efficiently
6908242656f495847c50c9ceaea92839dce78218a75Romain Guy     * be passed between processes. The bitmap is assumed to be in the sRGB
6918242656f495847c50c9ceaea92839dce78218a75Romain Guy     * color space.
692a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson     *
693a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson     * @hide
694a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson     */
695a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson    public Bitmap createAshmemBitmap(Config config) {
696a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson        checkRecycled("Can't copy a recycled bitmap");
6979fd259c9b0878ba52fec86173d76976e70977fcdsergeyv        noteHardwareBitmapSlowCall();
698a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson        Bitmap b = nativeCopyAshmemConfig(mNativePtr, config.nativeInt);
699a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson        if (b != null) {
700a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson            b.setPremultiplied(mRequestPremultiplied);
701a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson            b.mDensity = mDensity;
702a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson        }
703a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson        return b;
704a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson    }
705a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson
706a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson    /**
7070a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv     * Create hardware bitmap backed GraphicBuffer.
7080a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv     *
7090a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv     * @return Bitmap or null if this GraphicBuffer has unsupported PixelFormat.
7100a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv     *         currently PIXEL_FORMAT_RGBA_8888 is the only supported format
7110a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv     * @hide
7120a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv     */
7138242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createHardwareBitmap(@NonNull GraphicBuffer graphicBuffer) {
7140a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv        return nativeCreateHardwareBitmap(graphicBuffer);
7150a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv    }
7160a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv
7170a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv    /**
718f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * Creates a new bitmap, scaled from an existing bitmap, when possible. If the
719f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     * specified width and height are the same as the current width and height of
7209f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * the source bitmap, the source bitmap is returned and no new bitmap is
721f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * created.
72202d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     *
72302d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     * @param src       The source bitmap.
72402d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     * @param dstWidth  The new bitmap's desired width.
72502d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     * @param dstHeight The new bitmap's desired height.
72602d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     * @param filter    true if the source should be filtered.
727f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * @return The new scaled bitmap or the source bitmap if no scaling is required.
728790552b240c05d58407f7c14acba656b2e85c523Romain Guy     * @throws IllegalArgumentException if width is <= 0, or height is <= 0
72902d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     */
7308242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight,
731f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy            boolean filter) {
7328242656f495847c50c9ceaea92839dce78218a75Romain Guy        Matrix m = new Matrix();
7338cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
7349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final int width = src.getWidth();
7359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final int height = src.getHeight();
7368242656f495847c50c9ceaea92839dce78218a75Romain Guy        if (width != dstWidth || height != dstHeight) {
7378242656f495847c50c9ceaea92839dce78218a75Romain Guy            final float sx = dstWidth / (float) width;
7388242656f495847c50c9ceaea92839dce78218a75Romain Guy            final float sy = dstHeight / (float) height;
7398242656f495847c50c9ceaea92839dce78218a75Romain Guy            m.setScale(sx, sy);
7409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7418242656f495847c50c9ceaea92839dce78218a75Romain Guy        return Bitmap.createBitmap(src, 0, 0, width, height, m, filter);
7429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7438cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
7449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
7459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns an immutable bitmap from the source bitmap. The new bitmap may
74696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * be the same object as source, or a copy may have been made.  It is
7478242656f495847c50c9ceaea92839dce78218a75Romain Guy     * initialized with the same density and color space as the original bitmap.
7489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7498242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createBitmap(@NonNull Bitmap src) {
7509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return createBitmap(src, 0, 0, src.getWidth(), src.getHeight());
7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
7549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns an immutable bitmap from the specified subset of the source
7559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * bitmap. The new bitmap may be the same object as source, or a copy may
7568242656f495847c50c9ceaea92839dce78218a75Romain Guy     * have been made. It is initialized with the same density and color space
7578242656f495847c50c9ceaea92839dce78218a75Romain Guy     * as the original bitmap.
7589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
7599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param source   The bitmap we are subsetting
7609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x        The x coordinate of the first pixel in source
7619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y        The y coordinate of the first pixel in source
7629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The number of pixels in each row
7639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The number of rows
764f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * @return A copy of a subset of the source bitmap or the source bitmap itself.
765790552b240c05d58407f7c14acba656b2e85c523Romain Guy     * @throws IllegalArgumentException if the x, y, width, height values are
766790552b240c05d58407f7c14acba656b2e85c523Romain Guy     *         outside of the dimensions of the source bitmap, or width is <= 0,
767790552b240c05d58407f7c14acba656b2e85c523Romain Guy     *         or height is <= 0
7689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7698242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createBitmap(@NonNull Bitmap source, int x, int y, int width, int height) {
7709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return createBitmap(source, x, y, width, height, null, false);
7719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7728cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
7739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
7749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns an immutable bitmap from subset of the source bitmap,
775f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * transformed by the optional matrix. The new bitmap may be the
776f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * same object as source, or a copy may have been made. It is
7778242656f495847c50c9ceaea92839dce78218a75Romain Guy     * initialized with the same density and color space as the original
7788242656f495847c50c9ceaea92839dce78218a75Romain Guy     * bitmap.
779f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
780f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * If the source bitmap is immutable and the requested subset is the
781f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * same as the source bitmap itself, then the source bitmap is
782f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * returned and no new bitmap is created.
7839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
7849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param source   The bitmap we are subsetting
7859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x        The x coordinate of the first pixel in source
7869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y        The y coordinate of the first pixel in source
7879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The number of pixels in each row
7889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The number of rows
78960b88edea7132ddce90f2dced07c6706f1502270Ken Shirriff     * @param m        Optional matrix to be applied to the pixels
7909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param filter   true if the source should be filtered.
7919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                   Only applies if the matrix contains more than just
7929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                   translation.
7939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return A bitmap that represents the specified subset of source
7949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if the x, y, width, height values are
795790552b240c05d58407f7c14acba656b2e85c523Romain Guy     *         outside of the dimensions of the source bitmap, or width is <= 0,
796790552b240c05d58407f7c14acba656b2e85c523Romain Guy     *         or height is <= 0
7979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7988242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createBitmap(@NonNull Bitmap source, int x, int y, int width, int height,
7998242656f495847c50c9ceaea92839dce78218a75Romain Guy            @Nullable Matrix m, boolean filter) {
8009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkXYSign(x, y);
8029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkWidthHeight(width, height);
8039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (x + width > source.getWidth()) {
8049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("x + width must be <= bitmap.width()");
8059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (y + height > source.getHeight()) {
8079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("y + height must be <= bitmap.height()");
8089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // check if we can just return our argument unchanged
8119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() &&
8129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                height == source.getHeight() && (m == null || m.isIdentity())) {
8139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return source;
8149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8158cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
81681f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv        boolean isHardware = source.getConfig() == Config.HARDWARE;
81781f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv        if (isHardware) {
8189fd259c9b0878ba52fec86173d76976e70977fcdsergeyv            source.noteHardwareBitmapSlowCall();
81981f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv            source = nativeCopyPreserveInternalConfig(source.mNativePtr);
82081f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv        }
82181f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv
8229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int neww = width;
8239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int newh = height;
8249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Bitmap bitmap;
8259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Paint paint;
8269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Rect srcR = new Rect(x, y, x + width, y + height);
8289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        RectF dstR = new RectF(0, 0, width, height);
82929cd3e922612afff4cd5fa9694013e5e8ae93661Derek Sollenberger        RectF deviceR = new RectF();
8309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
831feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        Config newConfig = Config.ARGB_8888;
832feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        final Config config = source.getConfig();
833feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        // GIF files generate null configs, assume ARGB_8888
834feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        if (config != null) {
835feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy            switch (config) {
836feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                case RGB_565:
837feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    newConfig = Config.RGB_565;
838feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    break;
839feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                case ALPHA_8:
840feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    newConfig = Config.ALPHA_8;
841feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    break;
8429505a6552764461c22ce48f1ac13d025d23e1579Romain Guy                case RGBA_F16:
8439505a6552764461c22ce48f1ac13d025d23e1579Romain Guy                    newConfig = Config.RGBA_F16;
8449505a6552764461c22ce48f1ac13d025d23e1579Romain Guy                    break;
845676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy                //noinspection deprecation
846feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                case ARGB_4444:
847feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                case ARGB_8888:
848feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                default:
849feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    newConfig = Config.ARGB_8888;
850feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    break;
851feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy            }
852feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        }
853d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
8549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (m == null || m.isIdentity()) {
855d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy            bitmap = createBitmap(neww, newh, newConfig, source.hasAlpha());
8569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            paint = null;   // not needed
8579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
858d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy            final boolean transformed = !m.rectStaysRect();
859d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
8609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            m.mapRect(deviceR, dstR);
861d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
8629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            neww = Math.round(deviceR.width());
8639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            newh = Math.round(deviceR.height());
864d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
8659505a6552764461c22ce48f1ac13d025d23e1579Romain Guy            Config transformedConfig = newConfig;
8669505a6552764461c22ce48f1ac13d025d23e1579Romain Guy            if (transformed) {
8679505a6552764461c22ce48f1ac13d025d23e1579Romain Guy                if (transformedConfig != Config.ARGB_8888 && transformedConfig != Config.RGBA_F16) {
8689505a6552764461c22ce48f1ac13d025d23e1579Romain Guy                    transformedConfig = Config.ARGB_8888;
8699505a6552764461c22ce48f1ac13d025d23e1579Romain Guy                }
8709505a6552764461c22ce48f1ac13d025d23e1579Romain Guy            }
8719505a6552764461c22ce48f1ac13d025d23e1579Romain Guy            bitmap = createBitmap(neww, newh, transformedConfig, transformed || source.hasAlpha());
872d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
8739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            paint = new Paint();
8749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            paint.setFilterBitmap(filter);
875d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy            if (transformed) {
8769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                paint.setAntiAlias(true);
8779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
8789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8798790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III
8808242656f495847c50c9ceaea92839dce78218a75Romain Guy        nativeCopyColorSpace(source.mNativePtr, bitmap.mNativePtr);
8818242656f495847c50c9ceaea92839dce78218a75Romain Guy
8829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // The new bitmap was created from a known bitmap source so assume that
88311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        // they use the same density
88411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        bitmap.mDensity = source.mDensity;
88557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        bitmap.setHasAlpha(source.hasAlpha());
88657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        bitmap.setPremultiplied(source.mRequestPremultiplied);
8878790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III
88829cd3e922612afff4cd5fa9694013e5e8ae93661Derek Sollenberger        Canvas canvas = new Canvas(bitmap);
88929cd3e922612afff4cd5fa9694013e5e8ae93661Derek Sollenberger        canvas.translate(-deviceR.left, -deviceR.top);
89029cd3e922612afff4cd5fa9694013e5e8ae93661Derek Sollenberger        canvas.concat(m);
89196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        canvas.drawBitmap(source, srcR, dstR, paint);
8926311d0a079702b29984c0d31937345be105e1a5eDianne Hackborn        canvas.setBitmap(null);
89381f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv        if (isHardware) {
89481f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv            return bitmap.copy(Config.HARDWARE, false);
89581f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv        }
8969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bitmap;
8979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8988cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
8999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
90096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * Returns a mutable bitmap with the specified width and height.  Its
9018242656f495847c50c9ceaea92839dce78218a75Romain Guy     * initial density is as per {@link #getDensity}. The newly created
9028242656f495847c50c9ceaea92839dce78218a75Romain Guy     * bitmap is in the {@link ColorSpace.Named#SRGB sRGB} color space.
9039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
9049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The width of the bitmap
9059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The height of the bitmap
9069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param config   The bitmap config to create.
9079fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     * @throws IllegalArgumentException if the width or height are <= 0, or if
9089fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     *         Config is Config.HARDWARE, because hardware bitmaps are always immutable
9099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
9108242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createBitmap(int width, int height, @NonNull Config config) {
911d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy        return createBitmap(width, height, config, true);
912d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy    }
913d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
914d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy    /**
915d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * Returns a mutable bitmap with the specified width and height.  Its
916dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * initial density is determined from the given {@link DisplayMetrics}.
9178242656f495847c50c9ceaea92839dce78218a75Romain Guy     * The newly created bitmap is in the {@link ColorSpace.Named#SRGB sRGB}
9188242656f495847c50c9ceaea92839dce78218a75Romain Guy     * color space.
919dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *
920dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param display  Display metrics for the display this bitmap will be
921dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 drawn on.
922dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param width    The width of the bitmap
923dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param height   The height of the bitmap
924dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param config   The bitmap config to create.
9259fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     * @throws IllegalArgumentException if the width or height are <= 0, or if
9269fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     *         Config is Config.HARDWARE, because hardware bitmaps are always immutable
927dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     */
9288242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createBitmap(@Nullable DisplayMetrics display, int width,
9298242656f495847c50c9ceaea92839dce78218a75Romain Guy            int height, @NonNull Config config) {
930dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(display, width, height, config, true);
931dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    }
932dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
933dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    /**
934dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * Returns a mutable bitmap with the specified width and height.  Its
9358242656f495847c50c9ceaea92839dce78218a75Romain Guy     * initial density is as per {@link #getDensity}. The newly created
9368242656f495847c50c9ceaea92839dce78218a75Romain Guy     * bitmap is in the {@link ColorSpace.Named#SRGB sRGB} color space.
937d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     *
938d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * @param width    The width of the bitmap
939d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * @param height   The height of the bitmap
940d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * @param config   The bitmap config to create.
9419505a6552764461c22ce48f1ac13d025d23e1579Romain Guy     * @param hasAlpha If the bitmap is ARGB_8888 or RGBA_16F this flag can be used to
9429505a6552764461c22ce48f1ac13d025d23e1579Romain Guy     *                 mark the bitmap as opaque. Doing so will clear the bitmap in black
943f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *                 instead of transparent.
944f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
9459fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     * @throws IllegalArgumentException if the width or height are <= 0, or if
9469fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     *         Config is Config.HARDWARE, because hardware bitmaps are always immutable
947d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     */
9488242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createBitmap(int width, int height,
9498242656f495847c50c9ceaea92839dce78218a75Romain Guy            @NonNull Config config, boolean hasAlpha) {
950dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(null, width, height, config, hasAlpha);
951dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    }
952dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
953dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    /**
954dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * Returns a mutable bitmap with the specified width and height.  Its
9558242656f495847c50c9ceaea92839dce78218a75Romain Guy     * initial density is as per {@link #getDensity}.
9568242656f495847c50c9ceaea92839dce78218a75Romain Guy     *
9578242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @param width    The width of the bitmap
9588242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @param height   The height of the bitmap
9598242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @param config   The bitmap config to create.
9608242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @param hasAlpha If the bitmap is ARGB_8888 or RGBA_16F this flag can be used to
9618242656f495847c50c9ceaea92839dce78218a75Romain Guy     *                 mark the bitmap as opaque. Doing so will clear the bitmap in black
9628242656f495847c50c9ceaea92839dce78218a75Romain Guy     *                 instead of transparent.
9631fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy     * @param colorSpace The color space of the bitmap. If the config is {@link Config#RGBA_F16},
9641fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy     *                   {@link ColorSpace.Named#EXTENDED_SRGB scRGB} is assumed, and if the
9651fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy     *                   config is not {@link Config#ARGB_8888}, {@link ColorSpace.Named#SRGB sRGB}
9661fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy     *                   is assumed.
9678242656f495847c50c9ceaea92839dce78218a75Romain Guy     *
9688242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @throws IllegalArgumentException if the width or height are <= 0, if
9698242656f495847c50c9ceaea92839dce78218a75Romain Guy     *         Config is Config.HARDWARE (because hardware bitmaps are always
9708242656f495847c50c9ceaea92839dce78218a75Romain Guy     *         immutable), if the specified color space is not {@link ColorSpace.Model#RGB RGB},
9711fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy     *         if the specified color space's transfer function is not an
9721fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy     *         {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}, or if
9731fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy     *         the color space is null
9748242656f495847c50c9ceaea92839dce78218a75Romain Guy     */
9758242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createBitmap(int width, int height, @NonNull Config config,
9761fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy            boolean hasAlpha, @NonNull ColorSpace colorSpace) {
9778242656f495847c50c9ceaea92839dce78218a75Romain Guy        return createBitmap(null, width, height, config, hasAlpha, colorSpace);
9788242656f495847c50c9ceaea92839dce78218a75Romain Guy    }
9798242656f495847c50c9ceaea92839dce78218a75Romain Guy
9808242656f495847c50c9ceaea92839dce78218a75Romain Guy    /**
9818242656f495847c50c9ceaea92839dce78218a75Romain Guy     * Returns a mutable bitmap with the specified width and height.  Its
982dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * initial density is determined from the given {@link DisplayMetrics}.
9838242656f495847c50c9ceaea92839dce78218a75Romain Guy     * The newly created bitmap is in the {@link ColorSpace.Named#SRGB sRGB}
9848242656f495847c50c9ceaea92839dce78218a75Romain Guy     * color space.
985dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *
986dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param display  Display metrics for the display this bitmap will be
987dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 drawn on.
988dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param width    The width of the bitmap
989dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param height   The height of the bitmap
990dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param config   The bitmap config to create.
9919505a6552764461c22ce48f1ac13d025d23e1579Romain Guy     * @param hasAlpha If the bitmap is ARGB_8888 or RGBA_16F this flag can be used to
9929505a6552764461c22ce48f1ac13d025d23e1579Romain Guy     *                 mark the bitmap as opaque. Doing so will clear the bitmap in black
993f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *                 instead of transparent.
994f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
9959fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     * @throws IllegalArgumentException if the width or height are <= 0, or if
9969fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     *         Config is Config.HARDWARE, because hardware bitmaps are always immutable
997dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     */
9988242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createBitmap(@Nullable DisplayMetrics display, int width, int height,
9998242656f495847c50c9ceaea92839dce78218a75Romain Guy            @NonNull Config config, boolean hasAlpha) {
10001fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy        return createBitmap(display, width, height, config, hasAlpha,
10011fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy                ColorSpace.get(ColorSpace.Named.SRGB));
10028242656f495847c50c9ceaea92839dce78218a75Romain Guy    }
10038242656f495847c50c9ceaea92839dce78218a75Romain Guy
10048242656f495847c50c9ceaea92839dce78218a75Romain Guy    /**
10058242656f495847c50c9ceaea92839dce78218a75Romain Guy     * Returns a mutable bitmap with the specified width and height.  Its
10068242656f495847c50c9ceaea92839dce78218a75Romain Guy     * initial density is determined from the given {@link DisplayMetrics}.
10078242656f495847c50c9ceaea92839dce78218a75Romain Guy     * The newly created bitmap is in the {@link ColorSpace.Named#SRGB sRGB}
10088242656f495847c50c9ceaea92839dce78218a75Romain Guy     * color space.
10098242656f495847c50c9ceaea92839dce78218a75Romain Guy     *
10108242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @param display  Display metrics for the display this bitmap will be
10118242656f495847c50c9ceaea92839dce78218a75Romain Guy     *                 drawn on.
10128242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @param width    The width of the bitmap
10138242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @param height   The height of the bitmap
10148242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @param config   The bitmap config to create.
10158242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @param hasAlpha If the bitmap is ARGB_8888 or RGBA_16F this flag can be used to
10168242656f495847c50c9ceaea92839dce78218a75Romain Guy     *                 mark the bitmap as opaque. Doing so will clear the bitmap in black
10178242656f495847c50c9ceaea92839dce78218a75Romain Guy     *                 instead of transparent.
10181fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy     * @param colorSpace The color space of the bitmap. If the config is {@link Config#RGBA_F16},
10191fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy     *                   {@link ColorSpace.Named#EXTENDED_SRGB scRGB} is assumed, and if the
10201fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy     *                   config is not {@link Config#ARGB_8888}, {@link ColorSpace.Named#SRGB sRGB}
10211fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy     *                   is assumed.
10228242656f495847c50c9ceaea92839dce78218a75Romain Guy     *
10238242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @throws IllegalArgumentException if the width or height are <= 0, if
10248242656f495847c50c9ceaea92839dce78218a75Romain Guy     *         Config is Config.HARDWARE (because hardware bitmaps are always
10258242656f495847c50c9ceaea92839dce78218a75Romain Guy     *         immutable), if the specified color space is not {@link ColorSpace.Model#RGB RGB},
10261fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy     *         if the specified color space's transfer function is not an
10271fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy     *         {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}, or if
10281fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy     *         the color space is null
10298242656f495847c50c9ceaea92839dce78218a75Romain Guy     */
10308242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createBitmap(@Nullable DisplayMetrics display, int width, int height,
10311fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy            @NonNull Config config, boolean hasAlpha, @NonNull ColorSpace colorSpace) {
1032e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        if (width <= 0 || height <= 0) {
1033e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy            throw new IllegalArgumentException("width and height must be > 0");
1034e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        }
10359fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv        if (config == Config.HARDWARE) {
10369fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv            throw new IllegalArgumentException("can't create mutable bitmap with Config.HARDWARE");
10379fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv        }
10381fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy        if (colorSpace == null) {
10391fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy            throw new IllegalArgumentException("can't create bitmap without a color space");
10401fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy        }
10418242656f495847c50c9ceaea92839dce78218a75Romain Guy
10428242656f495847c50c9ceaea92839dce78218a75Romain Guy        Bitmap bm;
104308587d45b103d749c1483ec7fae80888985b5eaaRomain Guy        // nullptr color spaces have a particular meaning in native and are interpreted as sRGB
104408587d45b103d749c1483ec7fae80888985b5eaaRomain Guy        // (we also avoid the unnecessary extra work of the else branch)
104508587d45b103d749c1483ec7fae80888985b5eaaRomain Guy        if (config != Config.ARGB_8888 || colorSpace == ColorSpace.get(ColorSpace.Named.SRGB)) {
10468242656f495847c50c9ceaea92839dce78218a75Romain Guy            bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true, null, null);
10478242656f495847c50c9ceaea92839dce78218a75Romain Guy        } else {
10488242656f495847c50c9ceaea92839dce78218a75Romain Guy            if (!(colorSpace instanceof ColorSpace.Rgb)) {
10498242656f495847c50c9ceaea92839dce78218a75Romain Guy                throw new IllegalArgumentException("colorSpace must be an RGB color space");
10508242656f495847c50c9ceaea92839dce78218a75Romain Guy            }
10518242656f495847c50c9ceaea92839dce78218a75Romain Guy            ColorSpace.Rgb rgb = (ColorSpace.Rgb) colorSpace;
10528242656f495847c50c9ceaea92839dce78218a75Romain Guy            ColorSpace.Rgb.TransferParameters parameters = rgb.getTransferParameters();
10538242656f495847c50c9ceaea92839dce78218a75Romain Guy            if (parameters == null) {
10548242656f495847c50c9ceaea92839dce78218a75Romain Guy                throw new IllegalArgumentException("colorSpace must use an ICC "
10558242656f495847c50c9ceaea92839dce78218a75Romain Guy                        + "parametric transfer function");
10568242656f495847c50c9ceaea92839dce78218a75Romain Guy            }
10578242656f495847c50c9ceaea92839dce78218a75Romain Guy
10588242656f495847c50c9ceaea92839dce78218a75Romain Guy            ColorSpace.Rgb d50 = (ColorSpace.Rgb) ColorSpace.adapt(rgb, ColorSpace.ILLUMINANT_D50);
10598242656f495847c50c9ceaea92839dce78218a75Romain Guy            bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true,
10608242656f495847c50c9ceaea92839dce78218a75Romain Guy                    d50.getTransform(), parameters);
10618242656f495847c50c9ceaea92839dce78218a75Romain Guy        }
10628242656f495847c50c9ceaea92839dce78218a75Romain Guy
1063dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        if (display != null) {
1064dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            bm.mDensity = display.densityDpi;
1065dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        }
10668790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III        bm.setHasAlpha(hasAlpha);
10679505a6552764461c22ce48f1ac13d025d23e1579Romain Guy        if ((config == Config.ARGB_8888 || config == Config.RGBA_F16) && !hasAlpha) {
1068775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler            nativeErase(bm.mNativePtr, 0xff000000);
1069d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy        }
10703b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        // No need to initialize the bitmap to zeroes with other configs;
10713b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        // it is backed by a VM byte array which is by definition preinitialized
10723b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        // to all zeroes.
10739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bm;
10749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10758cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
10769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
10779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a immutable bitmap with the specified width and height, with each
107896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * pixel value set to the corresponding value in the colors array.  Its
10798242656f495847c50c9ceaea92839dce78218a75Romain Guy     * initial density is as per {@link #getDensity}. The newly created
10808242656f495847c50c9ceaea92839dce78218a75Romain Guy     * bitmap is in the {@link ColorSpace.Named#SRGB sRGB} color space.
10819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
10828242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @param colors   Array of sRGB {@link Color colors} used to initialize the pixels.
10839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset   Number of values to skip before the first color in the
10849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 array of colors.
10859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stride   Number of colors in the array between rows (must be >=
10869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 width or <= -width).
10879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The width of the bitmap
10889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The height of the bitmap
10899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param config   The bitmap config to create. If the config does not
10909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 support per-pixel alpha (e.g. RGB_565), then the alpha
10919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 bytes in the colors[] will be ignored (assumed to be FF)
10929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if the width or height are <= 0, or if
10939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         the color array's length is less than the number of pixels.
10949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10958242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createBitmap(@NonNull @ColorInt int[] colors, int offset, int stride,
10968242656f495847c50c9ceaea92839dce78218a75Romain Guy            int width, int height, @NonNull Config config) {
1097dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(null, colors, offset, stride, width, height, config);
1098dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    }
1099dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
1100dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    /**
1101dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * Returns a immutable bitmap with the specified width and height, with each
1102dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * pixel value set to the corresponding value in the colors array.  Its
1103dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * initial density is determined from the given {@link DisplayMetrics}.
11048242656f495847c50c9ceaea92839dce78218a75Romain Guy     * The newly created bitmap is in the {@link ColorSpace.Named#SRGB sRGB}
11058242656f495847c50c9ceaea92839dce78218a75Romain Guy     * color space.
1106dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *
1107dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param display  Display metrics for the display this bitmap will be
1108dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 drawn on.
11098242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @param colors   Array of sRGB {@link Color colors} used to initialize the pixels.
1110dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param offset   Number of values to skip before the first color in the
1111dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 array of colors.
1112dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param stride   Number of colors in the array between rows (must be >=
1113dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 width or <= -width).
1114dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param width    The width of the bitmap
1115dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param height   The height of the bitmap
1116dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param config   The bitmap config to create. If the config does not
1117dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 support per-pixel alpha (e.g. RGB_565), then the alpha
1118dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 bytes in the colors[] will be ignored (assumed to be FF)
1119dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @throws IllegalArgumentException if the width or height are <= 0, or if
1120dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *         the color array's length is less than the number of pixels.
1121dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     */
11228242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createBitmap(@NonNull DisplayMetrics display,
11238242656f495847c50c9ceaea92839dce78218a75Romain Guy            @NonNull @ColorInt int[] colors, int offset, int stride,
11248242656f495847c50c9ceaea92839dce78218a75Romain Guy            int width, int height, @NonNull Config config) {
11259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkWidthHeight(width, height);
11279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (Math.abs(stride) < width) {
11289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("abs(stride) must be >= width");
11299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int lastScanline = offset + (height - 1) * stride;
11319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int length = colors.length;
11329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (offset < 0 || (offset + width > length) || lastScanline < 0 ||
11339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (lastScanline + width > length)) {
11349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new ArrayIndexOutOfBoundsException();
11359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1136e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        if (width <= 0 || height <= 0) {
1137e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy            throw new IllegalArgumentException("width and height must be > 0");
1138e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        }
1139dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        Bitmap bm = nativeCreate(colors, offset, stride, width, height,
11408242656f495847c50c9ceaea92839dce78218a75Romain Guy                            config.nativeInt, false, null, null);
1141dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        if (display != null) {
1142dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            bm.mDensity = display.densityDpi;
1143dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        }
1144dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return bm;
11459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
11489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a immutable bitmap with the specified width and height, with each
114996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * pixel value set to the corresponding value in the colors array.  Its
11508242656f495847c50c9ceaea92839dce78218a75Romain Guy     * initial density is as per {@link #getDensity}. The newly created
11518242656f495847c50c9ceaea92839dce78218a75Romain Guy     * bitmap is in the {@link ColorSpace.Named#SRGB sRGB} color space.
11529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
11538242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @param colors   Array of sRGB {@link Color colors} used to initialize the pixels.
11549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 This array must be at least as large as width * height.
11559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The width of the bitmap
11569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The height of the bitmap
11579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param config   The bitmap config to create. If the config does not
11589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 support per-pixel alpha (e.g. RGB_565), then the alpha
11599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 bytes in the colors[] will be ignored (assumed to be FF)
11609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if the width or height are <= 0, or if
11619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         the color array's length is less than the number of pixels.
11629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
11638242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createBitmap(@NonNull @ColorInt int[] colors,
11648242656f495847c50c9ceaea92839dce78218a75Romain Guy            int width, int height, Config config) {
1165dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(null, colors, 0, width, width, height, config);
1166dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    }
1167dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
1168dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    /**
1169dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * Returns a immutable bitmap with the specified width and height, with each
1170dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * pixel value set to the corresponding value in the colors array.  Its
1171dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * initial density is determined from the given {@link DisplayMetrics}.
11728242656f495847c50c9ceaea92839dce78218a75Romain Guy     * The newly created bitmap is in the {@link ColorSpace.Named#SRGB sRGB}
11738242656f495847c50c9ceaea92839dce78218a75Romain Guy     * color space.
1174dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *
1175dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param display  Display metrics for the display this bitmap will be
1176dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 drawn on.
11778242656f495847c50c9ceaea92839dce78218a75Romain Guy     * @param colors   Array of sRGB {@link Color colors} used to initialize the pixels.
1178dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 This array must be at least as large as width * height.
1179dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param width    The width of the bitmap
1180dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param height   The height of the bitmap
1181dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param config   The bitmap config to create. If the config does not
1182dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 support per-pixel alpha (e.g. RGB_565), then the alpha
1183dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 bytes in the colors[] will be ignored (assumed to be FF)
1184dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @throws IllegalArgumentException if the width or height are <= 0, or if
1185dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *         the color array's length is less than the number of pixels.
1186dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     */
11878242656f495847c50c9ceaea92839dce78218a75Romain Guy    public static Bitmap createBitmap(@Nullable DisplayMetrics display,
11888242656f495847c50c9ceaea92839dce78218a75Romain Guy            @NonNull @ColorInt int colors[], int width, int height, @NonNull Config config) {
1189dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(display, colors, 0, width, width, height, config);
11909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1193716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * Creates a Bitmap from the given {@link Picture} source of recorded drawing commands.
1194716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     *
1195716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * Equivalent to calling {@link #createBitmap(Picture, int, int, Config)} with
1196716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * width and height the same as the Picture's width and height and a Config.HARDWARE
1197716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * config.
1198716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     *
1199716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * @param source The recorded {@link Picture} of drawing commands that will be
1200716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     *               drawn into the returned Bitmap.
1201716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * @return An immutable bitmap with a HARDWARE config whose contents are created
1202716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * from the recorded drawing commands in the Picture source.
1203716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     */
1204716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik    public static @NonNull Bitmap createBitmap(@NonNull Picture source) {
1205716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik        return createBitmap(source, source.getWidth(), source.getHeight(), Config.HARDWARE);
1206716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik    }
1207716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik
1208716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik    /**
1209716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * Creates a Bitmap from the given {@link Picture} source of recorded drawing commands.
1210716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     *
1211716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * The bitmap will be immutable with the given width and height. If the width and height
1212716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * are not the same as the Picture's width & height, the Picture will be scaled to
1213716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * fit the given width and height.
1214716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     *
1215716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * @param source The recorded {@link Picture} of drawing commands that will be
1216716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     *               drawn into the returned Bitmap.
1217716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * @param width The width of the bitmap to create. The picture's width will be
1218716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     *              scaled to match if necessary.
1219716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * @param height The height of the bitmap to create. The picture's height will be
1220716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     *              scaled to match if necessary.
1221716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * @param config The {@link Config} of the created bitmap. If this is null then
1222716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     *               the bitmap will be {@link Config#HARDWARE}.
1223716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     *
1224716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * @return An immutable bitmap with a HARDWARE config whose contents are created
1225716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     * from the recorded drawing commands in the Picture source.
1226716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik     */
1227716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik    public static @NonNull Bitmap createBitmap(@NonNull Picture source, int width, int height,
1228716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            @NonNull Config config) {
1229716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik        if (width <= 0 || height <= 0) {
1230716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            throw new IllegalArgumentException("width & height must be > 0");
1231716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik        }
1232716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik        if (config == null) {
1233716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            throw new IllegalArgumentException("Config must not be null");
1234716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik        }
123544fc6317f11d75b8126a52a763c415c4fabd6ff4John Reck        source.endRecording();
1236716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik        if (source.requiresHardwareAcceleration() && config != Config.HARDWARE) {
1237716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            StrictMode.noteSlowCall("GPU readback");
1238716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik        }
1239716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik        if (config == Config.HARDWARE || source.requiresHardwareAcceleration()) {
1240716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            final RenderNode node = RenderNode.create("BitmapTemporary", null);
1241716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            node.setLeftTopRightBottom(0, 0, width, height);
1242716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            node.setClipToBounds(false);
1243716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            final DisplayListCanvas canvas = node.start(width, height);
1244716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            if (source.getWidth() != width || source.getHeight() != height) {
1245716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik                canvas.scale(width / (float) source.getWidth(),
1246716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik                        height / (float) source.getHeight());
1247716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            }
1248716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            canvas.drawPicture(source);
1249716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            node.end(canvas);
1250716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            Bitmap bitmap = ThreadedRenderer.createHardwareBitmap(node, width, height);
1251716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            if (config != Config.HARDWARE) {
1252716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik                bitmap = bitmap.copy(config, false);
1253716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            }
1254716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            return bitmap;
1255716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik        } else {
1256716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            Bitmap bitmap = Bitmap.createBitmap(width, height, config);
1257716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            Canvas canvas = new Canvas(bitmap);
1258716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            if (source.getWidth() != width || source.getHeight() != height) {
1259716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik                canvas.scale(width / (float) source.getWidth(),
1260716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik                        height / (float) source.getHeight());
1261716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            }
1262716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            canvas.drawPicture(source);
1263716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            canvas.setBitmap(null);
1264716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            bitmap.makeImmutable();
1265716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik            return bitmap;
1266716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik        }
1267716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik    }
1268716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik
1269716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik    /**
12709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns an optional array of private data, used by the UI system for
12719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * some bitmaps. Not intended to be called by applications.
12729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
12739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public byte[] getNinePatchChunk() {
12749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mNinePatchChunk;
12759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1278c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette     * Populates a rectangle with the bitmap's optical insets.
1279c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette     *
1280c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette     * @param outInsets Rect to populate with optical insets
1281ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani     * @hide
1282ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani     */
1283c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette    public void getOpticalInsets(@NonNull Rect outInsets) {
128447cd8e921db73e894f94ec4729ade90da50996f5Chris Craik        if (mNinePatchInsets == null) {
1285c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette            outInsets.setEmpty();
1286c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette        } else {
128747cd8e921db73e894f94ec4729ade90da50996f5Chris Craik            outInsets.set(mNinePatchInsets.opticalRect);
1288c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette        }
1289ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani    }
1290ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani
129147cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    /** @hide */
129247cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    public NinePatch.InsetStruct getNinePatchInsets() {
129347cd8e921db73e894f94ec4729ade90da50996f5Chris Craik        return mNinePatchInsets;
129447cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    }
129547cd8e921db73e894f94ec4729ade90da50996f5Chris Craik
1296ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani    /**
12979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Specifies the known formats a bitmap can be compressed into
12989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
12999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public enum CompressFormat {
13009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JPEG    (0),
13012305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora        PNG     (1),
13022305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora        WEBP    (2);
13039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        CompressFormat(int nativeInt) {
13059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            this.nativeInt = nativeInt;
13069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final int nativeInt;
13089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
13119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Number of bytes of temp storage we use for communicating between the
13129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * native compressor and the java OutputStream.
13139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
13149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final static int WORKING_COMPRESS_STORAGE = 4096;
13159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
13179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write a compressed version of the bitmap to the specified outputstream.
13189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * If this returns true, the bitmap can be reconstructed by passing a
13199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * corresponding inputstream to BitmapFactory.decodeStream(). Note: not
13209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * all Formats support all bitmap configs directly, so it is possible that
13219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the returned bitmap from BitmapFactory could be in a different bitdepth,
13229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque
13239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pixels).
13249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
13259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param format   The format of the compressed image
13269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param quality  Hint to the compressor, 0-100. 0 meaning compress for
13279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 small size, 100 meaning compress for max quality. Some
13289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 formats, like PNG which is lossless, will ignore the
13299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 quality setting
13309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stream   The outputstream to write the compressed data.
13319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true if successfully compressed to the specified stream.
13329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
13335d3501172c9e2329f46bc6bb53050fd2ec7d2305John Reck    @WorkerThread
13349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean compress(CompressFormat format, int quality, OutputStream stream) {
13359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't compress a recycled bitmap");
13369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // do explicit check before calling the native method
13379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (stream == null) {
13389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new NullPointerException();
13399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (quality < 0 || quality > 100) {
13419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("quality must be 0..100");
13429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13439fd259c9b0878ba52fec86173d76976e70977fcdsergeyv        StrictMode.noteSlowCall("Compression of a bitmap is slow");
13440df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reck        Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "Bitmap.compress");
1345775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        boolean result = nativeCompress(mNativePtr, format.nativeInt,
1346f29ed28c7b878ef28058bc730715d0d32445bc57John Reck                quality, stream, new byte[WORKING_COMPRESS_STORAGE]);
13470df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reck        Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
13480df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reck        return result;
13499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13508cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
13519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1352ff236f5fc1a088b1210d37a67b0e88b06c0fbdb2Newton Allen     * Returns true if the bitmap is marked as mutable (i.e.&nbsp;can be drawn into)
13539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
13549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final boolean isMutable() {
13559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mIsMutable;
13569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1358716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik    /** @hide */
1359716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik    public final void makeImmutable() {
1360716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik        // todo mIsMutable = false;
1361716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik        // todo nMakeImmutable();
1362716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik    }
1363716f38177ee1401373ced8d1f665431bb7a57d14Chris Craik
13643849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy    /**
13653849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Indicates whether pixels stored in this bitmaps are stored pre-multiplied.
13663849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * When a pixel is pre-multiplied, the RGB components have been multiplied by
13673849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * the alpha component. For instance, if the original color is a 50%
1368f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     * translucent red <code>(128, 255, 0, 0)</code>, the pre-multiplied form is
13693849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <code>(128, 128, 0, 0)</code>.</p>
1370f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
13713849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>This method always returns false if {@link #getConfig()} is
13724ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy     * {@link Bitmap.Config#RGB_565}.</p>
1373f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
137457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * <p>The return value is undefined if {@link #getConfig()} is
137557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * {@link Bitmap.Config#ALPHA_8}.</p>
137657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     *
13774ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy     * <p>This method only returns true if {@link #hasAlpha()} returns true.
13784ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy     * A bitmap with no alpha channel can be used both as a pre-multiplied and
13794ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy     * as a non pre-multiplied bitmap.</p>
13801abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
13811abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * <p>Only pre-multiplied bitmaps may be drawn by the view system or
13821abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * {@link Canvas}. If a non-pre-multiplied bitmap with an alpha channel is
13831abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * drawn to a Canvas, a RuntimeException will be thrown.</p>
13841abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
13853849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * @return true if the underlying pixels have been pre-multiplied, false
13863849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     *         otherwise
13871abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
13881abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * @see Bitmap#setPremultiplied(boolean)
13891abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * @see BitmapFactory.Options#inPremultiplied
13903849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     */
13913849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy    public final boolean isPremultiplied() {
139201a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
139301a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called isPremultiplied() on a recycle()'d bitmap! This is undefined behavior!");
139401a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1395775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeIsPremultiplied(mNativePtr);
13961abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    }
13971abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik
13981abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    /**
13991abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * Sets whether the bitmap should treat its data as pre-multiplied.
14001abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
14011abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * <p>Bitmaps are always treated as pre-multiplied by the view system and
14021abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * {@link Canvas} for performance reasons. Storing un-pre-multiplied data in
14031abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * a Bitmap (through {@link #setPixel}, {@link #setPixels}, or {@link
14041abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * BitmapFactory.Options#inPremultiplied BitmapFactory.Options.inPremultiplied})
14051abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * can lead to incorrect blending if drawn by the framework.</p>
14061abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
14071abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * <p>This method will not affect the behavior of a bitmap without an alpha
14081abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * channel, or if {@link #hasAlpha()} returns false.</p>
14091abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
1410889a1ddd00b74933d5f7daa651d98133bcb034baBen Murdoch     * <p>Calling {@link #createBitmap} or {@link #createScaledBitmap} with a source
14118790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III     * Bitmap whose colors are not pre-multiplied may result in a RuntimeException,
14128790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III     * since those functions require drawing the source, which is not supported for
14138790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III     * un-pre-multiplied Bitmaps.</p>
14148790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III     *
14151abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * @see Bitmap#isPremultiplied()
14161abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * @see BitmapFactory.Options#inPremultiplied
14171abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     */
14181abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    public final void setPremultiplied(boolean premultiplied) {
14193e776dee3e8af646cdcfb7a60e0e301c99f05293John Reck        checkRecycled("setPremultiplied called on a recycled bitmap");
142057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        mRequestPremultiplied = premultiplied;
1421775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetPremultiplied(mNativePtr, premultiplied);
14223849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy    }
14233849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy
14249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Returns the bitmap's width */
14259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final int getWidth() {
142601a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
142701a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getWidth() on a recycle()'d bitmap! This is undefined behavior!");
142801a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
14291abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        return mWidth;
14309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Returns the bitmap's height */
14339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final int getHeight() {
143401a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
143501a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getHeight() on a recycle()'d bitmap! This is undefined behavior!");
143601a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
14371abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        return mHeight;
14389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14398cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
14409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
144111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * Convenience for calling {@link #getScaledWidth(int)} with the target
144211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * density of the given {@link Canvas}.
14439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
14442784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    public int getScaledWidth(Canvas canvas) {
144511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getWidth(), mDensity, canvas.mDensity);
14462784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    }
14472784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn
14482784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    /**
144911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * Convenience for calling {@link #getScaledHeight(int)} with the target
145011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * density of the given {@link Canvas}.
14512784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     */
14522784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    public int getScaledHeight(Canvas canvas) {
145311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getHeight(), mDensity, canvas.mDensity);
145411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    }
145511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn
145611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    /**
145711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * Convenience for calling {@link #getScaledWidth(int)} with the target
145811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * density of the given {@link DisplayMetrics}.
145911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     */
146011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getScaledWidth(DisplayMetrics metrics) {
146111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getWidth(), mDensity, metrics.densityDpi);
146211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    }
146311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn
146411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    /**
146511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * Convenience for calling {@link #getScaledHeight(int)} with the target
146611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * density of the given {@link DisplayMetrics}.
146711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     */
146811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getScaledHeight(DisplayMetrics metrics) {
146911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getHeight(), mDensity, metrics.densityDpi);
14702784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    }
14712784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn
14722784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    /**
14732784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     * Convenience method that returns the width of this bitmap divided
14742784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     * by the density scale factor.
14752784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     *
147611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @param targetDensity The density of the target canvas of the bitmap.
14772784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     * @return The scaled width of this bitmap, according to the density scale factor.
14782784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     */
147911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getScaledWidth(int targetDensity) {
148011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getWidth(), mDensity, targetDensity);
14819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
14849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Convenience method that returns the height of this bitmap divided
14859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * by the density scale factor.
14869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
148711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @param targetDensity The density of the target canvas of the bitmap.
14889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return The scaled height of this bitmap, according to the density scale factor.
14899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
149011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getScaledHeight(int targetDensity) {
149111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getHeight(), mDensity, targetDensity);
149211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    }
1493f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
149411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    /**
149511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @hide
149611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     */
149711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    static public int scaleFromDensity(int size, int sdensity, int tdensity) {
14980b68477f8287fe5ddac1beb1c9d0811ded034dadRomain Guy        if (sdensity == DENSITY_NONE || tdensity == DENSITY_NONE || sdensity == tdensity) {
149911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn            return size;
15002784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn        }
1501f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
150211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        // Scale by tdensity / sdensity, rounding up.
1503366a84056c9a695ee6702d9d30bf9f3b521ba7ccRomain Guy        return ((size * tdensity) + (sdensity >> 1)) / sdensity;
15049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1505f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
15069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Return the number of bytes between rows in the bitmap's pixels. Note that
15089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * this refers to the pixels as stored natively by the bitmap. If you call
15099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * getPixels() or setPixels(), then the pixels are uniformly treated as
15109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * 32bit values, packed according to the Color class.
15119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1512e8222dddaf2e3da14380101e818d4254899e0c0dChet Haase     * <p>As of {@link android.os.Build.VERSION_CODES#KITKAT}, this method
15139f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * should not be used to calculate the memory usage of the bitmap. Instead,
15149f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * see {@link #getAllocationByteCount()}.
15159f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     *
15169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return number of bytes between rows of the native bitmap pixels.
15179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
15189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final int getRowBytes() {
151901a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
152001a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getRowBytes() on a recycle()'d bitmap! This is undefined behavior!");
152101a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1522775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeRowBytes(mNativePtr);
15239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15248cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
15259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15269f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * Returns the minimum number of bytes that can be used to store this bitmap's pixels.
15279f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     *
1528e8222dddaf2e3da14380101e818d4254899e0c0dChet Haase     * <p>As of {@link android.os.Build.VERSION_CODES#KITKAT}, the result of this method can
15299f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * no longer be used to determine memory usage of a bitmap. See {@link
1530c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * #getAllocationByteCount()}.</p>
1531f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson     */
1532f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson    public final int getByteCount() {
15333bdecbf0fd2d4b6137477094e1ea96502c353248John Reck        if (mRecycled) {
15343bdecbf0fd2d4b6137477094e1ea96502c353248John Reck            Log.w(TAG, "Called getByteCount() on a recycle()'d bitmap! "
15353bdecbf0fd2d4b6137477094e1ea96502c353248John Reck                    + "This is undefined behavior!");
15363bdecbf0fd2d4b6137477094e1ea96502c353248John Reck            return 0;
15373bdecbf0fd2d4b6137477094e1ea96502c353248John Reck        }
1538f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson        // int result permits bitmaps up to 46,340 x 46,340
1539f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson        return getRowBytes() * getHeight();
1540f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson    }
1541f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson
1542f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson    /**
15439f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * Returns the size of the allocated memory used to store this bitmap's pixels.
15449f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     *
15459f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * <p>This can be larger than the result of {@link #getByteCount()} if a bitmap is reused to
1546c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * decode other bitmaps of smaller size, or by manual reconfiguration. See {@link
1547c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * #reconfigure(int, int, Config)}, {@link #setWidth(int)}, {@link #setHeight(int)}, {@link
1548c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * #setConfig(Bitmap.Config)}, and {@link BitmapFactory.Options#inBitmap
1549c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * BitmapFactory.Options.inBitmap}. If a bitmap is not modified in this way, this value will be
1550c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the same as that returned by {@link #getByteCount()}.</p>
1551c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
1552c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>This value will not change over the lifetime of a Bitmap.</p>
15539f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     *
1554c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #reconfigure(int, int, Config)
15559f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     */
15569f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    public final int getAllocationByteCount() {
15573bdecbf0fd2d4b6137477094e1ea96502c353248John Reck        if (mRecycled) {
15583bdecbf0fd2d4b6137477094e1ea96502c353248John Reck            Log.w(TAG, "Called getAllocationByteCount() on a recycle()'d bitmap! "
15593bdecbf0fd2d4b6137477094e1ea96502c353248John Reck                    + "This is undefined behavior!");
15603bdecbf0fd2d4b6137477094e1ea96502c353248John Reck            return 0;
15613bdecbf0fd2d4b6137477094e1ea96502c353248John Reck        }
15624508218850faedea95371188da587b6734f5f3dasergeyv        return nativeGetAllocationByteCount(mNativePtr);
15639f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    }
15649f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik
15659f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    /**
15669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * If the bitmap's internal config is in one of the public formats, return
15679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * that config, otherwise return null.
15689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
15699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final Config getConfig() {
157001a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
157101a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getConfig() on a recycle()'d bitmap! This is undefined behavior!");
157201a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1573775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return Config.nativeToConfig(nativeConfig(mNativePtr));
15749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1576a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    /** Returns true if the bitmap's config supports per-pixel alpha, and
1577a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * if the pixels may contain non-opaque alpha values. For some configs,
1578a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * this is always false (e.g. RGB_565), since they do not support per-pixel
1579a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * alpha. However, for configs that do, the bitmap may be flagged to be
1580a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * known that all of its pixels are opaque. In this case hasAlpha() will
1581a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * also return false. If a config such as ARGB_8888 is not so flagged,
1582a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * it will return true by default.
1583a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     */
15849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final boolean hasAlpha() {
158501a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
158601a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called hasAlpha() on a recycle()'d bitmap! This is undefined behavior!");
158701a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1588775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeHasAlpha(mNativePtr);
15899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1592a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * Tell the bitmap if all of the pixels are known to be opaque (false)
1593a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * or if some of the pixels may contain non-opaque alpha values (true).
1594366a84056c9a695ee6702d9d30bf9f3b521ba7ccRomain Guy     * Note, for some configs (e.g. RGB_565) this call is ignored, since it
1595366a84056c9a695ee6702d9d30bf9f3b521ba7ccRomain Guy     * does not support per-pixel alpha values.
1596a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     *
1597a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * This is meant as a drawing hint, as in some cases a bitmap that is known
1598a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * to be opaque can take a faster drawing case than one that may have
1599a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * non-opaque per-pixel alpha values.
1600a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     */
1601a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    public void setHasAlpha(boolean hasAlpha) {
16023e776dee3e8af646cdcfb7a60e0e301c99f05293John Reck        checkRecycled("setHasAlpha called on a recycled bitmap");
1603775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetHasAlpha(mNativePtr, hasAlpha, mRequestPremultiplied);
1604a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    }
1605a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed
1606a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    /**
1607713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * Indicates whether the renderer responsible for drawing this
1608713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * bitmap should attempt to use mipmaps when this bitmap is drawn
1609713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * scaled down.
1610f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1611713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * If you know that you are going to draw this bitmap at less than
1612713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * 50% of its original size, you may be able to obtain a higher
1613713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * quality
1614f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1615713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * This property is only a suggestion that can be ignored by the
1616713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * renderer. It is not guaranteed to have any effect.
1617f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1618713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * @return true if the renderer should attempt to use mipmaps,
1619713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *         false otherwise
1620f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1621713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * @see #setHasMipMap(boolean)
1622713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     */
1623713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    public final boolean hasMipMap() {
162401a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
162501a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called hasMipMap() on a recycle()'d bitmap! This is undefined behavior!");
162601a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1627775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeHasMipMap(mNativePtr);
1628713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    }
1629713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy
1630713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    /**
1631713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * Set a hint for the renderer responsible for drawing this bitmap
1632713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * indicating that it should attempt to use mipmaps when this bitmap
1633713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * is drawn scaled down.
1634713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *
1635713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * If you know that you are going to draw this bitmap at less than
1636713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * 50% of its original size, you may be able to obtain a higher
1637713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * quality by turning this property on.
1638f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1639713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * Note that if the renderer respects this hint it might have to
1640713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * allocate extra memory to hold the mipmap levels for this bitmap.
1641713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *
1642713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * This property is only a suggestion that can be ignored by the
1643713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * renderer. It is not guaranteed to have any effect.
1644713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *
1645713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * @param hasMipMap indicates whether the renderer should attempt
1646713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *                  to use mipmaps
1647713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *
1648713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * @see #hasMipMap()
1649713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     */
1650713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    public final void setHasMipMap(boolean hasMipMap) {
16513e776dee3e8af646cdcfb7a60e0e301c99f05293John Reck        checkRecycled("setHasMipMap called on a recycled bitmap");
1652775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetHasMipMap(mNativePtr, hasMipMap);
1653713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    }
1654713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy
1655713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    /**
1656efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy     * Returns the color space associated with this bitmap. If the color
1657efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy     * space is unknown, this method returns null.
1658efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy     */
1659efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy    @Nullable
1660efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy    public final ColorSpace getColorSpace() {
1661efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy        // A reconfigure can change the configuration and rgba16f is
1662efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy        // always linear scRGB at this time
1663efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy        if (getConfig() == Config.RGBA_F16) {
1664efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy            // Reset the color space for potential future reconfigurations
1665efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy            mColorSpace = null;
1666efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy            return ColorSpace.get(ColorSpace.Named.LINEAR_EXTENDED_SRGB);
1667efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy        }
1668efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy
1669efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy        // Cache the color space retrieval since it can be fairly expensive
1670efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy        if (mColorSpace == null) {
1671efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy            if (nativeIsSRGB(mNativePtr)) {
1672efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                mColorSpace = ColorSpace.get(ColorSpace.Named.SRGB);
1673ce89a6e656a4d4d2c0e724c485e109cd6bc60997Leon Scroggins III            } else if (getConfig() == Config.HARDWARE && nativeIsSRGBLinear(mNativePtr)) {
1674ce89a6e656a4d4d2c0e724c485e109cd6bc60997Leon Scroggins III                mColorSpace = ColorSpace.get(ColorSpace.Named.LINEAR_EXTENDED_SRGB);
1675efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy            } else {
1676efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                float[] xyz = new float[9];
1677efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                float[] params = new float[7];
1678efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy
1679efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                boolean hasColorSpace = nativeGetColorSpace(mNativePtr, xyz, params);
1680efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                if (hasColorSpace) {
1681efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                    ColorSpace.Rgb.TransferParameters parameters =
1682efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                            new ColorSpace.Rgb.TransferParameters(
1683efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                                    params[0], params[1], params[2],
1684efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                                    params[3], params[4], params[5], params[6]);
1685efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                    ColorSpace cs = ColorSpace.match(xyz, parameters);
1686efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                    if (cs != null) {
1687efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                        mColorSpace = cs;
1688efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                    } else {
1689efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                        mColorSpace = new ColorSpace.Rgb("Unknown", xyz, parameters);
1690efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                    }
1691efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy                }
1692efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy            }
1693efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy        }
1694efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy
1695efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy        return mColorSpace;
1696efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy    }
1697efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy
1698efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy    /**
16999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Fills the bitmap's pixels with the specified {@link Color}.
17009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
17019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalStateException if the bitmap is not mutable.
17029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
170380756e38882720860db52f1fcc21fa1505a02abfTor Norbye    public void eraseColor(@ColorInt int c) {
17049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't erase a recycled bitmap");
17059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!isMutable()) {
17069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalStateException("cannot erase immutable bitmaps");
17079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1708775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeErase(mNativePtr, c);
17099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
17109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
17119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
17129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the {@link Color} at the specified location. Throws an exception
17139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * if x or y are out of bounds (negative or >= to the width or height
1714ce217faddb4b40c1b3e698944da1951027080427Romain Guy     * respectively). The returned color is a non-premultiplied ARGB value in
1715ce217faddb4b40c1b3e698944da1951027080427Romain Guy     * the {@link ColorSpace.Named#SRGB sRGB} color space.
17169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
17179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x    The x coordinate (0...width-1) of the pixel to return
17189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y    The y coordinate (0...height-1) of the pixel to return
17199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return     The argb {@link Color} at the specified coordinate
17209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if x, y exceed the bitmap's bounds
172157eafc6f90a1ce2dd97079dd82b4b2f8dcd9e00fsergeyv     * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE}
17229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
172380756e38882720860db52f1fcc21fa1505a02abfTor Norbye    @ColorInt
17249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getPixel(int x, int y) {
17259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't call getPixel() on a recycled bitmap");
1726980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv        checkHardware("unable to getPixel(), "
1727980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv                + "pixel access is not supported on Config#HARDWARE bitmaps");
17289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkPixelAccess(x, y);
1729775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeGetPixel(mNativePtr, x, y);
17309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
17318cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
17329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
17339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns in pixels[] a copy of the data in the bitmap. Each value is
17349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * a packed int representing a {@link Color}. The stride parameter allows
17359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the caller to allow for gaps in the returned pixels array between
17369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * rows. For normal packed results, just pass width for the stride value.
1737ce217faddb4b40c1b3e698944da1951027080427Romain Guy     * The returned colors are non-premultiplied ARGB values in the
1738ce217faddb4b40c1b3e698944da1951027080427Romain Guy     * {@link ColorSpace.Named#SRGB sRGB} color space.
17399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
17409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param pixels   The array to receive the bitmap's colors
17419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset   The first index to write into pixels[]
17429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stride   The number of entries in pixels[] to skip between
17439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 rows (must be >= bitmap's width). Can be negative.
17449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x        The x coordinate of the first pixel to read from
17459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap
17469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y        The y coordinate of the first pixel to read from
17479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap
17489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The number of pixels to read from each row
17499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The number of rows to read
17503849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     *
17519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if x, y, width, height exceed the
17529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         bounds of the bitmap, or if abs(stride) < width.
17539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws ArrayIndexOutOfBoundsException if the pixels array is too small
17549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         to receive the specified number of pixels.
175557eafc6f90a1ce2dd97079dd82b4b2f8dcd9e00fsergeyv     * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE}
17569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
175780756e38882720860db52f1fcc21fa1505a02abfTor Norbye    public void getPixels(@ColorInt int[] pixels, int offset, int stride,
17589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          int x, int y, int width, int height) {
17599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't call getPixels() on a recycled bitmap");
1760980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv        checkHardware("unable to getPixels(), "
1761980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv                + "pixel access is not supported on Config#HARDWARE bitmaps");
17629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (width == 0 || height == 0) {
17639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return; // nothing to do
17649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
17659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkPixelsAccess(x, y, width, height, offset, stride, pixels);
1766775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeGetPixels(mNativePtr, pixels, offset, stride,
176757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                        x, y, width, height);
17689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
17698cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
17709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
17719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Shared code to check for illegal arguments passed to getPixel()
17729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or setPixel()
1773f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
17749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x x coordinate of the pixel
17759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y y coordinate of the pixel
17769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
17779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void checkPixelAccess(int x, int y) {
17789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkXYSign(x, y);
17799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (x >= getWidth()) {
17809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("x must be < bitmap.width()");
17819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
17829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (y >= getHeight()) {
17839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("y must be < bitmap.height()");
17849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
17859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
17869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
17879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
17889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Shared code to check for illegal arguments passed to getPixels()
17899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or setPixels()
17909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
17919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x left edge of the area of pixels to access
17929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y top edge of the area of pixels to access
17939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width width of the area of pixels to access
17949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height height of the area of pixels to access
17959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset offset into pixels[] array
17969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stride number of elements in pixels[] between each logical row
17979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param pixels array to hold the area of pixels being accessed
17989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    */
17999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void checkPixelsAccess(int x, int y, int width, int height,
18009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                   int offset, int stride, int pixels[]) {
18019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkXYSign(x, y);
18029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (width < 0) {
18039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("width must be >= 0");
18049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
18059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (height < 0) {
18069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("height must be >= 0");
18079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
18089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (x + width > getWidth()) {
18099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException(
18109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    "x + width must be <= bitmap.width()");
18119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
18129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (y + height > getHeight()) {
18139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException(
18149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    "y + height must be <= bitmap.height()");
18159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
18169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (Math.abs(stride) < width) {
18179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("abs(stride) must be >= width");
18189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
18199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int lastScanline = offset + (height - 1) * stride;
18209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int length = pixels.length;
18219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (offset < 0 || (offset + width > length)
18229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                || lastScanline < 0
18239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                || (lastScanline + width > length)) {
18249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new ArrayIndexOutOfBoundsException();
18259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
18269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
18278cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
18289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
18293849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Write the specified {@link Color} into the bitmap (assuming it is
18303849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * mutable) at the x,y coordinate. The color must be a
1831ce217faddb4b40c1b3e698944da1951027080427Romain Guy     * non-premultiplied ARGB value in the {@link ColorSpace.Named#SRGB sRGB}
1832ce217faddb4b40c1b3e698944da1951027080427Romain Guy     * color space.</p>
18339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
18349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x     The x coordinate of the pixel to replace (0...width-1)
18359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y     The y coordinate of the pixel to replace (0...height-1)
18363849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * @param color The ARGB color to write into the bitmap
18373849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     *
18389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalStateException if the bitmap is not mutable
18399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if x, y are outside of the bitmap's
18409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         bounds.
18419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
184280756e38882720860db52f1fcc21fa1505a02abfTor Norbye    public void setPixel(int x, int y, @ColorInt int color) {
18439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't call setPixel() on a recycled bitmap");
18449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!isMutable()) {
18459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalStateException();
18469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
18479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkPixelAccess(x, y);
1848775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetPixel(mNativePtr, x, y, color);
18499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
18508cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
18519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
18523849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Replace pixels in the bitmap with the colors in the array. Each element
18539505a6552764461c22ce48f1ac13d025d23e1579Romain Guy     * in the array is a packed int representing a non-premultiplied ARGB
1854ce217faddb4b40c1b3e698944da1951027080427Romain Guy     * {@link Color} in the {@link ColorSpace.Named#SRGB sRGB} color space.</p>
18559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
18569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param pixels   The colors to write to the bitmap
18579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset   The index of the first color to read from pixels[]
18589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stride   The number of colors in pixels[] to skip between rows.
18599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 Normally this value will be the same as the width of
18609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap, but it can be larger (or negative).
18619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x        The x coordinate of the first pixel to write to in
18629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap.
18639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y        The y coordinate of the first pixel to write to in
18649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap.
18659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The number of colors to copy from pixels[] per row
18669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The number of rows to write to the bitmap
18673849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     *
18689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalStateException if the bitmap is not mutable
18699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if x, y, width, height are outside of
18709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         the bitmap's bounds.
18719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws ArrayIndexOutOfBoundsException if the pixels array is too small
18729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         to receive the specified number of pixels.
18739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
187480756e38882720860db52f1fcc21fa1505a02abfTor Norbye    public void setPixels(@ColorInt int[] pixels, int offset, int stride,
18753849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy            int x, int y, int width, int height) {
18769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't call setPixels() on a recycled bitmap");
18779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!isMutable()) {
18789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalStateException();
18799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
18809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (width == 0 || height == 0) {
18819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return; // nothing to do
18829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
18839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkPixelsAccess(x, y, width, height, offset, stride, pixels);
1884775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetPixels(mNativePtr, pixels, offset, stride,
188557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                        x, y, width, height);
18869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
18878cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
18889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final Parcelable.Creator<Bitmap> CREATOR
18899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            = new Parcelable.Creator<Bitmap>() {
18909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
18919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * Rebuilds a bitmap previously stored with writeToParcel().
18929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         *
18939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * @param p    Parcel object to read the bitmap from
18949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * @return a new bitmap created from the data in the parcel
18959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
18969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public Bitmap createFromParcel(Parcel p) {
18979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Bitmap bm = nativeCreateFromParcel(p);
18989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (bm == null) {
18999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                throw new RuntimeException("Failed to unparcel Bitmap");
19009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
19019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return bm;
19029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
19039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public Bitmap[] newArray(int size) {
19049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return new Bitmap[size];
19059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
19069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    };
19079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
19099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * No special parcel contents.
19109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
19119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int describeContents() {
1912b6377170960d40e66858d8b4d335a95eac773762Bart Sears        return 0;
19139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
19149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
19169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write the bitmap and its pixels to the parcel. The bitmap can be
19179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * rebuilt from the parcel by calling CREATOR.createFromParcel().
1918f161dce70eab5ba1011bd26226c14e31675d5955sergeyv     *
1919f161dce70eab5ba1011bd26226c14e31675d5955sergeyv     * If this bitmap is {@link Config#HARDWARE}, it may be unparceled with a different pixel
1920f161dce70eab5ba1011bd26226c14e31675d5955sergeyv     * format (e.g. 565, 8888), but the content will be preserved to the best quality permitted
1921f161dce70eab5ba1011bd26226c14e31675d5955sergeyv     * by the final pixel format
19229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param p    Parcel object to write the bitmap data into
19239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
19249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void writeToParcel(Parcel p, int flags) {
19259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't parcel a recycled bitmap");
19269fd259c9b0878ba52fec86173d76976e70977fcdsergeyv        noteHardwareBitmapSlowCall();
1927775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        if (!nativeWriteToParcel(mNativePtr, mIsMutable, mDensity, p)) {
19289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("native writeToParcel failed");
19299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
19309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
19319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
19339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a new bitmap that captures the alpha values of the original.
19349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This may be drawn with Canvas.drawBitmap(), where the color(s) will be
19359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * taken from the paint that is passed to the draw call.
19369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
19379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return new bitmap containing the alpha channel of the original bitmap.
19389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
19391c2bf03d1082fc6b7eb42cbd163c60c07cf2bcccTor Norbye    @CheckResult
19409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public Bitmap extractAlpha() {
19419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return extractAlpha(null, null);
19429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
19438cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
19449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
19459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a new bitmap that captures the alpha values of the original.
19469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * These values may be affected by the optional Paint parameter, which
19479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * can contain its own alpha, and may also contain a MaskFilter which
19489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * could change the actual dimensions of the resulting bitmap (e.g.
19499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * a blur maskfilter might enlarge the resulting bitmap). If offsetXY
19509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * is not null, it returns the amount to offset the returned bitmap so
19519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * that it will logically align with the original. For example, if the
19529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * paint contains a blur of radius 2, then offsetXY[] would contains
19539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * -2, -2, so that drawing the alpha bitmap offset by (-2, -2) and then
19549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * drawing the original would result in the blur visually aligning with
19559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the original.
1956f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
195796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * <p>The initial density of the returned bitmap is the same as the original's.
1958f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
19599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param paint Optional paint used to modify the alpha values in the
19609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              resulting bitmap. Pass null for default behavior.
19619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offsetXY Optional array that returns the X (index 0) and Y
19629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 (index 1) offset needed to position the returned bitmap
19639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 so that it visually lines up with the original.
19649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return new bitmap containing the (optionally modified by paint) alpha
19659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         channel of the original bitmap. This may be drawn with
19669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         Canvas.drawBitmap(), where the color(s) will be taken from the
19679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         paint that is passed to the draw call.
19689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
19691c2bf03d1082fc6b7eb42cbd163c60c07cf2bcccTor Norbye    @CheckResult
19709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public Bitmap extractAlpha(Paint paint, int[] offsetXY) {
19719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't extractAlpha on a recycled bitmap");
1972dfba4d3d11bbf47dff45f94d61d4d97510b3034aDerek Sollenberger        long nativePaint = paint != null ? paint.getNativeInstance() : 0;
19739fd259c9b0878ba52fec86173d76976e70977fcdsergeyv        noteHardwareBitmapSlowCall();
1974775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        Bitmap bm = nativeExtractAlpha(mNativePtr, nativePaint, offsetXY);
19759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (bm == null) {
19769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("Failed to extractAlpha on Bitmap");
19779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
197896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        bm.mDensity = mDensity;
19799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bm;
19809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
19819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19828cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen    /**
198376d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed     *  Given another bitmap, return true if it has the same dimensions, config,
198476d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed     *  and pixel data as this bitmap. If any of those differ, return false.
198576d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed     *  If other is null, return false.
198676d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed     */
198776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    public boolean sameAs(Bitmap other) {
19889298c5414172344f2462beda70e624af5f774483John Reck        checkRecycled("Can't call sameAs on a recycled bitmap!");
19899fd259c9b0878ba52fec86173d76976e70977fcdsergeyv        noteHardwareBitmapSlowCall();
19903df4869a77bdeb72d5810bbcf7819ed1b8dd0ec7John Reck        if (this == other) return true;
19913df4869a77bdeb72d5810bbcf7819ed1b8dd0ec7John Reck        if (other == null) return false;
1992e1fc36d197639ee13194de74b2e7e42c39b12286sergeyv        other.noteHardwareBitmapSlowCall();
19939298c5414172344f2462beda70e624af5f774483John Reck        if (other.isRecycled()) {
19949298c5414172344f2462beda70e624af5f774483John Reck            throw new IllegalArgumentException("Can't compare to a recycled bitmap!");
19959298c5414172344f2462beda70e624af5f774483John Reck        }
1996775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeSameAs(mNativePtr, other.mNativePtr);
199776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    }
199876d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
199976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    /**
2000b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     * Builds caches associated with the bitmap that are used for drawing it.
2001b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     *
2002b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     * <p>Starting in {@link android.os.Build.VERSION_CODES#N}, this call initiates an asynchronous
2003b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     * upload to the GPU on RenderThread, if the Bitmap is not already uploaded. With Hardware
2004b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     * Acceleration, Bitmaps must be uploaded to the GPU in order to be rendered. This is done by
2005b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     * default the first time a Bitmap is drawn, but the process can take several milliseconds,
2006b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     * depending on the size of the Bitmap. Each time a Bitmap is modified and drawn again, it must
2007b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     * be re-uploaded.</p>
2008b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     *
2009b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     * <p>Calling this method in advance can save time in the first frame it's used. For example, it
2010b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     * is recommended to call this on an image decoding worker thread when a decoded Bitmap is about
2011b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     * to be displayed. It is recommended to make any pre-draw modifications to the Bitmap before
2012b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     * calling this method, so the cached, uploaded copy may be reused without re-uploading.</p>
2013b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     *
2014b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     * In {@link android.os.Build.VERSION_CODES#KITKAT} and below, for purgeable bitmaps, this call
2015b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik     * would attempt to ensure that the pixels have been decoded.
20168cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     */
20178cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen    public void prepareToDraw() {
2018ad57442fd00e7fd7f1885d23372ec5c85e176820John Reck        checkRecycled("Can't prepareToDraw on a recycled bitmap!");
20194387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck        // Kick off an update/upload of the bitmap outside of the normal
20204387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck        // draw path.
20214387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck        nativePrepareToDraw(mNativePtr);
2022f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck    }
2023f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
20246e3658a63843096058ed444d073fbcd191fd7e1bsergeyv    /**
20256e3658a63843096058ed444d073fbcd191fd7e1bsergeyv     *
20266e3658a63843096058ed444d073fbcd191fd7e1bsergeyv     * @return {@link GraphicBuffer} which is internally used by hardware bitmap
20276e3658a63843096058ed444d073fbcd191fd7e1bsergeyv     * @hide
20286e3658a63843096058ed444d073fbcd191fd7e1bsergeyv     */
20296e3658a63843096058ed444d073fbcd191fd7e1bsergeyv    public GraphicBuffer createGraphicBufferHandle() {
20306e3658a63843096058ed444d073fbcd191fd7e1bsergeyv        return nativeCreateGraphicBufferHandle(mNativePtr);
20316e3658a63843096058ed444d073fbcd191fd7e1bsergeyv    }
20326e3658a63843096058ed444d073fbcd191fd7e1bsergeyv
20339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //////////// native methods
20349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
20359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static native Bitmap nativeCreate(int[] colors, int offset,
20369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                              int stride, int width, int height,
20378242656f495847c50c9ceaea92839dce78218a75Romain Guy                                              int nativeConfig, boolean mutable,
20388242656f495847c50c9ceaea92839dce78218a75Romain Guy                                              @Nullable @Size(9) float[] xyzD50,
20398242656f495847c50c9ceaea92839dce78218a75Romain Guy                                              @Nullable ColorSpace.Rgb.TransferParameters p);
204036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native Bitmap nativeCopy(long nativeSrcBitmap, int nativeConfig,
20419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            boolean isMutable);
2042721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    private static native Bitmap nativeCopyAshmem(long nativeSrcBitmap);
2043a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson    private static native Bitmap nativeCopyAshmemConfig(long nativeSrcBitmap, int nativeConfig);
2044775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler    private static native long nativeGetNativeFinalizer();
204536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeRecycle(long nativeBitmap);
204636bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeReconfigure(long nativeBitmap, int width, int height,
20474508218850faedea95371188da587b6734f5f3dasergeyv                                                 int config, boolean isPremultiplied);
20489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
204936bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeCompress(long nativeBitmap, int format,
20509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            int quality, OutputStream stream,
20519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            byte[] tempStorage);
205236bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeErase(long nativeBitmap, int color);
205336bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native int nativeRowBytes(long nativeBitmap);
205436bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native int nativeConfig(long nativeBitmap);
20558cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
205657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native int nativeGetPixel(long nativeBitmap, int x, int y);
205736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeGetPixels(long nativeBitmap, int[] pixels,
20581abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik                                               int offset, int stride, int x, int y,
205957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                               int width, int height);
20608cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
206157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native void nativeSetPixel(long nativeBitmap, int x, int y, int color);
206236bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeSetPixels(long nativeBitmap, int[] colors,
20631abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik                                               int offset, int stride, int x, int y,
206457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                               int width, int height);
206536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeCopyPixelsToBuffer(long nativeBitmap,
20669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                        Buffer dst);
206736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeCopyPixelsFromBuffer(long nativeBitmap, Buffer src);
206836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native int nativeGenerationId(long nativeBitmap);
20699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
20709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static native Bitmap nativeCreateFromParcel(Parcel p);
20719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // returns true on success
207236bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeWriteToParcel(long nativeBitmap,
20739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                      boolean isMutable,
2074de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn                                                      int density,
20759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                      Parcel p);
20769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // returns a new bitmap built from the native bitmap's alpha, and the paint
207736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native Bitmap nativeExtractAlpha(long nativeBitmap,
207836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat                                                    long nativePaint,
20799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                    int[] offsetXY);
20809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
208136bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeHasAlpha(long nativeBitmap);
208257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native boolean nativeIsPremultiplied(long nativeBitmap);
208357ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native void nativeSetPremultiplied(long nativeBitmap,
208457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                                      boolean isPremul);
208557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native void nativeSetHasAlpha(long nativeBitmap,
208657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                                 boolean hasAlpha,
208757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                                 boolean requestPremul);
208836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeHasMipMap(long nativeBitmap);
208936bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeSetHasMipMap(long nativeBitmap, boolean hasMipMap);
209036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeSameAs(long nativeBitmap0, long nativeBitmap1);
20914387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    private static native void nativePrepareToDraw(long nativeBitmap);
20924508218850faedea95371188da587b6734f5f3dasergeyv    private static native int nativeGetAllocationByteCount(long nativeBitmap);
209381f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv    private static native Bitmap nativeCopyPreserveInternalConfig(long nativeBitmap);
20940a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv    private static native Bitmap nativeCreateHardwareBitmap(GraphicBuffer buffer);
20956e3658a63843096058ed444d073fbcd191fd7e1bsergeyv    private static native GraphicBuffer nativeCreateGraphicBufferHandle(long nativeBitmap);
2096efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy    private static native boolean nativeGetColorSpace(long nativePtr, float[] xyz, float[] params);
2097efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy    private static native boolean nativeIsSRGB(long nativePtr);
2098ce89a6e656a4d4d2c0e724c485e109cd6bc60997Leon Scroggins III    private static native boolean nativeIsSRGBLinear(long nativePtr);
20998242656f495847c50c9ceaea92839dce78218a75Romain Guy    private static native void nativeCopyColorSpace(long srcBitmap, long dstBitmap);
21009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2101