summaryrefslogtreecommitdiff
path: root/libs/hwui/jni/ScopedParcel.cpp
blob: 52cd988344b0eef40f6e4d72a71f1d98cf2e560d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "ScopedParcel.h"

#ifdef __linux__  // Only Linux support parcel

using namespace android;

int32_t ScopedParcel::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 ScopedParcel::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;
}

int64_t ScopedParcel::readInt64() {
    int64_t temp = 0;
    // TODO: This behavior-matches what android::Parcel does
    // but this should probably be better
    if (AParcel_readInt64(mParcel, &temp) != STATUS_OK) {
        temp = 0;
    }
    return temp;
}

float ScopedParcel::readFloat() {
    float temp = 0.;
    if (AParcel_readFloat(mParcel, &temp) != STATUS_OK) {
        temp = 0.;
    }
    return temp;
}

std::optional<sk_sp<SkData>> ScopedParcel::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 ScopedParcel::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);
    }
}
#endif  // __linux__  // Only Linux support parcel