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