blob: 8c8384399ce380f87f43d17d6f4996733c259272 [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);
Robert Carr9524cb32017-02-13 11:32:32 -0800179 status_t detachChildren(const sp<SurfaceComposerClient>& client,
180 const sp<IBinder>& id);
Robert Carrc3574f72016-03-24 12:19:32 -0700181 status_t setOverrideScalingMode(const sp<SurfaceComposerClient>& client,
182 const sp<IBinder>& id, int32_t overrideScalingMode);
Robert Carr99e27f02016-06-16 15:18:02 -0700183 status_t setGeometryAppliesWithResize(const sp<SurfaceComposerClient>& client,
Robert Carr82364e32016-05-15 11:27:47 -0700184 const sp<IBinder>& id);
Mathias Agopian698c0872011-06-28 19:09:31 -0700185
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700186 status_t setDisplaySurface(const sp<IBinder>& token,
187 sp<IGraphicBufferProducer> bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700188 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700189 void setDisplayProjection(const sp<IBinder>& token,
190 uint32_t orientation,
191 const Rect& layerStackRect,
192 const Rect& displayRect);
Michael Wright1f6078a2014-06-26 16:01:02 -0700193 void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700194
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700195 static void setAnimationTransaction() {
196 Composer::getInstance().setAnimationTransactionImpl();
197 }
198
Jeff Brownf3f7db62012-08-31 02:18:38 -0700199 static void openGlobalTransaction() {
200 Composer::getInstance().openGlobalTransactionImpl();
201 }
202
Jamie Gennis28378392011-10-12 17:39:00 -0700203 static void closeGlobalTransaction(bool synchronous) {
204 Composer::getInstance().closeGlobalTransactionImpl(synchronous);
Mathias Agopiand4784a32010-05-27 19:41:15 -0700205 }
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700206
207 static status_t enableVSyncInjections(bool enable) {
208 return Composer::getInstance().enableVSyncInjectionsImpl(enable);
209 }
210
211 static status_t injectVSync(nsecs_t when) {
212 return Composer::getInstance().injectVSyncImpl(when);
213 }
Mathias Agopiand4784a32010-05-27 19:41:15 -0700214};
215
216ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218// ---------------------------------------------------------------------------
219
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700220sp<IBinder> Composer::createDisplay(const String8& displayName, bool secure) {
221 return ComposerService::getComposerService()->createDisplay(displayName,
222 secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700223}
224
Jesse Hall6c913be2013-08-08 12:15:49 -0700225void Composer::destroyDisplay(const sp<IBinder>& display) {
226 return ComposerService::getComposerService()->destroyDisplay(display);
227}
228
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700229sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
230 return ComposerService::getComposerService()->getBuiltInDisplay(id);
231}
232
Jeff Brownf3f7db62012-08-31 02:18:38 -0700233void Composer::openGlobalTransactionImpl() {
234 { // scope for the lock
235 Mutex::Autolock _l(mLock);
236 mTransactionNestCount += 1;
237 }
238}
239
Jamie Gennis28378392011-10-12 17:39:00 -0700240void Composer::closeGlobalTransactionImpl(bool synchronous) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700241 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopian698c0872011-06-28 19:09:31 -0700242
243 Vector<ComposerState> transaction;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700244 Vector<DisplayState> displayTransaction;
Jamie Gennis28378392011-10-12 17:39:00 -0700245 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -0700246
247 { // scope for the lock
248 Mutex::Autolock _l(mLock);
Jeff Brownf3f7db62012-08-31 02:18:38 -0700249 mForceSynchronous |= synchronous;
250 if (!mTransactionNestCount) {
251 ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
252 "call to openGlobalTransaction().");
253 } else if (--mTransactionNestCount) {
254 return;
255 }
256
Mathias Agopiane57f2922012-08-09 16:29:12 -0700257 transaction = mComposerStates;
258 mComposerStates.clear();
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700259
Mathias Agopiane57f2922012-08-09 16:29:12 -0700260 displayTransaction = mDisplayStates;
261 mDisplayStates.clear();
Jamie Gennis28378392011-10-12 17:39:00 -0700262
Jeff Brownf3f7db62012-08-31 02:18:38 -0700263 if (mForceSynchronous) {
Jamie Gennis28378392011-10-12 17:39:00 -0700264 flags |= ISurfaceComposer::eSynchronous;
265 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700266 if (mAnimation) {
267 flags |= ISurfaceComposer::eAnimation;
268 }
269
Jamie Gennis28378392011-10-12 17:39:00 -0700270 mForceSynchronous = false;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700271 mAnimation = false;
Mathias Agopian698c0872011-06-28 19:09:31 -0700272 }
273
Mathias Agopian8b33f032012-07-24 20:43:54 -0700274 sm->setTransactionState(transaction, displayTransaction, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275}
276
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700277status_t Composer::enableVSyncInjectionsImpl(bool enable) {
278 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
279 return sm->enableVSyncInjections(enable);
280}
281
282status_t Composer::injectVSyncImpl(nsecs_t when) {
283 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
284 return sm->injectVSync(when);
285}
286
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700287void Composer::setAnimationTransactionImpl() {
288 Mutex::Autolock _l(mLock);
289 mAnimation = true;
290}
291
Mathias Agopian698c0872011-06-28 19:09:31 -0700292layer_state_t* Composer::getLayerStateLocked(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800293 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700294
295 ComposerState s;
296 s.client = client->mClient;
297 s.state.surface = id;
298
Mathias Agopiane57f2922012-08-09 16:29:12 -0700299 ssize_t index = mComposerStates.indexOf(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700300 if (index < 0) {
301 // we don't have it, add an initialized layer_state to our list
Mathias Agopiane57f2922012-08-09 16:29:12 -0700302 index = mComposerStates.add(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700303 }
304
Mathias Agopiane57f2922012-08-09 16:29:12 -0700305 ComposerState* const out = mComposerStates.editArray();
Mathias Agopian698c0872011-06-28 19:09:31 -0700306 return &(out[index].state);
307}
308
309status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800310 const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700311 Mutex::Autolock _l(mLock);
312 layer_state_t* s = getLayerStateLocked(client, id);
313 if (!s)
314 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700315 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700316 s->x = x;
317 s->y = y;
318 return NO_ERROR;
319}
320
321status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800322 const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700323 Mutex::Autolock _l(mLock);
324 layer_state_t* s = getLayerStateLocked(client, id);
325 if (!s)
326 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700327 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700328 s->w = w;
329 s->h = h;
Jamie Gennis28378392011-10-12 17:39:00 -0700330
Jorim Jaggi092123c2016-04-13 01:40:35 +0000331 // Resizing a surface makes the transaction synchronous.
332 mForceSynchronous = true;
333
Mathias Agopian698c0872011-06-28 19:09:31 -0700334 return NO_ERROR;
335}
336
337status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
Robert Carrae060832016-11-28 10:51:00 -0800338 const sp<IBinder>& id, int32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700339 Mutex::Autolock _l(mLock);
340 layer_state_t* s = getLayerStateLocked(client, id);
341 if (!s)
342 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700343 s->what |= layer_state_t::eLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700344 s->z = z;
345 return NO_ERROR;
346}
347
Robert Carrdb66e622017-04-10 16:55:57 -0700348status_t Composer::setRelativeLayer(const sp<SurfaceComposerClient>& client,
349 const sp<IBinder>& id, const sp<IBinder>& relativeTo,
350 int32_t z) {
351 Mutex::Autolock _l(mLock);
352 layer_state_t* s = getLayerStateLocked(client, id);
353 if (!s) {
354 return BAD_INDEX;
355 }
356 s->what |= layer_state_t::eRelativeLayerChanged;
357 s->relativeLayerHandle = relativeTo;
358 s->z = z;
359 return NO_ERROR;
360}
361
Mathias Agopian698c0872011-06-28 19:09:31 -0700362status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800363 const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700364 uint32_t mask) {
365 Mutex::Autolock _l(mLock);
366 layer_state_t* s = getLayerStateLocked(client, id);
367 if (!s)
368 return BAD_INDEX;
Pablo Ceballos53390e12015-08-04 11:25:59 -0700369 if ((mask & layer_state_t::eLayerOpaque) ||
370 (mask & layer_state_t::eLayerHidden) ||
371 (mask & layer_state_t::eLayerSecure)) {
Dan Stoza23116082015-06-18 14:58:39 -0700372 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-29 17:17:11 -0800373 }
Mathias Agopian698c0872011-06-28 19:09:31 -0700374 s->flags &= ~mask;
375 s->flags |= (flags & mask);
376 s->mask |= mask;
377 return NO_ERROR;
378}
379
380status_t Composer::setTransparentRegionHint(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800381 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700382 const Region& transparentRegion) {
383 Mutex::Autolock _l(mLock);
384 layer_state_t* s = getLayerStateLocked(client, id);
385 if (!s)
386 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700387 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700388 s->transparentRegion = transparentRegion;
389 return NO_ERROR;
390}
391
392status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800393 const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700394 Mutex::Autolock _l(mLock);
395 layer_state_t* s = getLayerStateLocked(client, id);
396 if (!s)
397 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700398 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700399 s->alpha = alpha;
400 return NO_ERROR;
401}
402
Mathias Agopian87855782012-07-24 21:41:09 -0700403status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800404 const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700405 Mutex::Autolock _l(mLock);
406 layer_state_t* s = getLayerStateLocked(client, id);
407 if (!s)
408 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700409 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700410 s->layerStack = layerStack;
411 return NO_ERROR;
412}
413
Mathias Agopian698c0872011-06-28 19:09:31 -0700414status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800415 const sp<IBinder>& id, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800416 float dtdy, float dsdy) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700417 Mutex::Autolock _l(mLock);
418 layer_state_t* s = getLayerStateLocked(client, id);
419 if (!s)
420 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700421 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700422 layer_state_t::matrix22_t matrix;
423 matrix.dsdx = dsdx;
424 matrix.dtdx = dtdx;
425 matrix.dsdy = dsdy;
426 matrix.dtdy = dtdy;
427 s->matrix = matrix;
428 return NO_ERROR;
429}
430
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700431status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800432 const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700433 Mutex::Autolock _l(mLock);
434 layer_state_t* s = getLayerStateLocked(client, id);
435 if (!s)
436 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700437 s->what |= layer_state_t::eCropChanged;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700438 s->crop = crop;
439 return NO_ERROR;
440}
441
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442status_t Composer::setFinalCrop(const sp<SurfaceComposerClient>& client,
443 const sp<IBinder>& id, const Rect& crop) {
444 Mutex::Autolock _l(mLock);
445 layer_state_t* s = getLayerStateLocked(client, id);
446 if (!s) {
447 return BAD_INDEX;
448 }
449 s->what |= layer_state_t::eFinalCropChanged;
450 s->finalCrop = crop;
451 return NO_ERROR;
452}
453
Dan Stoza7dde5992015-05-22 09:51:44 -0700454status_t Composer::deferTransactionUntil(
455 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
456 const sp<IBinder>& handle, uint64_t frameNumber) {
457 Mutex::Autolock lock(mLock);
458 layer_state_t* s = getLayerStateLocked(client, id);
459 if (!s) {
460 return BAD_INDEX;
461 }
462 s->what |= layer_state_t::eDeferTransaction;
Robert Carr0d480722017-01-10 16:42:54 -0800463 s->barrierHandle = handle;
464 s->frameNumber = frameNumber;
465 return NO_ERROR;
466}
467
468status_t Composer::deferTransactionUntil(
469 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
470 const sp<Surface>& barrierSurface, uint64_t frameNumber) {
471 Mutex::Autolock lock(mLock);
472 layer_state_t* s = getLayerStateLocked(client, id);
473 if (!s) {
474 return BAD_INDEX;
475 }
476 s->what |= layer_state_t::eDeferTransaction;
477 s->barrierGbp = barrierSurface->getIGraphicBufferProducer();
Dan Stoza7dde5992015-05-22 09:51:44 -0700478 s->frameNumber = frameNumber;
479 return NO_ERROR;
480}
481
Robert Carr1db73f62016-12-21 12:58:51 -0800482status_t Composer::reparentChildren(
483 const sp<SurfaceComposerClient>& client,
484 const sp<IBinder>& id,
485 const sp<IBinder>& newParentHandle) {
486 Mutex::Autolock lock(mLock);
487 layer_state_t* s = getLayerStateLocked(client, id);
488 if (!s) {
489 return BAD_INDEX;
490 }
491 s->what |= layer_state_t::eReparentChildren;
492 s->reparentHandle = newParentHandle;
493 return NO_ERROR;
494}
495
Robert Carr9524cb32017-02-13 11:32:32 -0800496status_t Composer::detachChildren(
497 const sp<SurfaceComposerClient>& client,
498 const sp<IBinder>& id) {
499 Mutex::Autolock lock(mLock);
500 layer_state_t* s = getLayerStateLocked(client, id);
501 if (!s) {
502 return BAD_INDEX;
503 }
504 s->what |= layer_state_t::eDetachChildren;
505 return NO_ERROR;
506}
507
Robert Carrc3574f72016-03-24 12:19:32 -0700508status_t Composer::setOverrideScalingMode(
509 const sp<SurfaceComposerClient>& client,
510 const sp<IBinder>& id, int32_t overrideScalingMode) {
511 Mutex::Autolock lock(mLock);
512 layer_state_t* s = getLayerStateLocked(client, id);
513 if (!s) {
514 return BAD_INDEX;
515 }
516
517 switch (overrideScalingMode) {
518 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
519 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
520 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
521 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
522 case -1:
523 break;
524 default:
525 ALOGE("unknown scaling mode: %d",
526 overrideScalingMode);
527 return BAD_VALUE;
528 }
529
530 s->what |= layer_state_t::eOverrideScalingModeChanged;
531 s->overrideScalingMode = overrideScalingMode;
532 return NO_ERROR;
533}
534
Robert Carr99e27f02016-06-16 15:18:02 -0700535status_t Composer::setGeometryAppliesWithResize(
Robert Carr82364e32016-05-15 11:27:47 -0700536 const sp<SurfaceComposerClient>& client,
537 const sp<IBinder>& id) {
538 Mutex::Autolock lock(mLock);
539 layer_state_t* s = getLayerStateLocked(client, id);
540 if (!s) {
541 return BAD_INDEX;
542 }
Robert Carr99e27f02016-06-16 15:18:02 -0700543 s->what |= layer_state_t::eGeometryAppliesWithResize;
Robert Carr82364e32016-05-15 11:27:47 -0700544 return NO_ERROR;
545}
546
Mathias Agopian698c0872011-06-28 19:09:31 -0700547// ---------------------------------------------------------------------------
548
Mathias Agopiane57f2922012-08-09 16:29:12 -0700549DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
550 DisplayState s;
551 s.token = token;
552 ssize_t index = mDisplayStates.indexOf(s);
553 if (index < 0) {
554 // we don't have it, add an initialized layer_state to our list
555 s.what = 0;
556 index = mDisplayStates.add(s);
557 }
Dan Stozad723bd72014-11-18 10:24:03 -0800558 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700559}
560
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700561status_t Composer::setDisplaySurface(const sp<IBinder>& token,
562 sp<IGraphicBufferProducer> bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -0700563 if (bufferProducer.get() != nullptr) {
564 // Make sure that composition can never be stalled by a virtual display
565 // consumer that isn't processing buffers fast enough.
566 status_t err = bufferProducer->setAsyncMode(true);
567 if (err != NO_ERROR) {
568 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
569 "BufferQueue. This BufferQueue cannot be used for virtual "
570 "display. (%d)", err);
571 return err;
572 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700573 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700574 Mutex::Autolock _l(mLock);
575 DisplayState& s(getDisplayStateLocked(token));
Andy McFadden2adaf042012-12-18 09:49:45 -0800576 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700577 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700578 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700579}
580
581void Composer::setDisplayLayerStack(const sp<IBinder>& token,
582 uint32_t layerStack) {
583 Mutex::Autolock _l(mLock);
584 DisplayState& s(getDisplayStateLocked(token));
585 s.layerStack = layerStack;
586 s.what |= DisplayState::eLayerStackChanged;
587}
588
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700589void Composer::setDisplayProjection(const sp<IBinder>& token,
590 uint32_t orientation,
591 const Rect& layerStackRect,
592 const Rect& displayRect) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700593 Mutex::Autolock _l(mLock);
594 DisplayState& s(getDisplayStateLocked(token));
595 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700596 s.viewport = layerStackRect;
597 s.frame = displayRect;
598 s.what |= DisplayState::eDisplayProjectionChanged;
Jorim Jaggi092123c2016-04-13 01:40:35 +0000599 mForceSynchronous = true; // TODO: do we actually still need this?
Mathias Agopiane57f2922012-08-09 16:29:12 -0700600}
601
Michael Wright1f6078a2014-06-26 16:01:02 -0700602void Composer::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
603 Mutex::Autolock _l(mLock);
604 DisplayState& s(getDisplayStateLocked(token));
605 s.width = width;
606 s.height = height;
607 s.what |= DisplayState::eDisplaySizeChanged;
608}
609
Mathias Agopiane57f2922012-08-09 16:29:12 -0700610// ---------------------------------------------------------------------------
611
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612SurfaceComposerClient::SurfaceComposerClient()
Mathias Agopian698c0872011-06-28 19:09:31 -0700613 : mStatus(NO_INIT), mComposer(Composer::getInstance())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615}
616
Robert Carr1db73f62016-12-21 12:58:51 -0800617SurfaceComposerClient::SurfaceComposerClient(const sp<IGraphicBufferProducer>& root)
618 : mStatus(NO_INIT), mComposer(Composer::getInstance()), mParent(root)
619{
620}
621
Mathias Agopian698c0872011-06-28 19:09:31 -0700622void SurfaceComposerClient::onFirstRef() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700623 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopiand4784a32010-05-27 19:41:15 -0700624 if (sm != 0) {
Robert Carr1db73f62016-12-21 12:58:51 -0800625 auto rootProducer = mParent.promote();
626 sp<ISurfaceComposerClient> conn;
627 conn = (rootProducer != nullptr) ? sm->createScopedConnection(rootProducer) :
628 sm->createConnection();
Mathias Agopiand4784a32010-05-27 19:41:15 -0700629 if (conn != 0) {
630 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700631 mStatus = NO_ERROR;
632 }
633 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634}
635
Mathias Agopian698c0872011-06-28 19:09:31 -0700636SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -0700637 dispose();
638}
Mathias Agopiandd3423c2009-09-23 15:44:05 -0700639
Mathias Agopian698c0872011-06-28 19:09:31 -0700640status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 return mStatus;
642}
643
Mathias Agopian698c0872011-06-28 19:09:31 -0700644sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800645 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646}
647
Mathias Agopiand4784a32010-05-27 19:41:15 -0700648status_t SurfaceComposerClient::linkToComposerDeath(
649 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -0700650 void* cookie, uint32_t flags) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700651 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800652 return IInterface::asBinder(sm)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653}
654
Mathias Agopian698c0872011-06-28 19:09:31 -0700655void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -0700657 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700658 Mutex::Autolock _lm(mLock);
659 if (mClient != 0) {
Mathias Agopiand4784a32010-05-27 19:41:15 -0700660 client = mClient; // hold ref while lock is held
661 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662 }
Mathias Agopiand4784a32010-05-27 19:41:15 -0700663 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664}
665
Mathias Agopian698c0872011-06-28 19:09:31 -0700666sp<SurfaceControl> SurfaceComposerClient::createSurface(
Mathias Agopian698c0872011-06-28 19:09:31 -0700667 const String8& name,
Mathias Agopian698c0872011-06-28 19:09:31 -0700668 uint32_t w,
669 uint32_t h,
670 PixelFormat format,
Robert Carr1f0a16a2016-10-24 16:27:39 -0700671 uint32_t flags,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500672 SurfaceControl* parent,
673 uint32_t windowType,
674 uint32_t ownerUid)
Mathias Agopian698c0872011-06-28 19:09:31 -0700675{
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700676 sp<SurfaceControl> sur;
Mathias Agopian698c0872011-06-28 19:09:31 -0700677 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700678 sp<IBinder> handle;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700679 sp<IBinder> parentHandle;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700680 sp<IGraphicBufferProducer> gbp;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700681
682 if (parent != nullptr) {
683 parentHandle = parent->getHandle();
684 }
685 status_t err = mClient->createSurface(name, w, h, format, flags, parentHandle,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500686 windowType, ownerUid, &handle, &gbp);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700687 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
688 if (err == NO_ERROR) {
689 sur = new SurfaceControl(this, handle, gbp);
Mathias Agopian698c0872011-06-28 19:09:31 -0700690 }
691 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700692 return sur;
Mathias Agopian698c0872011-06-28 19:09:31 -0700693}
694
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700695sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
696 bool secure) {
697 return Composer::getInstance().createDisplay(displayName, secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700698}
699
Jesse Hall6c913be2013-08-08 12:15:49 -0700700void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
701 Composer::getInstance().destroyDisplay(display);
702}
703
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700704sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
705 return Composer::getInstance().getBuiltInDisplay(id);
706}
707
Mathias Agopianac9fa422013-02-11 16:40:36 -0800708status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700709 if (mStatus != NO_ERROR)
710 return mStatus;
711 status_t err = mClient->destroySurface(sid);
712 return err;
713}
714
Svetoslavd85084b2014-03-20 10:28:31 -0700715status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
716 if (mStatus != NO_ERROR) {
717 return mStatus;
718 }
719 return mClient->clearLayerFrameStats(token);
720}
721
722status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
723 FrameStats* outStats) const {
724 if (mStatus != NO_ERROR) {
725 return mStatus;
726 }
727 return mClient->getLayerFrameStats(token, outStats);
728}
729
Mathias Agopian698c0872011-06-28 19:09:31 -0700730inline Composer& SurfaceComposerClient::getComposer() {
731 return mComposer;
732}
733
734// ----------------------------------------------------------------------------
735
736void SurfaceComposerClient::openGlobalTransaction() {
Jeff Brownf3f7db62012-08-31 02:18:38 -0700737 Composer::openGlobalTransaction();
Mathias Agopian698c0872011-06-28 19:09:31 -0700738}
739
Jamie Gennis28378392011-10-12 17:39:00 -0700740void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
741 Composer::closeGlobalTransaction(synchronous);
Mathias Agopian698c0872011-06-28 19:09:31 -0700742}
743
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700744void SurfaceComposerClient::setAnimationTransaction() {
745 Composer::setAnimationTransaction();
746}
747
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700748status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
749 return Composer::enableVSyncInjections(enable);
750}
751
752status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
753 return Composer::injectVSync(when);
754}
755
Mathias Agopian698c0872011-06-28 19:09:31 -0700756// ----------------------------------------------------------------------------
757
Mathias Agopianac9fa422013-02-11 16:40:36 -0800758status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700759 return getComposer().setCrop(this, id, crop);
760}
761
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000762status_t SurfaceComposerClient::setFinalCrop(const sp<IBinder>& id,
763 const Rect& crop) {
764 return getComposer().setFinalCrop(this, id, crop);
765}
766
Mathias Agopianac9fa422013-02-11 16:40:36 -0800767status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700768 return getComposer().setPosition(this, id, x, y);
769}
770
Mathias Agopianac9fa422013-02-11 16:40:36 -0800771status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700772 return getComposer().setSize(this, id, w, h);
773}
774
Robert Carrae060832016-11-28 10:51:00 -0800775status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, int32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700776 return getComposer().setLayer(this, id, z);
777}
778
Robert Carrdb66e622017-04-10 16:55:57 -0700779status_t SurfaceComposerClient::setRelativeLayer(const sp<IBinder>& id,
780 const sp<IBinder>& relativeTo, int32_t z) {
781 return getComposer().setRelativeLayer(this, id, relativeTo, z);
782}
783
Mathias Agopianac9fa422013-02-11 16:40:36 -0800784status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700785 return getComposer().setFlags(this, id,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700786 layer_state_t::eLayerHidden,
787 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700788}
789
Mathias Agopianac9fa422013-02-11 16:40:36 -0800790status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700791 return getComposer().setFlags(this, id,
792 0,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700793 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700794}
795
Mathias Agopianac9fa422013-02-11 16:40:36 -0800796status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700797 uint32_t mask) {
798 return getComposer().setFlags(this, id, flags, mask);
799}
800
Mathias Agopianac9fa422013-02-11 16:40:36 -0800801status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700802 const Region& transparentRegion) {
803 return getComposer().setTransparentRegionHint(this, id, transparentRegion);
804}
805
Mathias Agopianac9fa422013-02-11 16:40:36 -0800806status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700807 return getComposer().setAlpha(this, id, alpha);
808}
809
Mathias Agopianac9fa422013-02-11 16:40:36 -0800810status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700811 return getComposer().setLayerStack(this, id, layerStack);
812}
813
Mathias Agopianac9fa422013-02-11 16:40:36 -0800814status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800815 float dtdy, float dsdy) {
816 return getComposer().setMatrix(this, id, dsdx, dtdx, dtdy, dsdy);
Mathias Agopian698c0872011-06-28 19:09:31 -0700817}
818
Dan Stoza7dde5992015-05-22 09:51:44 -0700819status_t SurfaceComposerClient::deferTransactionUntil(const sp<IBinder>& id,
820 const sp<IBinder>& handle, uint64_t frameNumber) {
821 return getComposer().deferTransactionUntil(this, id, handle, frameNumber);
822}
823
Robert Carr0d480722017-01-10 16:42:54 -0800824status_t SurfaceComposerClient::deferTransactionUntil(const sp<IBinder>& id,
825 const sp<Surface>& barrierSurface, uint64_t frameNumber) {
826 return getComposer().deferTransactionUntil(this, id, barrierSurface, frameNumber);
827}
828
Robert Carr1db73f62016-12-21 12:58:51 -0800829status_t SurfaceComposerClient::reparentChildren(const sp<IBinder>& id,
830 const sp<IBinder>& newParentHandle) {
831 return getComposer().reparentChildren(this, id, newParentHandle);
832}
833
Robert Carr9524cb32017-02-13 11:32:32 -0800834status_t SurfaceComposerClient::detachChildren(const sp<IBinder>& id) {
835 return getComposer().detachChildren(this, id);
836}
837
Robert Carrc3574f72016-03-24 12:19:32 -0700838status_t SurfaceComposerClient::setOverrideScalingMode(
839 const sp<IBinder>& id, int32_t overrideScalingMode) {
840 return getComposer().setOverrideScalingMode(
841 this, id, overrideScalingMode);
842}
843
Robert Carr99e27f02016-06-16 15:18:02 -0700844status_t SurfaceComposerClient::setGeometryAppliesWithResize(
Robert Carr82364e32016-05-15 11:27:47 -0700845 const sp<IBinder>& id) {
Robert Carr99e27f02016-06-16 15:18:02 -0700846 return getComposer().setGeometryAppliesWithResize(this, id);
Robert Carr82364e32016-05-15 11:27:47 -0700847}
848
Mathias Agopian698c0872011-06-28 19:09:31 -0700849// ----------------------------------------------------------------------------
850
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700851status_t SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
852 sp<IGraphicBufferProducer> bufferProducer) {
853 return Composer::getInstance().setDisplaySurface(token, bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700854}
855
856void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
857 uint32_t layerStack) {
858 Composer::getInstance().setDisplayLayerStack(token, layerStack);
859}
860
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700861void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
862 uint32_t orientation,
863 const Rect& layerStackRect,
864 const Rect& displayRect) {
865 Composer::getInstance().setDisplayProjection(token, orientation,
866 layerStackRect, displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700867}
868
Michael Wright1f6078a2014-06-26 16:01:02 -0700869void SurfaceComposerClient::setDisplaySize(const sp<IBinder>& token,
870 uint32_t width, uint32_t height) {
871 Composer::getInstance().setDisplaySize(token, width, height);
872}
873
Mathias Agopiane57f2922012-08-09 16:29:12 -0700874// ----------------------------------------------------------------------------
875
Dan Stoza7f7da322014-05-02 15:26:25 -0700876status_t SurfaceComposerClient::getDisplayConfigs(
877 const sp<IBinder>& display, Vector<DisplayInfo>* configs)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878{
Dan Stoza7f7da322014-05-02 15:26:25 -0700879 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
880}
881
882status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
883 DisplayInfo* info) {
884 Vector<DisplayInfo> configs;
885 status_t result = getDisplayConfigs(display, &configs);
886 if (result != NO_ERROR) {
887 return result;
888 }
889
890 int activeId = getActiveConfig(display);
891 if (activeId < 0) {
892 ALOGE("No active configuration found");
893 return NAME_NOT_FOUND;
894 }
895
Dan Stozad723bd72014-11-18 10:24:03 -0800896 *info = configs[static_cast<size_t>(activeId)];
Dan Stoza7f7da322014-05-02 15:26:25 -0700897 return NO_ERROR;
898}
899
900int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
901 return ComposerService::getComposerService()->getActiveConfig(display);
902}
903
904status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
905 return ComposerService::getComposerService()->setActiveConfig(display, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906}
907
Michael Wright28f24d02016-07-12 13:30:53 -0700908status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
909 Vector<android_color_mode_t>* outColorModes) {
910 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
911}
912
913android_color_mode_t SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
914 return ComposerService::getComposerService()->getActiveColorMode(display);
915}
916
917status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
918 android_color_mode_t colorMode) {
919 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
920}
921
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700922void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
923 int mode) {
924 ComposerService::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -0700925}
926
Svetoslavd85084b2014-03-20 10:28:31 -0700927status_t SurfaceComposerClient::clearAnimationFrameStats() {
928 return ComposerService::getComposerService()->clearAnimationFrameStats();
929}
930
931status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
932 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
933}
934
Dan Stozac4f471e2016-03-24 09:31:08 -0700935status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
936 HdrCapabilities* outCapabilities) {
937 return ComposerService::getComposerService()->getHdrCapabilities(display,
938 outCapabilities);
939}
940
Mathias Agopian698c0872011-06-28 19:09:31 -0700941// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800943status_t ScreenshotClient::capture(
944 const sp<IBinder>& display,
945 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -0700946 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -0800947 int32_t minLayerZ, int32_t maxLayerZ, bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800948 sp<ISurfaceComposer> s(ComposerService::getComposerService());
949 if (s == NULL) return NO_INIT;
Dan Stozac1879002014-05-22 15:59:05 -0700950 return s->captureScreen(display, producer, sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -0800951 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800952}
953
Robert Carr673134e2017-01-09 19:48:38 -0800954status_t ScreenshotClient::captureToBuffer(const sp<IBinder>& display,
955 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -0800956 int32_t minLayerZ, int32_t maxLayerZ, bool useIdentityTransform,
Robert Carr673134e2017-01-09 19:48:38 -0800957 uint32_t rotation,
958 sp<GraphicBuffer>* outBuffer) {
959 sp<ISurfaceComposer> s(ComposerService::getComposerService());
960 if (s == NULL) return NO_INIT;
961
962 sp<IGraphicBufferConsumer> gbpConsumer;
963 sp<IGraphicBufferProducer> producer;
964 BufferQueue::createBufferQueue(&producer, &gbpConsumer);
965 sp<BufferItemConsumer> consumer(new BufferItemConsumer(gbpConsumer,
966 GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_NEVER,
967 1, true));
968
969 status_t ret = s->captureScreen(display, producer, sourceCrop, reqWidth, reqHeight,
970 minLayerZ, maxLayerZ, useIdentityTransform,
971 static_cast<ISurfaceComposer::Rotation>(rotation));
972 if (ret != NO_ERROR) {
973 return ret;
974 }
975 BufferItem b;
976 consumer->acquireBuffer(&b, 0, true);
977 *outBuffer = b.mGraphicBuffer;
978 return ret;
979}
980
Mathias Agopian74c40c02010-09-29 13:02:36 -0700981ScreenshotClient::ScreenshotClient()
Mathias Agopianabe815d2013-03-19 22:22:21 -0700982 : mHaveBuffer(false) {
983 memset(&mBuffer, 0, sizeof(mBuffer));
Mathias Agopian74c40c02010-09-29 13:02:36 -0700984}
985
Mathias Agopian8000d062013-03-26 18:15:35 -0700986ScreenshotClient::~ScreenshotClient() {
987 ScreenshotClient::release();
988}
989
Mathias Agopianabe815d2013-03-19 22:22:21 -0700990sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
991 if (mCpuConsumer == NULL) {
Dan Stoza6d5a7bb2014-03-13 11:39:09 -0700992 sp<IGraphicBufferConsumer> consumer;
993 BufferQueue::createBufferQueue(&mProducer, &consumer);
994 mCpuConsumer = new CpuConsumer(consumer, 1);
Mathias Agopianabe815d2013-03-19 22:22:21 -0700995 mCpuConsumer->setName(String8("ScreenshotClient"));
996 }
997 return mCpuConsumer;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800998}
999
Jeff Brown9d4e3d22012-08-24 20:00:51 -07001000status_t ScreenshotClient::update(const sp<IBinder>& display,
Dan Stozac1879002014-05-22 15:59:05 -07001001 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -08001002 int32_t minLayerZ, int32_t maxLayerZ,
Riley Andrewsd15ef272014-09-04 16:19:44 -07001003 bool useIdentityTransform, uint32_t rotation) {
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08001004 sp<ISurfaceComposer> s(ComposerService::getComposerService());
1005 if (s == NULL) return NO_INIT;
Mathias Agopianabe815d2013-03-19 22:22:21 -07001006 sp<CpuConsumer> cpuConsumer = getCpuConsumer();
1007
1008 if (mHaveBuffer) {
1009 mCpuConsumer->unlockBuffer(mBuffer);
1010 memset(&mBuffer, 0, sizeof(mBuffer));
1011 mHaveBuffer = false;
1012 }
1013
Dan Stozac1879002014-05-22 15:59:05 -07001014 status_t err = s->captureScreen(display, mProducer, sourceCrop,
Riley Andrewsd15ef272014-09-04 16:19:44 -07001015 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform,
1016 static_cast<ISurfaceComposer::Rotation>(rotation));
Mathias Agopianabe815d2013-03-19 22:22:21 -07001017
1018 if (err == NO_ERROR) {
1019 err = mCpuConsumer->lockNextBuffer(&mBuffer);
1020 if (err == NO_ERROR) {
1021 mHaveBuffer = true;
1022 }
1023 }
1024 return err;
1025}
1026
Riley Andrewsd15ef272014-09-04 16:19:44 -07001027status_t ScreenshotClient::update(const sp<IBinder>& display,
1028 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -08001029 int32_t minLayerZ, int32_t maxLayerZ,
Riley Andrewsd15ef272014-09-04 16:19:44 -07001030 bool useIdentityTransform) {
1031
1032 return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
1033 minLayerZ, maxLayerZ, useIdentityTransform, ISurfaceComposer::eRotateNone);
1034}
1035
Dan Stozac1879002014-05-22 15:59:05 -07001036status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -08001037 bool useIdentityTransform) {
Robert Carrae060832016-11-28 10:51:00 -08001038 return ScreenshotClient::update(display, sourceCrop, 0, 0,
1039 INT32_MIN, INT32_MAX,
Riley Andrewsd15ef272014-09-04 16:19:44 -07001040 useIdentityTransform, ISurfaceComposer::eRotateNone);
Mathias Agopianabe815d2013-03-19 22:22:21 -07001041}
1042
Dan Stozac1879002014-05-22 15:59:05 -07001043status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -08001044 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform) {
Dan Stozac1879002014-05-22 15:59:05 -07001045 return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
Robert Carrae060832016-11-28 10:51:00 -08001046 INT32_MIN, INT32_MAX,
1047 useIdentityTransform, ISurfaceComposer::eRotateNone);
Mathias Agopian74c40c02010-09-29 13:02:36 -07001048}
1049
1050void ScreenshotClient::release() {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001051 if (mHaveBuffer) {
1052 mCpuConsumer->unlockBuffer(mBuffer);
1053 memset(&mBuffer, 0, sizeof(mBuffer));
1054 mHaveBuffer = false;
1055 }
1056 mCpuConsumer.clear();
Mathias Agopian74c40c02010-09-29 13:02:36 -07001057}
1058
1059void const* ScreenshotClient::getPixels() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001060 return mBuffer.data;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001061}
1062
1063uint32_t ScreenshotClient::getWidth() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001064 return mBuffer.width;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001065}
1066
1067uint32_t ScreenshotClient::getHeight() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001068 return mBuffer.height;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001069}
1070
1071PixelFormat ScreenshotClient::getFormat() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001072 return mBuffer.format;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001073}
1074
1075uint32_t ScreenshotClient::getStride() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001076 return mBuffer.stride;
Mathias Agopian74c40c02010-09-29 13:02:36 -07001077}
1078
1079size_t ScreenshotClient::getSize() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -07001080 return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
Mathias Agopian74c40c02010-09-29 13:02:36 -07001081}
1082
1083// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084}; // namespace android