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
19 #include <android/binder_libbinder.h>
20 #include <android/binder_parcel.h>
21 #include <android/native_window.h>
22 #include <binder/Parcel.h>
23 #include <gui/Surface.h>
24 #include <gui/view/Surface.h>
25 #include <system/window.h>
26 #include <utils/Log.h>
27
28 namespace android {
29 namespace view {
30
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
android_view_Surface_writeToParcel(ANativeWindow * _Nonnull window,Parcel * _Nonnull parcel)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
android_view_Surface_readFromParcel(const Parcel * _Nonnull parcel,ANativeWindow * _Nullable * _Nonnull outWindow)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
writeToParcel(Parcel * parcel) const71 status_t Surface::writeToParcel(Parcel* parcel) const {
72 return writeToParcel(parcel, false);
73 }
74
writeToParcel(Parcel * parcel,bool nameAlreadyWritten) const75 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
89 res = IGraphicBufferProducer::exportToParcel(graphicBufferProducer, parcel);
90 if (res != OK) return res;
91 return parcel->writeStrongBinder(surfaceControlHandle);
92 }
93
readFromParcel(const Parcel * parcel)94 status_t Surface::readFromParcel(const Parcel* parcel) {
95 return readFromParcel(parcel, false);
96 }
97
readFromParcel(const Parcel * parcel,bool nameAlreadyRead)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
113 graphicBufferProducer = IGraphicBufferProducer::createFromParcel(parcel);
114 surfaceControlHandle = parcel->readStrongBinder();
115 return OK;
116 }
117
readMaybeEmptyString16(const Parcel * parcel)118 String16 Surface::readMaybeEmptyString16(const Parcel* parcel) {
119 std::optional<String16> str;
120 parcel->readString16(&str);
121 return str.value_or(String16());
122 }
123
124 } // namespace view
125 } // namespace android
126