1 /* 2 * Copyright (c) 2021 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 UTEST_CAMERA_HOST_IMPL_TEST_H 17 #define UTEST_CAMERA_HOST_IMPL_TEST_H 18 19 #include "utest_camera_hdi_base.h" 20 21 class OffileStreamOperatorImplTest : public CameraHdiBaseTest { 22 public: 23 static void SetUpTestCase(void); 24 static void TearDownTestCase(void); 25 26 void SetUp(void); 27 void TearDown(void); 28 29 class TestBufferConsumerListener : public IBufferConsumerListener { 30 public: TestBufferConsumerListener()31 TestBufferConsumerListener() 32 { 33 } OnBufferAvailable()34 void OnBufferAvailable() 35 { 36 } ~TestBufferConsumerListener()37 ~TestBufferConsumerListener() 38 { 39 } 40 }; 41 42 class StreamConsumer { 43 public: CreateProducer(std::function<void (void *,uint32_t)> callback)44 OHOS::sptr<OHOS::IBufferProducer> CreateProducer(std::function<void(void*, uint32_t)> callback) 45 { 46 consumer_ = OHOS::Surface::CreateSurfaceAsConsumer(); 47 if (consumer_ == nullptr) { 48 return nullptr; 49 } 50 sptr<IBufferConsumerListener> listener = new TestBufferConsumerListener(); 51 consumer_->RegisterConsumerListener(listener); 52 53 auto producer = consumer_->GetProducer(); 54 CAMERA_LOGI("%{public}s, to create a buffer queue producer %{public}p", __FUNCTION__, producer.GetRefPtr()); 55 56 if (producer == nullptr) { 57 return nullptr; 58 } 59 callback_ = callback; 60 61 consumerThread_ = new std::thread([this] { 62 OHOS::Rect damage; 63 int32_t flushFence = 0; 64 int64_t timestamp = 0; 65 while (running_ == true) { 66 OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr; 67 consumer_->AcquireBuffer(buffer, flushFence, timestamp, damage); 68 if (buffer != nullptr) { 69 void* address = buffer->GetVirAddr(); 70 uint32_t size = buffer->GetSize(); 71 if (callback_ != nullptr) { 72 callback_(address, size); 73 } 74 consumer_->ReleaseBuffer(buffer, -1); 75 shotCount_--; 76 if (shotCount_ == 0) { 77 std::unique_lock<std::mutex> l(l_); 78 cv_.notify_one(); 79 } 80 } 81 } 82 }); 83 84 return producer; 85 } 86 TakeSnapshot()87 void TakeSnapshot() 88 { 89 shotCount_++; 90 } 91 WaitSnapshotEnd()92 void WaitSnapshotEnd() const 93 { 94 std::unique_lock<std::mutex> l(l_); 95 cv_.wait(l, [this]() { return shotCount_ == 0; }); 96 } 97 ~StreamConsumer()98 ~StreamConsumer() 99 { 100 running_ = false; 101 if (consumerThread_ != nullptr) { 102 consumerThread_->join(); 103 delete consumerThread_; 104 } 105 } 106 107 public: 108 std::atomic<uint64_t> shotCount_ = 0; 109 std::mutex l_; 110 bool running_ = true; 111 std::condition_variable cv_; 112 OHOS::sptr<OHOS::Surface> consumer_ = nullptr; 113 std::function<void(void*, uint32_t)> callback_ = nullptr; 114 std::thread* consumerThread_ = nullptr; 115 }; 116 }; 117 #endif /* UTEST_CAMERA_HOST_IMPL_TEST_H */ 118