SurfaceComposerClient.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  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. #define LOG_TAG "SurfaceComposerClient"
  17. #include <stdint.h>
  18. #include <sys/types.h>
  19. #include <utils/Errors.h>
  20. #include <utils/Log.h>
  21. #include <utils/Singleton.h>
  22. #include <utils/SortedVector.h>
  23. #include <utils/String8.h>
  24. #include <utils/threads.h>
  25. #include <binder/IMemory.h>
  26. #include <binder/IServiceManager.h>
  27. #include <ui/DisplayInfo.h>
  28. #include <gui/CpuConsumer.h>
  29. #include <gui/IGraphicBufferProducer.h>
  30. #include <gui/ISurfaceComposer.h>
  31. #include <gui/ISurfaceComposerClient.h>
  32. #include <gui/SurfaceComposerClient.h>
  33. #include <private/gui/ComposerService.h>
  34. #include <private/gui/LayerState.h>
  35. namespace android {
  36. // ---------------------------------------------------------------------------
  37. ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
  38. ComposerService::ComposerService()
  39. : Singleton<ComposerService>() {
  40. Mutex::Autolock _l(mLock);
  41. connectLocked();
  42. }
  43. void ComposerService::connectLocked() {
  44. const String16 name("SurfaceFlinger");
  45. while (getService(name, &mComposerService) != NO_ERROR) {
  46. usleep(250000);
  47. }
  48. assert(mComposerService != NULL);
  49. // Create the death listener.
  50. class DeathObserver : public IBinder::DeathRecipient {
  51. ComposerService& mComposerService;
  52. virtual void binderDied(const wp<IBinder>& who) {
  53. ALOGW("ComposerService remote (surfaceflinger) died [%p]",
  54. who.unsafe_get());
  55. mComposerService.composerServiceDied();
  56. }
  57. public:
  58. DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
  59. };
  60. mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
  61. IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
  62. }
  63. /*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
  64. ComposerService& instance = ComposerService::getInstance();
  65. Mutex::Autolock _l(instance.mLock);
  66. if (instance.mComposerService == NULL) {
  67. ComposerService::getInstance().connectLocked();
  68. assert(instance.mComposerService != NULL);
  69. ALOGD("ComposerService reconnected");
  70. }
  71. return instance.mComposerService;
  72. }
  73. void ComposerService::composerServiceDied()
  74. {
  75. Mutex::Autolock _l(mLock);
  76. mComposerService = NULL;
  77. mDeathObserver = NULL;
  78. }
  79. // ---------------------------------------------------------------------------
  80. static inline
  81. int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
  82. if (lhs.client < rhs.client) return -1;
  83. if (lhs.client > rhs.client) return 1;
  84. if (lhs.state.surface < rhs.state.surface) return -1;
  85. if (lhs.state.surface > rhs.state.surface) return 1;
  86. return 0;
  87. }
  88. static inline
  89. int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
  90. return compare_type(lhs.token, rhs.token);
  91. }
  92. class Composer : public Singleton<Composer>
  93. {
  94. friend class Singleton<Composer>;
  95. mutable Mutex mLock;
  96. SortedVector<ComposerState> mComposerStates;
  97. SortedVector<DisplayState > mDisplayStates;
  98. uint32_t mForceSynchronous;
  99. uint32_t mTransactionNestCount;
  100. bool mAnimation;
  101. Composer() : Singleton<Composer>(),
  102. mForceSynchronous(0), mTransactionNestCount(0),
  103. mAnimation(false)
  104. { }
  105. void openGlobalTransactionImpl();
  106. void closeGlobalTransactionImpl(bool synchronous);
  107. void setAnimationTransactionImpl();
  108. layer_state_t* getLayerStateLocked(
  109. const sp<SurfaceComposerClient>& client, const sp<IBinder>& id);
  110. DisplayState& getDisplayStateLocked(const sp<IBinder>& token);
  111. public:
  112. sp<IBinder> createDisplay(const String8& displayName, bool secure);
  113. void destroyDisplay(const sp<IBinder>& display);
  114. sp<IBinder> getBuiltInDisplay(int32_t id);
  115. status_t setPosition(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
  116. float x, float y);
  117. status_t setSize(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
  118. uint32_t w, uint32_t h);
  119. status_t setLayer(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
  120. uint32_t z);
  121. status_t setBlur(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
  122. float blur);
  123. status_t setBlurMaskSurface(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
  124. const sp<IBinder>& maskSurfaceId);
  125. status_t setBlurMaskSampling(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
  126. uint32_t blurMaskSampling);
  127. status_t setBlurMaskAlphaThreshold(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
  128. float alpha);
  129. status_t setFlags(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
  130. uint32_t flags, uint32_t mask);
  131. status_t setTransparentRegionHint(
  132. const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
  133. const Region& transparentRegion);
  134. status_t setAlpha(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
  135. float alpha);
  136. status_t setMatrix(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
  137. float dsdx, float dtdx, float dsdy, float dtdy);
  138. status_t setOrientation(int orientation);
  139. status_t setCrop(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
  140. const Rect& crop);
  141. status_t setLayerStack(const sp<SurfaceComposerClient>& client,
  142. const sp<IBinder>& id, uint32_t layerStack);
  143. void setDisplaySurface(const sp<IBinder>& token,
  144. const sp<IGraphicBufferProducer>& bufferProducer);
  145. void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
  146. void setDisplayProjection(const sp<IBinder>& token,
  147. uint32_t orientation,
  148. const Rect& layerStackRect,
  149. const Rect& displayRect);
  150. void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
  151. static void setAnimationTransaction() {
  152. Composer::getInstance().setAnimationTransactionImpl();
  153. }
  154. static void openGlobalTransaction() {
  155. Composer::getInstance().openGlobalTransactionImpl();
  156. }
  157. static void closeGlobalTransaction(bool synchronous) {
  158. Composer::getInstance().closeGlobalTransactionImpl(synchronous);
  159. }
  160. };
  161. ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
  162. // ---------------------------------------------------------------------------
  163. sp<IBinder> Composer::createDisplay(const String8& displayName, bool secure) {
  164. return ComposerService::getComposerService()->createDisplay(displayName,
  165. secure);
  166. }
  167. void Composer::destroyDisplay(const sp<IBinder>& display) {
  168. return ComposerService::getComposerService()->destroyDisplay(display);
  169. }
  170. sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
  171. return ComposerService::getComposerService()->getBuiltInDisplay(id);
  172. }
  173. void Composer::openGlobalTransactionImpl() {
  174. { // scope for the lock
  175. Mutex::Autolock _l(mLock);
  176. mTransactionNestCount += 1;
  177. }
  178. }
  179. void Composer::closeGlobalTransactionImpl(bool synchronous) {
  180. sp<ISurfaceComposer> sm(ComposerService::getComposerService());
  181. Vector<ComposerState> transaction;
  182. Vector<DisplayState> displayTransaction;
  183. uint32_t flags = 0;
  184. { // scope for the lock
  185. Mutex::Autolock _l(mLock);
  186. mForceSynchronous |= synchronous;
  187. if (!mTransactionNestCount) {
  188. ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
  189. "call to openGlobalTransaction().");
  190. } else if (--mTransactionNestCount) {
  191. return;
  192. }
  193. transaction = mComposerStates;
  194. mComposerStates.clear();
  195. displayTransaction = mDisplayStates;
  196. mDisplayStates.clear();
  197. if (mForceSynchronous) {
  198. flags |= ISurfaceComposer::eSynchronous;
  199. }
  200. if (mAnimation) {
  201. flags |= ISurfaceComposer::eAnimation;
  202. }
  203. mForceSynchronous = false;
  204. mAnimation = false;
  205. }
  206. sm->setTransactionState(transaction, displayTransaction, flags);
  207. }
  208. void Composer::setAnimationTransactionImpl() {
  209. Mutex::Autolock _l(mLock);
  210. mAnimation = true;
  211. }
  212. layer_state_t* Composer::getLayerStateLocked(
  213. const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
  214. ComposerState s;
  215. s.client = client->mClient;
  216. s.state.surface = id;
  217. ssize_t index = mComposerStates.indexOf(s);
  218. if (index < 0) {
  219. // we don't have it, add an initialized layer_state to our list
  220. index = mComposerStates.add(s);
  221. }
  222. ComposerState* const out = mComposerStates.editArray();
  223. return &(out[index].state);
  224. }
  225. status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
  226. const sp<IBinder>& id, float x, float y) {
  227. Mutex::Autolock _l(mLock);
  228. layer_state_t* s = getLayerStateLocked(client, id);
  229. if (!s)
  230. return BAD_INDEX;
  231. s->what |= layer_state_t::ePositionChanged;
  232. s->x = x;
  233. s->y = y;
  234. return NO_ERROR;
  235. }
  236. status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
  237. const sp<IBinder>& id, uint32_t w, uint32_t h) {
  238. Mutex::Autolock _l(mLock);
  239. layer_state_t* s = getLayerStateLocked(client, id);
  240. if (!s)
  241. return BAD_INDEX;
  242. s->what |= layer_state_t::eSizeChanged;
  243. s->w = w;
  244. s->h = h;
  245. // Resizing a surface makes the transaction synchronous.
  246. mForceSynchronous = true;
  247. return NO_ERROR;
  248. }
  249. status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
  250. const sp<IBinder>& id, uint32_t z) {
  251. Mutex::Autolock _l(mLock);
  252. layer_state_t* s = getLayerStateLocked(client, id);
  253. if (!s)
  254. return BAD_INDEX;
  255. s->what |= layer_state_t::eLayerChanged;
  256. s->z = z;
  257. return NO_ERROR;
  258. }
  259. status_t Composer::setBlur(const sp<SurfaceComposerClient>& client,
  260. const sp<IBinder>& id, float blur) {
  261. Mutex::Autolock _l(mLock);
  262. layer_state_t* s = getLayerStateLocked(client, id);
  263. if (!s)
  264. return BAD_INDEX;
  265. s->what |= layer_state_t::eBlurChanged;
  266. s->blur = blur;
  267. return NO_ERROR;
  268. }
  269. status_t Composer::setBlurMaskSurface(const sp<SurfaceComposerClient>& client,
  270. const sp<IBinder>& id, const sp<IBinder>& maskSurfaceId) {
  271. Mutex::Autolock _l(mLock);
  272. layer_state_t* s = getLayerStateLocked(client, id);
  273. if (!s)
  274. return BAD_INDEX;
  275. s->what |= layer_state_t::eBlurMaskSurfaceChanged;
  276. s->blurMaskSurface = maskSurfaceId;
  277. return NO_ERROR;
  278. }
  279. status_t Composer::setBlurMaskSampling(const sp<SurfaceComposerClient>& client,
  280. const sp<IBinder>& id, uint32_t blurMaskSampling) {
  281. Mutex::Autolock _l(mLock);
  282. layer_state_t* s = getLayerStateLocked(client, id);
  283. if (!s)
  284. return BAD_INDEX;
  285. s->what |= layer_state_t::eBlurMaskSamplingChanged;
  286. s->blurMaskSampling = blurMaskSampling;
  287. return NO_ERROR;
  288. }
  289. status_t Composer::setBlurMaskAlphaThreshold(const sp<SurfaceComposerClient>& client,
  290. const sp<IBinder>& id, float alpha) {
  291. Mutex::Autolock _l(mLock);
  292. layer_state_t* s = getLayerStateLocked(client, id);
  293. if (!s)
  294. return BAD_INDEX;
  295. s->what |= layer_state_t::eBlurMaskAlphaThresholdChanged;
  296. s->blurMaskAlphaThreshold = alpha;
  297. return NO_ERROR;
  298. }
  299. status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
  300. const sp<IBinder>& id, uint32_t flags,
  301. uint32_t mask) {
  302. Mutex::Autolock _l(mLock);
  303. layer_state_t* s = getLayerStateLocked(client, id);
  304. if (!s)
  305. return BAD_INDEX;
  306. if (mask & layer_state_t::eLayerOpaque ||
  307. mask & layer_state_t::eLayerHidden ||
  308. mask & layer_state_t::eLayerSecure) {
  309. s->what |= layer_state_t::eFlagsChanged;
  310. }
  311. s->flags &= ~mask;
  312. s->flags |= (flags & mask);
  313. s->mask |= mask;
  314. return NO_ERROR;
  315. }
  316. status_t Composer::setTransparentRegionHint(
  317. const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
  318. const Region& transparentRegion) {
  319. Mutex::Autolock _l(mLock);
  320. layer_state_t* s = getLayerStateLocked(client, id);
  321. if (!s)
  322. return BAD_INDEX;
  323. s->what |= layer_state_t::eTransparentRegionChanged;
  324. s->transparentRegion = transparentRegion;
  325. return NO_ERROR;
  326. }
  327. status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
  328. const sp<IBinder>& id, float alpha) {
  329. Mutex::Autolock _l(mLock);
  330. layer_state_t* s = getLayerStateLocked(client, id);
  331. if (!s)
  332. return BAD_INDEX;
  333. s->what |= layer_state_t::eAlphaChanged;
  334. s->alpha = alpha;
  335. return NO_ERROR;
  336. }
  337. status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
  338. const sp<IBinder>& id, uint32_t layerStack) {
  339. Mutex::Autolock _l(mLock);
  340. layer_state_t* s = getLayerStateLocked(client, id);
  341. if (!s)
  342. return BAD_INDEX;
  343. s->what |= layer_state_t::eLayerStackChanged;
  344. s->layerStack = layerStack;
  345. return NO_ERROR;
  346. }
  347. status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
  348. const sp<IBinder>& id, float dsdx, float dtdx,
  349. float dsdy, float dtdy) {
  350. Mutex::Autolock _l(mLock);
  351. layer_state_t* s = getLayerStateLocked(client, id);
  352. if (!s)
  353. return BAD_INDEX;
  354. s->what |= layer_state_t::eMatrixChanged;
  355. layer_state_t::matrix22_t matrix;
  356. matrix.dsdx = dsdx;
  357. matrix.dtdx = dtdx;
  358. matrix.dsdy = dsdy;
  359. matrix.dtdy = dtdy;
  360. s->matrix = matrix;
  361. return NO_ERROR;
  362. }
  363. status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
  364. const sp<IBinder>& id, const Rect& crop) {
  365. Mutex::Autolock _l(mLock);
  366. layer_state_t* s = getLayerStateLocked(client, id);
  367. if (!s)
  368. return BAD_INDEX;
  369. s->what |= layer_state_t::eCropChanged;
  370. s->crop = crop;
  371. return NO_ERROR;
  372. }
  373. // ---------------------------------------------------------------------------
  374. DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
  375. DisplayState s;
  376. s.token = token;
  377. ssize_t index = mDisplayStates.indexOf(s);
  378. if (index < 0) {
  379. // we don't have it, add an initialized layer_state to our list
  380. s.what = 0;
  381. index = mDisplayStates.add(s);
  382. }
  383. return mDisplayStates.editItemAt(static_cast<size_t>(index));
  384. }
  385. void Composer::setDisplaySurface(const sp<IBinder>& token,
  386. const sp<IGraphicBufferProducer>& bufferProducer) {
  387. Mutex::Autolock _l(mLock);
  388. DisplayState& s(getDisplayStateLocked(token));
  389. s.surface = bufferProducer;
  390. s.what |= DisplayState::eSurfaceChanged;
  391. }
  392. void Composer::setDisplayLayerStack(const sp<IBinder>& token,
  393. uint32_t layerStack) {
  394. Mutex::Autolock _l(mLock);
  395. DisplayState& s(getDisplayStateLocked(token));
  396. s.layerStack = layerStack;
  397. s.what |= DisplayState::eLayerStackChanged;
  398. }
  399. void Composer::setDisplayProjection(const sp<IBinder>& token,
  400. uint32_t orientation,
  401. const Rect& layerStackRect,
  402. const Rect& displayRect) {
  403. Mutex::Autolock _l(mLock);
  404. DisplayState& s(getDisplayStateLocked(token));
  405. s.orientation = orientation;
  406. s.viewport = layerStackRect;
  407. s.frame = displayRect;
  408. s.what |= DisplayState::eDisplayProjectionChanged;
  409. mForceSynchronous = true; // TODO: do we actually still need this?
  410. }
  411. void Composer::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
  412. Mutex::Autolock _l(mLock);
  413. DisplayState& s(getDisplayStateLocked(token));
  414. s.width = width;
  415. s.height = height;
  416. s.what |= DisplayState::eDisplaySizeChanged;
  417. }
  418. // ---------------------------------------------------------------------------
  419. SurfaceComposerClient::SurfaceComposerClient()
  420. : mStatus(NO_INIT), mComposer(Composer::getInstance())
  421. {
  422. }
  423. void SurfaceComposerClient::onFirstRef() {
  424. sp<ISurfaceComposer> sm(ComposerService::getComposerService());
  425. if (sm != 0) {
  426. sp<ISurfaceComposerClient> conn = sm->createConnection();
  427. if (conn != 0) {
  428. mClient = conn;
  429. mStatus = NO_ERROR;
  430. }
  431. }
  432. }
  433. SurfaceComposerClient::~SurfaceComposerClient() {
  434. dispose();
  435. }
  436. status_t SurfaceComposerClient::initCheck() const {
  437. return mStatus;
  438. }
  439. sp<IBinder> SurfaceComposerClient::connection() const {
  440. return IInterface::asBinder(mClient);
  441. }
  442. status_t SurfaceComposerClient::linkToComposerDeath(
  443. const sp<IBinder::DeathRecipient>& recipient,
  444. void* cookie, uint32_t flags) {
  445. sp<ISurfaceComposer> sm(ComposerService::getComposerService());
  446. return IInterface::asBinder(sm)->linkToDeath(recipient, cookie, flags);
  447. }
  448. void SurfaceComposerClient::dispose() {
  449. // this can be called more than once.
  450. sp<ISurfaceComposerClient> client;
  451. Mutex::Autolock _lm(mLock);
  452. if (mClient != 0) {
  453. client = mClient; // hold ref while lock is held
  454. mClient.clear();
  455. }
  456. mStatus = NO_INIT;
  457. }
  458. sp<SurfaceControl> SurfaceComposerClient::createSurface(
  459. const String8& name,
  460. uint32_t w,
  461. uint32_t h,
  462. PixelFormat format,
  463. uint32_t flags)
  464. {
  465. sp<SurfaceControl> sur;
  466. if (mStatus == NO_ERROR) {
  467. sp<IBinder> handle;
  468. sp<IGraphicBufferProducer> gbp;
  469. status_t err = mClient->createSurface(name, w, h, format, flags,
  470. &handle, &gbp);
  471. ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
  472. if (err == NO_ERROR) {
  473. sur = new SurfaceControl(this, handle, gbp);
  474. }
  475. }
  476. return sur;
  477. }
  478. sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
  479. bool secure) {
  480. return Composer::getInstance().createDisplay(displayName, secure);
  481. }
  482. void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
  483. Composer::getInstance().destroyDisplay(display);
  484. }
  485. sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
  486. return Composer::getInstance().getBuiltInDisplay(id);
  487. }
  488. status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
  489. if (mStatus != NO_ERROR)
  490. return mStatus;
  491. status_t err = mClient->destroySurface(sid);
  492. return err;
  493. }
  494. status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
  495. if (mStatus != NO_ERROR) {
  496. return mStatus;
  497. }
  498. return mClient->clearLayerFrameStats(token);
  499. }
  500. status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
  501. FrameStats* outStats) const {
  502. if (mStatus != NO_ERROR) {
  503. return mStatus;
  504. }
  505. return mClient->getLayerFrameStats(token, outStats);
  506. }
  507. inline Composer& SurfaceComposerClient::getComposer() {
  508. return mComposer;
  509. }
  510. // ----------------------------------------------------------------------------
  511. void SurfaceComposerClient::openGlobalTransaction() {
  512. Composer::openGlobalTransaction();
  513. }
  514. void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
  515. Composer::closeGlobalTransaction(synchronous);
  516. }
  517. void SurfaceComposerClient::setAnimationTransaction() {
  518. Composer::setAnimationTransaction();
  519. }
  520. // ----------------------------------------------------------------------------
  521. status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
  522. return getComposer().setCrop(this, id, crop);
  523. }
  524. status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
  525. return getComposer().setPosition(this, id, x, y);
  526. }
  527. status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
  528. return getComposer().setSize(this, id, w, h);
  529. }
  530. status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, uint32_t z) {
  531. return getComposer().setLayer(this, id, z);
  532. }
  533. status_t SurfaceComposerClient::setBlur(const sp<IBinder>& id, float blur) {
  534. return getComposer().setBlur(this, id, blur);
  535. }
  536. status_t SurfaceComposerClient::setBlurMaskSurface(const sp<IBinder>& id, const sp<IBinder>& maskSurfaceId) {
  537. return getComposer().setBlurMaskSurface(this, id, maskSurfaceId);
  538. }
  539. status_t SurfaceComposerClient::setBlurMaskSampling(const sp<IBinder>& id, uint32_t blurMaskSampling) {
  540. return getComposer().setBlurMaskSampling(this, id, blurMaskSampling);
  541. }
  542. status_t SurfaceComposerClient::setBlurMaskAlphaThreshold(const sp<IBinder>& id, float alpha) {
  543. return getComposer().setBlurMaskAlphaThreshold(this, id, alpha);
  544. }
  545. status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
  546. return getComposer().setFlags(this, id,
  547. layer_state_t::eLayerHidden,
  548. layer_state_t::eLayerHidden);
  549. }
  550. status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
  551. return getComposer().setFlags(this, id,
  552. 0,
  553. layer_state_t::eLayerHidden);
  554. }
  555. status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
  556. uint32_t mask) {
  557. return getComposer().setFlags(this, id, flags, mask);
  558. }
  559. status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
  560. const Region& transparentRegion) {
  561. return getComposer().setTransparentRegionHint(this, id, transparentRegion);
  562. }
  563. status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
  564. return getComposer().setAlpha(this, id, alpha);
  565. }
  566. status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
  567. return getComposer().setLayerStack(this, id, layerStack);
  568. }
  569. status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
  570. float dsdy, float dtdy) {
  571. return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
  572. }
  573. // ----------------------------------------------------------------------------
  574. void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
  575. const sp<IGraphicBufferProducer>& bufferProducer) {
  576. Composer::getInstance().setDisplaySurface(token, bufferProducer);
  577. }
  578. void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
  579. uint32_t layerStack) {
  580. Composer::getInstance().setDisplayLayerStack(token, layerStack);
  581. }
  582. void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
  583. uint32_t orientation,
  584. const Rect& layerStackRect,
  585. const Rect& displayRect) {
  586. Composer::getInstance().setDisplayProjection(token, orientation,
  587. layerStackRect, displayRect);
  588. }
  589. void SurfaceComposerClient::setDisplaySize(const sp<IBinder>& token,
  590. uint32_t width, uint32_t height) {
  591. Composer::getInstance().setDisplaySize(token, width, height);
  592. }
  593. // ----------------------------------------------------------------------------
  594. status_t SurfaceComposerClient::getDisplayConfigs(
  595. const sp<IBinder>& display, Vector<DisplayInfo>* configs)
  596. {
  597. return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
  598. }
  599. status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
  600. DisplayInfo* info) {
  601. Vector<DisplayInfo> configs;
  602. status_t result = getDisplayConfigs(display, &configs);
  603. if (result != NO_ERROR) {
  604. return result;
  605. }
  606. int activeId = getActiveConfig(display);
  607. if (activeId < 0) {
  608. ALOGE("No active configuration found");
  609. return NAME_NOT_FOUND;
  610. }
  611. *info = configs[static_cast<size_t>(activeId)];
  612. return NO_ERROR;
  613. }
  614. int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
  615. return ComposerService::getComposerService()->getActiveConfig(display);
  616. }
  617. status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
  618. return ComposerService::getComposerService()->setActiveConfig(display, id);
  619. }
  620. void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
  621. int mode) {
  622. ComposerService::getComposerService()->setPowerMode(token, mode);
  623. }
  624. status_t SurfaceComposerClient::clearAnimationFrameStats() {
  625. return ComposerService::getComposerService()->clearAnimationFrameStats();
  626. }
  627. status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
  628. return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
  629. }
  630. // ----------------------------------------------------------------------------
  631. #ifndef FORCE_SCREENSHOT_CPU_PATH
  632. #define SS_CPU_CONSUMER false
  633. #else
  634. #define SS_CPU_CONSUMER true
  635. #endif
  636. status_t ScreenshotClient::capture(
  637. const sp<IBinder>& display,
  638. const sp<IGraphicBufferProducer>& producer,
  639. Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
  640. uint32_t minLayerZ, uint32_t maxLayerZ, bool useIdentityTransform) {
  641. sp<ISurfaceComposer> s(ComposerService::getComposerService());
  642. if (s == NULL) return NO_INIT;
  643. return s->captureScreen(display, producer, sourceCrop,
  644. reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform,
  645. ISurfaceComposer::eRotateNone, SS_CPU_CONSUMER);
  646. }
  647. ScreenshotClient::ScreenshotClient()
  648. : mHaveBuffer(false) {
  649. memset(&mBuffer, 0, sizeof(mBuffer));
  650. }
  651. ScreenshotClient::~ScreenshotClient() {
  652. ScreenshotClient::release();
  653. }
  654. sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
  655. if (mCpuConsumer == NULL) {
  656. sp<IGraphicBufferConsumer> consumer;
  657. BufferQueue::createBufferQueue(&mProducer, &consumer);
  658. mCpuConsumer = new CpuConsumer(consumer, 1);
  659. mCpuConsumer->setName(String8("ScreenshotClient"));
  660. }
  661. return mCpuConsumer;
  662. }
  663. status_t ScreenshotClient::update(const sp<IBinder>& display,
  664. Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
  665. uint32_t minLayerZ, uint32_t maxLayerZ,
  666. bool useIdentityTransform, uint32_t rotation) {
  667. sp<ISurfaceComposer> s(ComposerService::getComposerService());
  668. if (s == NULL) return NO_INIT;
  669. sp<CpuConsumer> cpuConsumer = getCpuConsumer();
  670. if (mHaveBuffer) {
  671. mCpuConsumer->unlockBuffer(mBuffer);
  672. memset(&mBuffer, 0, sizeof(mBuffer));
  673. mHaveBuffer = false;
  674. }
  675. status_t err = s->captureScreen(display, mProducer, sourceCrop,
  676. reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform,
  677. static_cast<ISurfaceComposer::Rotation>(rotation), true);
  678. if (err == NO_ERROR) {
  679. err = mCpuConsumer->lockNextBuffer(&mBuffer);
  680. if (err == NO_ERROR) {
  681. mHaveBuffer = true;
  682. }
  683. }
  684. return err;
  685. }
  686. status_t ScreenshotClient::update(const sp<IBinder>& display,
  687. Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
  688. uint32_t minLayerZ, uint32_t maxLayerZ,
  689. bool useIdentityTransform) {
  690. return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
  691. minLayerZ, maxLayerZ, useIdentityTransform, ISurfaceComposer::eRotateNone);
  692. }
  693. status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
  694. bool useIdentityTransform) {
  695. return ScreenshotClient::update(display, sourceCrop, 0, 0, 0, -1U,
  696. useIdentityTransform, ISurfaceComposer::eRotateNone);
  697. }
  698. status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
  699. uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform) {
  700. return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
  701. 0, -1U, useIdentityTransform, ISurfaceComposer::eRotateNone);
  702. }
  703. void ScreenshotClient::release() {
  704. if (mHaveBuffer) {
  705. mCpuConsumer->unlockBuffer(mBuffer);
  706. memset(&mBuffer, 0, sizeof(mBuffer));
  707. mHaveBuffer = false;
  708. }
  709. mCpuConsumer.clear();
  710. }
  711. void const* ScreenshotClient::getPixels() const {
  712. return mBuffer.data;
  713. }
  714. uint32_t ScreenshotClient::getWidth() const {
  715. return mBuffer.width;
  716. }
  717. uint32_t ScreenshotClient::getHeight() const {
  718. return mBuffer.height;
  719. }
  720. PixelFormat ScreenshotClient::getFormat() const {
  721. return mBuffer.format;
  722. }
  723. uint32_t ScreenshotClient::getStride() const {
  724. return mBuffer.stride;
  725. }
  726. size_t ScreenshotClient::getSize() const {
  727. return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
  728. }
  729. // ----------------------------------------------------------------------------
  730. }; // namespace android