frameworks/native
Revision | 79c6f61a2a719fcf222bbc73abc1eebaac8257df (tree) |
---|---|
Zeit | 2020-03-13 13:59:25 |
Autor | Chih-Wei Huang <cwhuang@linu...> |
Commiter | Chih-Wei Huang |
Merge tag 'android-9.0.0_r54' into pie-x86
Android 9.0.0 release 54
Conflicts:
services/surfaceflinger/SurfaceFlinger.h
@@ -76,6 +76,10 @@ public: | ||
76 | 76 | |
77 | 77 | sp<SurfaceComposerClient> getClient() const; |
78 | 78 | |
79 | + SurfaceControl(const sp<SurfaceComposerClient>& client, | |
80 | + const sp<IBinder>& handle, | |
81 | + const sp<IGraphicBufferProducer>& gbp, | |
82 | + bool owned); | |
79 | 83 | private: |
80 | 84 | // can't be copied |
81 | 85 | SurfaceControl& operator = (SurfaceControl& rhs); |
@@ -84,12 +88,6 @@ private: | ||
84 | 88 | friend class SurfaceComposerClient; |
85 | 89 | friend class Surface; |
86 | 90 | |
87 | - SurfaceControl( | |
88 | - const sp<SurfaceComposerClient>& client, | |
89 | - const sp<IBinder>& handle, | |
90 | - const sp<IGraphicBufferProducer>& gbp, | |
91 | - bool owned); | |
92 | - | |
93 | 91 | ~SurfaceControl(); |
94 | 92 | |
95 | 93 | sp<Surface> generateSurfaceLocked() const; |
@@ -106,19 +106,18 @@ void Client::detachLayer(const Layer* layer) | ||
106 | 106 | } |
107 | 107 | } |
108 | 108 | } |
109 | -sp<Layer> Client::getLayerUser(const sp<IBinder>& handle) const | |
109 | + | |
110 | +bool Client::isAttached(const sp<IBinder>& handle) const | |
110 | 111 | { |
111 | 112 | Mutex::Autolock _l(mLock); |
112 | 113 | sp<Layer> lbc; |
113 | 114 | wp<Layer> layer(mLayers.valueFor(handle)); |
114 | 115 | if (layer != 0) { |
115 | - lbc = layer.promote(); | |
116 | - ALOGE_IF(lbc==0, "getLayerUser(name=%p) is dead", handle.get()); | |
116 | + return true; | |
117 | 117 | } |
118 | - return lbc; | |
118 | + return false; | |
119 | 119 | } |
120 | 120 | |
121 | - | |
122 | 121 | status_t Client::onTransact( |
123 | 122 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
124 | 123 | { |
@@ -144,7 +143,6 @@ status_t Client::onTransact( | ||
144 | 143 | return BnSurfaceComposerClient::onTransact(code, data, reply, flags); |
145 | 144 | } |
146 | 145 | |
147 | - | |
148 | 146 | status_t Client::createSurface( |
149 | 147 | const String8& name, |
150 | 148 | uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, |
@@ -152,22 +150,11 @@ status_t Client::createSurface( | ||
152 | 150 | sp<IBinder>* handle, |
153 | 151 | sp<IGraphicBufferProducer>* gbp) |
154 | 152 | { |
155 | - sp<Layer> parent = nullptr; | |
156 | - if (parentHandle != nullptr) { | |
157 | - auto layerHandle = reinterpret_cast<Layer::Handle*>(parentHandle.get()); | |
158 | - parent = layerHandle->owner.promote(); | |
159 | - if (parent == nullptr) { | |
160 | - return NAME_NOT_FOUND; | |
161 | - } | |
162 | - } | |
163 | - if (parent == nullptr) { | |
164 | - bool parentDied; | |
165 | - parent = getParentLayer(&parentDied); | |
166 | - // If we had a parent, but it died, we've lost all | |
167 | - // our capabilities. | |
168 | - if (parentDied) { | |
169 | - return NAME_NOT_FOUND; | |
170 | - } | |
153 | + bool parentDied; | |
154 | + sp<Layer> parentLayer; | |
155 | + if (!parentHandle) parentLayer = getParentLayer(&parentDied); | |
156 | + if (parentHandle == nullptr && parentDied) { | |
157 | + return NAME_NOT_FOUND; | |
171 | 158 | } |
172 | 159 | |
173 | 160 | /* |
@@ -184,7 +171,8 @@ status_t Client::createSurface( | ||
184 | 171 | uint32_t w, h; |
185 | 172 | PixelFormat format; |
186 | 173 | uint32_t flags; |
187 | - sp<Layer>* parent; | |
174 | + const sp<IBinder>& parentHandle; | |
175 | + const sp<Layer>& parentLayer; | |
188 | 176 | int32_t windowType; |
189 | 177 | int32_t ownerUid; |
190 | 178 | public: |
@@ -193,23 +181,25 @@ status_t Client::createSurface( | ||
193 | 181 | uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, |
194 | 182 | sp<IBinder>* handle, int32_t windowType, int32_t ownerUid, |
195 | 183 | sp<IGraphicBufferProducer>* gbp, |
196 | - sp<Layer>* parent) | |
184 | + const sp<IBinder>& parentHandle, | |
185 | + const sp<Layer>& parentLayer) | |
197 | 186 | : flinger(flinger), client(client), |
198 | 187 | handle(handle), gbp(gbp), result(NO_ERROR), |
199 | 188 | name(name), w(w), h(h), format(format), flags(flags), |
200 | - parent(parent), windowType(windowType), ownerUid(ownerUid) { | |
189 | + parentHandle(parentHandle), parentLayer(parentLayer), | |
190 | + windowType(windowType), ownerUid(ownerUid) { | |
201 | 191 | } |
202 | 192 | status_t getResult() const { return result; } |
203 | 193 | virtual bool handler() { |
204 | 194 | result = flinger->createLayer(name, client, w, h, format, flags, |
205 | - windowType, ownerUid, handle, gbp, parent); | |
195 | + windowType, ownerUid, handle, gbp, parentHandle, parentLayer); | |
206 | 196 | return true; |
207 | 197 | } |
208 | 198 | }; |
209 | 199 | |
210 | 200 | sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(), |
211 | 201 | name, this, w, h, format, flags, handle, |
212 | - windowType, ownerUid, gbp, &parent); | |
202 | + windowType, ownerUid, gbp, parentHandle, parentLayer); | |
213 | 203 | mFlinger->postMessageSync(msg); |
214 | 204 | return static_cast<MessageCreateLayer*>( msg.get() )->getResult(); |
215 | 205 | } |
@@ -219,21 +209,11 @@ status_t Client::destroySurface(const sp<IBinder>& handle) { | ||
219 | 209 | } |
220 | 210 | |
221 | 211 | status_t Client::clearLayerFrameStats(const sp<IBinder>& handle) const { |
222 | - sp<Layer> layer = getLayerUser(handle); | |
223 | - if (layer == nullptr) { | |
224 | - return NAME_NOT_FOUND; | |
225 | - } | |
226 | - layer->clearFrameStats(); | |
227 | - return NO_ERROR; | |
212 | + return mFlinger->clearLayerFrameStats(this, handle); | |
228 | 213 | } |
229 | 214 | |
230 | 215 | status_t Client::getLayerFrameStats(const sp<IBinder>& handle, FrameStats* outStats) const { |
231 | - sp<Layer> layer = getLayerUser(handle); | |
232 | - if (layer == nullptr) { | |
233 | - return NAME_NOT_FOUND; | |
234 | - } | |
235 | - layer->getFrameStats(outStats); | |
236 | - return NO_ERROR; | |
216 | + return mFlinger->getLayerFrameStats(this, handle, outStats); | |
237 | 217 | } |
238 | 218 | |
239 | 219 | // --------------------------------------------------------------------------- |
@@ -49,7 +49,7 @@ public: | ||
49 | 49 | |
50 | 50 | void detachLayer(const Layer* layer); |
51 | 51 | |
52 | - sp<Layer> getLayerUser(const sp<IBinder>& handle) const; | |
52 | + bool isAttached (const sp<IBinder>& handle) const; | |
53 | 53 | |
54 | 54 | void updateParent(const sp<Layer>& parentLayer); |
55 | 55 |
@@ -58,7 +58,7 @@ private: | ||
58 | 58 | virtual status_t createSurface( |
59 | 59 | const String8& name, |
60 | 60 | uint32_t w, uint32_t h,PixelFormat format, uint32_t flags, |
61 | - const sp<IBinder>& parent, int32_t windowType, int32_t ownerUid, | |
61 | + const sp<IBinder>& parentHandle, int32_t windowType, int32_t ownerUid, | |
62 | 62 | sp<IBinder>* handle, |
63 | 63 | sp<IGraphicBufferProducer>* gbp); |
64 | 64 |
@@ -170,7 +170,6 @@ void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {} | ||
170 | 170 | |
171 | 171 | void Layer::onRemovedFromCurrentState() { |
172 | 172 | // the layer is removed from SF mCurrentState to mLayersPendingRemoval |
173 | - | |
174 | 173 | mPendingRemoval = true; |
175 | 174 | |
176 | 175 | if (mCurrentState.zOrderRelativeOf != nullptr) { |
@@ -212,6 +211,11 @@ bool Layer::getPremultipledAlpha() const { | ||
212 | 211 | |
213 | 212 | sp<IBinder> Layer::getHandle() { |
214 | 213 | Mutex::Autolock _l(mLock); |
214 | + if (mGetHandleCalled) { | |
215 | + ALOGE("Get handle called twice" ); | |
216 | + return nullptr; | |
217 | + } | |
218 | + mGetHandleCalled = true; | |
215 | 219 | return new Handle(mFlinger, this); |
216 | 220 | } |
217 | 221 |
@@ -699,6 +699,8 @@ public: | ||
699 | 699 | wp<Layer> owner; |
700 | 700 | }; |
701 | 701 | |
702 | + // Creates a new handle each time, so we only expect | |
703 | + // this to be called once. | |
702 | 704 | sp<IBinder> getHandle(); |
703 | 705 | const String8& getName() const; |
704 | 706 | virtual void notifyAvailableFrames() {} |
@@ -803,6 +805,7 @@ private: | ||
803 | 805 | const LayerVector::Visitor& visitor); |
804 | 806 | LayerVector makeChildrenTraversalList(LayerVector::StateSet stateSet, |
805 | 807 | const std::vector<Layer*>& layersInTree); |
808 | + bool mGetHandleCalled = false; | |
806 | 809 | }; |
807 | 810 | |
808 | 811 | // --------------------------------------------------------------------------- |
@@ -3086,20 +3086,34 @@ void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& displayDevice, | ||
3086 | 3086 | engine.fillRegionWithColor(region, height, 0, 0, 0, 0); |
3087 | 3087 | } |
3088 | 3088 | |
3089 | -status_t SurfaceFlinger::addClientLayer(const sp<Client>& client, | |
3090 | - const sp<IBinder>& handle, | |
3091 | - const sp<IGraphicBufferProducer>& gbc, | |
3092 | - const sp<Layer>& lbc, | |
3093 | - const sp<Layer>& parent) | |
3094 | -{ | |
3089 | +status_t SurfaceFlinger::addClientLayer(const sp<Client>& client, const sp<IBinder>& handle, | |
3090 | + const sp<IGraphicBufferProducer>& gbc, const sp<Layer>& lbc, | |
3091 | + const sp<IBinder>& parentHandle, | |
3092 | + const sp<Layer>& parentLayer) { | |
3095 | 3093 | // add this layer to the current state list |
3096 | 3094 | { |
3097 | 3095 | Mutex::Autolock _l(mStateLock); |
3096 | + sp<Layer> parent; | |
3097 | + | |
3098 | + if (parentHandle != nullptr) { | |
3099 | + parent = fromHandle(parentHandle); | |
3100 | + if (parent == nullptr) { | |
3101 | + return NAME_NOT_FOUND; | |
3102 | + } | |
3103 | + } else { | |
3104 | + parent = parentLayer; | |
3105 | + } | |
3106 | + | |
3098 | 3107 | if (mNumLayers >= MAX_LAYERS) { |
3099 | 3108 | ALOGE("AddClientLayer failed, mNumLayers (%zu) >= MAX_LAYERS (%zu)", mNumLayers, |
3100 | 3109 | MAX_LAYERS); |
3101 | 3110 | return NO_MEMORY; |
3102 | 3111 | } |
3112 | + | |
3113 | + auto [itr, inserted] = mLayersByLocalBinderToken.emplace(handle->localBinder(), lbc); | |
3114 | + if (!inserted) { | |
3115 | + ALOGE("-----------ERROR REGISTERING HANDLE, layer pair: handle is already a key"); | |
3116 | + } | |
3103 | 3117 | if (parent == nullptr) { |
3104 | 3118 | mCurrentState.layersSortedByZ.add(lbc); |
3105 | 3119 | } else { |
@@ -3128,6 +3142,23 @@ status_t SurfaceFlinger::addClientLayer(const sp<Client>& client, | ||
3128 | 3142 | return NO_ERROR; |
3129 | 3143 | } |
3130 | 3144 | |
3145 | +status_t SurfaceFlinger::removeLayerFromMap(const wp<Layer>& layer) { | |
3146 | + auto it = mLayersByLocalBinderToken.begin(); | |
3147 | + while (it != mLayersByLocalBinderToken.end()) { | |
3148 | + if (it->second == layer) { | |
3149 | + it = mLayersByLocalBinderToken.erase(it); | |
3150 | + break; | |
3151 | + } else { | |
3152 | + it++; | |
3153 | + } | |
3154 | + } | |
3155 | + if (it == mLayersByLocalBinderToken.end()) { | |
3156 | + ALOGE("Failed to remove layer from mapping - could not find matching layer"); | |
3157 | + return BAD_VALUE; | |
3158 | + } | |
3159 | + return NO_ERROR; | |
3160 | +} | |
3161 | + | |
3131 | 3162 | status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer, bool topLevelOnly) { |
3132 | 3163 | Mutex::Autolock _l(mStateLock); |
3133 | 3164 | return removeLayerLocked(mStateLock, layer, topLevelOnly); |
@@ -3366,8 +3397,8 @@ uint32_t SurfaceFlinger::setClientStateLocked(const ComposerState& composerState | ||
3366 | 3397 | const layer_state_t& s = composerState.state; |
3367 | 3398 | sp<Client> client(static_cast<Client*>(composerState.client.get())); |
3368 | 3399 | |
3369 | - sp<Layer> layer(client->getLayerUser(s.surface)); | |
3370 | - if (layer == nullptr) { | |
3400 | + sp<Layer> layer = fromHandle(s.surface); | |
3401 | + if (layer == nullptr || !(client->isAttached(s.surface))) { | |
3371 | 3402 | return 0; |
3372 | 3403 | } |
3373 | 3404 |
@@ -3527,8 +3558,8 @@ void SurfaceFlinger::setDestroyStateLocked(const ComposerState& composerState) { | ||
3527 | 3558 | const layer_state_t& state = composerState.state; |
3528 | 3559 | sp<Client> client(static_cast<Client*>(composerState.client.get())); |
3529 | 3560 | |
3530 | - sp<Layer> layer(client->getLayerUser(state.surface)); | |
3531 | - if (layer == nullptr) { | |
3561 | + sp<Layer> layer = fromHandle(state.surface); | |
3562 | + if (layer == nullptr || !(client->isAttached(state.surface))) { | |
3532 | 3563 | return; |
3533 | 3564 | } |
3534 | 3565 |
@@ -3542,13 +3573,12 @@ void SurfaceFlinger::setDestroyStateLocked(const ComposerState& composerState) { | ||
3542 | 3573 | } |
3543 | 3574 | } |
3544 | 3575 | |
3545 | -status_t SurfaceFlinger::createLayer( | |
3546 | - const String8& name, | |
3547 | - const sp<Client>& client, | |
3548 | - uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, | |
3549 | - int32_t windowType, int32_t ownerUid, sp<IBinder>* handle, | |
3550 | - sp<IGraphicBufferProducer>* gbp, sp<Layer>* parent) | |
3551 | -{ | |
3576 | +status_t SurfaceFlinger::createLayer(const String8& name, const sp<Client>& client, uint32_t w, | |
3577 | + uint32_t h, PixelFormat format, uint32_t flags, | |
3578 | + int32_t windowType, int32_t ownerUid, sp<IBinder>* handle, | |
3579 | + sp<IGraphicBufferProducer>* gbp, | |
3580 | + const sp<IBinder>& parentHandle, | |
3581 | + const sp<Layer>& parentLayer) { | |
3552 | 3582 | if (int32_t(w|h) < 0) { |
3553 | 3583 | ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)", |
3554 | 3584 | int(w), int(h)); |
@@ -3591,7 +3621,7 @@ status_t SurfaceFlinger::createLayer( | ||
3591 | 3621 | |
3592 | 3622 | layer->setInfo(windowType, ownerUid); |
3593 | 3623 | |
3594 | - result = addClientLayer(client, *handle, *gbp, layer, *parent); | |
3624 | + result = addClientLayer(client, *handle, *gbp, layer, parentHandle, parentLayer); | |
3595 | 3625 | if (result != NO_ERROR) { |
3596 | 3626 | return result; |
3597 | 3627 | } |
@@ -3661,14 +3691,35 @@ status_t SurfaceFlinger::createColorLayer(const sp<Client>& client, | ||
3661 | 3691 | return NO_ERROR; |
3662 | 3692 | } |
3663 | 3693 | |
3694 | +status_t SurfaceFlinger::clearLayerFrameStats(const sp<const Client>& client, const sp<IBinder>& handle) { | |
3695 | + Mutex::Autolock _l(mStateLock); | |
3696 | + sp<Layer> layer = fromHandle(handle); | |
3697 | + if (layer == nullptr || !(client->isAttached(handle))) { | |
3698 | + return NAME_NOT_FOUND; | |
3699 | + } | |
3700 | + layer->clearFrameStats(); | |
3701 | + return NO_ERROR; | |
3702 | +} | |
3703 | + | |
3704 | +status_t SurfaceFlinger::getLayerFrameStats(const sp<const Client>& client, const sp<IBinder>& handle, FrameStats* outStats) { | |
3705 | + Mutex::Autolock _l(mStateLock); | |
3706 | + sp<Layer> layer = fromHandle(handle); | |
3707 | + if (layer == nullptr || !(client->isAttached(handle))) { | |
3708 | + return NAME_NOT_FOUND; | |
3709 | + } | |
3710 | + layer->getFrameStats(outStats); | |
3711 | + return NO_ERROR; | |
3712 | +} | |
3713 | + | |
3664 | 3714 | status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle) |
3665 | 3715 | { |
3716 | + Mutex::Autolock _l(mStateLock); | |
3666 | 3717 | // called by a client when it wants to remove a Layer |
3667 | 3718 | status_t err = NO_ERROR; |
3668 | - sp<Layer> l(client->getLayerUser(handle)); | |
3669 | - if (l != nullptr) { | |
3719 | + sp<Layer> l = fromHandle(handle); | |
3720 | + if (l != nullptr || client->isAttached(handle)) { | |
3670 | 3721 | mInterceptor->saveSurfaceDeletion(l); |
3671 | - err = removeLayer(l); | |
3722 | + err = removeLayerLocked(mStateLock, l); | |
3672 | 3723 | ALOGE_IF(err<0 && err != NAME_NOT_FOUND, |
3673 | 3724 | "error removing layer=%p (%s)", l.get(), strerror(-err)); |
3674 | 3725 | } |
@@ -3677,15 +3728,18 @@ status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBind | ||
3677 | 3728 | |
3678 | 3729 | status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer) |
3679 | 3730 | { |
3731 | + Mutex::Autolock _l(mStateLock); | |
3680 | 3732 | // called by ~LayerCleaner() when all references to the IBinder (handle) |
3681 | 3733 | // are gone |
3682 | 3734 | sp<Layer> l = layer.promote(); |
3683 | 3735 | if (l == nullptr) { |
3736 | + removeLayerFromMap(layer); | |
3684 | 3737 | // The layer has already been removed, carry on |
3685 | 3738 | return NO_ERROR; |
3686 | 3739 | } |
3740 | + removeLayerFromMap(layer); | |
3687 | 3741 | // If we have a parent, then we can continue to live as long as it does. |
3688 | - return removeLayer(l, true); | |
3742 | + return removeLayerLocked(mStateLock, l, true); | |
3689 | 3743 | } |
3690 | 3744 | |
3691 | 3745 | // --------------------------------------------------------------------------- |
@@ -4843,34 +4897,40 @@ status_t SurfaceFlinger::captureLayers(const sp<IBinder>& layerHandleBinder, | ||
4843 | 4897 | const bool mChildrenOnly; |
4844 | 4898 | }; |
4845 | 4899 | |
4846 | - auto layerHandle = reinterpret_cast<Layer::Handle*>(layerHandleBinder.get()); | |
4847 | - auto parent = layerHandle->owner.promote(); | |
4900 | + int reqWidth = 0; | |
4901 | + int reqHeight = 0; | |
4902 | + sp<Layer> parent; | |
4903 | + Rect crop(sourceCrop); | |
4848 | 4904 | |
4849 | - if (parent == nullptr || parent->isPendingRemoval()) { | |
4850 | - ALOGE("captureLayers called with a removed parent"); | |
4851 | - return NAME_NOT_FOUND; | |
4852 | - } | |
4905 | + { | |
4906 | + Mutex::Autolock _l(mStateLock); | |
4853 | 4907 | |
4854 | - const int uid = IPCThreadState::self()->getCallingUid(); | |
4855 | - const bool forSystem = uid == AID_GRAPHICS || uid == AID_SYSTEM; | |
4856 | - if (!forSystem && parent->getCurrentState().flags & layer_state_t::eLayerSecure) { | |
4857 | - ALOGW("Attempting to capture secure layer: PERMISSION_DENIED"); | |
4858 | - return PERMISSION_DENIED; | |
4859 | - } | |
4908 | + parent = fromHandle(layerHandleBinder); | |
4909 | + if (parent == nullptr || parent->isPendingRemoval()) { | |
4910 | + ALOGE("captureLayers called with an invalid or removed parent"); | |
4911 | + return NAME_NOT_FOUND; | |
4912 | + } | |
4860 | 4913 | |
4861 | - Rect crop(sourceCrop); | |
4862 | - if (sourceCrop.width() <= 0) { | |
4863 | - crop.left = 0; | |
4864 | - crop.right = parent->getCurrentState().active.w; | |
4865 | - } | |
4914 | + const int uid = IPCThreadState::self()->getCallingUid(); | |
4915 | + const bool forSystem = uid == AID_GRAPHICS || uid == AID_SYSTEM; | |
4916 | + if (!forSystem && parent->getCurrentState().flags & layer_state_t::eLayerSecure) { | |
4917 | + ALOGW("Attempting to capture secure layer: PERMISSION_DENIED"); | |
4918 | + return PERMISSION_DENIED; | |
4919 | + } | |
4866 | 4920 | |
4867 | - if (sourceCrop.height() <= 0) { | |
4868 | - crop.top = 0; | |
4869 | - crop.bottom = parent->getCurrentState().active.h; | |
4870 | - } | |
4921 | + if (sourceCrop.width() <= 0) { | |
4922 | + crop.left = 0; | |
4923 | + crop.right = parent->getCurrentState().active.w; | |
4924 | + } | |
4871 | 4925 | |
4872 | - int32_t reqWidth = crop.width() * frameScale; | |
4873 | - int32_t reqHeight = crop.height() * frameScale; | |
4926 | + if (sourceCrop.height() <= 0) { | |
4927 | + crop.top = 0; | |
4928 | + crop.bottom = parent->getCurrentState().active.h; | |
4929 | + } | |
4930 | + | |
4931 | + reqWidth = crop.width() * frameScale; | |
4932 | + reqHeight = crop.height() * frameScale; | |
4933 | + } // mStateLock | |
4874 | 4934 | |
4875 | 4935 | LayerRenderArea renderArea(this, parent, crop, reqWidth, reqHeight, childrenOnly); |
4876 | 4936 |
@@ -5195,8 +5255,22 @@ void SurfaceFlinger::traverseLayersInDisplay(const sp<const DisplayDevice>& hw, | ||
5195 | 5255 | } |
5196 | 5256 | } |
5197 | 5257 | |
5198 | -}; // namespace android | |
5258 | +sp<Layer> SurfaceFlinger::fromHandle(const sp<IBinder>& handle) { | |
5259 | + if (!handle) return nullptr; | |
5260 | + | |
5261 | + BBinder *b = handle->localBinder(); | |
5262 | + if (b == nullptr) { | |
5263 | + return nullptr; | |
5264 | + } | |
5265 | + auto it = mLayersByLocalBinderToken.find(b); | |
5266 | + if (it != mLayersByLocalBinderToken.end()) { | |
5267 | + auto ret = it->second.promote(); | |
5268 | + return ret; | |
5269 | + } | |
5270 | + return nullptr; | |
5271 | +} | |
5199 | 5272 | |
5273 | +} // namespace android | |
5200 | 5274 | |
5201 | 5275 | #if defined(__gl_h_) |
5202 | 5276 | #error "don't include gl/gl.h in this file" |
@@ -343,6 +343,12 @@ public: | ||
343 | 343 | |
344 | 344 | int getPrimaryDisplayOrientation() const { return mPrimaryDisplayOrientation; } |
345 | 345 | |
346 | + status_t clearLayerFrameStats(const sp<const Client>& client, const sp<IBinder>& handle); | |
347 | + | |
348 | + status_t getLayerFrameStats(const sp<const Client>& client, const sp<IBinder>& handle, FrameStats* outStats); | |
349 | + | |
350 | + sp<Layer> fromHandle(const sp<IBinder>& handle) REQUIRES(mStateLock); | |
351 | + | |
346 | 352 | void screenReleased(const sp<IBinder>& display); |
347 | 353 | void screenAcquired(const sp<IBinder>& display); |
348 | 354 |
@@ -519,17 +525,17 @@ private: | ||
519 | 525 | uint32_t setTransactionFlags(uint32_t flags, VSyncModulator::TransactionStart transactionStart); |
520 | 526 | void commitTransaction(); |
521 | 527 | bool containsAnyInvalidClientState(const Vector<ComposerState>& states); |
522 | - uint32_t setClientStateLocked(const ComposerState& composerState); | |
528 | + uint32_t setClientStateLocked(const ComposerState& composerState) REQUIRES(mStateLock); | |
523 | 529 | uint32_t setDisplayStateLocked(const DisplayState& s); |
524 | - void setDestroyStateLocked(const ComposerState& composerState); | |
530 | + void setDestroyStateLocked(const ComposerState& composerState) REQUIRES(mStateLock); | |
525 | 531 | |
526 | 532 | /* ------------------------------------------------------------------------ |
527 | 533 | * Layer management |
528 | 534 | */ |
529 | - status_t createLayer(const String8& name, const sp<Client>& client, | |
530 | - uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, | |
531 | - int32_t windowType, int32_t ownerUid, sp<IBinder>* handle, | |
532 | - sp<IGraphicBufferProducer>* gbp, sp<Layer>* parent); | |
535 | + status_t createLayer(const String8& name, const sp<Client>& client, uint32_t w, uint32_t h, | |
536 | + PixelFormat format, uint32_t flags, int32_t windowType, int32_t ownerUid, | |
537 | + sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, | |
538 | + const sp<IBinder>& parentHandle, const sp<Layer>& parentLayer = nullptr); | |
533 | 539 | |
534 | 540 | status_t createBufferLayer(const sp<Client>& client, const String8& name, |
535 | 541 | uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format, |
@@ -555,12 +561,13 @@ private: | ||
555 | 561 | status_t removeLayer(const sp<Layer>& layer, bool topLevelOnly = false); |
556 | 562 | status_t removeLayerLocked(const Mutex&, const sp<Layer>& layer, bool topLevelOnly = false); |
557 | 563 | |
564 | + // remove layer from mapping | |
565 | + status_t removeLayerFromMap(const wp<Layer>& layer); | |
566 | + | |
558 | 567 | // add a layer to SurfaceFlinger |
559 | - status_t addClientLayer(const sp<Client>& client, | |
560 | - const sp<IBinder>& handle, | |
561 | - const sp<IGraphicBufferProducer>& gbc, | |
562 | - const sp<Layer>& lbc, | |
563 | - const sp<Layer>& parent); | |
568 | + status_t addClientLayer(const sp<Client>& client, const sp<IBinder>& handle, | |
569 | + const sp<IGraphicBufferProducer>& gbc, const sp<Layer>& lbc, | |
570 | + const sp<IBinder>& parentHandle, const sp<Layer>& parentLayer); | |
564 | 571 | |
565 | 572 | /* ------------------------------------------------------------------------ |
566 | 573 | * Boot animation, on/off animations and screen capture |
@@ -823,6 +830,9 @@ private: | ||
823 | 830 | // it may be read from other threads with mStateLock held |
824 | 831 | DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays; |
825 | 832 | |
833 | + // protected by mStateLock | |
834 | + std::unordered_map<BBinder*, wp<Layer>> mLayersByLocalBinderToken; | |
835 | + | |
826 | 836 | // don't use a lock for these, we don't care |
827 | 837 | int mDebugRegion; |
828 | 838 | int mDebugDDMS; |
@@ -18,7 +18,8 @@ cc_test { | ||
18 | 18 | tags: ["test"], |
19 | 19 | test_suites: ["device-tests"], |
20 | 20 | srcs: [ |
21 | - "Stress_test.cpp", | |
21 | + "InvalidHandles_test.cpp", | |
22 | + "Stress_test.cpp", | |
22 | 23 | "SurfaceInterceptor_test.cpp", |
23 | 24 | "Transaction_test.cpp", |
24 | 25 | ], |
@@ -0,0 +1,67 @@ | ||
1 | +/* | |
2 | + * Copyright (C) 2019 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 | +#include <binder/Binder.h> | |
18 | + | |
19 | +#include <gtest/gtest.h> | |
20 | + | |
21 | +#include <gui/ISurfaceComposer.h> | |
22 | +#include <gui/SurfaceComposerClient.h> | |
23 | +#include <private/gui/ComposerService.h> | |
24 | +#include <ui/Rect.h> | |
25 | + | |
26 | +namespace android { | |
27 | +namespace { | |
28 | + | |
29 | +class NotALayer : public BBinder {}; | |
30 | + | |
31 | +/** | |
32 | + * For all of these tests we make a SurfaceControl with an invalid layer handle | |
33 | + * and verify we aren't able to trick SurfaceFlinger. | |
34 | + */ | |
35 | +class InvalidHandleTest : public ::testing::Test { | |
36 | +protected: | |
37 | + sp<SurfaceComposerClient> mScc; | |
38 | + sp<SurfaceControl> mNotSc; | |
39 | + void SetUp() override { | |
40 | + mScc = new SurfaceComposerClient; | |
41 | + ASSERT_EQ(NO_ERROR, mScc->initCheck()); | |
42 | + mNotSc = makeNotSurfaceControl(); | |
43 | + } | |
44 | + | |
45 | + sp<SurfaceControl> makeNotSurfaceControl() { | |
46 | + return new SurfaceControl(mScc, new NotALayer(), nullptr, true); | |
47 | + } | |
48 | +}; | |
49 | + | |
50 | +TEST_F(InvalidHandleTest, createSurfaceInvalidHandle) { | |
51 | + auto notSc = makeNotSurfaceControl(); | |
52 | + ASSERT_EQ(nullptr, | |
53 | + mScc->createSurface(String8("lolcats"), 19, 47, PIXEL_FORMAT_RGBA_8888, 0, | |
54 | + notSc.get()) | |
55 | + .get()); | |
56 | +} | |
57 | + | |
58 | +TEST_F(InvalidHandleTest, captureLayersInvalidHandle) { | |
59 | + sp<ISurfaceComposer> sf(ComposerService::getComposerService()); | |
60 | + sp<GraphicBuffer> outBuffer; | |
61 | + | |
62 | + ASSERT_EQ(NAME_NOT_FOUND, | |
63 | + sf->captureLayers(mNotSc->getHandle(), &outBuffer, Rect::EMPTY_RECT, 1.0f)); | |
64 | +} | |
65 | + | |
66 | +} // namespace | |
67 | +} // namespace android |