Make gainmap parcelable.
Bug: 267215989
Test: android.graphics.cts.GainmapTest
Change-Id: I189f652f26f2f92e37afdc3f69ae7b7653d69bf0
diff --git a/libs/hwui/jni/Bitmap.cpp b/libs/hwui/jni/Bitmap.cpp
index e71a2a5..3f9c4bd 100644
--- a/libs/hwui/jni/Bitmap.cpp
+++ b/libs/hwui/jni/Bitmap.cpp
@@ -10,6 +10,7 @@
 #include "Gainmap.h"
 #include "GraphicsJNI.h"
 #include "HardwareBufferHelpers.h"
+#include "ScopedParcel.h"
 #include "SkBitmap.h"
 #include "SkBlendMode.h"
 #include "SkCanvas.h"
@@ -27,12 +28,7 @@
 
 #ifdef __ANDROID__ // Layoutlib does not support graphic buffer, parcel or render thread
 #include <android-base/unique_fd.h>
-#include <android/binder_parcel.h>
-#include <android/binder_parcel_jni.h>
-#include <android/binder_parcel_platform.h>
-#include <cutils/ashmem.h>
 #include <renderthread/RenderProxy.h>
-#include <sys/mman.h>
 #endif
 
 #include <inttypes.h>
@@ -616,91 +612,7 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 // TODO: Move somewhere else
-#ifdef __ANDROID__ // Layoutlib does not support parcel
-
-class ScopedParcel {
-public:
-    explicit ScopedParcel(JNIEnv* env, jobject parcel) {
-        mParcel = AParcel_fromJavaParcel(env, parcel);
-    }
-
-    ~ScopedParcel() { AParcel_delete(mParcel); }
-
-    int32_t readInt32() {
-        int32_t temp = 0;
-        // TODO: This behavior-matches what android::Parcel does
-        // but this should probably be better
-        if (AParcel_readInt32(mParcel, &temp) != STATUS_OK) {
-            temp = 0;
-        }
-        return temp;
-    }
-
-    uint32_t readUint32() {
-        uint32_t temp = 0;
-        // TODO: This behavior-matches what android::Parcel does
-        // but this should probably be better
-        if (AParcel_readUint32(mParcel, &temp) != STATUS_OK) {
-            temp = 0;
-        }
-        return temp;
-    }
-
-    void writeInt32(int32_t value) { AParcel_writeInt32(mParcel, value); }
-
-    void writeUint32(uint32_t value) { AParcel_writeUint32(mParcel, value); }
-
-    bool allowFds() const { return AParcel_getAllowFds(mParcel); }
-
-    std::optional<sk_sp<SkData>> readData() {
-        struct Data {
-            void* ptr = nullptr;
-            size_t size = 0;
-        } data;
-        auto error = AParcel_readByteArray(mParcel, &data,
-                                           [](void* arrayData, int32_t length,
-                                              int8_t** outBuffer) -> bool {
-                                               Data* data = reinterpret_cast<Data*>(arrayData);
-                                               if (length > 0) {
-                                                   data->ptr = sk_malloc_canfail(length);
-                                                   if (!data->ptr) {
-                                                       return false;
-                                                   }
-                                                   *outBuffer =
-                                                           reinterpret_cast<int8_t*>(data->ptr);
-                                                   data->size = length;
-                                               }
-                                               return true;
-                                           });
-        if (error != STATUS_OK || data.size <= 0) {
-            sk_free(data.ptr);
-            return std::nullopt;
-        } else {
-            return SkData::MakeFromMalloc(data.ptr, data.size);
-        }
-    }
-
-    void writeData(const std::optional<sk_sp<SkData>>& optData) {
-        if (optData) {
-            const auto& data = *optData;
-            AParcel_writeByteArray(mParcel, reinterpret_cast<const int8_t*>(data->data()),
-                                   data->size());
-        } else {
-            AParcel_writeByteArray(mParcel, nullptr, -1);
-        }
-    }
-
-    AParcel* get() { return mParcel; }
-
-private:
-    AParcel* mParcel;
-};
-
-enum class BlobType : int32_t {
-    IN_PLACE,
-    ASHMEM,
-};
-
+#ifdef __ANDROID__  // Layoutlib does not support parcel
 #define ON_ERROR_RETURN(X) \
     if ((error = (X)) != STATUS_OK) return error