| Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | #define LOG_TAG "Surface" |
| 18 | |
| John Reck | 42fcb0d | 2023-02-23 17:55:07 -0500 | [diff] [blame] | 19 | #include <android/binder_libbinder.h> |
| 20 | #include <android/binder_parcel.h> |
| 21 | #include <android/native_window.h> |
| Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 22 | #include <binder/Parcel.h> |
| Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 23 | #include <gui/IGraphicBufferProducer.h> |
| John Reck | 42fcb0d | 2023-02-23 17:55:07 -0500 | [diff] [blame] | 24 | #include <gui/Surface.h> |
| 25 | #include <gui/view/Surface.h> |
| 26 | #include <system/window.h> |
| 27 | #include <utils/Log.h> |
| Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace view { |
| 31 | |
| John Reck | 42fcb0d | 2023-02-23 17:55:07 -0500 | [diff] [blame] | 32 | // Since this is a parcelable utility and we want to keep the wire format stable, only build this |
| 33 | // when building the system libgui to detect any issues loading the wrong libgui from |
| 34 | // libnativewindow |
| 35 | |
| 36 | #if (!defined(__ANDROID_APEX__) && !defined(__ANDROID_VNDK__)) |
| 37 | |
| 38 | extern "C" status_t android_view_Surface_writeToParcel(ANativeWindow* _Nonnull window, |
| 39 | Parcel* _Nonnull parcel) { |
| 40 | int value; |
| 41 | int err = (*window->query)(window, NATIVE_WINDOW_CONCRETE_TYPE, &value); |
| 42 | if (err != OK || value != NATIVE_WINDOW_SURFACE) { |
| 43 | ALOGE("Error: ANativeWindow is not backed by Surface"); |
| 44 | return STATUS_BAD_VALUE; |
| 45 | } |
| 46 | // Use a android::view::Surface to parcelize the window |
| 47 | android::view::Surface shimSurface; |
| 48 | shimSurface.graphicBufferProducer = android::Surface::getIGraphicBufferProducer(window); |
| 49 | shimSurface.surfaceControlHandle = android::Surface::getSurfaceControlHandle(window); |
| 50 | return shimSurface.writeToParcel(parcel); |
| 51 | } |
| 52 | |
| 53 | extern "C" status_t android_view_Surface_readFromParcel( |
| 54 | const Parcel* _Nonnull parcel, ANativeWindow* _Nullable* _Nonnull outWindow) { |
| 55 | // Use a android::view::Surface to unparcel the window |
| 56 | android::view::Surface shimSurface; |
| 57 | status_t ret = shimSurface.readFromParcel(parcel); |
| 58 | if (ret != OK) { |
| 59 | ALOGE("%s: Error: Failed to create android::view::Surface from AParcel", __FUNCTION__); |
| 60 | return STATUS_BAD_VALUE; |
| 61 | } |
| 62 | auto surface = sp<android::Surface>::make(shimSurface.graphicBufferProducer, false, |
| 63 | shimSurface.surfaceControlHandle); |
| 64 | ANativeWindow* anw = surface.get(); |
| 65 | ANativeWindow_acquire(anw); |
| 66 | *outWindow = anw; |
| 67 | return STATUS_OK; |
| 68 | } |
| 69 | |
| 70 | #endif |
| 71 | |
| Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 72 | status_t Surface::writeToParcel(Parcel* parcel) const { |
| 73 | return writeToParcel(parcel, false); |
| 74 | } |
| 75 | |
| 76 | status_t Surface::writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const { |
| 77 | if (parcel == nullptr) return BAD_VALUE; |
| 78 | |
| 79 | status_t res = OK; |
| 80 | |
| 81 | if (!nameAlreadyWritten) { |
| 82 | res = parcel->writeString16(name); |
| 83 | if (res != OK) return res; |
| 84 | |
| 85 | /* isSingleBuffered defaults to no */ |
| 86 | res = parcel->writeInt32(0); |
| 87 | if (res != OK) return res; |
| 88 | } |
| 89 | |
| Vishnu Nair | 992496b | 2020-10-22 17:27:21 -0700 | [diff] [blame] | 90 | res = IGraphicBufferProducer::exportToParcel(graphicBufferProducer, parcel); |
| 91 | if (res != OK) return res; |
| 92 | return parcel->writeStrongBinder(surfaceControlHandle); |
| Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | status_t Surface::readFromParcel(const Parcel* parcel) { |
| 96 | return readFromParcel(parcel, false); |
| 97 | } |
| 98 | |
| 99 | status_t Surface::readFromParcel(const Parcel* parcel, bool nameAlreadyRead) { |
| 100 | if (parcel == nullptr) return BAD_VALUE; |
| 101 | |
| 102 | status_t res = OK; |
| 103 | if (!nameAlreadyRead) { |
| 104 | name = readMaybeEmptyString16(parcel); |
| 105 | // Discard this for now |
| 106 | int isSingleBuffered; |
| 107 | res = parcel->readInt32(&isSingleBuffered); |
| 108 | if (res != OK) { |
| 109 | ALOGE("Can't read isSingleBuffered"); |
| 110 | return res; |
| 111 | } |
| 112 | } |
| 113 | |
| Jiwen 'Steve' Cai | c90a77f | 2018-01-14 15:42:29 -0800 | [diff] [blame] | 114 | graphicBufferProducer = IGraphicBufferProducer::createFromParcel(parcel); |
| Vishnu Nair | 992496b | 2020-10-22 17:27:21 -0700 | [diff] [blame] | 115 | surfaceControlHandle = parcel->readStrongBinder(); |
| Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 116 | return OK; |
| 117 | } |
| 118 | |
| 119 | String16 Surface::readMaybeEmptyString16(const Parcel* parcel) { |
| Steven Moreland | 46f79a2 | 2020-11-04 23:40:49 +0000 | [diff] [blame] | 120 | std::optional<String16> str; |
| 121 | parcel->readString16(&str); |
| 122 | return str.value_or(String16()); |
| Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | } // namespace view |
| 126 | } // namespace android |