blob: 9cd3a47126218998740324026f04300efd1161ca [file] [log] [blame]
Yin-Chia Yehb97babb2015-03-12 13:42:44 -07001/*
2**
3** Copyright 2015, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "OutputConfiguration"
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080019//#define LOG_NDEBUG 0
20
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070021#include <utils/Log.h>
22
23#include <camera/camera2/OutputConfiguration.h>
24#include <binder/Parcel.h>
Brian Andersonf6753562016-10-11 14:51:05 -070025#include <gui/Surface.h>
26#include <utils/String8.h>
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070027
28namespace android {
29
30
31const int OutputConfiguration::INVALID_ROTATION = -1;
Zhijun He018107a2016-01-18 15:32:50 -080032const int OutputConfiguration::INVALID_SET_ID = -1;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070033
Shuzhen Wang0129d522016-10-30 22:43:41 -070034const std::vector<sp<IGraphicBufferProducer>>&
35 OutputConfiguration::getGraphicBufferProducers() const {
36 return mGbps;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070037}
38
39int OutputConfiguration::getRotation() const {
40 return mRotation;
41}
42
Zhijun He018107a2016-01-18 15:32:50 -080043int OutputConfiguration::getSurfaceSetID() const {
44 return mSurfaceSetID;
45}
46
Zhijun He5d677d12016-05-29 16:52:39 -070047int OutputConfiguration::getSurfaceType() const {
48 return mSurfaceType;
49}
50
51int OutputConfiguration::getWidth() const {
52 return mWidth;
53}
54
55int OutputConfiguration::getHeight() const {
56 return mHeight;
57}
58
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080059OutputConfiguration::OutputConfiguration() :
60 mRotation(INVALID_ROTATION),
Zhijun He5d677d12016-05-29 16:52:39 -070061 mSurfaceSetID(INVALID_SET_ID),
62 mSurfaceType(SURFACE_TYPE_UNKNOWN),
63 mWidth(0),
64 mHeight(0) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080065}
66
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080067OutputConfiguration::OutputConfiguration(const android::Parcel& parcel) :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080068 mRotation(INVALID_ROTATION),
69 mSurfaceSetID(INVALID_SET_ID) {
70 readFromParcel(&parcel);
71}
72
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080073status_t OutputConfiguration::readFromParcel(const android::Parcel* parcel) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080074 status_t err = OK;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070075 int rotation = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080076
77 if (parcel == nullptr) return BAD_VALUE;
78
79 if ((err = parcel->readInt32(&rotation)) != OK) {
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070080 ALOGE("%s: Failed to read rotation from parcel", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080081 return err;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070082 }
83
Zhijun He018107a2016-01-18 15:32:50 -080084 int setID = INVALID_SET_ID;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080085 if ((err = parcel->readInt32(&setID)) != OK) {
Zhijun He018107a2016-01-18 15:32:50 -080086 ALOGE("%s: Failed to read surface set ID from parcel", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080087 return err;
Zhijun He018107a2016-01-18 15:32:50 -080088 }
89
Zhijun He5d677d12016-05-29 16:52:39 -070090 int surfaceType = SURFACE_TYPE_UNKNOWN;
91 if ((err = parcel->readInt32(&surfaceType)) != OK) {
92 ALOGE("%s: Failed to read surface type from parcel", __FUNCTION__);
93 return err;
94 }
95
96 int width = 0;
97 if ((err = parcel->readInt32(&width)) != OK) {
98 ALOGE("%s: Failed to read surface width from parcel", __FUNCTION__);
99 return err;
100 }
101
102 int height = 0;
103 if ((err = parcel->readInt32(&height)) != OK) {
104 ALOGE("%s: Failed to read surface height from parcel", __FUNCTION__);
105 return err;
106 }
107
Shuzhen Wang0129d522016-10-30 22:43:41 -0700108 // numSurfaces is the total number of surfaces for this OutputConfiguration,
109 // regardless the surface is deferred or not.
110 int numSurfaces = 0;
111 if ((err = parcel->readInt32(&numSurfaces)) != OK) {
112 ALOGE("%s: Failed to read maxSurfaces from parcel", __FUNCTION__);
113 return err;
114 }
115 if (numSurfaces < 1) {
116 ALOGE("%s: there has to be at least 1 surface per"
117 " outputConfiguration", __FUNCTION__);
118 return BAD_VALUE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800119 }
120
Shuzhen Wang0129d522016-10-30 22:43:41 -0700121 // Read all surfaces from parcel. If a surface is deferred, readFromPacel
122 // returns error, and a null surface is put into the mGbps. We assume all
123 // deferred surfaces are after non-deferred surfaces in the parcel.
124 // TODO: Need better way to detect deferred surface than using error
125 // return from readFromParcel.
126 std::vector<sp<IGraphicBufferProducer>> gbps;
127 for (int i = 0; i < numSurfaces; i++) {
128 view::Surface surfaceShim;
129 if ((err = surfaceShim.readFromParcel(parcel)) != OK) {
130 // Read surface failure for deferred surface configuration is expected.
131 if ((surfaceType == SURFACE_TYPE_SURFACE_VIEW ||
132 surfaceType == SURFACE_TYPE_SURFACE_TEXTURE)) {
133 ALOGV("%s: Get null surface from a deferred surface configuration (%dx%d)",
134 __FUNCTION__, width, height);
135 err = OK;
136 } else {
137 ALOGE("%s: Failed to read surface from parcel", __FUNCTION__);
138 return err;
139 }
140 }
141 gbps.push_back(surfaceShim.graphicBufferProducer);
142 ALOGV("%s: OutputConfiguration: gbps[%d] : %p, name %s", __FUNCTION__,
143 i, gbps[i].get(), String8(surfaceShim.name).string());
144 }
145
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700146 mRotation = rotation;
Zhijun He018107a2016-01-18 15:32:50 -0800147 mSurfaceSetID = setID;
Zhijun He5d677d12016-05-29 16:52:39 -0700148 mSurfaceType = surfaceType;
149 mWidth = width;
150 mHeight = height;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700151 mGbps = std::move(gbps);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700152
Shuzhen Wang0129d522016-10-30 22:43:41 -0700153 ALOGV("%s: OutputConfiguration: rotation = %d, setId = %d, surfaceType = %d",
154 __FUNCTION__, mRotation, mSurfaceSetID, mSurfaceType);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800155
156 return err;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700157}
158
Zhijun He018107a2016-01-18 15:32:50 -0800159OutputConfiguration::OutputConfiguration(sp<IGraphicBufferProducer>& gbp, int rotation,
160 int surfaceSetID) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700161 mGbps.push_back(gbp);
Ruben Brunk3450ba72015-06-16 11:00:37 -0700162 mRotation = rotation;
Zhijun He018107a2016-01-18 15:32:50 -0800163 mSurfaceSetID = surfaceSetID;
Ruben Brunk3450ba72015-06-16 11:00:37 -0700164}
165
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800166status_t OutputConfiguration::writeToParcel(android::Parcel* parcel) const {
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700167
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800168 if (parcel == nullptr) return BAD_VALUE;
169 status_t err = OK;
170
171 err = parcel->writeInt32(mRotation);
172 if (err != OK) return err;
173
174 err = parcel->writeInt32(mSurfaceSetID);
175 if (err != OK) return err;
176
Zhijun He5d677d12016-05-29 16:52:39 -0700177 err = parcel->writeInt32(mSurfaceType);
178 if (err != OK) return err;
179
180 err = parcel->writeInt32(mWidth);
181 if (err != OK) return err;
182
183 err = parcel->writeInt32(mHeight);
184 if (err != OK) return err;
185
Shuzhen Wang0129d522016-10-30 22:43:41 -0700186 int numSurfaces = mGbps.size();
187 err = parcel->writeInt32(numSurfaces);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800188 if (err != OK) return err;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700189
Shuzhen Wang0129d522016-10-30 22:43:41 -0700190 for (int i = 0; i < numSurfaces; i++) {
191 view::Surface surfaceShim;
192 surfaceShim.name = String16("unknown_name"); // name of surface
193 surfaceShim.graphicBufferProducer = mGbps[i];
194
195 err = surfaceShim.writeToParcel(parcel);
196 if (err != OK) return err;
197 }
198
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700199 return OK;
200}
201
Shuzhen Wang0129d522016-10-30 22:43:41 -0700202bool OutputConfiguration::gbpsEqual(const OutputConfiguration& other) const {
203 const std::vector<sp<IGraphicBufferProducer> >& otherGbps =
204 other.getGraphicBufferProducers();
205
206 if (mGbps.size() != otherGbps.size()) {
207 return false;
208 }
209
210 for (size_t i = 0; i < mGbps.size(); i++) {
211 if (mGbps[i] != otherGbps[i]) {
212 return false;
213 }
214 }
215
216 return true;
217}
218
219bool OutputConfiguration::gbpsLessThan(const OutputConfiguration& other) const {
220 const std::vector<sp<IGraphicBufferProducer> >& otherGbps =
221 other.getGraphicBufferProducers();
222
223 if (mGbps.size() != otherGbps.size()) {
224 return mGbps.size() < otherGbps.size();
225 }
226
227 for (size_t i = 0; i < mGbps.size(); i++) {
228 if (mGbps[i] != otherGbps[i]) {
229 return mGbps[i] < otherGbps[i];
230 }
231 }
232
233 return false;
234}
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700235}; // namespace android