blob: be7b1d2f6a48997918f084bb8f94c38b3563942d [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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 "SurfaceComposerClient"
18
19#include <stdint.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <utils/Errors.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/Log.h>
Mathias Agopiand4784a32010-05-27 19:41:15 -070024#include <utils/Singleton.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070025#include <utils/SortedVector.h>
26#include <utils/String8.h>
27#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <binder/IServiceManager.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080030
Michael Wright28f24d02016-07-12 13:30:53 -070031#include <system/graphics.h>
32
Mathias Agopian076b1cc2009-04-10 14:24:30 -070033#include <ui/DisplayInfo.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034
Robert Carr673134e2017-01-09 19:48:38 -080035#include <gui/BufferItemConsumer.h>
Mathias Agopianabe815d2013-03-19 22:22:21 -070036#include <gui/CpuConsumer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080037#include <gui/IGraphicBufferProducer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080038#include <gui/ISurfaceComposer.h>
39#include <gui/ISurfaceComposerClient.h>
Robert Carr0d480722017-01-10 16:42:54 -080040#include <gui/Surface.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080041#include <gui/SurfaceComposerClient.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
Mathias Agopian41f673c2011-11-17 17:48:35 -080043#include <private/gui/ComposerService.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080044#include <private/gui/LayerState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
46namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047// ---------------------------------------------------------------------------
48
Mathias Agopian7e27f052010-05-28 14:22:23 -070049ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
50
Mathias Agopianb7e930d2010-06-01 15:12:58 -070051ComposerService::ComposerService()
52: Singleton<ComposerService>() {
Andy McFadden6652b3e2012-09-06 18:45:56 -070053 Mutex::Autolock _l(mLock);
54 connectLocked();
55}
56
57void ComposerService::connectLocked() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -070058 const String16 name("SurfaceFlinger");
59 while (getService(name, &mComposerService) != NO_ERROR) {
60 usleep(250000);
61 }
Andy McFadden6652b3e2012-09-06 18:45:56 -070062 assert(mComposerService != NULL);
63
64 // Create the death listener.
65 class DeathObserver : public IBinder::DeathRecipient {
66 ComposerService& mComposerService;
67 virtual void binderDied(const wp<IBinder>& who) {
68 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
69 who.unsafe_get());
70 mComposerService.composerServiceDied();
71 }
72 public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070073 explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
Andy McFadden6652b3e2012-09-06 18:45:56 -070074 };
75
76 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -080077 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
Mathias Agopianb7e930d2010-06-01 15:12:58 -070078}
79
Andy McFadden6652b3e2012-09-06 18:45:56 -070080/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
81 ComposerService& instance = ComposerService::getInstance();
82 Mutex::Autolock _l(instance.mLock);
83 if (instance.mComposerService == NULL) {
84 ComposerService::getInstance().connectLocked();
85 assert(instance.mComposerService != NULL);
86 ALOGD("ComposerService reconnected");
87 }
88 return instance.mComposerService;
89}
90
91void ComposerService::composerServiceDied()
92{
93 Mutex::Autolock _l(mLock);
94 mComposerService = NULL;
95 mDeathObserver = NULL;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070096}
97
Mathias Agopian7e27f052010-05-28 14:22:23 -070098// ---------------------------------------------------------------------------
99
Mathias Agopian698c0872011-06-28 19:09:31 -0700100static inline
Mathias Agopiane57f2922012-08-09 16:29:12 -0700101int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700102 if (lhs.client < rhs.client) return -1;
103 if (lhs.client > rhs.client) return 1;
104 if (lhs.state.surface < rhs.state.surface) return -1;
105 if (lhs.state.surface > rhs.state.surface) return 1;
106 return 0;
107}
108
Mathias Agopiane57f2922012-08-09 16:29:12 -0700109static inline
110int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
111 return compare_type(lhs.token, rhs.token);
112}
113
Mathias Agopian7e27f052010-05-28 14:22:23 -0700114class Composer : public Singleton<Composer>
115{
Mathias Agopiand4784a32010-05-27 19:41:15 -0700116 friend class Singleton<Composer>;
117
Mathias Agopian698c0872011-06-28 19:09:31 -0700118 mutable Mutex mLock;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700119 SortedVector<ComposerState> mComposerStates;
120 SortedVector<DisplayState > mDisplayStates;
Jamie Gennis28378392011-10-12 17:39:00 -0700121 uint32_t mForceSynchronous;
Jeff Brownf3f7db62012-08-31 02:18:38 -0700122 uint32_t mTransactionNestCount;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700123 bool mAnimation;
Mathias Agopian698c0872011-06-28 19:09:31 -0700124
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700125 Composer() : Singleton<Composer>(),
Jeff Brownf3f7db62012-08-31 02:18:38 -0700126 mForceSynchronous(0), mTransactionNestCount(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700127 mAnimation(false)
Jamie Gennis28378392011-10-12 17:39:00 -0700128 { }
Mathias Agopian698c0872011-06-28 19:09:31 -0700129
Jeff Brownf3f7db62012-08-31 02:18:38 -0700130 void openGlobalTransactionImpl();
Jamie Gennis28378392011-10-12 17:39:00 -0700131 void closeGlobalTransactionImpl(bool synchronous);
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700132 void setAnimationTransactionImpl();
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700133 status_t enableVSyncInjectionsImpl(bool enable);
134 status_t injectVSyncImpl(nsecs_t when);
Mathias Agopian698c0872011-06-28 19:09:31 -0700135
136 layer_state_t* getLayerStateLocked(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800137 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id);
Mathias Agopian698c0872011-06-28 19:09:31 -0700138
Mathias Agopiane57f2922012-08-09 16:29:12 -0700139 DisplayState& getDisplayStateLocked(const sp<IBinder>& token);
140
Mathias Agopiand4784a32010-05-27 19:41:15 -0700141public:
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700142 sp<IBinder> createDisplay(const String8& displayName, bool secure);
Jesse Hall6c913be2013-08-08 12:15:49 -0700143 void destroyDisplay(const sp<IBinder>& display);
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700144 sp<IBinder> getBuiltInDisplay(int32_t id);
Mathias Agopian698c0872011-06-28 19:09:31 -0700145
Mathias Agopianac9fa422013-02-11 16:40:36 -0800146 status_t setPosition(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700147 float x, float y);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800148 status_t setSize(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700149 uint32_t w, uint32_t h);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800150 status_t setLayer(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Robert Carrae060832016-11-28 10:51:00 -0800151 int32_t z);
Robert Carrdb66e622017-04-10 16:55:57 -0700152 status_t setRelativeLayer(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
153 const sp<IBinder>& relativeTo, int32_t z);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800154 status_t setFlags(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700155 uint32_t flags, uint32_t mask);
156 status_t setTransparentRegionHint(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800157 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700158 const Region& transparentRegion);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800159 status_t setAlpha(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700160 float alpha);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800161 status_t setMatrix(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800162 float dsdx, float dtdx, float dtdy, float dsdy);
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700163 status_t setOrientation(int orientation);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800164 status_t setCrop(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700165 const Rect& crop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000166 status_t setFinalCrop(const sp<SurfaceComposerClient>& client,
167 const sp<IBinder>& id, const Rect& crop);
Mathias Agopian87855782012-07-24 21:41:09 -0700168 status_t setLayerStack(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800169 const sp<IBinder>& id, uint32_t layerStack);
Dan Stoza7dde5992015-05-22 09:51:44 -0700170 status_t deferTransactionUntil(const sp<SurfaceComposerClient>& client,
171 const sp<IBinder>& id, const sp<IBinder>& handle,
172 uint64_t frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800173 status_t deferTransactionUntil(const sp<SurfaceComposerClient>& client,
174 const sp<IBinder>& id, const sp<Surface>& barrierSurface,
175 uint64_t frameNumber);
Robert Carr1db73f62016-12-21 12:58:51 -0800176 status_t reparentChildren(const sp<SurfaceComposerClient>& client,
177 const sp<IBinder>& id,
178 const sp<IBinder>& newParentHandle);
chaviwf1961f72017-09-18 16:41:07 -0700179 status_t reparent(const sp<SurfaceComposerClient>& client,
180 const sp<IBinder>& id, const sp<IBinder>& newParentHandle);
Robert Carr9524cb32017-02-13 11:32:32 -0800181 status_t detachChildren(const sp<SurfaceComposerClient>& client,
182 const sp<IBinder>& id);
Robert Carrc3574f72016-03-24 12:19:32 -0700183 status_t setOverrideScalingMode(const sp<SurfaceComposerClient>& client,
184 const sp<IBinder>& id, int32_t overrideScalingMode);
Robert Carr99e27f02016-06-16 15:18:02 -0700185 status_t setGeometryAppliesWithResize(const sp<SurfaceComposerClient>& client,
Robert Carr82364e32016-05-15 11:27:47 -0700186 const sp<IBinder>& id);
Mathias Agopian698c0872011-06-28 19:09:31 -0700187
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700188 status_t setDisplaySurface(const sp<IBinder>& token,
189 sp<IGraphicBufferProducer> bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700190 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700191 void setDisplayProjection(const sp<IBinder>& token,
192 uint32_t orientation,
193 const Rect& layerStackRect,
194 const Rect& displayRect);
Michael Wright1f6078a2014-06-26 16:01:02 -0700195 void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700196
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700197 static void setAnimationTransaction() {
198 Composer::getInstance().setAnimationTransactionImpl();
199 }
200
Jeff Brownf3f7db62012-08-31 02:18:38 -0700201 static void openGlobalTransaction() {
202 Composer::getInstance().openGlobalTransactionImpl();
203 }
204
Jamie Gennis28378392011-10-12 17:39:00 -0700205 static void closeGlobalTransaction(bool synchronous) {
206 Composer::getInstance().closeGlobalTransactionImpl(synchronous);
Mathias Agopiand4784a32010-05-27 19:41:15 -0700207 }
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700208
209 static status_t enableVSyncInjections(bool enable) {
210 return Composer::getInstance().enableVSyncInjectionsImpl(enable);
211 }
212
213 static status_t injectVSync(nsecs_t when) {
214 return Composer::getInstance().injectVSyncImpl(when);
215 }
Mathias Agopiand4784a32010-05-27 19:41:15 -0700216};
217
218ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220// ---------------------------------------------------------------------------
221
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700222sp<IBinder> Composer::createDisplay(const String8& displayName, bool secure) {
223 return ComposerService::getComposerService()->createDisplay(displayName,
224 secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700225}
226
Jesse Hall6c913be2013-08-08 12:15:49 -0700227void Composer::destroyDisplay(const sp<IBinder>& display) {
228 return ComposerService::getComposerService()->destroyDisplay(display);
229}
230
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700231sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
232 return ComposerService::getComposerService()->getBuiltInDisplay(id);
233}
234
Jeff Brownf3f7db62012-08-31 02:18:38 -0700235void Composer::openGlobalTransactionImpl() {
236 { // scope for the lock
237 Mutex::Autolock _l(mLock);
238 mTransactionNestCount += 1;
239 }
240}
241
Jamie Gennis28378392011-10-12 17:39:00 -0700242void Composer::closeGlobalTransactionImpl(bool synchronous) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700243 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopian698c0872011-06-28 19:09:31 -0700244
245 Vector<ComposerState> transaction;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700246 Vector<DisplayState> displayTransaction;
Jamie Gennis28378392011-10-12 17:39:00 -0700247 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -0700248
249 { // scope for the lock
250 Mutex::Autolock _l(mLock);
Jeff Brownf3f7db62012-08-31 02:18:38 -0700251 mForceSynchronous |= synchronous;
252 if (!mTransactionNestCount) {
253 ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
254 "call to openGlobalTransaction().");
255 } else if (--mTransactionNestCount) {
256 return;
257 }
258
Mathias Agopiane57f2922012-08-09 16:29:12 -0700259 transaction = mComposerStates;
260 mComposerStates.clear();
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700261
Mathias Agopiane57f2922012-08-09 16:29:12 -0700262 displayTransaction = mDisplayStates;
263 mDisplayStates.clear();
Jamie Gennis28378392011-10-12 17:39:00 -0700264
Jeff Brownf3f7db62012-08-31 02:18:38 -0700265 if (mForceSynchronous) {
Jamie Gennis28378392011-10-12 17:39:00 -0700266 flags |= ISurfaceComposer::eSynchronous;
267 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700268 if (mAnimation) {
269 flags |= ISurfaceComposer::eAnimation;
270 }
271
Jamie Gennis28378392011-10-12 17:39:00 -0700272 mForceSynchronous = false;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700273 mAnimation = false;
Mathias Agopian698c0872011-06-28 19:09:31 -0700274 }
275
Mathias Agopian8b33f032012-07-24 20:43:54 -0700276 sm->setTransactionState(transaction, displayTransaction, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277}
278
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700279status_t Composer::enableVSyncInjectionsImpl(bool enable) {
280 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
281 return sm->enableVSyncInjections(enable);
282}
283
284status_t Composer::injectVSyncImpl(nsecs_t when) {
285 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
286 return sm->injectVSync(when);
287}
288
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700289void Composer::setAnimationTransactionImpl() {
290 Mutex::Autolock _l(mLock);
291 mAnimation = true;
292}
293
Mathias Agopian698c0872011-06-28 19:09:31 -0700294layer_state_t* Composer::getLayerStateLocked(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800295 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700296
297 ComposerState s;
298 s.client = client->mClient;
299 s.state.surface = id;
300
Mathias Agopiane57f2922012-08-09 16:29:12 -0700301 ssize_t index = mComposerStates.indexOf(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700302 if (index < 0) {
303 // we don't have it, add an initialized layer_state to our list
Mathias Agopiane57f2922012-08-09 16:29:12 -0700304 index = mComposerStates.add(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700305 }
306
Mathias Agopiane57f2922012-08-09 16:29:12 -0700307 ComposerState* const out = mComposerStates.editArray();
Mathias Agopian698c0872011-06-28 19:09:31 -0700308 return &(out[index].state);
309}
310
311status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800312 const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700313 Mutex::Autolock _l(mLock);
314 layer_state_t* s = getLayerStateLocked(client, id);
315 if (!s)
316 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700317 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700318 s->x = x;
319 s->y = y;
320 return NO_ERROR;
321}
322
323status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800324 const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700325 Mutex::Autolock _l(mLock);
326 layer_state_t* s = getLayerStateLocked(client, id);
327 if (!s)
328 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700329 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700330 s->w = w;
331 s->h = h;
Jamie Gennis28378392011-10-12 17:39:00 -0700332
Jorim Jaggi092123c2016-04-13 01:40:35 +0000333 // Resizing a surface makes the transaction synchronous.
334 mForceSynchronous = true;
335
Mathias Agopian698c0872011-06-28 19:09:31 -0700336 return NO_ERROR;
337}
338
339status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
Robert Carrae060832016-11-28 10:51:00 -0800340 const sp<IBinder>& id, int32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700341 Mutex::Autolock _l(mLock);
342 layer_state_t* s = getLayerStateLocked(client, id);
343 if (!s)
344 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700345 s->what |= layer_state_t::eLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700346 s->z = z;
347 return NO_ERROR;
348}
349
Robert Carrdb66e622017-04-10 16:55:57 -0700350status_t Composer::setRelativeLayer(const sp<SurfaceComposerClient>& client,
351 const sp<IBinder>& id, const sp<IBinder>& relativeTo,
352 int32_t z) {
353 Mutex::Autolock _l(mLock);
354 layer_state_t* s = getLayerStateLocked(client, id);
355 if (!s) {
356 return BAD_INDEX;
357 }
358 s->what |= layer_state_t::eRelativeLayerChanged;
359 s->relativeLayerHandle = relativeTo;
360 s->z = z;
361 return NO_ERROR;
362}
363
Mathias Agopian698c0872011-06-28 19:09:31 -0700364status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800365 const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700366 uint32_t mask) {
367 Mutex::Autolock _l(mLock);
368 layer_state_t* s = getLayerStateLocked(client, id);
369 if (!s)
370 return BAD_INDEX;
Pablo Ceballos53390e12015-08-04 11:25:59 -0700371 if ((mask & layer_state_t::eLayerOpaque) ||
372 (mask & layer_state_t::eLayerHidden) ||
373 (mask & layer_state_t::eLayerSecure)) {
Dan Stoza23116082015-06-18 14:58:39 -0700374 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-29 17:17:11 -0800375 }
Mathias Agopian698c0872011-06-28 19:09:31 -0700376 s->flags &= ~mask;
377 s->flags |= (flags & mask);
378 s->mask |= mask;
379 return NO_ERROR;
380}
381
382status_t Composer::setTransparentRegionHint(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800383 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700384 const Region& transparentRegion) {
385 Mutex::Autolock _l(mLock);
386 layer_state_t* s = getLayerStateLocked(client, id);
387 if (!s)
388 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700389 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700390 s->transparentRegion = transparentRegion;
391 return NO_ERROR;
392}
393
394status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800395 const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700396 Mutex::Autolock _l(mLock);
397 layer_state_t* s = getLayerStateLocked(client, id);
398 if (!s)
399 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700400 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700401 s->alpha = alpha;
402 return NO_ERROR;
403}
404
Mathias Agopian87855782012-07-24 21:41:09 -0700405status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800406 const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700407 Mutex::Autolock _l(mLock);
408 layer_state_t* s = getLayerStateLocked(client, id);
409 if (!s)
410 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700411 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700412 s->layerStack = layerStack;
413 return NO_ERROR;
414}
415
Mathias Agopian698c0872011-06-28 19:09:31 -0700416status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800417 const sp<IBinder>& id, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800418 float dtdy, float dsdy) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700419 Mutex::Autolock _l(mLock);
420 layer_state_t* s = getLayerStateLocked(client, id);
421 if (!s)
422 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700423 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700424 layer_state_t::matrix22_t matrix;
425 matrix.dsdx = dsdx;
426 matrix.dtdx = dtdx;
427 matrix.dsdy = dsdy;
428 matrix.dtdy = dtdy;
429 s->matrix = matrix;
430 return NO_ERROR;
431}
432
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700433status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800434 const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700435 Mutex::Autolock _l(mLock);
436 layer_state_t* s = getLayerStateLocked(client, id);
437 if (!s)
438 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700439 s->what |= layer_state_t::eCropChanged;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700440 s->crop = crop;
441 return NO_ERROR;
442}
443
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444status_t Composer::setFinalCrop(const sp<SurfaceComposerClient>& client,
445 const sp<IBinder>& id, const Rect& crop) {
446 Mutex::Autolock _l(mLock);
447 layer_state_t* s = getLayerStateLocked(client, id);
448 if (!s) {
449 return BAD_INDEX;
450 }
451 s->what |= layer_state_t::eFinalCropChanged;
452 s->finalCrop = crop;
453 return NO_ERROR;
454}
455
Dan Stoza7dde5992015-05-22 09:51:44 -0700456status_t Composer::deferTransactionUntil(
457 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
458 const sp<IBinder>& handle, uint64_t frameNumber) {
459 Mutex::Autolock lock(mLock);
460 layer_state_t* s = getLayerStateLocked(client, id);
461 if (!s) {
462 return BAD_INDEX;
463 }
464 s->what |= layer_state_t::eDeferTransaction;
Robert Carr0d480722017-01-10 16:42:54 -0800465 s->barrierHandle = handle;
466 s->frameNumber = frameNumber;
467 return NO_ERROR;
468}
469
470status_t Composer::deferTransactionUntil(
471 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
472 const sp<Surface>& barrierSurface, uint64_t frameNumber) {
473 Mutex::Autolock lock(mLock);
474 layer_state_t* s = getLayerStateLocked(client, id);
475 if (!s) {
476 return BAD_INDEX;
477 }
478 s->what |= layer_state_t::eDeferTransaction;
479 s->barrierGbp = barrierSurface->getIGraphicBufferProducer();
Dan Stoza7dde5992015-05-22 09:51:44 -0700480 s->frameNumber = frameNumber;
481 return NO_ERROR;
482}
483
Robert Carr1db73f62016-12-21 12:58:51 -0800484status_t Composer::reparentChildren(
485 const sp<SurfaceComposerClient>& client,
486 const sp<IBinder>& id,
487 const sp<IBinder>& newParentHandle) {
488 Mutex::Autolock lock(mLock);
489 layer_state_t* s = getLayerStateLocked(client, id);
490 if (!s) {
491 return BAD_INDEX;
492 }
493 s->what |= layer_state_t::eReparentChildren;
494 s->reparentHandle = newParentHandle;
495 return NO_ERROR;
496}
497
chaviwf1961f72017-09-18 16:41:07 -0700498status_t Composer::reparent(const sp<SurfaceComposerClient>& client,
chaviw06178942017-07-27 10:25:59 -0700499 const sp<IBinder>& id,
chaviwf1961f72017-09-18 16:41:07 -0700500 const sp<IBinder>& newParentHandle) {
chaviw06178942017-07-27 10:25:59 -0700501 Mutex::Autolock lock(mLock);
502 layer_state_t* s = getLayerStateLocked(client, id);
503 if (!s) {
504 return BAD_INDEX;
505 }
chaviwf1961f72017-09-18 16:41:07 -0700506 s->what |= layer_state_t::eReparent;
chaviw06178942017-07-27 10:25:59 -0700507 s->parentHandleForChild = newParentHandle;
chaviw06178942017-07-27 10:25:59 -0700508 return NO_ERROR;
509}
510
Robert Carr9524cb32017-02-13 11:32:32 -0800511status_t Composer::detachChildren(
512 const sp<SurfaceComposerClient>& client,
513 const sp<IBinder>& id) {
514 Mutex::Autolock lock(mLock);
515 layer_state_t* s = getLayerStateLocked(client, id);
516 if (!s) {
517 return BAD_INDEX;
518 }
519 s->what |= layer_state_t::eDetachChildren;
520 return NO_ERROR;
521}
522
Robert Carrc3574f72016-03-24 12:19:32 -0700523status_t Composer::setOverrideScalingMode(
524 const sp<SurfaceComposerClient>& client,
525 const sp<IBinder>& id, int32_t overrideScalingMode) {
526 Mutex::Autolock lock(mLock);
527 layer_state_t* s = getLayerStateLocked(client, id);
528 if (!s) {
529 return BAD_INDEX;
530 }
531
532 switch (overrideScalingMode) {
533 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
534 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
535 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
536 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
537 case -1:
538 break;
539 default:
540 ALOGE("unknown scaling mode: %d",
541 overrideScalingMode);
542 return BAD_VALUE;
543 }
544
545 s->what |= layer_state_t::eOverrideScalingModeChanged;
546 s->overrideScalingMode = overrideScalingMode;
547 return NO_ERROR;
548}
549
Robert Carr99e27f02016-06-16 15:18:02 -0700550status_t Composer::setGeometryAppliesWithResize(
Robert Carr82364e32016-05-15 11:27:47 -0700551 const sp<SurfaceComposerClient>& client,
552 const sp<IBinder>& id) {
553 Mutex::Autolock lock(mLock);
554 layer_state_t* s = getLayerStateLocked(client, id);
555 if (!s) {
556 return BAD_INDEX;
557 }
Robert Carr99e27f02016-06-16 15:18:02 -0700558 s->what |= layer_state_t::eGeometryAppliesWithResize;
Robert Carr82364e32016-05-15 11:27:47 -0700559 return NO_ERROR;
560}
561
Mathias Agopian698c0872011-06-28 19:09:31 -0700562// ---------------------------------------------------------------------------
563
Mathias Agopiane57f2922012-08-09 16:29:12 -0700564DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
565 DisplayState s;
566 s.token = token;
567 ssize_t index = mDisplayStates.indexOf(s);
568 if (index < 0) {
569 // we don't have it, add an initialized layer_state to our list
570 s.what = 0;
571 index = mDisplayStates.add(s);
572 }
Dan Stozad723bd72014-11-18 10:24:03 -0800573 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700574}
575
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700576status_t Composer::setDisplaySurface(const sp<IBinder>& token,
577 sp<IGraphicBufferProducer> bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -0700578 if (bufferProducer.get() != nullptr) {
579 // Make sure that composition can never be stalled by a virtual display
580 // consumer that isn't processing buffers fast enough.
581 status_t err = bufferProducer->setAsyncMode(true);
582 if (err != NO_ERROR) {
583 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
584 "BufferQueue. This BufferQueue cannot be used for virtual "
585 "display. (%d)", err);
586 return err;
587 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700588 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700589 Mutex::Autolock _l(mLock);
590 DisplayState& s(getDisplayStateLocked(token));
Andy McFadden2adaf042012-12-18 09:49:45 -0800591 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700592 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700593 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700594}
595
596void Composer::setDisplayLayerStack(const sp<IBinder>& token,
597 uint32_t layerStack) {
598 Mutex::Autolock _l(mLock);
599 DisplayState& s(getDisplayStateLocked(token));
600 s.layerStack = layerStack;
601 s.what |= DisplayState::eLayerStackChanged;
602}
603
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700604void Composer::setDisplayProjection(const sp<IBinder>& token,
605 uint32_t orientation,
606 const Rect& layerStackRect,
607 const Rect& displayRect) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700608 Mutex::Autolock _l(mLock);
609 DisplayState& s(getDisplayStateLocked(token));
610 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700611 s.viewport = layerStackRect;
612 s.frame = displayRect;
613 s.what |= DisplayState::eDisplayProjectionChanged;
Jorim Jaggi092123c2016-04-13 01:40:35 +0000614 mForceSynchronous = true; // TODO: do we actually still need this?
Mathias Agopiane57f2922012-08-09 16:29:12 -0700615}
616
Michael Wright1f6078a2014-06-26 16:01:02 -0700617void Composer::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
618 Mutex::Autolock _l(mLock);
619 DisplayState& s(getDisplayStateLocked(token));
620 s.width = width;
621 s.height = height;
622 s.what |= DisplayState::eDisplaySizeChanged;
623}
624
Mathias Agopiane57f2922012-08-09 16:29:12 -0700625// ---------------------------------------------------------------------------
626
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627SurfaceComposerClient::SurfaceComposerClient()
Mathias Agopian698c0872011-06-28 19:09:31 -0700628 : mStatus(NO_INIT), mComposer(Composer::getInstance())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630}
631
Robert Carr1db73f62016-12-21 12:58:51 -0800632SurfaceComposerClient::SurfaceComposerClient(const sp<IGraphicBufferProducer>& root)
633 : mStatus(NO_INIT), mComposer(Composer::getInstance()), mParent(root)
634{
635}
636
Mathias Agopian698c0872011-06-28 19:09:31 -0700637void SurfaceComposerClient::onFirstRef() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700638 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopiand4784a32010-05-27 19:41:15 -0700639 if (sm != 0) {
Robert Carr1db73f62016-12-21 12:58:51 -0800640 auto rootProducer = mParent.promote();
641 sp<ISurfaceComposerClient> conn;
642 conn = (rootProducer != nullptr) ? sm->createScopedConnection(rootProducer) :
643 sm->createConnection();
Mathias Agopiand4784a32010-05-27 19:41:15 -0700644 if (conn != 0) {
645 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700646 mStatus = NO_ERROR;
647 }
648 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649}
650
Mathias Agopian698c0872011-06-28 19:09:31 -0700651SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -0700652 dispose();
653}
Mathias Agopiandd3423c2009-09-23 15:44:05 -0700654
Mathias Agopian698c0872011-06-28 19:09:31 -0700655status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656 return mStatus;
657}
658
Mathias Agopian698c0872011-06-28 19:09:31 -0700659sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800660 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661}
662
Mathias Agopiand4784a32010-05-27 19:41:15 -0700663status_t SurfaceComposerClient::linkToComposerDeath(
664 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -0700665 void* cookie, uint32_t flags) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700666 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800667 return IInterface::asBinder(sm)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668}
669
Mathias Agopian698c0872011-06-28 19:09:31 -0700670void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -0700672 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700673 Mutex::Autolock _lm(mLock);
674 if (mClient != 0) {
Mathias Agopiand4784a32010-05-27 19:41:15 -0700675 client = mClient; // hold ref while lock is held
676 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677 }
Mathias Agopiand4784a32010-05-27 19:41:15 -0700678 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679}
680
Mathias Agopian698c0872011-06-28 19:09:31 -0700681sp<SurfaceControl> SurfaceComposerClient::createSurface(
Mathias Agopian698c0872011-06-28 19:09:31 -0700682 const String8& name,
Mathias Agopian698c0872011-06-28 19:09:31 -0700683 uint32_t w,
684 uint32_t h,
685 PixelFormat format,
Robert Carr1f0a16a2016-10-24 16:27:39 -0700686 uint32_t flags,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500687 SurfaceControl* parent,
688 uint32_t windowType,
689 uint32_t ownerUid)
Mathias Agopian698c0872011-06-28 19:09:31 -0700690{
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700691 sp<SurfaceControl> sur;
Mathias Agopian698c0872011-06-28 19:09:31 -0700692 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700693 sp<IBinder> handle;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700694 sp<IBinder> parentHandle;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700695 sp<IGraphicBufferProducer> gbp;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700696
697 if (parent != nullptr) {
698 parentHandle = parent->getHandle();
699 }
700 status_t err = mClient->createSurface(name, w, h, format, flags, parentHandle,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500701 windowType, ownerUid, &handle, &gbp);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700702 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
703 if (err == NO_ERROR) {
704 sur = new SurfaceControl(this, handle, gbp);
Mathias Agopian698c0872011-06-28 19:09:31 -0700705 }
706 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700707 return sur;
Mathias Agopian698c0872011-06-28 19:09:31 -0700708}
709
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700710sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
711 bool secure) {
712 return Composer::getInstance().createDisplay(displayName, secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700713}
714
Jesse Hall6c913be2013-08-08 12:15:49 -0700715void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
716 Composer::getInstance().destroyDisplay(display);
717}
718
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700719sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
720 return Composer::getInstance().getBuiltInDisplay(id);
721}
722
Mathias Agopianac9fa422013-02-11 16:40:36 -0800723status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700724 if (mStatus != NO_ERROR)
725 return mStatus;
726 status_t err = mClient->destroySurface(sid);
727 return err;
728}
729
Svetoslavd85084b2014-03-20 10:28:31 -0700730status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
731 if (mStatus != NO_ERROR) {
732 return mStatus;
733 }
734 return mClient->clearLayerFrameStats(token);
735}
736
737status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
738 FrameStats* outStats) const {
739 if (mStatus != NO_ERROR) {
740 return mStatus;
741 }
742 return mClient->getLayerFrameStats(token, outStats);
743}
744
Mathias Agopian698c0872011-06-28 19:09:31 -0700745inline Composer& SurfaceComposerClient::getComposer() {
746 return mComposer;
747}
748
749// ----------------------------------------------------------------------------
750
751void SurfaceComposerClient::openGlobalTransaction() {
Jeff Brownf3f7db62012-08-31 02:18:38 -0700752 Composer::openGlobalTransaction();
Mathias Agopian698c0872011-06-28 19:09:31 -0700753}
754
Jamie Gennis28378392011-10-12 17:39:00 -0700755void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
756 Composer::closeGlobalTransaction(synchronous);
Mathias Agopian698c0872011-06-28 19:09:31 -0700757}
758
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700759void SurfaceComposerClient::setAnimationTransaction() {
760 Composer::setAnimationTransaction();
761}
762
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700763status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
764 return Composer::enableVSyncInjections(enable);
765}
766
767status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
768 return Composer::injectVSync(when);
769}
770
Mathias Agopian698c0872011-06-28 19:09:31 -0700771// ----------------------------------------------------------------------------
772
Mathias Agopianac9fa422013-02-11 16:40:36 -0800773status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700774 return getComposer().setCrop(this, id, crop);
775}
776
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000777status_t SurfaceComposerClient::setFinalCrop(const sp<IBinder>& id,
778 const Rect& crop) {
779 return getComposer().setFinalCrop(this, id, crop);
780}
781
Mathias Agopianac9fa422013-02-11 16:40:36 -0800782status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700783 return getComposer().setPosition(this, id, x, y);
784}
785
Mathias Agopianac9fa422013-02-11 16:40:36 -0800786status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700787 return getComposer().setSize(this, id, w, h);
788}
789
Robert Carrae060832016-11-28 10:51:00 -0800790status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, int32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700791 return getComposer().setLayer(this, id, z);
792}
793
Robert Carrdb66e622017-04-10 16:55:57 -0700794status_t SurfaceComposerClient::setRelativeLayer(const sp<IBinder>& id,
795 const sp<IBinder>& relativeTo, int32_t z) {
796 return getComposer().setRelativeLayer(this, id, relativeTo, z);
797}
798
Mathias Agopianac9fa422013-02-11 16:40:36 -0800799status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700800 return getComposer().setFlags(this, id,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700801 layer_state_t::eLayerHidden,
802 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700803}
804
Mathias Agopianac9fa422013-02-11 16:40:36 -0800805status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700806 return getComposer().setFlags(this, id,
807 0,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700808 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700809}
810
Mathias Agopianac9fa422013-02-11 16:40:36 -0800811status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700812 uint32_t mask) {
813 return getComposer().setFlags(this, id, flags, mask);
814}
815
Mathias Agopianac9fa422013-02-11 16:40:36 -0800816status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700817 const Region& transparentRegion) {
818 return getComposer().setTransparentRegionHint(this, id, transparentRegion);
819}
820
Mathias Agopianac9fa422013-02-11 16:40:36 -0800821status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700822 return getComposer().setAlpha(this, id, alpha);
823}
824
Mathias Agopianac9fa422013-02-11 16:40:36 -0800825status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700826 return getComposer().setLayerStack(this, id, layerStack);
827}
828
Mathias Agopianac9fa422013-02-11 16:40:36 -0800829status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800830 float dtdy, float dsdy) {
831 return getComposer().setMatrix(this, id, dsdx, dtdx, dtdy, dsdy);
Mathias Agopian698c0872011-06-28 19:09:31 -0700832}
833
Dan Stoza7dde5992015-05-22 09:51:44 -0700834status_t SurfaceComposerClient::deferTransactionUntil(const sp<IBinder>& id,
835 const sp<IBinder>& handle, uint64_t frameNumber) {
836 return getComposer().deferTransactionUntil(this, id, handle, frameNumber);
837}
838
Robert Carr0d480722017-01-10 16:42:54 -0800839status_t SurfaceComposerClient::deferTransactionUntil(const sp<IBinder>& id,
840 const sp<Surface>& barrierSurface, uint64_t frameNumber) {
841 return getComposer().deferTransactionUntil(this, id, barrierSurface, frameNumber);
842}
843
Robert Carr1db73f62016-12-21 12:58:51 -0800844status_t SurfaceComposerClient::reparentChildren(const sp<IBinder>& id,
845 const sp<IBinder>& newParentHandle) {
846 return getComposer().reparentChildren(this, id, newParentHandle);
847}
848
chaviwf1961f72017-09-18 16:41:07 -0700849status_t SurfaceComposerClient::reparent(const sp<IBinder>& id,
850 const sp<IBinder>& newParentHandle) {
851 return getComposer().reparent(this, id, newParentHandle);
chaviw06178942017-07-27 10:25:59 -0700852}
853
Robert Carr9524cb32017-02-13 11:32:32 -0800854status_t SurfaceComposerClient::detachChildren(const sp<IBinder>& id) {
855 return getComposer().detachChildren(this, id);
856}
857
Robert Carrc3574f72016-03-24 12:19:32 -0700858status_t SurfaceComposerClient::setOverrideScalingMode(
859 const sp<IBinder>& id, int32_t overrideScalingMode) {
860 return getComposer().setOverrideScalingMode(
861 this, id, overrideScalingMode);
862}
863
Robert Carr99e27f02016-06-16 15:18:02 -0700864status_t SurfaceComposerClient::setGeometryAppliesWithResize(
Robert Carr82364e32016-05-15 11:27:47 -0700865 const sp<IBinder>& id) {
Robert Carr99e27f02016-06-16 15:18:02 -0700866 return getComposer().setGeometryAppliesWithResize(this, id);
Robert Carr82364e32016-05-15 11:27:47 -0700867}
868
Mathias Agopian698c0872011-06-28 19:09:31 -0700869// ----------------------------------------------------------------------------
870
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700871status_t SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
872 sp<IGraphicBufferProducer> bufferProducer) {
873 return Composer::getInstance().setDisplaySurface(token, bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700874}
875
876void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
877 uint32_t layerStack) {
878 Composer::getInstance().setDisplayLayerStack(token, layerStack);
879}
880
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700881void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
882 uint32_t orientation,
883 const Rect& layerStackRect,
884 const Rect& displayRect) {
885 Composer::getInstance().setDisplayProjection(token, orientation,
886 layerStackRect, displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700887}
888
Michael Wright1f6078a2014-06-26 16:01:02 -0700889void SurfaceComposerClient::setDisplaySize(const sp<IBinder>& token,
890 uint32_t width, uint32_t height) {
891 Composer::getInstance().setDisplaySize(token, width, height);
892}
893
Mathias Agopiane57f2922012-08-09 16:29:12 -0700894// ----------------------------------------------------------------------------
895
Dan Stoza7f7da322014-05-02 15:26:25 -0700896status_t SurfaceComposerClient::getDisplayConfigs(
897 const sp<IBinder>& display, Vector<DisplayInfo>* configs)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898{
Dan Stoza7f7da322014-05-02 15:26:25 -0700899 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
900}
901
902status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
903 DisplayInfo* info) {
904 Vector<DisplayInfo> configs;
905 status_t result = getDisplayConfigs(display, &configs);
906 if (result != NO_ERROR) {
907 return result;
908 }
909
910 int activeId = getActiveConfig(display);
911 if (activeId < 0) {
912 ALOGE("No active configuration found");
913 return NAME_NOT_FOUND;
914 }
915
Dan Stozad723bd72014-11-18 10:24:03 -0800916 *info = configs[static_cast<size_t>(activeId)];
Dan Stoza7f7da322014-05-02 15:26:25 -0700917 return NO_ERROR;
918}
919
920int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
921 return ComposerService::getComposerService()->getActiveConfig(display);
922}
923
924status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
925 return ComposerService::getComposerService()->setActiveConfig(display, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926}
927
Michael Wright28f24d02016-07-12 13:30:53 -0700928status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
929 Vector<android_color_mode_t>* outColorModes) {
930 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
931}
932
933android_color_mode_t SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
934 return ComposerService::getComposerService()->getActiveColorMode(display);
935}
936
937status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
938 android_color_mode_t colorMode) {
939 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
940}
941
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700942void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
943 int mode) {
944 ComposerService::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -0700945}
946
Svetoslavd85084b2014-03-20 10:28:31 -0700947status_t SurfaceComposerClient::clearAnimationFrameStats() {
948 return ComposerService::getComposerService()->clearAnimationFrameStats();
949}
950
951status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
952 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
953}
954
Dan Stozac4f471e2016-03-24 09:31:08 -0700955status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
956 HdrCapabilities* outCapabilities) {
957 return ComposerService::getComposerService()->getHdrCapabilities(display,
958 outCapabilities);
959}
960
Mathias Agopian698c0872011-06-28 19:09:31 -0700961// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800963status_t ScreenshotClient::capture(
964 const sp<IBinder>& display,
965 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -0700966 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -0800967 int32_t minLayerZ, int32_t maxLayerZ, bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800968 sp<ISurfaceComposer> s(ComposerService::getComposerService());
969 if (s == NULL) return NO_INIT;
Dan Stozac1879002014-05-22 15:59:05 -0700970 return s->captureScreen(display, producer, sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -0800971 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800972}
973
Robert Carr673134e2017-01-09 19:48:38 -0800974status_t ScreenshotClient::captureToBuffer(const sp<IBinder>& display,
975 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -0800976 int32_t minLayerZ, int32_t maxLayerZ, bool useIdentityTransform,
Robert Carr673134e2017-01-09 19:48:38 -0800977 uint32_t rotation,
978 sp<GraphicBuffer>* outBuffer) {
979 sp<ISurfaceComposer> s(ComposerService::getComposerService());
980 if (s == NULL) return NO_INIT;
981
982 sp<IGraphicBufferConsumer> gbpConsumer;
983 sp<IGraphicBufferProducer> producer;
984 BufferQueue::createBufferQueue(&producer, &gbpConsumer);
985 sp<BufferItemConsumer> consumer(new BufferItemConsumer(gbpConsumer,
986 GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_NEVER,
987 1, true));
988
989 status_t ret = s->captureScreen(display, producer, sourceCrop, reqWidth, reqHeight,
990 minLayerZ, maxLayerZ, useIdentityTransform,
991 static_cast<ISurfaceComposer::Rotation>(rotation));
992 if (ret != NO_ERROR) {
993 return ret;
994 }
995 BufferItem b;
996 consumer->acquireBuffer(&b, 0, true);
997 *outBuffer = b.mGraphicBuffer;
998 return ret;
999}
1000
Mathias Agopian74c40c02010-09-29 13:02:36 -07001001ScreenshotClient::ScreenshotClient()
Mathias Agopianabe815d2013-03-19 22:22:21 -07001002 : mHaveBuffer(false) {
1003 memset(&mBuffer, 0, sizeof(mBuffer));
Mathias Agopian74c40c02010-09-29 13:02:36 -07001004}
1005
Mathias Agopian8000d062013-03-26 18:15:35 -07001006ScreenshotClient::~ScreenshotClient() {
1007 ScreenshotClient::release();
1008}
1009
Mathias Agopianabe815d2013-03-19 22:22:21 -07001010sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
1011 if (mCpuConsumer == NULL) {
Dan Stoza6d5a7bb2014-03-13 11:39:09 -07001012 sp<IGraphicBufferConsumer> consumer;
1013 BufferQueue::createBufferQueue(&mProducer, &consumer);
1014 mCpuConsumer = new CpuConsumer(consumer, 1);
Mathias Agopianabe815d2013-03-19 22:22:21 -07001015 mCpuConsumer->setName(String8("ScreenshotClient"));
1016 }
1017 return mCpuConsumer;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08001018}
1019
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001020status_t ScreenshotClient::update(const sp<IBinder>& display,
Dan Stozac1879002014-05-22 15:59:05 -07001021 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -08001022 int32_t minLayerZ, int32_t maxLayerZ,
Riley Andrewsd15ef272014-09-04 16:19:44 -07001023 bool useIdentityTransform, uint32_t rotation) {
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08001024 sp<ISurfaceComposer> s(ComposerService::getComposerService());
1025 if (s == NULL) return NO_INIT;
Mathias Agopianabe815d2013-03-19 22:22:21 -07001026 sp<CpuConsumer> cpuConsumer = getCpuConsumer();
1027
1028 if (mHaveBuffer) {
1029 mCpuConsumer->unlockBuffer(mBuffer);
1030 memset(&mBuffer, 0, sizeof(mBuffer));
1031 mHaveBuffer = false;
1032 }
1033
Dan Stozac1879002014-05-22 15:59:05 -07001034 status_t err = s->captureScreen(display, mProducer, sourceCrop,
Riley Andrewsd15ef272014-09-04 16:19:44 -07001035 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform,
1036 static_cast<ISurfaceComposer::Rotation>(rotation));
Mathias Agopianabe815d2013-03-19 22:22:21 -07001037
1038 if (err == NO_ERROR) {
1039 err = mCpuConsumer->lockNextBuffer(&mBuffer);
1040 if (err == NO_ERROR) {
1041 mHaveBuffer = true;
1042 }
1043 }
1044 return err;
1045}
1046
Riley Andrewsd15ef272014-09-04 16:19:44 -07001047status_t ScreenshotClient::update(const sp<IBinder>& display,
1048 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -08001049 int32_t minLayerZ, int32_t maxLayerZ,
Riley Andrewsd15ef272014-09-04 16:19:44 -07001050 bool useIdentityTransform) {
1051
1052 return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
1053 minLayerZ, maxLayerZ, useIdentityTransform, ISurfaceComposer::eRotateNone);
1054}
1055
Dan Stozac1879002014-05-22 15:59:05 -07001056status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -08001057 bool useIdentityTransform) {
Robert Carrae060832016-11-28 10:51:00 -08001058 return ScreenshotClient::update(display, sourceCrop, 0, 0,
1059 INT32_MIN, INT32_MAX,
Riley Andrewsd15ef272014-09-04 16:19:44 -07001060 useIdentityTransform, ISurfaceComposer::eRotateNone);
Mathias Agopianabe815d2013-03-19 22:22:21 -07001061}
1062
Dan Stozac1879002014-05-22 15:59:05 -07001063status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -08001064 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform) {
Dan Stozac1879002014-05-22 15:59:05 -07001065 return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
Robert Carrae060832016-11-28 10:51:00 -08001066 INT32_MIN, INT32_MAX,
1067 useIdentityTransform, ISurfaceComposer::eRotateNone);
Mathias Agopian74c40c02010-09-29 13:02:36 -07001068}
1069
1070void ScreenshotClient::release() {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001071 if (mHaveBuffer) {
1072 mCpuConsumer->unlockBuffer(mBuffer);
1073 memset(&mBuffer, 0, sizeof(mBuffer));
1074 mHaveBuffer = false;
1075 }
1076 mCpuConsumer.clear();
Mathias Agopian74c40c02010-09-29 13:02:36 -07001077}
1078
1079void const* ScreenshotClient::getPixels() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001080 return mBuffer.data;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001081}
1082
1083uint32_t ScreenshotClient::getWidth() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001084 return mBuffer.width;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001085}
1086
1087uint32_t ScreenshotClient::getHeight() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001088 return mBuffer.height;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001089}
1090
1091PixelFormat ScreenshotClient::getFormat() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001092 return mBuffer.format;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001093}
1094
1095uint32_t ScreenshotClient::getStride() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001096 return mBuffer.stride;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001097}
1098
1099size_t ScreenshotClient::getSize() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001100 return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
Mathias Agopian74c40c02010-09-29 13:02:36 -07001101}
1102
Romain Guy88d37dd2017-05-26 17:57:05 -07001103android_dataspace ScreenshotClient::getDataSpace() const {
1104 return mBuffer.dataSpace;
1105}
1106
Mathias Agopian74c40c02010-09-29 13:02:36 -07001107// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108}; // namespace android