1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef INTERFACES_INNERKITS_SURFACE_BUFFER_PRODUCER_LISTENER_H 17 #define INTERFACES_INNERKITS_SURFACE_BUFFER_PRODUCER_LISTENER_H 18 19 #include <refbase.h> 20 #include "iremote_broker.h" 21 #include "buffer_log.h" 22 #include "surface_buffer.h" 23 #include "ibuffer_producer_listener.h" 24 #include "iremote_proxy.h" 25 #include "iremote_stub.h" 26 #include "message_option.h" 27 28 namespace OHOS { 29 class ProducerListenerProxy : public IRemoteProxy<IProducerListener> { 30 public: ProducerListenerProxy(const sptr<IRemoteObject> & impl)31 explicit ProducerListenerProxy(const sptr<IRemoteObject>& impl) : IRemoteProxy<IProducerListener>(impl) {}; 32 virtual ~ProducerListenerProxy() noexcept = default; OnBufferReleased()33 GSError OnBufferReleased() override 34 { 35 MessageOption option; 36 MessageParcel arguments; 37 MessageParcel reply; 38 if (!arguments.WriteInterfaceToken(IProducerListener::GetDescriptor())) { 39 BLOGE("write interface token failed"); 40 return GSERROR_BINDER; 41 } 42 option.SetFlags(MessageOption::TF_ASYNC); 43 int32_t ret = Remote()->SendRequest(IProducerListener::ON_BUFFER_RELEASED, arguments, reply, option); 44 if (ret != ERR_NONE) { 45 return GSERROR_BINDER; 46 } 47 return GSERROR_OK; 48 }; 49 private: 50 static inline BrokerDelegator<ProducerListenerProxy> delegator_; 51 }; 52 53 class ProducerListenerStub : public IRemoteStub<IProducerListener> { 54 public: 55 ProducerListenerStub() = default; 56 ~ProducerListenerStub() = default; OnRemoteRequest(uint32_t code,MessageParcel & arguments,MessageParcel & reply,MessageOption & option)57 int32_t OnRemoteRequest(uint32_t code, MessageParcel &arguments, 58 MessageParcel &reply, MessageOption &option) override 59 { 60 (void)(option); 61 auto remoteDescriptor = arguments.ReadInterfaceToken(); 62 if (GetDescriptor() != remoteDescriptor) { 63 return ERR_INVALID_STATE; 64 } 65 66 auto ret = ERR_NONE; 67 switch (code) { 68 case ON_BUFFER_RELEASED: { 69 auto sret = OnBufferReleased(); 70 reply.WriteInt32(sret); 71 break; 72 } 73 default: { 74 ret = ERR_UNKNOWN_TRANSACTION; 75 break; 76 } 77 } 78 return ret; 79 }; 80 }; 81 82 class BufferReleaseProducerListener : public ProducerListenerStub { 83 public: BufferReleaseProducerListener(OnReleaseFunc func)84 explicit BufferReleaseProducerListener(OnReleaseFunc func): func_(func) {}; ~BufferReleaseProducerListener()85 ~BufferReleaseProducerListener() override {}; OnBufferReleased()86 GSError OnBufferReleased() override 87 { 88 if (func_ != nullptr) { 89 sptr<OHOS::SurfaceBuffer> sBuffer = nullptr; 90 return func_(sBuffer); 91 } 92 return GSERROR_INTERNAL; 93 }; 94 private: 95 OnReleaseFunc func_; 96 }; 97 } // namespace OHOS 98 99 #endif // INTERFACES_INNERKITS_SURFACE_BUFFER_PRODUCER_LISTENER_H 100