Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a7404d45 authored by Leon Scroggins's avatar Leon Scroggins Committed by Android (Google) Code Review
Browse files

Merge "Convert @ColorLongs in native code"

parents a1809f38 94ba100c
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -573,14 +573,14 @@ static void Bitmap_erase(JNIEnv* env, jobject, jlong bitmapHandle, jint color) {
    bitmapErase(skBitmap, SkColor4f::FromColor(color), SkColorSpace::MakeSRGB());
}

static void Bitmap_eraseLong(JNIEnv* env, jobject, jlong bitmapHandle, jlong colorSpaceHandle,
        jfloat r, jfloat g, jfloat b, jfloat a) {
static void Bitmap_eraseLong(JNIEnv* env, jobject, jlong bitmapHandle,
        jlong colorSpaceHandle, jlong colorLong) {
    LocalScopedBitmap bitmap(bitmapHandle);
    SkBitmap skBitmap;
    bitmap->getSkBitmap(&skBitmap);

    SkColor4f color = GraphicsJNI::convertColorLong(colorLong);
    sk_sp<SkColorSpace> cs = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
    SkColor4f color = SkColor4f{r, g, b, a};
    bitmapErase(skBitmap, color, cs);
}

@@ -1190,7 +1190,7 @@ static const JNINativeMethod gBitmapMethods[] = {
    {   "nativeCompress",           "(JIILjava/io/OutputStream;[B)Z",
        (void*)Bitmap_compress },
    {   "nativeErase",              "(JI)V", (void*)Bitmap_erase },
    {   "nativeErase",              "(JJFFFF)V", (void*)Bitmap_eraseLong },
    {   "nativeErase",              "(JJJ)V", (void*)Bitmap_eraseLong },
    {   "nativeRowBytes",           "(J)I", (void*)Bitmap_rowBytes },
    {   "nativeConfig",             "(J)I", (void*)Bitmap_config },
    {   "nativeHasAlpha",           "(J)Z", (void*)Bitmap_hasAlpha },
+10 −0
Original line number Diff line number Diff line
@@ -111,6 +111,16 @@ public:

    static jobject getColorSpace(JNIEnv* env, sk_sp<SkColorSpace>& decodeColorSpace,
            SkColorType decodeColorType);

    /**
     * Convert from a Java @ColorLong to an SkColor4f that Skia can use directly.
     *
     * This ignores the encoded ColorSpace, besides checking to see if it is sRGB,
     * which is encoded differently. The color space should be passed down separately
     * via ColorSpace#getNativeInstance(), and converted with getNativeColorSpace(),
     * above.
     */
    static SkColor4f convertColorLong(jlong color);
};

class HeapAllocator : public SkBRDAllocator {
+22 −0
Original line number Diff line number Diff line
@@ -18,7 +18,9 @@
#include "GraphicsJNI.h"
#include "core_jni_helpers.h"

#include "SkColor.h"
#include "SkColorSpace.h"
#include "SkHalf.h"

using namespace android;

@@ -40,6 +42,26 @@ static skcms_Matrix3x3 getNativeXYZMatrix(JNIEnv* env, jfloatArray xyzD50) {

///////////////////////////////////////////////////////////////////////////////

SkColor4f GraphicsJNI::convertColorLong(jlong color) {
    if ((color & 0x3f) == 0) {
        // This corresponds to sRGB, which is treated differently than the rest.
        uint8_t a = color >> 56 & 0xff;
        uint8_t r = color >> 48 & 0xff;
        uint8_t g = color >> 40 & 0xff;
        uint8_t b = color >> 32 & 0xff;
        SkColor c = SkColorSetARGB(a, r, g, b);
        return SkColor4f::FromColor(c);
    }

    // These match the implementation of android.graphics.Color#red(long) etc.
    float r = SkHalfToFloat((SkHalf)(color >> 48 & 0xffff));
    float g = SkHalfToFloat((SkHalf)(color >> 32 & 0xffff));
    float b = SkHalfToFloat((SkHalf)(color >> 16 & 0xffff));
    float a =                       (color >>  6 &  0x3ff) / 1023.0f;

    return SkColor4f{r, g, b, a};
}

sk_sp<SkColorSpace> GraphicsJNI::getNativeColorSpace(jlong colorSpaceHandle) {
    if (colorSpaceHandle == 0) return nullptr;
    return sk_ref_sp(reinterpret_cast<SkColorSpace*>(colorSpaceHandle));
+2 −7
Original line number Diff line number Diff line
@@ -1778,11 +1778,7 @@ public final class Bitmap implements Parcelable {
        }

        ColorSpace cs = Color.colorSpace(c);
        float r = Color.red(c);
        float g = Color.green(c);
        float b = Color.blue(c);
        float a = Color.alpha(c);
        nativeErase(mNativePtr, cs.getNativeInstance(), r, g, b, a);
        nativeErase(mNativePtr, cs.getNativeInstance(), c);
    }

    /**
@@ -2128,8 +2124,7 @@ public final class Bitmap implements Parcelable {
                                            int quality, OutputStream stream,
                                            byte[] tempStorage);
    private static native void nativeErase(long nativeBitmap, int color);
    private static native void nativeErase(long nativeBitmap, long colorSpacePtr,
                                           float r, float g, float b, float a);
    private static native void nativeErase(long nativeBitmap, long colorSpacePtr, long color);
    private static native int nativeRowBytes(long nativeBitmap);
    private static native int nativeConfig(long nativeBitmap);