blob: 2466d2555a0b8f7c00ee8dc27773c820d8513a6c [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 Agopiana67932f2011-04-20 14:20:59 -070024#include <utils/SortedVector.h>
25#include <utils/String8.h>
26#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Mathias Agopiana67932f2011-04-20 14:20:59 -070028#include <binder/IServiceManager.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080029
Michael Wright28f24d02016-07-12 13:30:53 -070030#include <system/graphics.h>
31
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <ui/DisplayInfo.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
Robert Carr673134e2017-01-09 19:48:38 -080034#include <gui/BufferItemConsumer.h>
Mathias Agopianabe815d2013-03-19 22:22:21 -070035#include <gui/CpuConsumer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080036#include <gui/IGraphicBufferProducer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080037#include <gui/ISurfaceComposer.h>
38#include <gui/ISurfaceComposerClient.h>
Robert Carr4cdc58f2017-08-23 14:22:20 -070039#include <gui/LayerState.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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
45namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046// ---------------------------------------------------------------------------
47
Mathias Agopian7e27f052010-05-28 14:22:23 -070048ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
49
Mathias Agopianb7e930d2010-06-01 15:12:58 -070050ComposerService::ComposerService()
51: Singleton<ComposerService>() {
Andy McFadden6652b3e2012-09-06 18:45:56 -070052 Mutex::Autolock _l(mLock);
53 connectLocked();
54}
55
56void ComposerService::connectLocked() {
Mathias Agopianb7e930d2010-06-01 15:12:58 -070057 const String16 name("SurfaceFlinger");
58 while (getService(name, &mComposerService) != NO_ERROR) {
59 usleep(250000);
60 }
Andy McFadden6652b3e2012-09-06 18:45:56 -070061 assert(mComposerService != NULL);
62
63 // Create the death listener.
64 class DeathObserver : public IBinder::DeathRecipient {
65 ComposerService& mComposerService;
66 virtual void binderDied(const wp<IBinder>& who) {
67 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
68 who.unsafe_get());
69 mComposerService.composerServiceDied();
70 }
71 public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070072 explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
Andy McFadden6652b3e2012-09-06 18:45:56 -070073 };
74
75 mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
Marco Nelissen2ea926b2014-11-14 08:01:01 -080076 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
Mathias Agopianb7e930d2010-06-01 15:12:58 -070077}
78
Andy McFadden6652b3e2012-09-06 18:45:56 -070079/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
80 ComposerService& instance = ComposerService::getInstance();
81 Mutex::Autolock _l(instance.mLock);
82 if (instance.mComposerService == NULL) {
83 ComposerService::getInstance().connectLocked();
84 assert(instance.mComposerService != NULL);
85 ALOGD("ComposerService reconnected");
86 }
87 return instance.mComposerService;
88}
89
90void ComposerService::composerServiceDied()
91{
92 Mutex::Autolock _l(mLock);
93 mComposerService = NULL;
94 mDeathObserver = NULL;
Mathias Agopianb7e930d2010-06-01 15:12:58 -070095}
96
Mathias Agopian7e27f052010-05-28 14:22:23 -070097// ---------------------------------------------------------------------------
98
Robert Carr4cdc58f2017-08-23 14:22:20 -070099SurfaceComposerClient::Transaction::Transaction(const Transaction& other) :
100 mForceSynchronous(other.mForceSynchronous),
101 mTransactionNestCount(other.mTransactionNestCount),
102 mAnimation(other.mAnimation) {
103 mDisplayStates = other.mDisplayStates;
104 mComposerStates = other.mComposerStates;
Mathias Agopian698c0872011-06-28 19:09:31 -0700105}
106
Robert Carr2c5f6d22017-09-26 12:30:35 -0700107SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::merge(Transaction&& other) {
108 for (auto const& state : other.mComposerStates) {
109 ssize_t index = mComposerStates.indexOf(state);
110 if (index < 0) {
111 mComposerStates.add(state);
112 } else {
113 mComposerStates.editItemAt(static_cast<size_t>(index)).state.merge(state.state);
114 }
115 }
116 other.mComposerStates.clear();
117
118 for (auto const& state : other.mDisplayStates) {
119 ssize_t index = mDisplayStates.indexOf(state);
120 if (index < 0) {
121 mDisplayStates.add(state);
122 } else {
123 mDisplayStates.editItemAt(static_cast<size_t>(index)).merge(state);
124 }
125 }
126 other.mDisplayStates.clear();
127
128 return *this;
129}
130
Robert Carr4cdc58f2017-08-23 14:22:20 -0700131status_t SurfaceComposerClient::Transaction::apply(bool synchronous) {
132 if (mStatus != NO_ERROR) {
133 return mStatus;
134 }
135
136 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
137
138 Vector<ComposerState> composerStates;
139 Vector<DisplayState> displayStates;
140 uint32_t flags = 0;
141
142 mForceSynchronous |= synchronous;
143
144 composerStates = mComposerStates;
145 mComposerStates.clear();
146
147 displayStates = mDisplayStates;
148 mDisplayStates.clear();
149
150 if (mForceSynchronous) {
151 flags |= ISurfaceComposer::eSynchronous;
152 }
153 if (mAnimation) {
154 flags |= ISurfaceComposer::eAnimation;
155 }
156
157 mForceSynchronous = false;
158 mAnimation = false;
159
160 sf->setTransactionState(composerStates, displayStates, flags);
161 mStatus = NO_ERROR;
162 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700163}
164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165// ---------------------------------------------------------------------------
166
Robert Carr4cdc58f2017-08-23 14:22:20 -0700167sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName, bool secure) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700168 return ComposerService::getComposerService()->createDisplay(displayName,
169 secure);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700170}
171
Robert Carr4cdc58f2017-08-23 14:22:20 -0700172void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
Jesse Hall6c913be2013-08-08 12:15:49 -0700173 return ComposerService::getComposerService()->destroyDisplay(display);
174}
175
Robert Carr4cdc58f2017-08-23 14:22:20 -0700176sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700177 return ComposerService::getComposerService()->getBuiltInDisplay(id);
178}
179
Robert Carr4cdc58f2017-08-23 14:22:20 -0700180void SurfaceComposerClient::Transaction::setAnimationTransaction() {
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700181 mAnimation = true;
182}
183
Robert Carr4cdc58f2017-08-23 14:22:20 -0700184layer_state_t* SurfaceComposerClient::Transaction::getLayerStateLocked(const sp<SurfaceControl>& sc) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700185 ComposerState s;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700186 s.client = sc->getClient()->mClient;
187 s.state.surface = sc->getHandle();
Mathias Agopian698c0872011-06-28 19:09:31 -0700188
Mathias Agopiane57f2922012-08-09 16:29:12 -0700189 ssize_t index = mComposerStates.indexOf(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700190 if (index < 0) {
191 // we don't have it, add an initialized layer_state to our list
Mathias Agopiane57f2922012-08-09 16:29:12 -0700192 index = mComposerStates.add(s);
Mathias Agopian698c0872011-06-28 19:09:31 -0700193 }
194
Mathias Agopiane57f2922012-08-09 16:29:12 -0700195 ComposerState* const out = mComposerStates.editArray();
Mathias Agopian698c0872011-06-28 19:09:31 -0700196 return &(out[index].state);
197}
198
Robert Carr4cdc58f2017-08-23 14:22:20 -0700199SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition(
200 const sp<SurfaceControl>& sc, float x, float y) {
201 layer_state_t* s = getLayerStateLocked(sc);
202 if (!s) {
203 mStatus = BAD_INDEX;
204 return *this;
205 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700206 s->what |= layer_state_t::ePositionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700207 s->x = x;
208 s->y = y;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700209 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700210}
211
Robert Carr4cdc58f2017-08-23 14:22:20 -0700212SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::show(
213 const sp<SurfaceControl>& sc) {
214 return setFlags(sc, 0, layer_state_t::eLayerHidden);
215}
216
217SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::hide(
218 const sp<SurfaceControl>& sc) {
219 return setFlags(sc, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden);
220}
221
222SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setSize(
223 const sp<SurfaceControl>& sc, uint32_t w, uint32_t h) {
224 layer_state_t* s = getLayerStateLocked(sc);
225 if (!s) {
226 mStatus = BAD_INDEX;
227 return *this;
228 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700229 s->what |= layer_state_t::eSizeChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700230 s->w = w;
231 s->h = h;
Jamie Gennis28378392011-10-12 17:39:00 -0700232
Jorim Jaggi092123c2016-04-13 01:40:35 +0000233 // Resizing a surface makes the transaction synchronous.
234 mForceSynchronous = true;
235
Robert Carr4cdc58f2017-08-23 14:22:20 -0700236 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700237}
238
Robert Carr4cdc58f2017-08-23 14:22:20 -0700239SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
240 const sp<SurfaceControl>& sc, int32_t z) {
241 layer_state_t* s = getLayerStateLocked(sc);
242 if (!s) {
243 mStatus = BAD_INDEX;
244 return *this;
245 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700246 s->what |= layer_state_t::eLayerChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700247 s->z = z;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700248 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700249}
250
Robert Carr4cdc58f2017-08-23 14:22:20 -0700251SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setRelativeLayer(const sp<SurfaceControl>& sc, const sp<IBinder>& relativeTo,
Robert Carrdb66e622017-04-10 16:55:57 -0700252 int32_t z) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700253 layer_state_t* s = getLayerStateLocked(sc);
Robert Carrdb66e622017-04-10 16:55:57 -0700254 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700255 mStatus = BAD_INDEX;
Robert Carrdb66e622017-04-10 16:55:57 -0700256 }
257 s->what |= layer_state_t::eRelativeLayerChanged;
258 s->relativeLayerHandle = relativeTo;
259 s->z = z;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700260 return *this;
Robert Carrdb66e622017-04-10 16:55:57 -0700261}
262
Robert Carr4cdc58f2017-08-23 14:22:20 -0700263SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFlags(
264 const sp<SurfaceControl>& sc, uint32_t flags,
Mathias Agopian698c0872011-06-28 19:09:31 -0700265 uint32_t mask) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700266 layer_state_t* s = getLayerStateLocked(sc);
267 if (!s) {
268 mStatus = BAD_INDEX;
269 return *this;
270 }
Pablo Ceballos53390e12015-08-04 11:25:59 -0700271 if ((mask & layer_state_t::eLayerOpaque) ||
272 (mask & layer_state_t::eLayerHidden) ||
273 (mask & layer_state_t::eLayerSecure)) {
Dan Stoza23116082015-06-18 14:58:39 -0700274 s->what |= layer_state_t::eFlagsChanged;
Andy McFadden4125a4f2014-01-29 17:17:11 -0800275 }
Mathias Agopian698c0872011-06-28 19:09:31 -0700276 s->flags &= ~mask;
277 s->flags |= (flags & mask);
278 s->mask |= mask;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700279 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700280}
281
Robert Carr4cdc58f2017-08-23 14:22:20 -0700282SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setTransparentRegionHint(
283 const sp<SurfaceControl>& sc,
Mathias Agopian698c0872011-06-28 19:09:31 -0700284 const Region& transparentRegion) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700285 layer_state_t* s = getLayerStateLocked(sc);
286 if (!s) {
287 mStatus = BAD_INDEX;
288 return *this;
289 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700290 s->what |= layer_state_t::eTransparentRegionChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700291 s->transparentRegion = transparentRegion;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700292 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700293}
294
Robert Carr4cdc58f2017-08-23 14:22:20 -0700295SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setAlpha(
296 const sp<SurfaceControl>& sc, float alpha) {
297 layer_state_t* s = getLayerStateLocked(sc);
298 if (!s) {
299 mStatus = BAD_INDEX;
300 return *this;
301 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700302 s->what |= layer_state_t::eAlphaChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700303 s->alpha = alpha;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700304 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700305}
306
Robert Carr4cdc58f2017-08-23 14:22:20 -0700307SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayerStack(
308 const sp<SurfaceControl>& sc, uint32_t layerStack) {
309 layer_state_t* s = getLayerStateLocked(sc);
310 if (!s) {
311 mStatus = BAD_INDEX;
312 return *this;
313 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700314 s->what |= layer_state_t::eLayerStackChanged;
Mathias Agopian87855782012-07-24 21:41:09 -0700315 s->layerStack = layerStack;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700316 return *this;
Mathias Agopian87855782012-07-24 21:41:09 -0700317}
318
Robert Carr4cdc58f2017-08-23 14:22:20 -0700319SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
320 const sp<SurfaceControl>& sc, float dsdx, float dtdx,
Robert Carrcb6e1e32017-02-21 19:48:26 -0800321 float dtdy, float dsdy) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700322 layer_state_t* s = getLayerStateLocked(sc);
323 if (!s) {
324 mStatus = BAD_INDEX;
325 return *this;
326 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700327 s->what |= layer_state_t::eMatrixChanged;
Mathias Agopian698c0872011-06-28 19:09:31 -0700328 layer_state_t::matrix22_t matrix;
329 matrix.dsdx = dsdx;
330 matrix.dtdx = dtdx;
331 matrix.dsdy = dsdy;
332 matrix.dtdy = dtdy;
333 s->matrix = matrix;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700334 return *this;
Mathias Agopian698c0872011-06-28 19:09:31 -0700335}
336
Robert Carr4cdc58f2017-08-23 14:22:20 -0700337SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCrop(
338 const sp<SurfaceControl>& sc, const Rect& crop) {
339 layer_state_t* s = getLayerStateLocked(sc);
340 if (!s) {
341 mStatus = BAD_INDEX;
342 return *this;
343 }
Mathias Agopian3165cc22012-08-08 19:42:09 -0700344 s->what |= layer_state_t::eCropChanged;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700345 s->crop = crop;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700346 return *this;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700347}
348
Robert Carr4cdc58f2017-08-23 14:22:20 -0700349SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setFinalCrop(const sp<SurfaceControl>& sc, const Rect& crop) {
350 layer_state_t* s = getLayerStateLocked(sc);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000351 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700352 mStatus = BAD_INDEX;
353 return *this;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000354 }
355 s->what |= layer_state_t::eFinalCropChanged;
356 s->finalCrop = crop;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700357 return *this;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000358}
359
Robert Carr4cdc58f2017-08-23 14:22:20 -0700360SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::deferTransactionUntil(
361 const sp<SurfaceControl>& sc,
Dan Stoza7dde5992015-05-22 09:51:44 -0700362 const sp<IBinder>& handle, uint64_t frameNumber) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700363 layer_state_t* s = getLayerStateLocked(sc);
Dan Stoza7dde5992015-05-22 09:51:44 -0700364 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700365 mStatus = BAD_INDEX;
366 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -0700367 }
368 s->what |= layer_state_t::eDeferTransaction;
Robert Carr0d480722017-01-10 16:42:54 -0800369 s->barrierHandle = handle;
370 s->frameNumber = frameNumber;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700371 return *this;
Robert Carr0d480722017-01-10 16:42:54 -0800372}
373
Robert Carr4cdc58f2017-08-23 14:22:20 -0700374SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::deferTransactionUntil(
375 const sp<SurfaceControl>& sc,
Robert Carr0d480722017-01-10 16:42:54 -0800376 const sp<Surface>& barrierSurface, uint64_t frameNumber) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700377 layer_state_t* s = getLayerStateLocked(sc);
Robert Carr0d480722017-01-10 16:42:54 -0800378 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700379 mStatus = BAD_INDEX;
380 return *this;
Robert Carr0d480722017-01-10 16:42:54 -0800381 }
382 s->what |= layer_state_t::eDeferTransaction;
383 s->barrierGbp = barrierSurface->getIGraphicBufferProducer();
Dan Stoza7dde5992015-05-22 09:51:44 -0700384 s->frameNumber = frameNumber;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700385 return *this;
Dan Stoza7dde5992015-05-22 09:51:44 -0700386}
387
Robert Carr4cdc58f2017-08-23 14:22:20 -0700388SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparentChildren(
389 const sp<SurfaceControl>& sc,
Robert Carr1db73f62016-12-21 12:58:51 -0800390 const sp<IBinder>& newParentHandle) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700391 layer_state_t* s = getLayerStateLocked(sc);
Robert Carr1db73f62016-12-21 12:58:51 -0800392 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700393 mStatus = BAD_INDEX;
394 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -0800395 }
396 s->what |= layer_state_t::eReparentChildren;
397 s->reparentHandle = newParentHandle;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700398 return *this;
Robert Carr1db73f62016-12-21 12:58:51 -0800399}
400
Robert Carr4cdc58f2017-08-23 14:22:20 -0700401SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::reparent(
402 const sp<SurfaceControl>& sc,
chaviwf1961f72017-09-18 16:41:07 -0700403 const sp<IBinder>& newParentHandle) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700404 layer_state_t* s = getLayerStateLocked(sc);
chaviw06178942017-07-27 10:25:59 -0700405 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700406 mStatus = BAD_INDEX;
407 return *this;
chaviw06178942017-07-27 10:25:59 -0700408 }
chaviwf1961f72017-09-18 16:41:07 -0700409 s->what |= layer_state_t::eReparent;
chaviw06178942017-07-27 10:25:59 -0700410 s->parentHandleForChild = newParentHandle;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700411 return *this;
chaviw06178942017-07-27 10:25:59 -0700412}
413
Robert Carr4cdc58f2017-08-23 14:22:20 -0700414SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setColor(
415 const sp<SurfaceControl>& sc,
416 const half3& color) {
417 layer_state_t* s = getLayerStateLocked(sc);
Robert Carr9524cb32017-02-13 11:32:32 -0800418 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700419 mStatus = BAD_INDEX;
420 return *this;
421 }
422 s->what |= layer_state_t::eColorChanged;
423 s->color = color;
424 return *this;
425}
426
427SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::detachChildren(
428 const sp<SurfaceControl>& sc) {
429 layer_state_t* s = getLayerStateLocked(sc);
430 if (!s) {
431 mStatus = BAD_INDEX;
Robert Carr9524cb32017-02-13 11:32:32 -0800432 }
433 s->what |= layer_state_t::eDetachChildren;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700434 return *this;
Robert Carr9524cb32017-02-13 11:32:32 -0800435}
436
Robert Carr4cdc58f2017-08-23 14:22:20 -0700437SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setOverrideScalingMode(
438 const sp<SurfaceControl>& sc, int32_t overrideScalingMode) {
439 layer_state_t* s = getLayerStateLocked(sc);
Robert Carrc3574f72016-03-24 12:19:32 -0700440 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700441 mStatus = BAD_INDEX;
442 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -0700443 }
444
445 switch (overrideScalingMode) {
446 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
447 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
448 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
449 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
450 case -1:
451 break;
452 default:
453 ALOGE("unknown scaling mode: %d",
454 overrideScalingMode);
Robert Carr4cdc58f2017-08-23 14:22:20 -0700455 mStatus = BAD_VALUE;
456 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -0700457 }
458
459 s->what |= layer_state_t::eOverrideScalingModeChanged;
460 s->overrideScalingMode = overrideScalingMode;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700461 return *this;
Robert Carrc3574f72016-03-24 12:19:32 -0700462}
463
Robert Carr4cdc58f2017-08-23 14:22:20 -0700464SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setGeometryAppliesWithResize(
465 const sp<SurfaceControl>& sc) {
466 layer_state_t* s = getLayerStateLocked(sc);
Robert Carr82364e32016-05-15 11:27:47 -0700467 if (!s) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700468 mStatus = BAD_INDEX;
469 return *this;
Robert Carr82364e32016-05-15 11:27:47 -0700470 }
Robert Carr99e27f02016-06-16 15:18:02 -0700471 s->what |= layer_state_t::eGeometryAppliesWithResize;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700472 return *this;
Robert Carr82364e32016-05-15 11:27:47 -0700473}
474
Mathias Agopian698c0872011-06-28 19:09:31 -0700475// ---------------------------------------------------------------------------
476
Robert Carr4cdc58f2017-08-23 14:22:20 -0700477DisplayState& SurfaceComposerClient::Transaction::getDisplayStateLocked(const sp<IBinder>& token) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700478 DisplayState s;
479 s.token = token;
480 ssize_t index = mDisplayStates.indexOf(s);
481 if (index < 0) {
482 // we don't have it, add an initialized layer_state to our list
483 s.what = 0;
484 index = mDisplayStates.add(s);
485 }
Dan Stozad723bd72014-11-18 10:24:03 -0800486 return mDisplayStates.editItemAt(static_cast<size_t>(index));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700487}
488
Robert Carr4cdc58f2017-08-23 14:22:20 -0700489status_t SurfaceComposerClient::Transaction::setDisplaySurface(const sp<IBinder>& token,
490 const sp<IGraphicBufferProducer>& bufferProducer) {
Pablo Ceballoseddbef82016-09-01 11:21:21 -0700491 if (bufferProducer.get() != nullptr) {
492 // Make sure that composition can never be stalled by a virtual display
493 // consumer that isn't processing buffers fast enough.
494 status_t err = bufferProducer->setAsyncMode(true);
495 if (err != NO_ERROR) {
496 ALOGE("Composer::setDisplaySurface Failed to enable async mode on the "
497 "BufferQueue. This BufferQueue cannot be used for virtual "
498 "display. (%d)", err);
499 return err;
500 }
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700501 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700502 DisplayState& s(getDisplayStateLocked(token));
Andy McFadden2adaf042012-12-18 09:49:45 -0800503 s.surface = bufferProducer;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700504 s.what |= DisplayState::eSurfaceChanged;
Pablo Ceballos1aad24c2016-08-04 10:24:22 -0700505 return NO_ERROR;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700506}
507
Robert Carr4cdc58f2017-08-23 14:22:20 -0700508void SurfaceComposerClient::Transaction::setDisplayLayerStack(const sp<IBinder>& token,
Mathias Agopiane57f2922012-08-09 16:29:12 -0700509 uint32_t layerStack) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700510 DisplayState& s(getDisplayStateLocked(token));
511 s.layerStack = layerStack;
512 s.what |= DisplayState::eLayerStackChanged;
513}
514
Robert Carr4cdc58f2017-08-23 14:22:20 -0700515void SurfaceComposerClient::Transaction::setDisplayProjection(const sp<IBinder>& token,
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700516 uint32_t orientation,
517 const Rect& layerStackRect,
518 const Rect& displayRect) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700519 DisplayState& s(getDisplayStateLocked(token));
520 s.orientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700521 s.viewport = layerStackRect;
522 s.frame = displayRect;
523 s.what |= DisplayState::eDisplayProjectionChanged;
Jorim Jaggi092123c2016-04-13 01:40:35 +0000524 mForceSynchronous = true; // TODO: do we actually still need this?
Mathias Agopiane57f2922012-08-09 16:29:12 -0700525}
526
Robert Carr4cdc58f2017-08-23 14:22:20 -0700527void SurfaceComposerClient::Transaction::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
Michael Wright1f6078a2014-06-26 16:01:02 -0700528 DisplayState& s(getDisplayStateLocked(token));
529 s.width = width;
530 s.height = height;
531 s.what |= DisplayState::eDisplaySizeChanged;
532}
533
Mathias Agopiane57f2922012-08-09 16:29:12 -0700534// ---------------------------------------------------------------------------
535
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536SurfaceComposerClient::SurfaceComposerClient()
Robert Carr4cdc58f2017-08-23 14:22:20 -0700537 : mStatus(NO_INIT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800539}
540
Robert Carr1db73f62016-12-21 12:58:51 -0800541SurfaceComposerClient::SurfaceComposerClient(const sp<IGraphicBufferProducer>& root)
Robert Carr4cdc58f2017-08-23 14:22:20 -0700542 : mStatus(NO_INIT), mParent(root)
Robert Carr1db73f62016-12-21 12:58:51 -0800543{
544}
545
Mathias Agopian698c0872011-06-28 19:09:31 -0700546void SurfaceComposerClient::onFirstRef() {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700547 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
548 if (sf != 0) {
Robert Carr1db73f62016-12-21 12:58:51 -0800549 auto rootProducer = mParent.promote();
550 sp<ISurfaceComposerClient> conn;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700551 conn = (rootProducer != nullptr) ? sf->createScopedConnection(rootProducer) :
552 sf->createConnection();
Mathias Agopiand4784a32010-05-27 19:41:15 -0700553 if (conn != 0) {
554 mClient = conn;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700555 mStatus = NO_ERROR;
556 }
557 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558}
559
Mathias Agopian698c0872011-06-28 19:09:31 -0700560SurfaceComposerClient::~SurfaceComposerClient() {
Mathias Agopian631f3582010-05-25 17:51:34 -0700561 dispose();
562}
Mathias Agopiandd3423c2009-09-23 15:44:05 -0700563
Mathias Agopian698c0872011-06-28 19:09:31 -0700564status_t SurfaceComposerClient::initCheck() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 return mStatus;
566}
567
Mathias Agopian698c0872011-06-28 19:09:31 -0700568sp<IBinder> SurfaceComposerClient::connection() const {
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800569 return IInterface::asBinder(mClient);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570}
571
Mathias Agopiand4784a32010-05-27 19:41:15 -0700572status_t SurfaceComposerClient::linkToComposerDeath(
573 const sp<IBinder::DeathRecipient>& recipient,
Mathias Agopian698c0872011-06-28 19:09:31 -0700574 void* cookie, uint32_t flags) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700575 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
576 return IInterface::asBinder(sf)->linkToDeath(recipient, cookie, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577}
578
Mathias Agopian698c0872011-06-28 19:09:31 -0700579void SurfaceComposerClient::dispose() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580 // this can be called more than once.
Mathias Agopian7e27f052010-05-28 14:22:23 -0700581 sp<ISurfaceComposerClient> client;
Mathias Agopiand4784a32010-05-27 19:41:15 -0700582 Mutex::Autolock _lm(mLock);
583 if (mClient != 0) {
Mathias Agopiand4784a32010-05-27 19:41:15 -0700584 client = mClient; // hold ref while lock is held
585 mClient.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 }
Mathias Agopiand4784a32010-05-27 19:41:15 -0700587 mStatus = NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588}
589
Mathias Agopian698c0872011-06-28 19:09:31 -0700590sp<SurfaceControl> SurfaceComposerClient::createSurface(
Mathias Agopian698c0872011-06-28 19:09:31 -0700591 const String8& name,
Mathias Agopian698c0872011-06-28 19:09:31 -0700592 uint32_t w,
593 uint32_t h,
594 PixelFormat format,
Robert Carr1f0a16a2016-10-24 16:27:39 -0700595 uint32_t flags,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500596 SurfaceControl* parent,
597 uint32_t windowType,
598 uint32_t ownerUid)
Mathias Agopian698c0872011-06-28 19:09:31 -0700599{
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700600 sp<SurfaceControl> sur;
Mathias Agopian698c0872011-06-28 19:09:31 -0700601 if (mStatus == NO_ERROR) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700602 sp<IBinder> handle;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700603 sp<IBinder> parentHandle;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700604 sp<IGraphicBufferProducer> gbp;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700605
606 if (parent != nullptr) {
607 parentHandle = parent->getHandle();
608 }
609 status_t err = mClient->createSurface(name, w, h, format, flags, parentHandle,
Albert Chaulk479c60c2017-01-27 14:21:34 -0500610 windowType, ownerUid, &handle, &gbp);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700611 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
612 if (err == NO_ERROR) {
613 sur = new SurfaceControl(this, handle, gbp);
Mathias Agopian698c0872011-06-28 19:09:31 -0700614 }
615 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700616 return sur;
Mathias Agopian698c0872011-06-28 19:09:31 -0700617}
618
Mathias Agopianac9fa422013-02-11 16:40:36 -0800619status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700620 if (mStatus != NO_ERROR)
621 return mStatus;
622 status_t err = mClient->destroySurface(sid);
623 return err;
624}
625
Svetoslavd85084b2014-03-20 10:28:31 -0700626status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
627 if (mStatus != NO_ERROR) {
628 return mStatus;
629 }
630 return mClient->clearLayerFrameStats(token);
631}
632
633status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
634 FrameStats* outStats) const {
635 if (mStatus != NO_ERROR) {
636 return mStatus;
637 }
638 return mClient->getLayerFrameStats(token, outStats);
639}
640
Mathias Agopian698c0872011-06-28 19:09:31 -0700641// ----------------------------------------------------------------------------
642
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700643status_t SurfaceComposerClient::enableVSyncInjections(bool enable) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700644 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
645 return sf->enableVSyncInjections(enable);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700646}
647
648status_t SurfaceComposerClient::injectVSync(nsecs_t when) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700649 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
650 return sf->injectVSync(when);
Sahil Dhanjuc1ba5c42016-06-07 20:09:20 -0700651}
652
Dan Stoza7f7da322014-05-02 15:26:25 -0700653status_t SurfaceComposerClient::getDisplayConfigs(
654 const sp<IBinder>& display, Vector<DisplayInfo>* configs)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655{
Dan Stoza7f7da322014-05-02 15:26:25 -0700656 return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
657}
658
659status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
660 DisplayInfo* info) {
661 Vector<DisplayInfo> configs;
662 status_t result = getDisplayConfigs(display, &configs);
663 if (result != NO_ERROR) {
664 return result;
665 }
666
667 int activeId = getActiveConfig(display);
668 if (activeId < 0) {
669 ALOGE("No active configuration found");
670 return NAME_NOT_FOUND;
671 }
672
Dan Stozad723bd72014-11-18 10:24:03 -0800673 *info = configs[static_cast<size_t>(activeId)];
Dan Stoza7f7da322014-05-02 15:26:25 -0700674 return NO_ERROR;
675}
676
677int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
678 return ComposerService::getComposerService()->getActiveConfig(display);
679}
680
681status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
682 return ComposerService::getComposerService()->setActiveConfig(display, id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683}
684
Michael Wright28f24d02016-07-12 13:30:53 -0700685status_t SurfaceComposerClient::getDisplayColorModes(const sp<IBinder>& display,
686 Vector<android_color_mode_t>* outColorModes) {
687 return ComposerService::getComposerService()->getDisplayColorModes(display, outColorModes);
688}
689
690android_color_mode_t SurfaceComposerClient::getActiveColorMode(const sp<IBinder>& display) {
691 return ComposerService::getComposerService()->getActiveColorMode(display);
692}
693
694status_t SurfaceComposerClient::setActiveColorMode(const sp<IBinder>& display,
695 android_color_mode_t colorMode) {
696 return ComposerService::getComposerService()->setActiveColorMode(display, colorMode);
697}
698
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700699void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
700 int mode) {
701 ComposerService::getComposerService()->setPowerMode(token, mode);
Jeff Brown2a09bb32012-10-08 19:13:57 -0700702}
703
Svetoslavd85084b2014-03-20 10:28:31 -0700704status_t SurfaceComposerClient::clearAnimationFrameStats() {
705 return ComposerService::getComposerService()->clearAnimationFrameStats();
706}
707
708status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
709 return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
710}
711
Dan Stozac4f471e2016-03-24 09:31:08 -0700712status_t SurfaceComposerClient::getHdrCapabilities(const sp<IBinder>& display,
713 HdrCapabilities* outCapabilities) {
714 return ComposerService::getComposerService()->getHdrCapabilities(display,
715 outCapabilities);
716}
717
Mathias Agopian698c0872011-06-28 19:09:31 -0700718// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800720status_t ScreenshotClient::capture(
721 const sp<IBinder>& display,
722 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -0700723 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -0800724 int32_t minLayerZ, int32_t maxLayerZ, bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800725 sp<ISurfaceComposer> s(ComposerService::getComposerService());
726 if (s == NULL) return NO_INIT;
Dan Stozac1879002014-05-22 15:59:05 -0700727 return s->captureScreen(display, producer, sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -0800728 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
Mathias Agopian2a9fc492013-03-01 13:42:57 -0800729}
730
Robert Carr673134e2017-01-09 19:48:38 -0800731status_t ScreenshotClient::captureToBuffer(const sp<IBinder>& display,
732 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -0800733 int32_t minLayerZ, int32_t maxLayerZ, bool useIdentityTransform,
Robert Carr673134e2017-01-09 19:48:38 -0800734 uint32_t rotation,
735 sp<GraphicBuffer>* outBuffer) {
736 sp<ISurfaceComposer> s(ComposerService::getComposerService());
737 if (s == NULL) return NO_INIT;
738
739 sp<IGraphicBufferConsumer> gbpConsumer;
740 sp<IGraphicBufferProducer> producer;
741 BufferQueue::createBufferQueue(&producer, &gbpConsumer);
742 sp<BufferItemConsumer> consumer(new BufferItemConsumer(gbpConsumer,
743 GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_NEVER,
744 1, true));
745
746 status_t ret = s->captureScreen(display, producer, sourceCrop, reqWidth, reqHeight,
747 minLayerZ, maxLayerZ, useIdentityTransform,
748 static_cast<ISurfaceComposer::Rotation>(rotation));
749 if (ret != NO_ERROR) {
750 return ret;
751 }
752 BufferItem b;
753 consumer->acquireBuffer(&b, 0, true);
754 *outBuffer = b.mGraphicBuffer;
755 return ret;
756}
757
chaviwa76b2712017-09-20 12:02:26 -0700758status_t ScreenshotClient::captureLayers(const sp<IBinder>& layerHandle,
759 const sp<IGraphicBufferProducer>& producer,
chaviw7206d492017-11-10 16:16:12 -0800760 Rect sourceCrop, float frameScale) {
chaviwa76b2712017-09-20 12:02:26 -0700761 sp<ISurfaceComposer> s(ComposerService::getComposerService());
762 if (s == NULL) return NO_INIT;
chaviw7206d492017-11-10 16:16:12 -0800763 return s->captureLayers(layerHandle, producer, sourceCrop, frameScale);
764}
765
766status_t ScreenshotClient::captureLayersToBuffer(const sp<IBinder>& layerHandle, Rect sourceCrop,
767 float frameScale, sp<GraphicBuffer>* outBuffer) {
768 sp<ISurfaceComposer> s(ComposerService::getComposerService());
769 if (s == NULL) return NO_INIT;
770
771 sp<IGraphicBufferConsumer> gbpConsumer;
772 sp<IGraphicBufferProducer> producer;
773 BufferQueue::createBufferQueue(&producer, &gbpConsumer);
774 sp<BufferItemConsumer> consumer(new BufferItemConsumer(gbpConsumer,
775 GRALLOC_USAGE_HW_TEXTURE |
776 GRALLOC_USAGE_SW_READ_NEVER |
777 GRALLOC_USAGE_SW_WRITE_NEVER,
778 1, true));
779
780 status_t ret = s->captureLayers(layerHandle, producer, sourceCrop, frameScale);
781 if (ret != NO_ERROR) {
782 return ret;
783 }
784 BufferItem b;
785 consumer->acquireBuffer(&b, 0, true);
786 *outBuffer = b.mGraphicBuffer;
787 return ret;
chaviwa76b2712017-09-20 12:02:26 -0700788}
789
Mathias Agopian74c40c02010-09-29 13:02:36 -0700790ScreenshotClient::ScreenshotClient()
Mathias Agopianabe815d2013-03-19 22:22:21 -0700791 : mHaveBuffer(false) {
792 memset(&mBuffer, 0, sizeof(mBuffer));
Mathias Agopian74c40c02010-09-29 13:02:36 -0700793}
794
Mathias Agopian8000d062013-03-26 18:15:35 -0700795ScreenshotClient::~ScreenshotClient() {
796 ScreenshotClient::release();
797}
798
Mathias Agopianabe815d2013-03-19 22:22:21 -0700799sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
800 if (mCpuConsumer == NULL) {
Dan Stoza6d5a7bb2014-03-13 11:39:09 -0700801 sp<IGraphicBufferConsumer> consumer;
802 BufferQueue::createBufferQueue(&mProducer, &consumer);
803 mCpuConsumer = new CpuConsumer(consumer, 1);
Mathias Agopianabe815d2013-03-19 22:22:21 -0700804 mCpuConsumer->setName(String8("ScreenshotClient"));
805 }
806 return mCpuConsumer;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800807}
808
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700809status_t ScreenshotClient::update(const sp<IBinder>& display,
Dan Stozac1879002014-05-22 15:59:05 -0700810 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -0800811 int32_t minLayerZ, int32_t maxLayerZ,
Riley Andrewsd15ef272014-09-04 16:19:44 -0700812 bool useIdentityTransform, uint32_t rotation) {
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800813 sp<ISurfaceComposer> s(ComposerService::getComposerService());
814 if (s == NULL) return NO_INIT;
Mathias Agopianabe815d2013-03-19 22:22:21 -0700815 sp<CpuConsumer> cpuConsumer = getCpuConsumer();
816
817 if (mHaveBuffer) {
818 mCpuConsumer->unlockBuffer(mBuffer);
819 memset(&mBuffer, 0, sizeof(mBuffer));
820 mHaveBuffer = false;
821 }
822
Dan Stozac1879002014-05-22 15:59:05 -0700823 status_t err = s->captureScreen(display, mProducer, sourceCrop,
Riley Andrewsd15ef272014-09-04 16:19:44 -0700824 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform,
825 static_cast<ISurfaceComposer::Rotation>(rotation));
Mathias Agopianabe815d2013-03-19 22:22:21 -0700826
827 if (err == NO_ERROR) {
828 err = mCpuConsumer->lockNextBuffer(&mBuffer);
829 if (err == NO_ERROR) {
830 mHaveBuffer = true;
831 }
832 }
833 return err;
834}
835
Riley Andrewsd15ef272014-09-04 16:19:44 -0700836status_t ScreenshotClient::update(const sp<IBinder>& display,
837 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Robert Carrae060832016-11-28 10:51:00 -0800838 int32_t minLayerZ, int32_t maxLayerZ,
Riley Andrewsd15ef272014-09-04 16:19:44 -0700839 bool useIdentityTransform) {
840
841 return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
842 minLayerZ, maxLayerZ, useIdentityTransform, ISurfaceComposer::eRotateNone);
843}
844
Dan Stozac1879002014-05-22 15:59:05 -0700845status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -0800846 bool useIdentityTransform) {
Robert Carrae060832016-11-28 10:51:00 -0800847 return ScreenshotClient::update(display, sourceCrop, 0, 0,
848 INT32_MIN, INT32_MAX,
Riley Andrewsd15ef272014-09-04 16:19:44 -0700849 useIdentityTransform, ISurfaceComposer::eRotateNone);
Mathias Agopianabe815d2013-03-19 22:22:21 -0700850}
851
Dan Stozac1879002014-05-22 15:59:05 -0700852status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
Dan Stozac7014012014-02-14 15:03:43 -0800853 uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform) {
Dan Stozac1879002014-05-22 15:59:05 -0700854 return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
Robert Carrae060832016-11-28 10:51:00 -0800855 INT32_MIN, INT32_MAX,
856 useIdentityTransform, ISurfaceComposer::eRotateNone);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700857}
858
859void ScreenshotClient::release() {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700860 if (mHaveBuffer) {
861 mCpuConsumer->unlockBuffer(mBuffer);
862 memset(&mBuffer, 0, sizeof(mBuffer));
863 mHaveBuffer = false;
864 }
865 mCpuConsumer.clear();
Mathias Agopian74c40c02010-09-29 13:02:36 -0700866}
867
868void const* ScreenshotClient::getPixels() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700869 return mBuffer.data;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700870}
871
872uint32_t ScreenshotClient::getWidth() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700873 return mBuffer.width;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700874}
875
876uint32_t ScreenshotClient::getHeight() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700877 return mBuffer.height;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700878}
879
880PixelFormat ScreenshotClient::getFormat() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700881 return mBuffer.format;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700882}
883
884uint32_t ScreenshotClient::getStride() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700885 return mBuffer.stride;
Mathias Agopian74c40c02010-09-29 13:02:36 -0700886}
887
888size_t ScreenshotClient::getSize() const {
Mathias Agopianabe815d2013-03-19 22:22:21 -0700889 return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
Mathias Agopian74c40c02010-09-29 13:02:36 -0700890}
891
Romain Guy88d37dd2017-05-26 17:57:05 -0700892android_dataspace ScreenshotClient::getDataSpace() const {
893 return mBuffer.dataSpace;
894}
895
Mathias Agopian74c40c02010-09-29 13:02:36 -0700896// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897}; // namespace android