Client.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/IPCThreadState.h>
  19. #include <private/android_filesystem_config.h>
  20. #include "Client.h"
  21. #include "Layer.h"
  22. #include "SurfaceFlinger.h"
  23. namespace android {
  24. // ---------------------------------------------------------------------------
  25. const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
  26. // ---------------------------------------------------------------------------
  27. Client::Client(const sp<SurfaceFlinger>& flinger)
  28. : mFlinger(flinger)
  29. {
  30. }
  31. status_t Client::initCheck() const {
  32. return NO_ERROR;
  33. }
  34. void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
  35. {
  36. Mutex::Autolock _l(mLock);
  37. mLayers.add(handle, layer);
  38. }
  39. void Client::detachLayer(const Layer* layer)
  40. {
  41. Mutex::Autolock _l(mLock);
  42. // we do a linear search here, because this doesn't happen often
  43. const size_t count = mLayers.size();
  44. for (size_t i=0 ; i<count ; i++) {
  45. if (mLayers.valueAt(i) == layer) {
  46. mLayers.removeItemsAt(i, 1);
  47. break;
  48. }
  49. }
  50. }
  51. sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const
  52. {
  53. Mutex::Autolock _l(mLock);
  54. sp<Layer> lbc;
  55. wp<Layer> layer(mLayers.valueFor(handle));
  56. if (layer != 0) {
  57. lbc = layer.promote();
  58. ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get());
  59. }
  60. return lbc;
  61. }
  62. status_t Client::createSurface(const String8& name, uint32_t w, uint32_t h, PixelFormat format,
  63. uint32_t flags, const sp<IBinder>& parentHandle,
  64. LayerMetadata metadata, sp<IBinder>* handle,
  65. sp<IGraphicBufferProducer>* gbp) {
  66. // We rely on createLayer to check permissions.
  67. return mFlinger->createLayer(name, String8(""), this, w, h, format, flags, std::move(metadata), handle, gbp,
  68. parentHandle);
  69. }
  70. status_t Client::createSurfaceX(const String8& name, const String8& systemname, uint32_t w, uint32_t h, PixelFormat format,
  71. uint32_t flags, const sp<IBinder>& parentHandle,
  72. LayerMetadata metadata, sp<IBinder>* handle,
  73. sp<IGraphicBufferProducer>* gbp) {
  74. // We rely on createLayer to check permissions.
  75. return mFlinger->createLayer(name, systemname, this, w, h, format, flags, std::move(metadata), handle, gbp,
  76. parentHandle);
  77. }
  78. status_t Client::createWithSurfaceParent(const String8& name, uint32_t w, uint32_t h,
  79. PixelFormat format, uint32_t flags,
  80. const sp<IGraphicBufferProducer>& parent,
  81. LayerMetadata metadata, sp<IBinder>* handle,
  82. sp<IGraphicBufferProducer>* gbp) {
  83. if (mFlinger->authenticateSurfaceTexture(parent) == false) {
  84. ALOGE("failed to authenticate surface texture");
  85. // The extra parent layer check below before returning is to help with debugging
  86. // b/134888387. Once the bug is fixed the check can be deleted.
  87. if ((static_cast<MonitoredProducer*>(parent.get()))->getLayer() == nullptr) {
  88. ALOGE("failed to find parent layer");
  89. }
  90. return BAD_VALUE;
  91. }
  92. const auto& layer = (static_cast<MonitoredProducer*>(parent.get()))->getLayer();
  93. if (layer == nullptr) {
  94. ALOGE("failed to find parent layer");
  95. return BAD_VALUE;
  96. }
  97. return mFlinger->createLayer(name, String8(""), this, w, h, format, flags, std::move(metadata), handle, gbp,
  98. nullptr, layer);
  99. }
  100. status_t Client::createWithSurfaceParentX(const String8& name, const String8& systemname, uint32_t w, uint32_t h,
  101. PixelFormat format, uint32_t flags,
  102. const sp<IGraphicBufferProducer>& parent,
  103. LayerMetadata metadata, sp<IBinder>* handle,
  104. sp<IGraphicBufferProducer>* gbp) {
  105. if (mFlinger->authenticateSurfaceTexture(parent) == false) {
  106. ALOGE("failed to authenticate surface texture");
  107. // The extra parent layer check below before returning is to help with debugging
  108. // b/134888387. Once the bug is fixed the check can be deleted.
  109. if ((static_cast<MonitoredProducer*>(parent.get()))->getLayer() == nullptr) {
  110. ALOGE("failed to find parent layer");
  111. }
  112. return BAD_VALUE;
  113. }
  114. const auto& layer = (static_cast<MonitoredProducer*>(parent.get()))->getLayer();
  115. if (layer == nullptr) {
  116. ALOGE("failed to find parent layer");
  117. return BAD_VALUE;
  118. }
  119. return mFlinger->createLayer(name, systemname, this, w, h, format, flags, std::move(metadata), handle, gbp,
  120. nullptr, layer);
  121. }
  122. status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const {
  123. sp<Layer> layer = getLayerUser(handle);
  124. if (layer == nullptr) {
  125. return NAME_NOT_FOUND;
  126. }
  127. layer->clearFrameStats();
  128. return NO_ERROR;
  129. }
  130. status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const {
  131. sp<Layer> layer = getLayerUser(handle);
  132. if (layer == nullptr) {
  133. return NAME_NOT_FOUND;
  134. }
  135. layer->getFrameStats(outStats);
  136. return NO_ERROR;
  137. }
  138. // ---------------------------------------------------------------------------
  139. }; // namespace android