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 | 7e2284c | 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> |
John Reck | 7e2284c | 2023-02-23 17:55:07 -0500 | [diff] [blame] | 23 | #include <gui/Surface.h> |
| 24 | #include <gui/view/Surface.h> |
| 25 | #include <system/window.h> |
| 26 | #include <utils/Log.h> |
Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace view { |
| 30 | |
John Reck | 7e2284c | 2023-02-23 17:55:07 -0500 | [diff] [blame] | 31 | // Since this is a parcelable utility and we want to keep the wire format stable, only build this |
| 32 | // when building the system libgui to detect any issues loading the wrong libgui from |
| 33 | // libnativewindow |
| 34 | |
| 35 | #if (!defined(__ANDROID_APEX__) && !defined(__ANDROID_VNDK__)) |
| 36 | |
| 37 | extern "C" status_t android_view_Surface_writeToParcel(ANativeWindow* _Nonnull window, |
| 38 | Parcel* _Nonnull parcel) { |
| 39 | int value; |
| 40 | int err = (*window->query)(window, NATIVE_WINDOW_CONCRETE_TYPE, &value); |
| 41 | if (err != OK || value != NATIVE_WINDOW_SURFACE) { |
| 42 | ALOGE("Error: ANativeWindow is not backed by Surface"); |
| 43 | return STATUS_BAD_VALUE; |
| 44 | } |
| 45 | // Use a android::view::Surface to parcelize the window |
| 46 | android::view::Surface shimSurface; |
| 47 | shimSurface.graphicBufferProducer = android::Surface::getIGraphicBufferProducer(window); |
| 48 | shimSurface.surfaceControlHandle = android::Surface::getSurfaceControlHandle(window); |
| 49 | return shimSurface.writeToParcel(parcel); |
| 50 | } |
| 51 | |
| 52 | extern "C" status_t android_view_Surface_readFromParcel( |
| 53 | const Parcel* _Nonnull parcel, ANativeWindow* _Nullable* _Nonnull outWindow) { |
| 54 | // Use a android::view::Surface to unparcel the window |
| 55 | android::view::Surface shimSurface; |
| 56 | status_t ret = shimSurface.readFromParcel(parcel); |
| 57 | if (ret != OK) { |
| 58 | ALOGE("%s: Error: Failed to create android::view::Surface from AParcel", __FUNCTION__); |
| 59 | return STATUS_BAD_VALUE; |
| 60 | } |
| 61 | auto surface = sp<android::Surface>::make(shimSurface.graphicBufferProducer, false, |
| 62 | shimSurface.surfaceControlHandle); |
| 63 | ANativeWindow* anw = surface.get(); |
| 64 | ANativeWindow_acquire(anw); |
| 65 | *outWindow = anw; |
| 66 | return STATUS_OK; |
| 67 | } |
| 68 | |
| 69 | #endif |
| 70 | |
Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 71 | status_t Surface::writeToParcel(Parcel* parcel) const { |
| 72 | return writeToParcel(parcel, false); |
| 73 | } |
| 74 | |
| 75 | status_t Surface::writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const { |
| 76 | if (parcel == nullptr) return BAD_VALUE; |
| 77 | |
| 78 | status_t res = OK; |
| 79 | |
| 80 | if (!nameAlreadyWritten) { |
| 81 | res = parcel->writeString16(name); |
| 82 | if (res != OK) return res; |
| 83 | |
| 84 | /* isSingleBuffered defaults to no */ |
| 85 | res = parcel->writeInt32(0); |
| 86 | if (res != OK) return res; |
| 87 | } |
| 88 | |
Vishnu Nair | 992496b | 2020-10-22 17:27:21 -0700 | [diff] [blame] | 89 | res = IGraphicBufferProducer::exportToParcel(graphicBufferProducer, parcel); |
| 90 | if (res != OK) return res; |
| 91 | return parcel->writeStrongBinder(surfaceControlHandle); |
Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | status_t Surface::readFromParcel(const Parcel* parcel) { |
| 95 | return readFromParcel(parcel, false); |
| 96 | } |
| 97 | |
| 98 | status_t Surface::readFromParcel(const Parcel* parcel, bool nameAlreadyRead) { |
| 99 | if (parcel == nullptr) return BAD_VALUE; |
| 100 | |
| 101 | status_t res = OK; |
| 102 | if (!nameAlreadyRead) { |
| 103 | name = readMaybeEmptyString16(parcel); |
| 104 | // Discard this for now |
| 105 | int isSingleBuffered; |
| 106 | res = parcel->readInt32(&isSingleBuffered); |
| 107 | if (res != OK) { |
| 108 | ALOGE("Can't read isSingleBuffered"); |
| 109 | return res; |
| 110 | } |
| 111 | } |
| 112 | |
Jiwen 'Steve' Cai | c90a77f | 2018-01-14 15:42:29 -0800 | [diff] [blame] | 113 | graphicBufferProducer = IGraphicBufferProducer::createFromParcel(parcel); |
Vishnu Nair | 992496b | 2020-10-22 17:27:21 -0700 | [diff] [blame] | 114 | surfaceControlHandle = parcel->readStrongBinder(); |
Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 115 | return OK; |
| 116 | } |
| 117 | |
| 118 | String16 Surface::readMaybeEmptyString16(const Parcel* parcel) { |
Steven Moreland | 46f79a2 | 2020-11-04 23:40:49 +0000 | [diff] [blame] | 119 | std::optional<String16> str; |
| 120 | parcel->readString16(&str); |
| 121 | return str.value_or(String16()); |
Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Carlos Martinez Romero | 1d2eb22 | 2024-09-12 09:24:41 -0700 | [diff] [blame] | 124 | #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES |
| 125 | Surface Surface::fromSurface(const sp<android::Surface>& surface) { |
| 126 | if (surface == nullptr) { |
| 127 | ALOGE("%s: Error: view::Surface::fromSurface failed due to null surface.", __FUNCTION__); |
| 128 | return Surface(); |
| 129 | } |
| 130 | Surface s; |
| 131 | s.name = String16(surface->getConsumerName()); |
| 132 | s.graphicBufferProducer = surface->getIGraphicBufferProducer(); |
| 133 | s.surfaceControlHandle = surface->getSurfaceControlHandle(); |
| 134 | return s; |
| 135 | } |
| 136 | |
| 137 | sp<android::Surface> Surface::toSurface() const { |
| 138 | if (graphicBufferProducer == nullptr) return nullptr; |
| 139 | return new android::Surface(graphicBufferProducer, false, surfaceControlHandle); |
| 140 | } |
| 141 | |
| 142 | status_t Surface::getUniqueId(uint64_t* out_id) const { |
| 143 | if (graphicBufferProducer == nullptr) { |
| 144 | ALOGE("android::viewSurface::getUniqueId() failed because it's not initialized."); |
| 145 | return UNEXPECTED_NULL; |
| 146 | } |
| 147 | status_t status = graphicBufferProducer->getUniqueId(out_id); |
| 148 | if (status != OK) { |
| 149 | ALOGE("android::viewSurface::getUniqueId() failed."); |
| 150 | return status; |
| 151 | } |
| 152 | return OK; |
| 153 | } |
| 154 | #endif |
| 155 | |
Jim Shargo | 785d2e2 | 2024-07-29 23:21:08 +0000 | [diff] [blame] | 156 | std::string Surface::toString() const { |
| 157 | std::stringstream out; |
| 158 | out << name; |
| 159 | return out.str(); |
| 160 | } |
| 161 | |
Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 162 | } // namespace view |
| 163 | } // namespace android |