1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.robolectric.nativeruntime;
18 
19 import android.graphics.Bitmap;
20 import android.graphics.ColorSpace;
21 import android.hardware.HardwareBuffer;
22 import android.os.Parcel;
23 import java.io.OutputStream;
24 import java.nio.Buffer;
25 
26 /**
27  * Native methods for Bitmap JNI registration.
28  *
29  * <p>Native method signatures are derived from
30  * https://cs.android.com/android/platform/superproject/+/android-12.0.0_r1:frameworks/base/graphics/java/android/graphics/Bitmap.java
31  */
32 public final class BitmapNatives {
33 
nativeCreate( int[] colors, int offset, int stride, int width, int height, int nativeConfig, boolean mutable, long nativeColorSpace)34   public static native Bitmap nativeCreate(
35       int[] colors,
36       int offset,
37       int stride,
38       int width,
39       int height,
40       int nativeConfig,
41       boolean mutable,
42       long nativeColorSpace);
43 
nativeCopy(long nativeSrcBitmap, int nativeConfig, boolean isMutable)44   public static native Bitmap nativeCopy(long nativeSrcBitmap, int nativeConfig, boolean isMutable);
45 
nativeCopyAshmem(long nativeSrcBitmap)46   public static native Bitmap nativeCopyAshmem(long nativeSrcBitmap);
47 
nativeCopyAshmemConfig(long nativeSrcBitmap, int nativeConfig)48   public static native Bitmap nativeCopyAshmemConfig(long nativeSrcBitmap, int nativeConfig);
49 
nativeGetNativeFinalizer()50   public static native long nativeGetNativeFinalizer();
51 
nativeRecycle(long nativeBitmap)52   public static native void nativeRecycle(long nativeBitmap);
53 
nativeReconfigure( long nativeBitmap, int width, int height, int config, boolean isPremultiplied)54   public static native void nativeReconfigure(
55       long nativeBitmap, int width, int height, int config, boolean isPremultiplied);
56 
nativeCompress( long nativeBitmap, int format, int quality, OutputStream stream, byte[] tempStorage)57   public static native boolean nativeCompress(
58       long nativeBitmap, int format, int quality, OutputStream stream, byte[] tempStorage);
59 
nativeErase(long nativeBitmap, int color)60   public static native void nativeErase(long nativeBitmap, int color);
61 
nativeErase(long nativeBitmap, long colorSpacePtr, long color)62   public static native void nativeErase(long nativeBitmap, long colorSpacePtr, long color);
63 
nativeRowBytes(long nativeBitmap)64   public static native int nativeRowBytes(long nativeBitmap);
65 
nativeConfig(long nativeBitmap)66   public static native int nativeConfig(long nativeBitmap);
67 
nativeGetPixel(long nativeBitmap, int x, int y)68   public static native int nativeGetPixel(long nativeBitmap, int x, int y);
69 
nativeGetColor(long nativeBitmap, int x, int y)70   public static native long nativeGetColor(long nativeBitmap, int x, int y);
71 
nativeGetPixels( long nativeBitmap, int[] pixels, int offset, int stride, int x, int y, int width, int height)72   public static native void nativeGetPixels(
73       long nativeBitmap, int[] pixels, int offset, int stride, int x, int y, int width, int height);
74 
nativeSetPixel(long nativeBitmap, int x, int y, int color)75   public static native void nativeSetPixel(long nativeBitmap, int x, int y, int color);
76 
nativeSetPixels( long nativeBitmap, int[] colors, int offset, int stride, int x, int y, int width, int height)77   public static native void nativeSetPixels(
78       long nativeBitmap, int[] colors, int offset, int stride, int x, int y, int width, int height);
79 
nativeCopyPixelsToBuffer(long nativeBitmap, Buffer dst)80   public static native void nativeCopyPixelsToBuffer(long nativeBitmap, Buffer dst);
81 
nativeCopyPixelsFromBuffer(long nativeBitmap, Buffer src)82   public static native void nativeCopyPixelsFromBuffer(long nativeBitmap, Buffer src);
83 
nativeGenerationId(long nativeBitmap)84   public static native int nativeGenerationId(long nativeBitmap);
85 
nativeCreateFromParcel(Parcel p)86   public static native Bitmap nativeCreateFromParcel(Parcel p);
87 
88   // returns true on success
nativeWriteToParcel(long nativeBitmap, int density, Parcel p)89   public static native boolean nativeWriteToParcel(long nativeBitmap, int density, Parcel p);
90 
91   // returns a new bitmap built from the native bitmap's alpha, and the paint
nativeExtractAlpha( long nativeBitmap, long nativePaint, int[] offsetXY)92   public static native Bitmap nativeExtractAlpha(
93       long nativeBitmap, long nativePaint, int[] offsetXY);
94 
nativeHasAlpha(long nativeBitmap)95   public static native boolean nativeHasAlpha(long nativeBitmap);
96 
nativeIsPremultiplied(long nativeBitmap)97   public static native boolean nativeIsPremultiplied(long nativeBitmap);
98 
nativeSetPremultiplied(long nativeBitmap, boolean isPremul)99   public static native void nativeSetPremultiplied(long nativeBitmap, boolean isPremul);
100 
nativeSetHasAlpha( long nativeBitmap, boolean hasAlpha, boolean requestPremul)101   public static native void nativeSetHasAlpha(
102       long nativeBitmap, boolean hasAlpha, boolean requestPremul);
103 
nativeHasMipMap(long nativeBitmap)104   public static native boolean nativeHasMipMap(long nativeBitmap);
105 
nativeSetHasMipMap(long nativeBitmap, boolean hasMipMap)106   public static native void nativeSetHasMipMap(long nativeBitmap, boolean hasMipMap);
107 
nativeSameAs(long nativeBitmap0, long nativeBitmap1)108   public static native boolean nativeSameAs(long nativeBitmap0, long nativeBitmap1);
109 
nativePrepareToDraw(long nativeBitmap)110   public static native void nativePrepareToDraw(long nativeBitmap);
111 
nativeGetAllocationByteCount(long nativeBitmap)112   public static native int nativeGetAllocationByteCount(long nativeBitmap);
113 
nativeCopyPreserveInternalConfig(long nativeBitmap)114   public static native Bitmap nativeCopyPreserveInternalConfig(long nativeBitmap);
115 
nativeWrapHardwareBufferBitmap( HardwareBuffer buffer, long nativeColorSpace)116   public static native Bitmap nativeWrapHardwareBufferBitmap(
117       HardwareBuffer buffer, long nativeColorSpace);
118 
nativeGetHardwareBuffer(long nativeBitmap)119   public static native HardwareBuffer nativeGetHardwareBuffer(long nativeBitmap);
120 
nativeComputeColorSpace(long nativePtr)121   public static native ColorSpace nativeComputeColorSpace(long nativePtr);
122 
nativeSetColorSpace(long nativePtr, long nativeColorSpace)123   public static native void nativeSetColorSpace(long nativePtr, long nativeColorSpace);
124 
nativeIsSRGB(long nativePtr)125   public static native boolean nativeIsSRGB(long nativePtr);
126 
nativeIsSRGBLinear(long nativePtr)127   public static native boolean nativeIsSRGBLinear(long nativePtr);
128 
nativeSetImmutable(long nativePtr)129   public static native void nativeSetImmutable(long nativePtr);
130 
nativeIsImmutable(long nativePtr)131   public static native boolean nativeIsImmutable(long nativePtr);
132 
nativeIsBackedByAshmem(long nativePtr)133   public static native boolean nativeIsBackedByAshmem(long nativePtr);
134 
nativeCopyColorSpaceP(long srcBitmap, long dstBitmap)135   public static native void nativeCopyColorSpaceP(long srcBitmap, long dstBitmap);
136 
BitmapNatives()137   private BitmapNatives() {}
138 }
139