• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 #include "cmsis_os.h"
16 #include "common/screen.h"
17 #include "components/root_view.h"
18 #include "components/ui_label.h"
19 #include "components/ui_label_button.h"
20 #include "components/ui_scroll_view.h"
21 #include "components/ui_image_view.h"
22 #include "components/ui_view.h"
23 #include "ui_test.h"
24 #include "logo_interface.h"
25 #ifdef LOSCFG_DRIVERS_USER_KEY_INPUT
26 #include "ui_key_input.h"
27 #endif
28 
29 static const int32_t IMG_HORIZONTAL_RESOLUTION = 240;
30 static const int32_t IMG_VERTICAL_RESOLUTION = 320;
31 static const int32_t IMG_ARGB_BYTES_PER_PIXEL = 4;
32 static const int32_t GIF_DELAY_TIME_US = 300000;
33 static const int32_t IMG_START_X = 40;
34 static const int32_t IMG_START_Y = 80;
35 
36 using namespace OHOS;
37 class ImageDemo : public UIView::OnClickListener {
38 public:
GetInstance()39     static ImageDemo *GetInstance()
40     {
41         static ImageDemo inst;
42         return &inst;
43     }
44     void SetUp();
45     UIView *GetView();
46     void GifPlayLoop();
47 
48 private:
ImageDemo()49     ImageDemo() {}
50     ~ImageDemo();
51     void CreateImageSwitch();
52     void GifPlayThread();
53 
54 #ifdef LOSCFG_DRIVERS_USER_KEY_INPUT
55     UITestKeyInputListener* keyListener_ = nullptr;
56 #endif
57     UIScrollView *container_ = nullptr;
58     UIImageView *gifImageView_ = nullptr;
59     UILabelButton *gifToGif_ = nullptr;
60     UILabelButton *gifToJpeg_ = nullptr;
61     UILabelButton *gifToPng_ = nullptr;
62     uint8_t* gifImageData_ = nullptr;
63     int16_t g_height = 0;
64     ImageInfo gifFrame_;
65 };
66 
DeleteChildren(UIView * view)67 static void DeleteChildren(UIView *view)
68 {
69     if (view == nullptr) {
70         return;
71     }
72     while (view != nullptr) {
73         UIView *tempView = view;
74         view = view->GetNextSibling();
75         if (tempView->IsViewGroup() && (tempView->GetViewType() != UI_DIGITAL_CLOCK)) {
76             DeleteChildren(static_cast<UIViewGroup *>(tempView)->GetChildrenHead());
77         }
78         if (tempView->GetParent()) {
79             static_cast<UIViewGroup *>(tempView->GetParent())->Remove(tempView);
80         }
81         delete tempView;
82     }
83 }
84 
SetUp()85 void ImageDemo::SetUp()
86 {
87     g_height = 0;
88     if (container_ == nullptr) {
89         container_ = new UIScrollView();
90         container_->SetPosition(0, 0, Screen::GetInstance().GetWidth() - 1, Screen::GetInstance().GetHeight() - 1);
91         container_->SetHorizontalScrollState(false);
92     }
93 }
94 
~ImageDemo()95 ImageDemo::~ImageDemo()
96 {
97     DeleteChildren(container_);
98 #ifdef LOSCFG_DRIVERS_USER_KEY_INPUT
99     keyListener_ = nullptr;
100 #endif
101     container_ = nullptr;
102     gifImageView_ = nullptr;
103     gifToGif_ = nullptr;
104     gifToJpeg_ = nullptr;
105     gifToPng_ = nullptr;
106 }
107 
GetView()108 UIView *ImageDemo::GetView()
109 {
110     CreateImageSwitch();
111     return container_;
112 }
113 
GifPlayLoop()114 void ImageDemo::GifPlayLoop()
115 {
116     int cnt = GetGifImgCnt();
117     while (1) {
118         for (int i = 0; i < cnt; i++) {
119             gifImageData_ = GetGifData(i);
120             gifFrame_.data = gifImageData_;
121             gifImageView_->SetSrc(&gifFrame_);
122             usleep(GIF_DELAY_TIME_US);
123         }
124     }
125 }
126 
GifPlayThread()127 static void GifPlayThread()
128 {
129     ImageDemo *view = ImageDemo::GetInstance();
130     view->GifPlayLoop();
131 }
132 
CreateImageSwitch()133 void ImageDemo::CreateImageSwitch()
134 {
135     if (container_ == nullptr) {
136         return;
137     }
138 
139     gifImageView_ = new UIImageView();
140     gifImageView_->SetPosition(IMG_START_X, IMG_START_Y);
141     gifImageView_->SetWidth(IMG_HORIZONTAL_RESOLUTION);
142     gifImageView_->SetHeight(IMG_VERTICAL_RESOLUTION);
143     gifImageView_->SetStyle(STYLE_IMAGE_OPA, OPA_OPAQUE);
144     gifFrame_.header.width = IMG_HORIZONTAL_RESOLUTION;
145     gifFrame_.header.height = IMG_VERTICAL_RESOLUTION;
146     gifFrame_.header.colorMode = ARGB8888;
147     gifFrame_.dataSize = IMG_HORIZONTAL_RESOLUTION * IMG_VERTICAL_RESOLUTION * IMG_ARGB_BYTES_PER_PIXEL;
148     container_->Add(gifImageView_);
149 #ifdef LOSCFG_DRIVERS_USER_KEY_INPUT
150     if (keyListener_ == nullptr) {
151         keyListener_ = new UITestKeyInputListener(gifImageView_, gifFrame_);
152     }
153     RootView::GetInstance()->SetOnKeyActListener(keyListener_);
154     HILOG_DEBUG(HILOG_MODULE_ACE, "CreateImageSwitch mark\r\n");
155 #endif
156 }
157 
ImageDemoStart(void)158 void ImageDemoStart(void)
159 {
160     RootView *rootView_ = RootView::GetInstance();
161     rootView_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
162     ImageDemo *view = ImageDemo::GetInstance();
163     view->SetUp();
164     rootView_->Add(view->GetView());
165     rootView_->Invalidate();
166 
167     osThreadAttr_t attr;
168     attr.name = "display-gif";
169     attr.attr_bits = 0U;
170     attr.cb_mem = NULL;
171     attr.cb_size = 0U;
172     attr.stack_mem = NULL;
173     const int32_t UI_THREAD_STACK_SIZE = 1024 * 64;
174     attr.stack_size = UI_THREAD_STACK_SIZE;
175     attr.priority = osPriorityNormal;
176 
177     if (osThreadNew((osThreadFunc_t)GifPlayThread, NULL, &attr) == NULL) {
178         GRAPHIC_LOGE("Failed to create UiMainTask");
179     }
180 }