1 /*
2 * Copyright (C) 2023 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 #include <dlfcn.h>
18 #include <stdint.h>
19
20 #include <android/imagedecoder.h>
21
22 #include "berberis/base/bit_util.h"
23 #include "berberis/guest_abi/function_wrappers.h"
24 #include "berberis/proxy_loader/proxy_library_builder.h"
25
26 namespace berberis {
27
28 namespace {
29
30 // int AImageDecoder_setCrop(AImageDecoder*, ARect)
DoCustomTrampoline_AImageDecoder_setCrop(HostCode,ProcessState * state)31 void DoCustomTrampoline_AImageDecoder_setCrop(HostCode /* callee */, ProcessState* state) {
32 #if defined(NATIVE_BRIDGE_GUEST_ARCH_ARM)
33 // On arm, ARect is passed the same way as 4x int32_t.
34 auto [decoder, left, top, right, bottom] =
35 GuestParamsValues<int(AImageDecoder*, int32_t, int32_t, int32_t, int32_t)>(state);
36 #elif defined(NATIVE_BRIDGE_GUEST_ARCH_ARM64) || defined(NATIVE_BRIDGE_GUEST_ARCH_RISCV64)
37 // ARect is passed the same way as 2x int64_t.
38 auto [decoder, left_and_top, right_and_bottom] =
39 GuestParamsValues<int(AImageDecoder*, int64_t, int64_t)>(state);
40 int32_t left = left_and_top;
41 int32_t top = left_and_top >> 32;
42 int32_t right = right_and_bottom;
43 int32_t bottom = right_and_bottom >> 32;
44 #else
45 #error "Unknown guest arch"
46 #endif
47 ARect crop{left, top, right, bottom};
48 auto&& [ret] = GuestReturnReference<decltype(AImageDecoder_setCrop)>(state);
49 ret = AImageDecoder_setCrop(decoder, crop);
50 }
51
52 // ARect AImageDecoderFrameInfo_getFrameRect(const AImageDecoderFrameInfo* info)
DoCustomTrampoline_AImageDecoderFrameInfo_getFrameRect(HostCode,ProcessState * state)53 void DoCustomTrampoline_AImageDecoderFrameInfo_getFrameRect(HostCode /* callee */,
54 ProcessState* state) {
55 #if defined(NATIVE_BRIDGE_GUEST_ARCH_ARM)
56 // ARect is returned by pointer which GuestArgumentInfo normally supports.
57 using FuncType = decltype(AImageDecoderFrameInfo_getFrameRect);
58 using ResType = ARect;
59 #elif defined(NATIVE_BRIDGE_GUEST_ARCH_ARM64) || defined(NATIVE_BRIDGE_GUEST_ARCH_RISCV64)
60 // ARect is returned in two integer registers which is equivalent to returning __uint128_t.
61 using FuncType = __uint128_t(const AImageDecoderFrameInfo* info);
62 using ResType = __uint128_t;
63 #else
64 #error "Unknown guest arch"
65 #endif
66 auto [info] = GuestParamsValues<FuncType>(state);
67 auto&& [ret] = GuestReturnReference<FuncType>(state);
68 ret = bit_cast<ResType>(AImageDecoderFrameInfo_getFrameRect(info));
69 }
70
71 #if defined(NATIVE_BRIDGE_GUEST_ARCH_ARM) && defined(__i386__)
72
73 #include "trampolines_arm_to_x86-inl.h" // generated file NOLINT [build/include]
74
75 #elif defined(NATIVE_BRIDGE_GUEST_ARCH_ARM64) && defined(__x86_64__)
76
77 #include "trampolines_arm64_to_x86_64-inl.h" // generated file NOLINT [build/include]
78
79 #elif defined(NATIVE_BRIDGE_GUEST_ARCH_RISCV64) && defined(__x86_64__)
80
81 #include "trampolines_riscv64_to_x86_64-inl.h" // generated file NOLINT [build/include]
82
83 #else
84
85 #error "Unknown guest/host arch combination"
86
87 #endif
88
89 DEFINE_INIT_PROXY_LIBRARY("libjnigraphics.so")
90
91 } // namespace
92
93 } // namespace berberis
94