Client.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (C) 2012 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. #include <stdint.h>
  17. #include <sys/types.h>
  18. #include <binder/PermissionCache.h>
  19. #include <binder/IPCThreadState.h>
  20. #include <private/android_filesystem_config.h>
  21. #include "Client.h"
  22. #include "Layer.h"
  23. #include "SurfaceFlinger.h"
  24. namespace android {
  25. // ---------------------------------------------------------------------------
  26. const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
  27. // ---------------------------------------------------------------------------
  28. Client::Client(const sp<SurfaceFlinger>& flinger)
  29. : mFlinger(flinger)
  30. {
  31. }
  32. Client::~Client()
  33. {
  34. const size_t count = mLayers.size();
  35. for (size_t i=0 ; i<count ; i++) {
  36. sp<Layer> layer(mLayers.valueAt(i).promote());
  37. if (layer != 0) {
  38. mFlinger->removeLayer(layer);
  39. }
  40. }
  41. }
  42. status_t Client::initCheck() const {
  43. return NO_ERROR;
  44. }
  45. void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
  46. {
  47. Mutex::Autolock _l(mLock);
  48. mLayers.add(handle, layer);
  49. }
  50. void Client::detachLayer(const Layer* layer)
  51. {
  52. Mutex::Autolock _l(mLock);
  53. // we do a linear search here, because this doesn't happen often
  54. const size_t count = mLayers.size();
  55. for (size_t i=0 ; i<count ; i++) {
  56. if (mLayers.valueAt(i) == layer) {
  57. mLayers.removeItemsAt(i, 1);
  58. break;
  59. }
  60. }
  61. }
  62. sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
  63. {
  64. Mutex::Autolock _l(mLock);
  65. sp<Layer> lbc;
  66. wp<Layer> layer(mLayers.valueFor(handle));
  67. if (layer != 0) {
  68. lbc = layer.promote();
  69. ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
  70. }
  71. return lbc;
  72. }
  73. status_t Client::onTransact(
  74. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  75. {
  76. // these must be checked
  77. IPCThreadState* ipc = IPCThreadState::self();
  78. const int pid = ipc->getCallingPid();
  79. const int uid = ipc->getCallingUid();
  80. const int self_pid = getpid();
  81. if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != AID_SYSTEM && uid != 0)) {
  82. // we're called from a different process, do the real check
  83. if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
  84. {
  85. ALOGE("Permission Denial: "
  86. "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
  87. return PERMISSION_DENIED;
  88. }
  89. }
  90. return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
  91. }
  92. status_t Client::createSurface(
  93. const String8& name,
  94. uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
  95. sp<IBinder>* handle,
  96. sp<IGraphicBufferProducer>* gbp)
  97. {
  98. /*
  99. * createSurface must be called from the GL thread so that it can
  100. * have access to the GL context.
  101. */
  102. class MessageCreateLayer : public MessageBase {
  103. SurfaceFlinger* flinger;
  104. Client* client;
  105. sp<IBinder>* handle;
  106. sp<IGraphicBufferProducer>* gbp;
  107. status_t result;
  108. const String8& name;
  109. uint32_t w, h;
  110. PixelFormat format;
  111. uint32_t flags;
  112. public:
  113. MessageCreateLayer(SurfaceFlinger* flinger,
  114. const String8& name, Client* client,
  115. uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
  116. sp<IBinder>* handle,
  117. sp<IGraphicBufferProducer>* gbp)
  118. : flinger(flinger), client(client),
  119. handle(handle), gbp(gbp),
  120. name(name), w(w), h(h), format(format), flags(flags) {
  121. }
  122. status_t getResult() const { return result; }
  123. virtual bool handler() {
  124. result = flinger->createLayer(name, client, w, h, format, flags,
  125. handle, gbp);
  126. return true;
  127. }
  128. };
  129. sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
  130. name, this, w, h, format, flags, handle, gbp);
  131. mFlinger->postMessageSync(msg);
  132. return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
  133. }
  134. status_t Client::destroySurface(const sp<IBinder>& handle) {
  135. return mFlinger->onLayerRemoved(this, handle);
  136. }
  137. status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
  138. sp<Layer> layer = getLayerUser(handle);
  139. if (layer == NULL) {
  140. return NAME_NOT_FOUND;
  141. }
  142. layer->clearFrameStats();
  143. return NO_ERROR;
  144. }
  145. status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
  146. sp<Layer> layer = getLayerUser(handle);
  147. if (layer == NULL) {
  148. return NAME_NOT_FOUND;
  149. }
  150. layer->getFrameStats(outStats);
  151. return NO_ERROR;
  152. }
  153. // ---------------------------------------------------------------------------
  154. }; // namespace android