blob: 198908d334b61a21994d58b53c7487e20ba85286 [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 Reck42fcb0d2023-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>
Mathias Agopian05debe12017-02-08 17:04:18 -080023#include <gui/IGraphicBufferProducer.h>
John Reck42fcb0d2023-02-23 17:55:07 -050024#include <gui/Surface.h>
25#include <gui/view/Surface.h>
26#include <system/window.h>
27#include <utils/Log.h>
Mathias Agopian05debe12017-02-08 17:04:18 -080028
29namespace android {
30namespace view {
31
John Reck42fcb0d2023-02-23 17:55:07 -050032// 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
38extern "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
53extern "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 Agopian05debe12017-02-08 17:04:18 -080072status_t Surface::writeToParcel(Parcel* parcel) const {
73 return writeToParcel(parcel, false);
74}
75
76status_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 Nair992496b2020-10-22 17:27:21 -070090 res = IGraphicBufferProducer::exportToParcel(graphicBufferProducer, parcel);
91 if (res != OK) return res;
92 return parcel->writeStrongBinder(surfaceControlHandle);
Mathias Agopian05debe12017-02-08 17:04:18 -080093}
94
95status_t Surface::readFromParcel(const Parcel* parcel) {
96 return readFromParcel(parcel, false);
97}
98
99status_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' Caic90a77f2018-01-14 15:42:29 -0800114 graphicBufferProducer = IGraphicBufferProducer::createFromParcel(parcel);
Vishnu Nair992496b2020-10-22 17:27:21 -0700115 surfaceControlHandle = parcel->readStrongBinder();
Mathias Agopian05debe12017-02-08 17:04:18 -0800116 return OK;
117}
118
119String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
Steven Moreland46f79a22020-11-04 23:40:49 +0000120 std::optional<String16> str;
121 parcel->readString16(&str);
122 return str.value_or(String16());
Mathias Agopian05debe12017-02-08 17:04:18 -0800123}
124
125} // namespace view
126} // namespace android