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 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_CANVAS_IMAGE_DATA_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_CANVAS_IMAGE_DATA_H
18 
19 #include "base/memory/referenced.h"
20 #include "bridge/declarative_frontend/engine/bindings_defines.h"
21 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
22 
23 namespace OHOS::Ace::Framework {
24 
25 class JSCanvasImageData : public Referenced {
26 public:
27     JSCanvasImageData() = default;
28     ~JSCanvasImageData() override = default;
29 
30     static void JSBind(BindingTarget globalObj);
31     static void Constructor(const JSCallbackInfo& args);
32     static void Destructor(JSCanvasImageData* controller);
33 
34     void JsGetWidth(const JSCallbackInfo& info);
35     void JsGetHeight(const JSCallbackInfo& info);
36     void JsGetData(const JSCallbackInfo& info);
37     void JsSetWidth(const JSCallbackInfo& info);
38     void JsSetHeight(const JSCallbackInfo& info);
39     void JsSetData(const JSCallbackInfo& info);
40     bool GetImageDataSize(const JSCallbackInfo& args, int32_t& finalWidth, int32_t& finalHeight);
41 
42     int32_t width_ = 0;
43     int32_t height_ = 0;
44     JSRef<JSUint8ClampedArray> colorArray_;
45 
setX(int32_t x)46     void setX(int32_t x)
47     {
48         x_ = x;
49     }
getX()50     int32_t getX() const
51     {
52         return x_;
53     }
setY(int32_t y)54     void setY(int32_t y)
55     {
56         y_ = y;
57     }
getY()58     int32_t getY() const
59     {
60         return y_;
61     }
setDirtyX(int32_t dirtyX)62     void setDirtyX(int32_t dirtyX)
63     {
64         dirtyX_ = dirtyX;
65     }
getDirtyX()66     int32_t getDirtyX() const
67     {
68         return dirtyX_;
69     }
setDirtyY(int32_t dirtyY)70     void setDirtyY(int32_t dirtyY)
71     {
72         dirtyY_ = dirtyY;
73     }
getDirtyY()74     int32_t getDirtyY() const
75     {
76         return dirtyY_;
77     }
setDirtyWidth(int32_t dirtyWidth)78     void setDirtyWidth(int32_t dirtyWidth)
79     {
80         dirtyWidth_ = dirtyWidth;
81     }
getDirtyWidth()82     int32_t getDirtyWidth() const
83     {
84         return dirtyWidth_;
85     }
setDirtyHeight(int32_t dirtyHeight)86     void setDirtyHeight(int32_t dirtyHeight)
87     {
88         dirtyHeight_ = dirtyHeight;
89     }
getDirtyHeight()90     int32_t getDirtyHeight() const
91     {
92         return dirtyHeight_;
93     }
setData(const std::vector<Color> & data)94     void setData(const std::vector<Color>& data)
95     {
96         data_ = data;
97     }
98 
SetUnit(CanvasUnit unit)99     void SetUnit(CanvasUnit unit)
100     {
101         unit_ = unit;
102     }
103 
GetUnit()104     CanvasUnit GetUnit()
105     {
106         return unit_;
107     }
108 
GetDensity()109     double GetDensity()
110     {
111         double density = PipelineBase::GetCurrentDensity();
112         return ((GetUnit() == CanvasUnit::DEFAULT) && !NearZero(density)) ? density : 1.0;
113     }
114 
115 private:
116     int32_t x_ = 0;
117     int32_t y_ = 0;
118     int32_t dirtyX_ = 0;
119     int32_t dirtyY_ = 0;
120     int32_t dirtyWidth_ = 0;
121     int32_t dirtyHeight_ = 0;
122     std::vector<Color> data_;
123     CanvasUnit unit_ = CanvasUnit::DEFAULT;
124     ACE_DISALLOW_COPY_AND_MOVE(JSCanvasImageData);
125 };
126 
127 } // namespace OHOS::Ace::Framework
128 
129 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_CANVAS_IMAGE_DATA_H