blob: 2cf7081edee18dbe4ab771887f0f4d34907c4a23 [file] [log] [blame]
Mathias Agopian05debe12017-02-08 17:04:18 -08001/*
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 Reck7e2284c2023-02-23 17:55:07 -050019#include <android/binder_libbinder.h>
20#include <android/binder_parcel.h>
21#include <android/native_window.h>
Mathias Agopian05debe12017-02-08 17:04:18 -080022#include <binder/Parcel.h>
John Reck7e2284c2023-02-23 17:55:07 -050023#include <gui/Surface.h>
24#include <gui/view/Surface.h>
25#include <system/window.h>
26#include <utils/Log.h>
Mathias Agopian05debe12017-02-08 17:04:18 -080027
28namespace android {
29namespace view {
30
John Reck7e2284c2023-02-23 17:55:07 -050031// 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
37extern "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
52extern "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 Agopian05debe12017-02-08 17:04:18 -080071status_t Surface::writeToParcel(Parcel* parcel) const {
72 return writeToParcel(parcel, false);
73}
74
75status_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 Nair992496b2020-10-22 17:27:21 -070089 res = IGraphicBufferProducer::exportToParcel(graphicBufferProducer, parcel);
90 if (res != OK) return res;
91 return parcel->writeStrongBinder(surfaceControlHandle);
Mathias Agopian05debe12017-02-08 17:04:18 -080092}
93
94status_t Surface::readFromParcel(const Parcel* parcel) {
95 return readFromParcel(parcel, false);
96}
97
98status_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' Caic90a77f2018-01-14 15:42:29 -0800113 graphicBufferProducer = IGraphicBufferProducer::createFromParcel(parcel);
Vishnu Nair992496b2020-10-22 17:27:21 -0700114 surfaceControlHandle = parcel->readStrongBinder();
Mathias Agopian05debe12017-02-08 17:04:18 -0800115 return OK;
116}
117
118String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
Steven Moreland46f79a22020-11-04 23:40:49 +0000119 std::optional<String16> str;
120 parcel->readString16(&str);
121 return str.value_or(String16());
Mathias Agopian05debe12017-02-08 17:04:18 -0800122}
123
Carlos Martinez Romero1d2eb222024-09-12 09:24:41 -0700124#if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
125Surface 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
137sp<android::Surface> Surface::toSurface() const {
138 if (graphicBufferProducer == nullptr) return nullptr;
139 return new android::Surface(graphicBufferProducer, false, surfaceControlHandle);
140}
141
142status_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}
Carlos Martinez Romero0cc2e2c2024-10-14 10:16:56 -0700154
155bool Surface::isEmpty() const {
156 return graphicBufferProducer == nullptr;
157}
Carlos Martinez Romero1d2eb222024-09-12 09:24:41 -0700158#endif
159
Jim Shargo785d2e22024-07-29 23:21:08 +0000160std::string Surface::toString() const {
161 std::stringstream out;
162 out << name;
163 return out.str();
164}
165
Mathias Agopian05debe12017-02-08 17:04:18 -0800166} // namespace view
167} // namespace android