blob: b0ae7e0bdf10c7f5d4b8c1c515f9122ee7c25a95 [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);
chaviw06178942017-07-27 10:25:59 -0700179 status_t reparentChild(const sp<SurfaceComposerClient>& client,
180 const sp<IBinder>& id, const sp<IBinder>& newParentHandle,
181 const sp<IBinder>& childHandle);
Robert Carr9524cb32017-02-13 11:32:32 -0800182 status_t detachChildren(const sp<SurfaceComposerClient>& client,
183 const sp<IBinder>& id);
Robert Carrc3574f72016-03-24 12:19:32 -0700184 status_t setOverrideScalingMode(const sp<SurfaceComposerClient>& client,
185 const sp<IBinder>& id, int32_t overrideScalingMode);
Robert Carr99e27f02016-06-16 15:18:02 -0700186 status_t setGeometryAppliesWithResize(const sp<SurfaceComposerClient>& client,
Robert Carr82364e32016-05-15 11:27:47 -0700187 const sp<IBinder>& id);
Mathias Agopian698c0872011-06-28 19:09:31 -0700188
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700189 status_t setDisplaySurface(const sp<IBinder>& token,
190 sp<IGraphicBufferProducer> bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700191 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700192 void setDisplayProjection(const sp<IBinder>& token,
193 uint32_t orientation,
194 const Rect& layerStackRect,
195 const Rect& displayRect);
Michael Wright1f6078a2014-06-26 16:01:02 -0700196 void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700197
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700198 static void setAnimationTransaction() {
199 Composer::getInstance().setAnimationTransactionImpl();
200 }
201
Jeff Brownf3f7db62012-08-31 02:18:38 -0700202 static void openGlobalTransaction() {
203 Composer::getInstance().openGlobalTransactionImpl();
204 }
205
Jamie Gennis28378392011-10-12 17:39:00 -0700206 static void closeGlobalTransaction(bool synchronous) {
207 Composer::getInstance().closeGlobalTransactionImpl(synchronous);
Mathias Agopiand4784a32010-05-27 19:41:15 -0700208 }
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700209
210 static status_t enableVSyncInjections(bool enable) {
211 return Composer::getInstance().enableVSyncInjectionsImpl(enable);
212 }
213
214 static status_t injectVSync(nsecs_t when) {
215 return Composer::getInstance().injectVSyncImpl(when);
216 }
Mathias Agopiand4784a32010-05-27 19:41:15 -0700217};
218
219ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221// ---------------------------------------------------------------------------
222
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700223sp<IBinder> Composer::createDisplay(const String8& displayName, bool secure) {
224 return ComposerService::getComposerService()->createDisplay(displayName,
225 secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700226}
227
Jesse Hall6c913be2013-08-08 12:15:49 -0700228void Composer::destroyDisplay(const sp<IBinder>& display) {
229 return ComposerService::getComposerService()->destroyDisplay(display);
230}
231
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700232sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
233 return ComposerService::getComposerService()->getBuiltInDisplay(id);
234}
235
Jeff Brownf3f7db62012-08-31 02:18:38 -0700236void Composer::openGlobalTransactionImpl() {
237 { // scope for the lock
238 Mutex::Autolock _l(mLock);
239 mTransactionNestCount += 1;
240 }
241}
242
Jamie Gennis28378392011-10-12 17:39:00 -0700243void Composer::closeGlobalTransactionImpl(bool synchronous) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700244 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopian698c0872011-06-28 19:09:31 -0700245
246 Vector<ComposerState> transaction;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700247 Vector<DisplayState> displayTransaction;
Jamie Gennis28378392011-10-12 17:39:00 -0700248 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -0700249
250 { // scope for the lock
251 Mutex::Autolock _l(mLock);
Jeff Brownf3f7db62012-08-31 02:18:38 -0700252 mForceSynchronous |= synchronous;
253 if (!mTransactionNestCount) {
254 ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
255 "call to openGlobalTransaction().");
256 } else if (--mTransactionNestCount) {
257 return;
258 }
259
Mathias Agopiane57f2922012-08-09 16:29:12 -0700260 transaction = mComposerStates;
261 mComposerStates.clear();
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700262
Mathias Agopiane57f2922012-08-09 16:29:12 -0700263 displayTransaction = mDisplayStates;
264 mDisplayStates.clear();
Jamie Gennis28378392011-10-12 17:39:00 -0700265
Jeff Brownf3f7db62012-08-31 02:18:38 -0700266 if (mForceSynchronous) {
Jamie Gennis28378392011-10-12 17:39:00 -0700267 flags |= ISurfaceComposer::eSynchronous;
268 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700269 if (mAnimation) {
270 flags |= ISurfaceComposer::eAnimation;
271 }
272
Jamie Gennis28378392011-10-12 17:39:00 -0700273 mForceSynchronous = false;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700274 mAnimation = false;
Mathias Agopian698c0872011-06-28 19:09:31 -0700275 }
276
Mathias Agopian8b33f032012-07-24 20:43:54 -0700277 sm->setTransactionState(transaction, displayTransaction, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278}
279
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700280status_t Composer::enableVSyncInjectionsImpl(bool enable) {
281 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
282 return sm->enableVSyncInjections(enable);
283}
284
285status_t Composer::injectVSyncImpl(nsecs_t when) {
286 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
287 return sm->injectVSync(when);
288}
289
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700290void Composer::setAnimationTransactionImpl() {
291 Mutex::Autolock _l(mLock);
292 mAnimation = true;
293}
294
Mathias Agopian698c0872011-06-28 19:09:31 -0700295layer_state_t* Composer::getLayerStateLocked(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800296 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700297
298 ComposerState s;
299 s.client = client->mClient;
300 s.state.surface = id;
301
Mathias Agopiane57f2922012-08-09 16:29:12 -0700302 ssize_t index = mComposerStates.indexOf(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700303 if (index < 0) {
304 // we don't have it, add an initialized layer_state to our list
Mathias Agopiane57f2922012-08-09 16:29:12 -0700305 index = mComposerStates.add(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700306 }
307
Mathias Agopiane57f2922012-08-09 16:29:12 -0700308 ComposerState* const out = mComposerStates.editArray();
Mathias Agopian698c0872011-06-28 19:09:31 -0700309 return &(out[index].state);
310}
311
312status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800313 const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700314 Mutex::Autolock _l(mLock);
315 layer_state_t* s = getLayerStateLocked(client, id);
316 if (!s)
317 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700318 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700319 s->x = x;
320 s->y = y;
321 return NO_ERROR;
322}
323
324status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800325 const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700326 Mutex::Autolock _l(mLock);
327 layer_state_t* s = getLayerStateLocked(client, id);
328 if (!s)
329 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700330 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700331 s->w = w;
332 s->h = h;
Jamie Gennis28378392011-10-12 17:39:00 -0700333
Jorim Jaggi092123c2016-04-13 01:40:35 +0000334 // Resizing a surface makes the transaction synchronous.
335 mForceSynchronous = true;
336
Mathias Agopian698c0872011-06-28 19:09:31 -0700337 return NO_ERROR;
338}
339
340status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
Robert Carrae060832016-11-28 10:51:00 -0800341 const sp<IBinder>& id, int32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700342 Mutex::Autolock _l(mLock);
343 layer_state_t* s = getLayerStateLocked(client, id);
344 if (!s)
345 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700346 s->what |= layer_state_t::eLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700347 s->z = z;
348 return NO_ERROR;
349}
350
Robert Carrdb66e622017-04-10 16:55:57 -0700351status_t Composer::setRelativeLayer(const sp<SurfaceComposerClient>& client,
352 const sp<IBinder>& id, const sp<IBinder>& relativeTo,
353 int32_t z) {
354 Mutex::Autolock _l(mLock);
355 layer_state_t* s = getLayerStateLocked(client, id);
356 if (!s) {
357 return BAD_INDEX;
358 }
359 s->what |= layer_state_t::eRelativeLayerChanged;
360 s->relativeLayerHandle = relativeTo;
361 s->z = z;
362 return NO_ERROR;
363}
364
Mathias Agopian698c0872011-06-28 19:09:31 -0700365status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800366 const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700367 uint32_t mask) {
368 Mutex::Autolock _l(mLock);
369 layer_state_t* s = getLayerStateLocked(client, id);
370 if (!s)
371 return BAD_INDEX;
Pablo Ceballos53390e12015-08-04 11:25:59 -0700372 if ((mask & layer_state_t::eLayerOpaque) ||
373 (mask & layer_state_t::eLayerHidden) ||
374 (mask & layer_state_t::eLayerSecure)) {
Dan Stoza23116082015-06-18 14:58:39 -0700375 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-29 17:17:11 -0800376 }
Mathias Agopian698c0872011-06-28 19:09:31 -0700377 s->flags &= ~mask;
378 s->flags |= (flags & mask);
379 s->mask |= mask;
380 return NO_ERROR;
381}
382
383status_t Composer::setTransparentRegionHint(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800384 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700385 const Region& transparentRegion) {
386 Mutex::Autolock _l(mLock);
387 layer_state_t* s = getLayerStateLocked(client, id);
388 if (!s)
389 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700390 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700391 s->transparentRegion = transparentRegion;
392 return NO_ERROR;
393}
394
395status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800396 const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700397 Mutex::Autolock _l(mLock);
398 layer_state_t* s = getLayerStateLocked(client, id);
399 if (!s)
400 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700401 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700402 s->alpha = alpha;
403 return NO_ERROR;
404}
405
Mathias Agopian87855782012-07-24 21:41:09 -0700406status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800407 const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700408 Mutex::Autolock _l(mLock);
409 layer_state_t* s = getLayerStateLocked(client, id);
410 if (!s)
411 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700412 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700413 s->layerStack = layerStack;
414 return NO_ERROR;
415}
416
Mathias Agopian698c0872011-06-28 19:09:31 -0700417status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800418 const sp<IBinder>& id, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800419 float dtdy, float dsdy) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700420 Mutex::Autolock _l(mLock);
421 layer_state_t* s = getLayerStateLocked(client, id);
422 if (!s)
423 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700424 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700425 layer_state_t::matrix22_t matrix;
426 matrix.dsdx = dsdx;
427 matrix.dtdx = dtdx;
428 matrix.dsdy = dsdy;
429 matrix.dtdy = dtdy;
430 s->matrix = matrix;
431 return NO_ERROR;
432}
433
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700434status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800435 const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700436 Mutex::Autolock _l(mLock);
437 layer_state_t* s = getLayerStateLocked(client, id);
438 if (!s)
439 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700440 s->what |= layer_state_t::eCropChanged;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700441 s->crop = crop;
442 return NO_ERROR;
443}
444
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000445status_t Composer::setFinalCrop(const sp<SurfaceComposerClient>& client,
446 const sp<IBinder>& id, const Rect& crop) {
447 Mutex::Autolock _l(mLock);
448 layer_state_t* s = getLayerStateLocked(client, id);
449 if (!s) {
450 return BAD_INDEX;
451 }
452 s->what |= layer_state_t::eFinalCropChanged;
453 s->finalCrop = crop;
454 return NO_ERROR;
455}
456
Dan Stoza7dde5992015-05-22 09:51:44 -0700457status_t Composer::deferTransactionUntil(
458 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
459 const sp<IBinder>& handle, uint64_t frameNumber) {
460 Mutex::Autolock lock(mLock);
461 layer_state_t* s = getLayerStateLocked(client, id);
462 if (!s) {
463 return BAD_INDEX;
464 }
465 s->what |= layer_state_t::eDeferTransaction;
Robert Carr0d480722017-01-10 16:42:54 -0800466 s->barrierHandle = handle;
467 s->frameNumber = frameNumber;
468 return NO_ERROR;
469}
470
471status_t Composer::deferTransactionUntil(
472 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
473 const sp<Surface>& barrierSurface, uint64_t frameNumber) {
474 Mutex::Autolock lock(mLock);
475 layer_state_t* s = getLayerStateLocked(client, id);
476 if (!s) {
477 return BAD_INDEX;
478 }
479 s->what |= layer_state_t::eDeferTransaction;
480 s->barrierGbp = barrierSurface->getIGraphicBufferProducer();
Dan Stoza7dde5992015-05-22 09:51:44 -0700481 s->frameNumber = frameNumber;
482 return NO_ERROR;
483}
484
Robert Carr1db73f62016-12-21 12:58:51 -0800485status_t Composer::reparentChildren(
486 const sp<SurfaceComposerClient>& client,
487 const sp<IBinder>& id,
488 const sp<IBinder>& newParentHandle) {
489 Mutex::Autolock lock(mLock);
490 layer_state_t* s = getLayerStateLocked(client, id);
491 if (!s) {
492 return BAD_INDEX;
493 }
494 s->what |= layer_state_t::eReparentChildren;
495 s->reparentHandle = newParentHandle;
496 return NO_ERROR;
497}
498
chaviw06178942017-07-27 10:25:59 -0700499status_t Composer::reparentChild(const sp<SurfaceComposerClient>& client,
500 const sp<IBinder>& id,
501 const sp<IBinder>& newParentHandle,
502 const sp<IBinder>& childHandle) {
503 Mutex::Autolock lock(mLock);
504 layer_state_t* s = getLayerStateLocked(client, id);
505 if (!s) {
506 return BAD_INDEX;
507 }
508 s->what |= layer_state_t::eReparentChild;
509 s->parentHandleForChild = newParentHandle;
510 s->childHandle = childHandle;
511 return NO_ERROR;
512}
513
Robert Carr9524cb32017-02-13 11:32:32 -0800514status_t Composer::detachChildren(
515 const sp<SurfaceComposerClient>& client,
516 const sp<IBinder>& id) {
517 Mutex::Autolock lock(mLock);
518 layer_state_t* s = getLayerStateLocked(client, id);
519 if (!s) {
520 return BAD_INDEX;
521 }
522 s->what |= layer_state_t::eDetachChildren;
523 return NO_ERROR;
524}
525
Robert Carrc3574f72016-03-24 12:19:32 -0700526status_t Composer::setOverrideScalingMode(
527 const sp<SurfaceComposerClient>& client,
528 const sp<IBinder>& id, int32_t overrideScalingMode) {
529 Mutex::Autolock lock(mLock);
530 layer_state_t* s = getLayerStateLocked(client, id);
531 if (!s) {
532 return BAD_INDEX;
533 }
534
535 switch (overrideScalingMode) {
536 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
537 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
538 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
539 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
540 case -1:
541 break;
542 default:
543 ALOGE("unknown scaling mode: %d",
544 overrideScalingMode);
545 return BAD_VALUE;
546 }
547
548 s->what |= layer_state_t::eOverrideScalingModeChanged;
549 s->overrideScalingMode = overrideScalingMode;
550 return NO_ERROR;
551}
552
Robert Carr99e27f02016-06-16 15:18:02 -0700553status_t Composer::setGeometryAppliesWithResize(
Robert Carr82364e32016-05-15 11:27:47 -0700554 const sp<SurfaceComposerClient>& client,
555 const sp<IBinder>& id) {
556 Mutex::Autolock lock(mLock);
557 layer_state_t* s = getLayerStateLocked(client, id);
558 if (!s) {
559 return BAD_INDEX;
560 }
Robert Carr99e27f02016-06-16 15:18:02 -0700561 s->what |= layer_state_t::eGeometryAppliesWithResize;
Robert Carr82364e32016-05-15 11:27:47 -0700562 return NO_ERROR;
563}
564
Mathias Agopian698c0872011-06-28 19:09:31 -0700565// ---------------------------------------------------------------------------
566
Mathias Agopiane57f2922012-08-09 16:29:12 -0700567DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
568 DisplayState s;
569 s.token = token;
570 ssize_t index = mDisplayStates.indexOf(s);
571 if (index < 0) {
572 // we don't have it, add an initialized layer_state to our list
573 s.what = 0;
574 index = mDisplayStates.add(s);
575 }
Dan Stozad723bd72014-11-18 10:24:03 -0800576 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700577}
578
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700579status_t Composer::setDisplaySurface(const sp<IBinder>& token,
580 sp<IGraphicBufferProducer> bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -0700581 if (bufferProducer.get() != nullptr) {
582 // Make sure that composition can never be stalled by a virtual display
583 // consumer that isn't processing buffers fast enough.
584 status_t err = bufferProducer->setAsyncMode(true);
585 if (err != NO_ERROR) {
586 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
587 "BufferQueue. This BufferQueue cannot be used for virtual "
588 "display. (%d)", err);
589 return err;
590 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700591 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700592 Mutex::Autolock _l(mLock);
593 DisplayState& s(getDisplayStateLocked(token));
Andy McFadden2adaf042012-12-18 09:49:45 -0800594 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700595 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700596 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700597}
598
599void Composer::setDisplayLayerStack(const sp<IBinder>& token,
600 uint32_t layerStack) {
601 Mutex::Autolock _l(mLock);
602 DisplayState& s(getDisplayStateLocked(token));
603 s.layerStack = layerStack;
604 s.what |= DisplayState::eLayerStackChanged;
605}
606
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700607void Composer::setDisplayProjection(const sp<IBinder>& token,
608 uint32_t orientation,
609 const Rect& layerStackRect,
610 const Rect& displayRect) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700611 Mutex::Autolock _l(mLock);
612 DisplayState& s(getDisplayStateLocked(token));
613 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700614 s.viewport = layerStackRect;
615 s.frame = displayRect;
616 s.what |= DisplayState::eDisplayProjectionChanged;
Jorim Jaggi092123c2016-04-13 01:40:35 +0000617 mForceSynchronous = true; // TODO: do we actually still need this?
Mathias Agopiane57f2922012-08-09 16:29:12 -0700618}
619
Michael Wright1f6078a2014-06-26 16:01:02 -0700620void Composer::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
621 Mutex::Autolock _l(mLock);
622 DisplayState& s(getDisplayStateLocked(token));
623 s.width = width;
624 s.height = height;
625 s.what |= DisplayState::eDisplaySizeChanged;
626}
627
Mathias Agopiane57f2922012-08-09 16:29:12 -0700628// ---------------------------------------------------------------------------
629
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630SurfaceComposerClient::SurfaceComposerClient()
Mathias Agopian698c0872011-06-28 19:09:31 -0700631 : mStatus(NO_INIT), mComposer(Composer::getInstance())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633}
634
Robert Carr1db73f62016-12-21 12:58:51 -0800635SurfaceComposerClient::SurfaceComposerClient(const sp<IGraphicBufferProducer>& root)
636 : mStatus(NO_INIT), mComposer(Composer::getInstance()), mParent(root)
637{
638}
639
Mathias Agopian698c0872011-06-28 19:09:31 -0700640void SurfaceComposerClient::onFirstRef() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700641 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopiand4784a32010-05-27 19:41:15 -0700642 if (sm != 0) {
Robert Carr1db73f62016-12-21 12:58:51 -0800643 auto rootProducer = mParent.promote();
644 sp<ISurfaceComposerClient> conn;
645 conn = (rootProducer != nullptr) ? sm->createScopedConnection(rootProducer) :
646 sm->createConnection();
Mathias Agopiand4784a32010-05-27 19:41:15 -0700647 if (conn != 0) {
648 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700649 mStatus = NO_ERROR;
650 }
651 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652}
653
Mathias Agopian698c0872011-06-28 19:09:31 -0700654SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -0700655 dispose();
656}
Mathias Agopiandd3423c2009-09-23 15:44:05 -0700657
Mathias Agopian698c0872011-06-28 19:09:31 -0700658status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659 return mStatus;
660}
661
Mathias Agopian698c0872011-06-28 19:09:31 -0700662sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800663 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664}
665
Mathias Agopiand4784a32010-05-27 19:41:15 -0700666status_t SurfaceComposerClient::linkToComposerDeath(
667 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -0700668 void* cookie, uint32_t flags) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700669 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800670 return IInterface::asBinder(sm)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671}
672
Mathias Agopian698c0872011-06-28 19:09:31 -0700673void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800674 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -0700675 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700676 Mutex::Autolock _lm(mLock);
677 if (mClient != 0) {
Mathias Agopiand4784a32010-05-27 19:41:15 -0700678 client = mClient; // hold ref while lock is held
679 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680 }
Mathias Agopiand4784a32010-05-27 19:41:15 -0700681 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800682}
683
Mathias Agopian698c0872011-06-28 19:09:31 -0700684sp<SurfaceControl> SurfaceComposerClient::createSurface(
Mathias Agopian698c0872011-06-28 19:09:31 -0700685 const String8& name,
Mathias Agopian698c0872011-06-28 19:09:31 -0700686 uint32_t w,
687 uint32_t h,
688 PixelFormat format,
Robert Carr1f0a16a2016-10-24 16:27:39 -0700689 uint32_t flags,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500690 SurfaceControl* parent,
691 uint32_t windowType,
692 uint32_t ownerUid)
Mathias Agopian698c0872011-06-28 19:09:31 -0700693{
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700694 sp<SurfaceControl> sur;
Mathias Agopian698c0872011-06-28 19:09:31 -0700695 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700696 sp<IBinder> handle;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700697 sp<IBinder> parentHandle;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700698 sp<IGraphicBufferProducer> gbp;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700699
700 if (parent != nullptr) {
701 parentHandle = parent->getHandle();
702 }
703 status_t err = mClient->createSurface(name, w, h, format, flags, parentHandle,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500704 windowType, ownerUid, &handle, &gbp);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700705 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
706 if (err == NO_ERROR) {
707 sur = new SurfaceControl(this, handle, gbp);
Mathias Agopian698c0872011-06-28 19:09:31 -0700708 }
709 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700710 return sur;
Mathias Agopian698c0872011-06-28 19:09:31 -0700711}
712
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700713sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
714 bool secure) {
715 return Composer::getInstance().createDisplay(displayName, secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700716}
717
Jesse Hall6c913be2013-08-08 12:15:49 -0700718void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
719 Composer::getInstance().destroyDisplay(display);
720}
721
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700722sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
723 return Composer::getInstance().getBuiltInDisplay(id);
724}
725
Mathias Agopianac9fa422013-02-11 16:40:36 -0800726status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700727 if (mStatus != NO_ERROR)
728 return mStatus;
729 status_t err = mClient->destroySurface(sid);
730 return err;
731}
732
Svetoslavd85084b2014-03-20 10:28:31 -0700733status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
734 if (mStatus != NO_ERROR) {
735 return mStatus;
736 }
737 return mClient->clearLayerFrameStats(token);
738}
739
740status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
741 FrameStats* outStats) const {
742 if (mStatus != NO_ERROR) {
743 return mStatus;
744 }
745 return mClient->getLayerFrameStats(token, outStats);
746}
747
Mathias Agopian698c0872011-06-28 19:09:31 -0700748inline Composer& SurfaceComposerClient::getComposer() {
749 return mComposer;
750}
751
752// ----------------------------------------------------------------------------
753
754void SurfaceComposerClient::openGlobalTransaction() {
Jeff Brownf3f7db62012-08-31 02:18:38 -0700755 Composer::openGlobalTransaction();
Mathias Agopian698c0872011-06-28 19:09:31 -0700756}
757
Jamie Gennis28378392011-10-12 17:39:00 -0700758void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
759 Composer::closeGlobalTransaction(synchronous);
Mathias Agopian698c0872011-06-28 19:09:31 -0700760}
761
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700762void SurfaceComposerClient::setAnimationTransaction() {
763 Composer::setAnimationTransaction();
764}
765
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700766status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
767 return Composer::enableVSyncInjections(enable);
768}
769
770status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
771 return Composer::injectVSync(when);
772}
773
Mathias Agopian698c0872011-06-28 19:09:31 -0700774// ----------------------------------------------------------------------------
775
Mathias Agopianac9fa422013-02-11 16:40:36 -0800776status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700777 return getComposer().setCrop(this, id, crop);
778}
779
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000780status_t SurfaceComposerClient::setFinalCrop(const sp<IBinder>& id,
781 const Rect& crop) {
782 return getComposer().setFinalCrop(this, id, crop);
783}
784
Mathias Agopianac9fa422013-02-11 16:40:36 -0800785status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700786 return getComposer().setPosition(this, id, x, y);
787}
788
Mathias Agopianac9fa422013-02-11 16:40:36 -0800789status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700790 return getComposer().setSize(this, id, w, h);
791}
792
Robert Carrae060832016-11-28 10:51:00 -0800793status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, int32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700794 return getComposer().setLayer(this, id, z);
795}
796
Robert Carrdb66e622017-04-10 16:55:57 -0700797status_t SurfaceComposerClient::setRelativeLayer(const sp<IBinder>& id,
798 const sp<IBinder>& relativeTo, int32_t z) {
799 return getComposer().setRelativeLayer(this, id, relativeTo, z);
800}
801
Mathias Agopianac9fa422013-02-11 16:40:36 -0800802status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700803 return getComposer().setFlags(this, id,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700804 layer_state_t::eLayerHidden,
805 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700806}
807
Mathias Agopianac9fa422013-02-11 16:40:36 -0800808status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700809 return getComposer().setFlags(this, id,
810 0,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700811 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700812}
813
Mathias Agopianac9fa422013-02-11 16:40:36 -0800814status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700815 uint32_t mask) {
816 return getComposer().setFlags(this, id, flags, mask);
817}
818
Mathias Agopianac9fa422013-02-11 16:40:36 -0800819status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700820 const Region& transparentRegion) {
821 return getComposer().setTransparentRegionHint(this, id, transparentRegion);
822}
823
Mathias Agopianac9fa422013-02-11 16:40:36 -0800824status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700825 return getComposer().setAlpha(this, id, alpha);
826}
827
Mathias Agopianac9fa422013-02-11 16:40:36 -0800828status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700829 return getComposer().setLayerStack(this, id, layerStack);
830}
831
Mathias Agopianac9fa422013-02-11 16:40:36 -0800832status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800833 float dtdy, float dsdy) {
834 return getComposer().setMatrix(this, id, dsdx, dtdx, dtdy, dsdy);
Mathias Agopian698c0872011-06-28 19:09:31 -0700835}
836
Dan Stoza7dde5992015-05-22 09:51:44 -0700837status_t SurfaceComposerClient::deferTransactionUntil(const sp<IBinder>& id,
838 const sp<IBinder>& handle, uint64_t frameNumber) {
839 return getComposer().deferTransactionUntil(this, id, handle, frameNumber);
840}
841
Robert Carr0d480722017-01-10 16:42:54 -0800842status_t SurfaceComposerClient::deferTransactionUntil(const sp<IBinder>& id,
843 const sp<Surface>& barrierSurface, uint64_t frameNumber) {
844 return getComposer().deferTransactionUntil(this, id, barrierSurface, frameNumber);
845}
846
Robert Carr1db73f62016-12-21 12:58:51 -0800847status_t SurfaceComposerClient::reparentChildren(const sp<IBinder>& id,
848 const sp<IBinder>& newParentHandle) {
849 return getComposer().reparentChildren(this, id, newParentHandle);
850}
851
chaviw06178942017-07-27 10:25:59 -0700852status_t SurfaceComposerClient::reparentChild(const sp<IBinder>& id,
853 const sp<IBinder>& newParentHandle, const sp<IBinder>& childHandle) {
854 return getComposer().reparentChild(this, id, newParentHandle, childHandle);
855}
856
Robert Carr9524cb32017-02-13 11:32:32 -0800857status_t SurfaceComposerClient::detachChildren(const sp<IBinder>& id) {
858 return getComposer().detachChildren(this, id);
859}
860
Robert Carrc3574f72016-03-24 12:19:32 -0700861status_t SurfaceComposerClient::setOverrideScalingMode(
862 const sp<IBinder>& id, int32_t overrideScalingMode) {
863 return getComposer().setOverrideScalingMode(
864 this, id, overrideScalingMode);
865}
866
Robert Carr99e27f02016-06-16 15:18:02 -0700867status_t SurfaceComposerClient::setGeometryAppliesWithResize(
Robert Carr82364e32016-05-15 11:27:47 -0700868 const sp<IBinder>& id) {
Robert Carr99e27f02016-06-16 15:18:02 -0700869 return getComposer().setGeometryAppliesWithResize(this, id);
Robert Carr82364e32016-05-15 11:27:47 -0700870}
871
Mathias Agopian698c0872011-06-28 19:09:31 -0700872// ----------------------------------------------------------------------------
873
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700874status_t SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
875 sp<IGraphicBufferProducer> bufferProducer) {
876 return Composer::getInstance().setDisplaySurface(token, bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700877}
878
879void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
880 uint32_t layerStack) {
881 Composer::getInstance().setDisplayLayerStack(token, layerStack);
882}
883
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700884void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
885 uint32_t orientation,
886 const Rect& layerStackRect,
887 const Rect& displayRect) {
888 Composer::getInstance().setDisplayProjection(token, orientation,
889 layerStackRect, displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700890}
891
Michael Wright1f6078a2014-06-26 16:01:02 -0700892void SurfaceComposerClient::setDisplaySize(const sp<IBinder>& token,
893 uint32_t width, uint32_t height) {
894 Composer::getInstance().setDisplaySize(token, width, height);
895}
896
Mathias Agopiane57f2922012-08-09 16:29:12 -0700897// ----------------------------------------------------------------------------
898
Dan Stoza7f7da322014-05-02 15:26:25 -0700899status_t SurfaceComposerClient::getDisplayConfigs(
900 const sp<IBinder>& display, Vector<DisplayInfo>* configs)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901{
Dan Stoza7f7da322014-05-02 15:26:25 -0700902 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
903}
904
905status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
906 DisplayInfo* info) {
907 Vector<DisplayInfo> configs;
908 status_t result = getDisplayConfigs(display, &configs);
909 if (result != NO_ERROR) {
910 return result;
911 }
912
913 int activeId = getActiveConfig(display);
914 if (activeId < 0) {
915 ALOGE("No active configuration found");
916 return NAME_NOT_FOUND;
917 }
918
Dan Stozad723bd72014-11-18 10:24:03 -0800919 *info = configs[static_cast<size_t>(activeId)];
Dan Stoza7f7da322014-05-02 15:26:25 -0700920 return NO_ERROR;
921}
922
923int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
924 return ComposerService::getComposerService()->getActiveConfig(display);
925}
926
927status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
928 return ComposerService::getComposerService()->setActiveConfig(display, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929}
930
Michael Wright28f24d02016-07-12 13:30:53 -0700931status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
932 Vector<android_color_mode_t>* outColorModes) {
933 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
934}
935
936android_color_mode_t SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
937 return ComposerService::getComposerService()->getActiveColorMode(display);
938}
939
940status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
941 android_color_mode_t colorMode) {
942 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
943}
944
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700945void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
946 int mode) {
947 ComposerService::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -0700948}
949
Svetoslavd85084b2014-03-20 10:28:31 -0700950status_t SurfaceComposerClient::clearAnimationFrameStats() {
951 return ComposerService::getComposerService()->clearAnimationFrameStats();
952}
953
954status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
955 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
956}
957
Dan Stozac4f471e2016-03-24 09:31:08 -0700958status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
959 HdrCapabilities* outCapabilities) {
960 return ComposerService::getComposerService()->getHdrCapabilities(display,
961 outCapabilities);
962}
963
Mathias Agopian698c0872011-06-28 19:09:31 -0700964// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800966status_t ScreenshotClient::capture(
967 const sp<IBinder>& display,
968 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -0700969 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -0800970 int32_t minLayerZ, int32_t maxLayerZ, bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800971 sp<ISurfaceComposer> s(ComposerService::getComposerService());
972 if (s == NULL) return NO_INIT;
Dan Stozac1879002014-05-22 15:59:05 -0700973 return s->captureScreen(display, producer, sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -0800974 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800975}
976
Robert Carr673134e2017-01-09 19:48:38 -0800977status_t ScreenshotClient::captureToBuffer(const sp<IBinder>& display,
978 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -0800979 int32_t minLayerZ, int32_t maxLayerZ, bool useIdentityTransform,
Robert Carr673134e2017-01-09 19:48:38 -0800980 uint32_t rotation,
981 sp<GraphicBuffer>* outBuffer) {
982 sp<ISurfaceComposer> s(ComposerService::getComposerService());
983 if (s == NULL) return NO_INIT;
984
985 sp<IGraphicBufferConsumer> gbpConsumer;
986 sp<IGraphicBufferProducer> producer;
987 BufferQueue::createBufferQueue(&producer, &gbpConsumer);
988 sp<BufferItemConsumer> consumer(new BufferItemConsumer(gbpConsumer,
989 GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_NEVER,
990 1, true));
991
992 status_t ret = s->captureScreen(display, producer, sourceCrop, reqWidth, reqHeight,
993 minLayerZ, maxLayerZ, useIdentityTransform,
994 static_cast<ISurfaceComposer::Rotation>(rotation));
995 if (ret != NO_ERROR) {
996 return ret;
997 }
998 BufferItem b;
999 consumer->acquireBuffer(&b, 0, true);
1000 *outBuffer = b.mGraphicBuffer;
1001 return ret;
1002}
1003
Mathias Agopian74c40c02010-09-29 13:02:36 -07001004ScreenshotClient::ScreenshotClient()
Mathias Agopianabe815d2013-03-19 22:22:21 -07001005 : mHaveBuffer(false) {
1006 memset(&mBuffer, 0, sizeof(mBuffer));
Mathias Agopian74c40c02010-09-29 13:02:36 -07001007}
1008
Mathias Agopian8000d062013-03-26 18:15:35 -07001009ScreenshotClient::~ScreenshotClient() {
1010 ScreenshotClient::release();
1011}
1012
Mathias Agopianabe815d2013-03-19 22:22:21 -07001013sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
1014 if (mCpuConsumer == NULL) {
Dan Stoza6d5a7bb2014-03-13 11:39:09 -07001015 sp<IGraphicBufferConsumer> consumer;
1016 BufferQueue::createBufferQueue(&mProducer, &consumer);
1017 mCpuConsumer = new CpuConsumer(consumer, 1);
Mathias Agopianabe815d2013-03-19 22:22:21 -07001018 mCpuConsumer->setName(String8("ScreenshotClient"));
1019 }
1020 return mCpuConsumer;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08001021}
1022
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001023status_t ScreenshotClient::update(const sp<IBinder>& display,
Dan Stozac1879002014-05-22 15:59:05 -07001024 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -08001025 int32_t minLayerZ, int32_t maxLayerZ,
Riley Andrewsd15ef272014-09-04 16:19:44 -07001026 bool useIdentityTransform, uint32_t rotation) {
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08001027 sp<ISurfaceComposer> s(ComposerService::getComposerService());
1028 if (s == NULL) return NO_INIT;
Mathias Agopianabe815d2013-03-19 22:22:21 -07001029 sp<CpuConsumer> cpuConsumer = getCpuConsumer();
1030
1031 if (mHaveBuffer) {
1032 mCpuConsumer->unlockBuffer(mBuffer);
1033 memset(&mBuffer, 0, sizeof(mBuffer));
1034 mHaveBuffer = false;
1035 }
1036
Dan Stozac1879002014-05-22 15:59:05 -07001037 status_t err = s->captureScreen(display, mProducer, sourceCrop,
Riley Andrewsd15ef272014-09-04 16:19:44 -07001038 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform,
1039 static_cast<ISurfaceComposer::Rotation>(rotation));
Mathias Agopianabe815d2013-03-19 22:22:21 -07001040
1041 if (err == NO_ERROR) {
1042 err = mCpuConsumer->lockNextBuffer(&mBuffer);
1043 if (err == NO_ERROR) {
1044 mHaveBuffer = true;
1045 }
1046 }
1047 return err;
1048}
1049
Riley Andrewsd15ef272014-09-04 16:19:44 -07001050status_t ScreenshotClient::update(const sp<IBinder>& display,
1051 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -08001052 int32_t minLayerZ, int32_t maxLayerZ,
Riley Andrewsd15ef272014-09-04 16:19:44 -07001053 bool useIdentityTransform) {
1054
1055 return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
1056 minLayerZ, maxLayerZ, useIdentityTransform, ISurfaceComposer::eRotateNone);
1057}
1058
Dan Stozac1879002014-05-22 15:59:05 -07001059status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -08001060 bool useIdentityTransform) {
Robert Carrae060832016-11-28 10:51:00 -08001061 return ScreenshotClient::update(display, sourceCrop, 0, 0,
1062 INT32_MIN, INT32_MAX,
Riley Andrewsd15ef272014-09-04 16:19:44 -07001063 useIdentityTransform, ISurfaceComposer::eRotateNone);
Mathias Agopianabe815d2013-03-19 22:22:21 -07001064}
1065
Dan Stozac1879002014-05-22 15:59:05 -07001066status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -08001067 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform) {
Dan Stozac1879002014-05-22 15:59:05 -07001068 return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
Robert Carrae060832016-11-28 10:51:00 -08001069 INT32_MIN, INT32_MAX,
1070 useIdentityTransform, ISurfaceComposer::eRotateNone);
Mathias Agopian74c40c02010-09-29 13:02:36 -07001071}
1072
1073void ScreenshotClient::release() {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001074 if (mHaveBuffer) {
1075 mCpuConsumer->unlockBuffer(mBuffer);
1076 memset(&mBuffer, 0, sizeof(mBuffer));
1077 mHaveBuffer = false;
1078 }
1079 mCpuConsumer.clear();
Mathias Agopian74c40c02010-09-29 13:02:36 -07001080}
1081
1082void const* ScreenshotClient::getPixels() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001083 return mBuffer.data;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001084}
1085
1086uint32_t ScreenshotClient::getWidth() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001087 return mBuffer.width;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001088}
1089
1090uint32_t ScreenshotClient::getHeight() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001091 return mBuffer.height;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001092}
1093
1094PixelFormat ScreenshotClient::getFormat() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001095 return mBuffer.format;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001096}
1097
1098uint32_t ScreenshotClient::getStride() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001099 return mBuffer.stride;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001100}
1101
1102size_t ScreenshotClient::getSize() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001103 return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
Mathias Agopian74c40c02010-09-29 13:02:36 -07001104}
1105
Romain Guy88d37dd2017-05-26 17:57:05 -07001106android_dataspace ScreenshotClient::getDataSpace() const {
1107 return mBuffer.dataSpace;
1108}
1109
Mathias Agopian74c40c02010-09-29 13:02:36 -07001110// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111}; // namespace android