• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include "frame.h"
17 #include <thread>
18 #include "battery_log.h"
19 
20 namespace OHOS {
21 namespace HDI {
22 namespace Battery {
23 namespace V1_1 {
24 using namespace std;
25 
Frame(uint32_t w,uint32_t h,View::PixelFormat pixType,SurfaceDev * sfDev)26 Frame::Frame(uint32_t w, uint32_t h, View::PixelFormat pixType, SurfaceDev* sfDev)
27 {
28     this->CreateBuffer(w, h, pixType);
29     this->startX_ = 0;
30     this->startY_ = 0;
31     sfDev_ = sfDev;
32     listIndex_ = 0;
33     flushFlag_ = false;
34     std::thread(&Frame::FlushThreadLoop, this).detach();
35 }
36 
~Frame()37 Frame::~Frame()
38 {
39     needStop_ = true;
40     FreeBuffer();
41 }
42 
FlushThreadLoop()43 void Frame::FlushThreadLoop()
44 {
45     while (!needStop_) {
46         std::unique_lock<std::mutex> locker(mutex_);
47         if (!flushFlag_) {
48             mCondFlush_.wait(mutex_, [&] {
49                 return flushFlag_;
50             });
51         }
52 
53         SyncBuffer();
54         frameMutex_.lock();
55         std::map<View*, int32_t>::iterator iter;
56         for (iter = viewMapList_.begin(); iter != viewMapList_.end(); ++iter) {
57             View* tmpView = (*iter).first;
58             BATTERY_HILOGD(FEATURE_CHARGING, "tmpView->IsVisiable()=%{public}d", tmpView->IsVisiable());
59             if (tmpView->IsVisiable()) {
60                 char* bufTmp = static_cast<char*>(tmpView->GetBuffer());
61                 DrawSubView(tmpView->startX_, tmpView->startY_, tmpView->viewWidth_, tmpView->viewHeight_, bufTmp);
62             }
63         }
64         frameMutex_.unlock();
65         sfDev_->Flip(this->GetBuffer());
66         flushFlag_ = false;
67     }
68 }
69 
ViewRegister(View * view)70 void Frame::ViewRegister(View* view)
71 {
72     std::unique_lock<std::mutex> locker(frameMutex_);
73     view->SetViewId(frameViewId + listIndex_);
74     viewMapList_.insert(std::make_pair(view, frameViewId + listIndex_));
75     if (view->IsFocusAble()) {
76         maxActionIndex_++;
77         BATTERY_HILOGD(FEATURE_CHARGING, "maxActionIndex_=%{public}d", maxActionIndex_);
78     }
79     listIndex_++;
80 }
81 
OnDraw()82 void Frame::OnDraw()
83 {
84     std::unique_lock<std::mutex> locker(mutex_);
85     flushFlag_ = true;
86     mCondFlush_.notify_all();
87 }
88 }  // namespace V1_1
89 }  // namespace Battery
90 }  // namespace HDI
91 }  // namespace OHOS
92