blob: 1620eb2b794e76649ff1d3aa440ad83546e54c96 [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 Agopian9cce3252010-02-09 17:46:37 -080029#include <binder/IMemory.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <binder/IServiceManager.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080031
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <ui/DisplayInfo.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
Mathias Agopianabe815d2013-03-19 22:22:21 -070034#include <gui/CpuConsumer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080035#include <gui/IGraphicBufferProducer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080036#include <gui/ISurfaceComposer.h>
37#include <gui/ISurfaceComposerClient.h>
38#include <gui/SurfaceComposerClient.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
Mathias Agopian41f673c2011-11-17 17:48:35 -080040#include <private/gui/ComposerService.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080041#include <private/gui/LayerState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044// ---------------------------------------------------------------------------
45
Mathias Agopian7e27f052010-05-28 14:22:23 -070046ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
47
Mathias Agopianb7e930d2010-06-01 15:12:58 -070048ComposerService::ComposerService()
49: Singleton<ComposerService>() {
Andy McFadden6652b3e2012-09-06 18:45:56 -070050 Mutex::Autolock _l(mLock);
51 connectLocked();
52}
53
54void ComposerService::connectLocked() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -070055 const String16 name("SurfaceFlinger");
56 while (getService(name, &mComposerService) != NO_ERROR) {
57 usleep(250000);
58 }
Andy McFadden6652b3e2012-09-06 18:45:56 -070059 assert(mComposerService != NULL);
60
61 // Create the death listener.
62 class DeathObserver : public IBinder::DeathRecipient {
63 ComposerService& mComposerService;
64 virtual void binderDied(const wp<IBinder>& who) {
65 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
66 who.unsafe_get());
67 mComposerService.composerServiceDied();
68 }
69 public:
70 DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
71 };
72
73 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -080074 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
Mathias Agopianb7e930d2010-06-01 15:12:58 -070075}
76
Andy McFadden6652b3e2012-09-06 18:45:56 -070077/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
78 ComposerService& instance = ComposerService::getInstance();
79 Mutex::Autolock _l(instance.mLock);
80 if (instance.mComposerService == NULL) {
81 ComposerService::getInstance().connectLocked();
82 assert(instance.mComposerService != NULL);
83 ALOGD("ComposerService reconnected");
84 }
85 return instance.mComposerService;
86}
87
88void ComposerService::composerServiceDied()
89{
90 Mutex::Autolock _l(mLock);
91 mComposerService = NULL;
92 mDeathObserver = NULL;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070093}
94
Mathias Agopian7e27f052010-05-28 14:22:23 -070095// ---------------------------------------------------------------------------
96
Mathias Agopian698c0872011-06-28 19:09:31 -070097static inline
Mathias Agopiane57f2922012-08-09 16:29:12 -070098int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
Mathias Agopian698c0872011-06-28 19:09:31 -070099 if (lhs.client < rhs.client) return -1;
100 if (lhs.client > rhs.client) return 1;
101 if (lhs.state.surface < rhs.state.surface) return -1;
102 if (lhs.state.surface > rhs.state.surface) return 1;
103 return 0;
104}
105
Mathias Agopiane57f2922012-08-09 16:29:12 -0700106static inline
107int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
108 return compare_type(lhs.token, rhs.token);
109}
110
Mathias Agopian7e27f052010-05-28 14:22:23 -0700111class Composer : public Singleton<Composer>
112{
Mathias Agopiand4784a32010-05-27 19:41:15 -0700113 friend class Singleton<Composer>;
114
Mathias Agopian698c0872011-06-28 19:09:31 -0700115 mutable Mutex mLock;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700116 SortedVector<ComposerState> mComposerStates;
117 SortedVector<DisplayState > mDisplayStates;
Jamie Gennis28378392011-10-12 17:39:00 -0700118 uint32_t mForceSynchronous;
Jeff Brownf3f7db62012-08-31 02:18:38 -0700119 uint32_t mTransactionNestCount;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700120 bool mAnimation;
Mathias Agopian698c0872011-06-28 19:09:31 -0700121
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700122 Composer() : Singleton<Composer>(),
Jeff Brownf3f7db62012-08-31 02:18:38 -0700123 mForceSynchronous(0), mTransactionNestCount(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700124 mAnimation(false)
Jamie Gennis28378392011-10-12 17:39:00 -0700125 { }
Mathias Agopian698c0872011-06-28 19:09:31 -0700126
Jeff Brownf3f7db62012-08-31 02:18:38 -0700127 void openGlobalTransactionImpl();
Jamie Gennis28378392011-10-12 17:39:00 -0700128 void closeGlobalTransactionImpl(bool synchronous);
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700129 void setAnimationTransactionImpl();
Mathias Agopian698c0872011-06-28 19:09:31 -0700130
131 layer_state_t* getLayerStateLocked(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800132 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id);
Mathias Agopian698c0872011-06-28 19:09:31 -0700133
Mathias Agopiane57f2922012-08-09 16:29:12 -0700134 DisplayState& getDisplayStateLocked(const sp<IBinder>& token);
135
Mathias Agopiand4784a32010-05-27 19:41:15 -0700136public:
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700137 sp<IBinder> createDisplay(const String8& displayName, bool secure);
Jesse Hall6c913be2013-08-08 12:15:49 -0700138 void destroyDisplay(const sp<IBinder>& display);
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700139 sp<IBinder> getBuiltInDisplay(int32_t id);
Mathias Agopian698c0872011-06-28 19:09:31 -0700140
Mathias Agopianac9fa422013-02-11 16:40:36 -0800141 status_t setPosition(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700142 float x, float y);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800143 status_t setSize(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700144 uint32_t w, uint32_t h);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800145 status_t setLayer(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Dan Stozad723bd72014-11-18 10:24:03 -0800146 uint32_t z);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800147 status_t setFlags(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700148 uint32_t flags, uint32_t mask);
149 status_t setTransparentRegionHint(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800150 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700151 const Region& transparentRegion);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800152 status_t setAlpha(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700153 float alpha);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800154 status_t setMatrix(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700155 float dsdx, float dtdx, float dsdy, float dtdy);
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700156 status_t setOrientation(int orientation);
Mathias Agopianac9fa422013-02-11 16:40:36 -0800157 status_t setCrop(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700158 const Rect& crop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000159 status_t setFinalCrop(const sp<SurfaceComposerClient>& client,
160 const sp<IBinder>& id, const Rect& crop);
Mathias Agopian87855782012-07-24 21:41:09 -0700161 status_t setLayerStack(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800162 const sp<IBinder>& id, uint32_t layerStack);
Dan Stoza7dde5992015-05-22 09:51:44 -0700163 status_t deferTransactionUntil(const sp<SurfaceComposerClient>& client,
164 const sp<IBinder>& id, const sp<IBinder>& handle,
165 uint64_t frameNumber);
Robert Carrc3574f72016-03-24 12:19:32 -0700166 status_t setOverrideScalingMode(const sp<SurfaceComposerClient>& client,
167 const sp<IBinder>& id, int32_t overrideScalingMode);
Robert Carr99e27f02016-06-16 15:18:02 -0700168 status_t setGeometryAppliesWithResize(const sp<SurfaceComposerClient>& client,
Robert Carr82364e32016-05-15 11:27:47 -0700169 const sp<IBinder>& id);
Mathias Agopian698c0872011-06-28 19:09:31 -0700170
Andy McFadden2adaf042012-12-18 09:49:45 -0800171 void setDisplaySurface(const sp<IBinder>& token,
172 const sp<IGraphicBufferProducer>& bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700173 void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700174 void setDisplayProjection(const sp<IBinder>& token,
175 uint32_t orientation,
176 const Rect& layerStackRect,
177 const Rect& displayRect);
Michael Wright1f6078a2014-06-26 16:01:02 -0700178 void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700179
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700180 static void setAnimationTransaction() {
181 Composer::getInstance().setAnimationTransactionImpl();
182 }
183
Jeff Brownf3f7db62012-08-31 02:18:38 -0700184 static void openGlobalTransaction() {
185 Composer::getInstance().openGlobalTransactionImpl();
186 }
187
Jamie Gennis28378392011-10-12 17:39:00 -0700188 static void closeGlobalTransaction(bool synchronous) {
189 Composer::getInstance().closeGlobalTransactionImpl(synchronous);
Mathias Agopiand4784a32010-05-27 19:41:15 -0700190 }
191};
192
193ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
194
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195// ---------------------------------------------------------------------------
196
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700197sp<IBinder> Composer::createDisplay(const String8& displayName, bool secure) {
198 return ComposerService::getComposerService()->createDisplay(displayName,
199 secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700200}
201
Jesse Hall6c913be2013-08-08 12:15:49 -0700202void Composer::destroyDisplay(const sp<IBinder>& display) {
203 return ComposerService::getComposerService()->destroyDisplay(display);
204}
205
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700206sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
207 return ComposerService::getComposerService()->getBuiltInDisplay(id);
208}
209
Jeff Brownf3f7db62012-08-31 02:18:38 -0700210void Composer::openGlobalTransactionImpl() {
211 { // scope for the lock
212 Mutex::Autolock _l(mLock);
213 mTransactionNestCount += 1;
214 }
215}
216
Jamie Gennis28378392011-10-12 17:39:00 -0700217void Composer::closeGlobalTransactionImpl(bool synchronous) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700218 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopian698c0872011-06-28 19:09:31 -0700219
220 Vector<ComposerState> transaction;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700221 Vector<DisplayState> displayTransaction;
Jamie Gennis28378392011-10-12 17:39:00 -0700222 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -0700223
224 { // scope for the lock
225 Mutex::Autolock _l(mLock);
Jeff Brownf3f7db62012-08-31 02:18:38 -0700226 mForceSynchronous |= synchronous;
227 if (!mTransactionNestCount) {
228 ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
229 "call to openGlobalTransaction().");
230 } else if (--mTransactionNestCount) {
231 return;
232 }
233
Mathias Agopiane57f2922012-08-09 16:29:12 -0700234 transaction = mComposerStates;
235 mComposerStates.clear();
Jamie Gennisb8d69a52011-10-10 15:48:06 -0700236
Mathias Agopiane57f2922012-08-09 16:29:12 -0700237 displayTransaction = mDisplayStates;
238 mDisplayStates.clear();
Jamie Gennis28378392011-10-12 17:39:00 -0700239
Jeff Brownf3f7db62012-08-31 02:18:38 -0700240 if (mForceSynchronous) {
Jamie Gennis28378392011-10-12 17:39:00 -0700241 flags |= ISurfaceComposer::eSynchronous;
242 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700243 if (mAnimation) {
244 flags |= ISurfaceComposer::eAnimation;
245 }
246
Jamie Gennis28378392011-10-12 17:39:00 -0700247 mForceSynchronous = false;
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700248 mAnimation = false;
Mathias Agopian698c0872011-06-28 19:09:31 -0700249 }
250
Mathias Agopian8b33f032012-07-24 20:43:54 -0700251 sm->setTransactionState(transaction, displayTransaction, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252}
253
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700254void Composer::setAnimationTransactionImpl() {
255 Mutex::Autolock _l(mLock);
256 mAnimation = true;
257}
258
Mathias Agopian698c0872011-06-28 19:09:31 -0700259layer_state_t* Composer::getLayerStateLocked(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800260 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700261
262 ComposerState s;
263 s.client = client->mClient;
264 s.state.surface = id;
265
Mathias Agopiane57f2922012-08-09 16:29:12 -0700266 ssize_t index = mComposerStates.indexOf(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700267 if (index < 0) {
268 // we don't have it, add an initialized layer_state to our list
Mathias Agopiane57f2922012-08-09 16:29:12 -0700269 index = mComposerStates.add(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700270 }
271
Mathias Agopiane57f2922012-08-09 16:29:12 -0700272 ComposerState* const out = mComposerStates.editArray();
Mathias Agopian698c0872011-06-28 19:09:31 -0700273 return &(out[index].state);
274}
275
276status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800277 const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700278 Mutex::Autolock _l(mLock);
279 layer_state_t* s = getLayerStateLocked(client, id);
280 if (!s)
281 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700282 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700283 s->x = x;
284 s->y = y;
285 return NO_ERROR;
286}
287
288status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800289 const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700290 Mutex::Autolock _l(mLock);
291 layer_state_t* s = getLayerStateLocked(client, id);
292 if (!s)
293 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700294 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700295 s->w = w;
296 s->h = h;
Jamie Gennis28378392011-10-12 17:39:00 -0700297
Jorim Jaggi092123c2016-04-13 01:40:35 +0000298 // Resizing a surface makes the transaction synchronous.
299 mForceSynchronous = true;
300
Mathias Agopian698c0872011-06-28 19:09:31 -0700301 return NO_ERROR;
302}
303
304status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
Dan Stozad723bd72014-11-18 10:24:03 -0800305 const sp<IBinder>& id, uint32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700306 Mutex::Autolock _l(mLock);
307 layer_state_t* s = getLayerStateLocked(client, id);
308 if (!s)
309 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700310 s->what |= layer_state_t::eLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700311 s->z = z;
312 return NO_ERROR;
313}
314
315status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800316 const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700317 uint32_t mask) {
318 Mutex::Autolock _l(mLock);
319 layer_state_t* s = getLayerStateLocked(client, id);
320 if (!s)
321 return BAD_INDEX;
Pablo Ceballos53390e12015-08-04 11:25:59 -0700322 if ((mask & layer_state_t::eLayerOpaque) ||
323 (mask & layer_state_t::eLayerHidden) ||
324 (mask & layer_state_t::eLayerSecure)) {
Dan Stoza23116082015-06-18 14:58:39 -0700325 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-29 17:17:11 -0800326 }
Mathias Agopian698c0872011-06-28 19:09:31 -0700327 s->flags &= ~mask;
328 s->flags |= (flags & mask);
329 s->mask |= mask;
330 return NO_ERROR;
331}
332
333status_t Composer::setTransparentRegionHint(
Mathias Agopianac9fa422013-02-11 16:40:36 -0800334 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700335 const Region& transparentRegion) {
336 Mutex::Autolock _l(mLock);
337 layer_state_t* s = getLayerStateLocked(client, id);
338 if (!s)
339 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700340 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700341 s->transparentRegion = transparentRegion;
342 return NO_ERROR;
343}
344
345status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800346 const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700347 Mutex::Autolock _l(mLock);
348 layer_state_t* s = getLayerStateLocked(client, id);
349 if (!s)
350 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700351 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700352 s->alpha = alpha;
353 return NO_ERROR;
354}
355
Mathias Agopian87855782012-07-24 21:41:09 -0700356status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800357 const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700358 Mutex::Autolock _l(mLock);
359 layer_state_t* s = getLayerStateLocked(client, id);
360 if (!s)
361 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700362 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700363 s->layerStack = layerStack;
364 return NO_ERROR;
365}
366
Mathias Agopian698c0872011-06-28 19:09:31 -0700367status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800368 const sp<IBinder>& id, float dsdx, float dtdx,
Mathias Agopian698c0872011-06-28 19:09:31 -0700369 float dsdy, float dtdy) {
370 Mutex::Autolock _l(mLock);
371 layer_state_t* s = getLayerStateLocked(client, id);
372 if (!s)
373 return BAD_INDEX;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700374 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700375 layer_state_t::matrix22_t matrix;
376 matrix.dsdx = dsdx;
377 matrix.dtdx = dtdx;
378 matrix.dsdy = dsdy;
379 matrix.dtdy = dtdy;
380 s->matrix = matrix;
381 return NO_ERROR;
382}
383
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700384status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -0800385 const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700386 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::eCropChanged;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700391 s->crop = crop;
392 return NO_ERROR;
393}
394
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000395status_t Composer::setFinalCrop(const sp<SurfaceComposerClient>& client,
396 const sp<IBinder>& id, const Rect& crop) {
397 Mutex::Autolock _l(mLock);
398 layer_state_t* s = getLayerStateLocked(client, id);
399 if (!s) {
400 return BAD_INDEX;
401 }
402 s->what |= layer_state_t::eFinalCropChanged;
403 s->finalCrop = crop;
404 return NO_ERROR;
405}
406
Dan Stoza7dde5992015-05-22 09:51:44 -0700407status_t Composer::deferTransactionUntil(
408 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
409 const sp<IBinder>& handle, uint64_t frameNumber) {
410 Mutex::Autolock lock(mLock);
411 layer_state_t* s = getLayerStateLocked(client, id);
412 if (!s) {
413 return BAD_INDEX;
414 }
415 s->what |= layer_state_t::eDeferTransaction;
416 s->handle = handle;
417 s->frameNumber = frameNumber;
418 return NO_ERROR;
419}
420
Robert Carrc3574f72016-03-24 12:19:32 -0700421status_t Composer::setOverrideScalingMode(
422 const sp<SurfaceComposerClient>& client,
423 const sp<IBinder>& id, int32_t overrideScalingMode) {
424 Mutex::Autolock lock(mLock);
425 layer_state_t* s = getLayerStateLocked(client, id);
426 if (!s) {
427 return BAD_INDEX;
428 }
429
430 switch (overrideScalingMode) {
431 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
432 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
433 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
434 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
435 case -1:
436 break;
437 default:
438 ALOGE("unknown scaling mode: %d",
439 overrideScalingMode);
440 return BAD_VALUE;
441 }
442
443 s->what |= layer_state_t::eOverrideScalingModeChanged;
444 s->overrideScalingMode = overrideScalingMode;
445 return NO_ERROR;
446}
447
Robert Carr99e27f02016-06-16 15:18:02 -0700448status_t Composer::setGeometryAppliesWithResize(
Robert Carr82364e32016-05-15 11:27:47 -0700449 const sp<SurfaceComposerClient>& client,
450 const sp<IBinder>& id) {
451 Mutex::Autolock lock(mLock);
452 layer_state_t* s = getLayerStateLocked(client, id);
453 if (!s) {
454 return BAD_INDEX;
455 }
Robert Carr99e27f02016-06-16 15:18:02 -0700456 s->what |= layer_state_t::eGeometryAppliesWithResize;
Robert Carr82364e32016-05-15 11:27:47 -0700457 return NO_ERROR;
458}
459
Mathias Agopian698c0872011-06-28 19:09:31 -0700460// ---------------------------------------------------------------------------
461
Mathias Agopiane57f2922012-08-09 16:29:12 -0700462DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
463 DisplayState s;
464 s.token = token;
465 ssize_t index = mDisplayStates.indexOf(s);
466 if (index < 0) {
467 // we don't have it, add an initialized layer_state to our list
468 s.what = 0;
469 index = mDisplayStates.add(s);
470 }
Dan Stozad723bd72014-11-18 10:24:03 -0800471 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700472}
473
474void Composer::setDisplaySurface(const sp<IBinder>& token,
Andy McFadden2adaf042012-12-18 09:49:45 -0800475 const sp<IGraphicBufferProducer>& bufferProducer) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700476 Mutex::Autolock _l(mLock);
477 DisplayState& s(getDisplayStateLocked(token));
Andy McFadden2adaf042012-12-18 09:49:45 -0800478 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700479 s.what |= DisplayState::eSurfaceChanged;
480}
481
482void Composer::setDisplayLayerStack(const sp<IBinder>& token,
483 uint32_t layerStack) {
484 Mutex::Autolock _l(mLock);
485 DisplayState& s(getDisplayStateLocked(token));
486 s.layerStack = layerStack;
487 s.what |= DisplayState::eLayerStackChanged;
488}
489
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700490void Composer::setDisplayProjection(const sp<IBinder>& token,
491 uint32_t orientation,
492 const Rect& layerStackRect,
493 const Rect& displayRect) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700494 Mutex::Autolock _l(mLock);
495 DisplayState& s(getDisplayStateLocked(token));
496 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700497 s.viewport = layerStackRect;
498 s.frame = displayRect;
499 s.what |= DisplayState::eDisplayProjectionChanged;
Jorim Jaggi092123c2016-04-13 01:40:35 +0000500 mForceSynchronous = true; // TODO: do we actually still need this?
Mathias Agopiane57f2922012-08-09 16:29:12 -0700501}
502
Michael Wright1f6078a2014-06-26 16:01:02 -0700503void Composer::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
504 Mutex::Autolock _l(mLock);
505 DisplayState& s(getDisplayStateLocked(token));
506 s.width = width;
507 s.height = height;
508 s.what |= DisplayState::eDisplaySizeChanged;
509}
510
Mathias Agopiane57f2922012-08-09 16:29:12 -0700511// ---------------------------------------------------------------------------
512
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513SurfaceComposerClient::SurfaceComposerClient()
Mathias Agopian698c0872011-06-28 19:09:31 -0700514 : mStatus(NO_INIT), mComposer(Composer::getInstance())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516}
517
Mathias Agopian698c0872011-06-28 19:09:31 -0700518void SurfaceComposerClient::onFirstRef() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700519 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Mathias Agopiand4784a32010-05-27 19:41:15 -0700520 if (sm != 0) {
Mathias Agopian7e27f052010-05-28 14:22:23 -0700521 sp<ISurfaceComposerClient> conn = sm->createConnection();
Mathias Agopiand4784a32010-05-27 19:41:15 -0700522 if (conn != 0) {
523 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700524 mStatus = NO_ERROR;
525 }
526 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527}
528
Mathias Agopian698c0872011-06-28 19:09:31 -0700529SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -0700530 dispose();
531}
Mathias Agopiandd3423c2009-09-23 15:44:05 -0700532
Mathias Agopian698c0872011-06-28 19:09:31 -0700533status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800534 return mStatus;
535}
536
Mathias Agopian698c0872011-06-28 19:09:31 -0700537sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800538 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800539}
540
Mathias Agopiand4784a32010-05-27 19:41:15 -0700541status_t SurfaceComposerClient::linkToComposerDeath(
542 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -0700543 void* cookie, uint32_t flags) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700544 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800545 return IInterface::asBinder(sm)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546}
547
Mathias Agopian698c0872011-06-28 19:09:31 -0700548void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -0700550 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700551 Mutex::Autolock _lm(mLock);
552 if (mClient != 0) {
Mathias Agopiand4784a32010-05-27 19:41:15 -0700553 client = mClient; // hold ref while lock is held
554 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555 }
Mathias Agopiand4784a32010-05-27 19:41:15 -0700556 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557}
558
Mathias Agopian698c0872011-06-28 19:09:31 -0700559sp<SurfaceControl> SurfaceComposerClient::createSurface(
Mathias Agopian698c0872011-06-28 19:09:31 -0700560 const String8& name,
Mathias Agopian698c0872011-06-28 19:09:31 -0700561 uint32_t w,
562 uint32_t h,
563 PixelFormat format,
564 uint32_t flags)
565{
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700566 sp<SurfaceControl> sur;
Mathias Agopian698c0872011-06-28 19:09:31 -0700567 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700568 sp<IBinder> handle;
569 sp<IGraphicBufferProducer> gbp;
570 status_t err = mClient->createSurface(name, w, h, format, flags,
571 &handle, &gbp);
572 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
573 if (err == NO_ERROR) {
574 sur = new SurfaceControl(this, handle, gbp);
Mathias Agopian698c0872011-06-28 19:09:31 -0700575 }
576 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700577 return sur;
Mathias Agopian698c0872011-06-28 19:09:31 -0700578}
579
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700580sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
581 bool secure) {
582 return Composer::getInstance().createDisplay(displayName, secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700583}
584
Jesse Hall6c913be2013-08-08 12:15:49 -0700585void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
586 Composer::getInstance().destroyDisplay(display);
587}
588
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700589sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
590 return Composer::getInstance().getBuiltInDisplay(id);
591}
592
Mathias Agopianac9fa422013-02-11 16:40:36 -0800593status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700594 if (mStatus != NO_ERROR)
595 return mStatus;
596 status_t err = mClient->destroySurface(sid);
597 return err;
598}
599
Svetoslavd85084b2014-03-20 10:28:31 -0700600status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
601 if (mStatus != NO_ERROR) {
602 return mStatus;
603 }
604 return mClient->clearLayerFrameStats(token);
605}
606
607status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
608 FrameStats* outStats) const {
609 if (mStatus != NO_ERROR) {
610 return mStatus;
611 }
612 return mClient->getLayerFrameStats(token, outStats);
613}
614
Mathias Agopian698c0872011-06-28 19:09:31 -0700615inline Composer& SurfaceComposerClient::getComposer() {
616 return mComposer;
617}
618
619// ----------------------------------------------------------------------------
620
621void SurfaceComposerClient::openGlobalTransaction() {
Jeff Brownf3f7db62012-08-31 02:18:38 -0700622 Composer::openGlobalTransaction();
Mathias Agopian698c0872011-06-28 19:09:31 -0700623}
624
Jamie Gennis28378392011-10-12 17:39:00 -0700625void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
626 Composer::closeGlobalTransaction(synchronous);
Mathias Agopian698c0872011-06-28 19:09:31 -0700627}
628
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700629void SurfaceComposerClient::setAnimationTransaction() {
630 Composer::setAnimationTransaction();
631}
632
Mathias Agopian698c0872011-06-28 19:09:31 -0700633// ----------------------------------------------------------------------------
634
Mathias Agopianac9fa422013-02-11 16:40:36 -0800635status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700636 return getComposer().setCrop(this, id, crop);
637}
638
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000639status_t SurfaceComposerClient::setFinalCrop(const sp<IBinder>& id,
640 const Rect& crop) {
641 return getComposer().setFinalCrop(this, id, crop);
642}
643
Mathias Agopianac9fa422013-02-11 16:40:36 -0800644status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700645 return getComposer().setPosition(this, id, x, y);
646}
647
Mathias Agopianac9fa422013-02-11 16:40:36 -0800648status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700649 return getComposer().setSize(this, id, w, h);
650}
651
Dan Stozad723bd72014-11-18 10:24:03 -0800652status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, uint32_t z) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700653 return getComposer().setLayer(this, id, z);
654}
655
Mathias Agopianac9fa422013-02-11 16:40:36 -0800656status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700657 return getComposer().setFlags(this, id,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700658 layer_state_t::eLayerHidden,
659 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700660}
661
Mathias Agopianac9fa422013-02-11 16:40:36 -0800662status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700663 return getComposer().setFlags(this, id,
664 0,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700665 layer_state_t::eLayerHidden);
Mathias Agopian698c0872011-06-28 19:09:31 -0700666}
667
Mathias Agopianac9fa422013-02-11 16:40:36 -0800668status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700669 uint32_t mask) {
670 return getComposer().setFlags(this, id, flags, mask);
671}
672
Mathias Agopianac9fa422013-02-11 16:40:36 -0800673status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
Mathias Agopian698c0872011-06-28 19:09:31 -0700674 const Region& transparentRegion) {
675 return getComposer().setTransparentRegionHint(this, id, transparentRegion);
676}
677
Mathias Agopianac9fa422013-02-11 16:40:36 -0800678status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700679 return getComposer().setAlpha(this, id, alpha);
680}
681
Mathias Agopianac9fa422013-02-11 16:40:36 -0800682status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
Mathias Agopian87855782012-07-24 21:41:09 -0700683 return getComposer().setLayerStack(this, id, layerStack);
684}
685
Mathias Agopianac9fa422013-02-11 16:40:36 -0800686status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
Mathias Agopian698c0872011-06-28 19:09:31 -0700687 float dsdy, float dtdy) {
688 return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
689}
690
Dan Stoza7dde5992015-05-22 09:51:44 -0700691status_t SurfaceComposerClient::deferTransactionUntil(const sp<IBinder>& id,
692 const sp<IBinder>& handle, uint64_t frameNumber) {
693 return getComposer().deferTransactionUntil(this, id, handle, frameNumber);
694}
695
Robert Carrc3574f72016-03-24 12:19:32 -0700696status_t SurfaceComposerClient::setOverrideScalingMode(
697 const sp<IBinder>& id, int32_t overrideScalingMode) {
698 return getComposer().setOverrideScalingMode(
699 this, id, overrideScalingMode);
700}
701
Robert Carr99e27f02016-06-16 15:18:02 -0700702status_t SurfaceComposerClient::setGeometryAppliesWithResize(
Robert Carr82364e32016-05-15 11:27:47 -0700703 const sp<IBinder>& id) {
Robert Carr99e27f02016-06-16 15:18:02 -0700704 return getComposer().setGeometryAppliesWithResize(this, id);
Robert Carr82364e32016-05-15 11:27:47 -0700705}
706
Mathias Agopian698c0872011-06-28 19:09:31 -0700707// ----------------------------------------------------------------------------
708
Mathias Agopiane57f2922012-08-09 16:29:12 -0700709void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
Andy McFadden2adaf042012-12-18 09:49:45 -0800710 const sp<IGraphicBufferProducer>& bufferProducer) {
711 Composer::getInstance().setDisplaySurface(token, bufferProducer);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700712}
713
714void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
715 uint32_t layerStack) {
716 Composer::getInstance().setDisplayLayerStack(token, layerStack);
717}
718
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700719void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
720 uint32_t orientation,
721 const Rect& layerStackRect,
722 const Rect& displayRect) {
723 Composer::getInstance().setDisplayProjection(token, orientation,
724 layerStackRect, displayRect);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700725}
726
Michael Wright1f6078a2014-06-26 16:01:02 -0700727void SurfaceComposerClient::setDisplaySize(const sp<IBinder>& token,
728 uint32_t width, uint32_t height) {
729 Composer::getInstance().setDisplaySize(token, width, height);
730}
731
Mathias Agopiane57f2922012-08-09 16:29:12 -0700732// ----------------------------------------------------------------------------
733
Dan Stoza7f7da322014-05-02 15:26:25 -0700734status_t SurfaceComposerClient::getDisplayConfigs(
735 const sp<IBinder>& display, Vector<DisplayInfo>* configs)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800736{
Dan Stoza7f7da322014-05-02 15:26:25 -0700737 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
738}
739
740status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
741 DisplayInfo* info) {
742 Vector<DisplayInfo> configs;
743 status_t result = getDisplayConfigs(display, &configs);
744 if (result != NO_ERROR) {
745 return result;
746 }
747
748 int activeId = getActiveConfig(display);
749 if (activeId < 0) {
750 ALOGE("No active configuration found");
751 return NAME_NOT_FOUND;
752 }
753
Dan Stozad723bd72014-11-18 10:24:03 -0800754 *info = configs[static_cast<size_t>(activeId)];
Dan Stoza7f7da322014-05-02 15:26:25 -0700755 return NO_ERROR;
756}
757
758int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
759 return ComposerService::getComposerService()->getActiveConfig(display);
760}
761
762status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
763 return ComposerService::getComposerService()->setActiveConfig(display, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764}
765
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700766void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
767 int mode) {
768 ComposerService::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -0700769}
770
Svetoslavd85084b2014-03-20 10:28:31 -0700771status_t SurfaceComposerClient::clearAnimationFrameStats() {
772 return ComposerService::getComposerService()->clearAnimationFrameStats();
773}
774
775status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
776 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
777}
778
Dan Stozac4f471e2016-03-24 09:31:08 -0700779status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
780 HdrCapabilities* outCapabilities) {
781 return ComposerService::getComposerService()->getHdrCapabilities(display,
782 outCapabilities);
783}
784
Mathias Agopian698c0872011-06-28 19:09:31 -0700785// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800787status_t ScreenshotClient::capture(
788 const sp<IBinder>& display,
789 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -0700790 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -0800791 uint32_t minLayerZ, uint32_t maxLayerZ, bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800792 sp<ISurfaceComposer> s(ComposerService::getComposerService());
793 if (s == NULL) return NO_INIT;
Dan Stozac1879002014-05-22 15:59:05 -0700794 return s->captureScreen(display, producer, sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -0800795 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800796}
797
Mathias Agopian74c40c02010-09-29 13:02:36 -0700798ScreenshotClient::ScreenshotClient()
Mathias Agopianabe815d2013-03-19 22:22:21 -0700799 : mHaveBuffer(false) {
800 memset(&mBuffer, 0, sizeof(mBuffer));
Mathias Agopian74c40c02010-09-29 13:02:36 -0700801}
802
Mathias Agopian8000d062013-03-26 18:15:35 -0700803ScreenshotClient::~ScreenshotClient() {
804 ScreenshotClient::release();
805}
806
Mathias Agopianabe815d2013-03-19 22:22:21 -0700807sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
808 if (mCpuConsumer == NULL) {
Dan Stoza6d5a7bb2014-03-13 11:39:09 -0700809 sp<IGraphicBufferConsumer> consumer;
810 BufferQueue::createBufferQueue(&mProducer, &consumer);
811 mCpuConsumer = new CpuConsumer(consumer, 1);
Mathias Agopianabe815d2013-03-19 22:22:21 -0700812 mCpuConsumer->setName(String8("ScreenshotClient"));
813 }
814 return mCpuConsumer;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800815}
816
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700817status_t ScreenshotClient::update(const sp<IBinder>& display,
Dan Stozac1879002014-05-22 15:59:05 -0700818 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -0800819 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsd15ef272014-09-04 16:19:44 -0700820 bool useIdentityTransform, uint32_t rotation) {
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800821 sp<ISurfaceComposer> s(ComposerService::getComposerService());
822 if (s == NULL) return NO_INIT;
Mathias Agopianabe815d2013-03-19 22:22:21 -0700823 sp<CpuConsumer> cpuConsumer = getCpuConsumer();
824
825 if (mHaveBuffer) {
826 mCpuConsumer->unlockBuffer(mBuffer);
827 memset(&mBuffer, 0, sizeof(mBuffer));
828 mHaveBuffer = false;
829 }
830
Dan Stozac1879002014-05-22 15:59:05 -0700831 status_t err = s->captureScreen(display, mProducer, sourceCrop,
Riley Andrewsd15ef272014-09-04 16:19:44 -0700832 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform,
833 static_cast<ISurfaceComposer::Rotation>(rotation));
Mathias Agopianabe815d2013-03-19 22:22:21 -0700834
835 if (err == NO_ERROR) {
836 err = mCpuConsumer->lockNextBuffer(&mBuffer);
837 if (err == NO_ERROR) {
838 mHaveBuffer = true;
839 }
840 }
841 return err;
842}
843
Riley Andrewsd15ef272014-09-04 16:19:44 -0700844status_t ScreenshotClient::update(const sp<IBinder>& display,
845 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
846 uint32_t minLayerZ, uint32_t maxLayerZ,
847 bool useIdentityTransform) {
848
849 return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
850 minLayerZ, maxLayerZ, useIdentityTransform, ISurfaceComposer::eRotateNone);
851}
852
Dan Stozac1879002014-05-22 15:59:05 -0700853status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -0800854 bool useIdentityTransform) {
Dan Stozaf10c46e2014-11-11 10:32:31 -0800855 return ScreenshotClient::update(display, sourceCrop, 0, 0, 0, -1U,
Riley Andrewsd15ef272014-09-04 16:19:44 -0700856 useIdentityTransform, ISurfaceComposer::eRotateNone);
Mathias Agopianabe815d2013-03-19 22:22:21 -0700857}
858
Dan Stozac1879002014-05-22 15:59:05 -0700859status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -0800860 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform) {
Dan Stozac1879002014-05-22 15:59:05 -0700861 return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
Dan Stozaf10c46e2014-11-11 10:32:31 -0800862 0, -1U, useIdentityTransform, ISurfaceComposer::eRotateNone);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700863}
864
865void ScreenshotClient::release() {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700866 if (mHaveBuffer) {
867 mCpuConsumer->unlockBuffer(mBuffer);
868 memset(&mBuffer, 0, sizeof(mBuffer));
869 mHaveBuffer = false;
870 }
871 mCpuConsumer.clear();
Mathias Agopian74c40c02010-09-29 13:02:36 -0700872}
873
874void const* ScreenshotClient::getPixels() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700875 return mBuffer.data;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700876}
877
878uint32_t ScreenshotClient::getWidth() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700879 return mBuffer.width;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700880}
881
882uint32_t ScreenshotClient::getHeight() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700883 return mBuffer.height;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700884}
885
886PixelFormat ScreenshotClient::getFormat() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700887 return mBuffer.format;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700888}
889
890uint32_t ScreenshotClient::getStride() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700891 return mBuffer.stride;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700892}
893
894size_t ScreenshotClient::getSize() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700895 return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700896}
897
898// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899}; // namespace android