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 FOUNDATION_ACE_FRAMEWORK_JAVASCRIPT_BRIDGE_JS_VIEW_JS_RENDERING_CONTEXT_H
17 #define FOUNDATION_ACE_FRAMEWORK_JAVASCRIPT_BRIDGE_JS_VIEW_JS_RENDERING_CONTEXT_H
18 
19 #include "base/memory/referenced.h"
20 #include "bridge/declarative_frontend/engine/bindings_defines.h"
21 #include "bridge/declarative_frontend/jsview/canvas/js_canvas_renderer.h"
22 #include "bridge/declarative_frontend/jsview/canvas/js_rendering_context_settings.h"
23 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
24 
25 namespace OHOS::Ace::Framework {
26 enum class CanvasCallbackType {
27     ON_ATTACH = 0,
28     ON_DETACH,
29     UNKNOWN
30 };
31 
32 using CanvasCallbackFuncPairList = std::list<std::pair<napi_ref, std::function<void()>>>;
33 
34 class JSRenderingContext : public JSCanvasRenderer {
35 public:
36     JSRenderingContext();
37     ~JSRenderingContext() override = default;
38 
39     static void JSBind(BindingTarget globalObj);
40     static void Constructor(const JSCallbackInfo& args);
41     static void Destructor(JSRenderingContext* controller);
42 
43     void OnAttachToCanvas();
44     void OnDetachFromCanvas();
45 
46     void JsToDataUrl(const JSCallbackInfo& info);
47     void JsGetWidth(const JSCallbackInfo& info);
48     void JsGetHeight(const JSCallbackInfo& info);
49     void JsSetHeight(const JSCallbackInfo& info);
50     void JsSetWidth(const JSCallbackInfo& info);
51     void JsTransferFromImageBitmap(const JSCallbackInfo& info);
52     void JsStartImageAnalyzer(const JSCallbackInfo& info);
53     void JsStopImageAnalyzer(const JSCallbackInfo& info);
54     void JsGetCanvas(const JSCallbackInfo& info);
55     void JsSetCanvas(const JSCallbackInfo& info);
56     void JsOn(const JSCallbackInfo& info);
57     void JsOff(const JSCallbackInfo& info);
58 
59     ACE_DISALLOW_COPY_AND_MOVE(JSRenderingContext);
60 
61 private:
62     void DeleteCallbackFromList(int argc, napi_env env, napi_value cb, CanvasCallbackType type);
63     void AddCallbackToList(napi_env env, napi_value cb, CanvasCallbackType type, const std::function<void()>&& onFunc);
64     CanvasCallbackFuncPairList::const_iterator FindCbList(
65         napi_env env, napi_value cb, CanvasCallbackFuncPairList& callbackFuncPairList);
66     CanvasCallbackType GetCanvasCallbackType(const std::string& strType);
67 
68     bool isImageAnalyzing_ = false;
69     CanvasCallbackFuncPairList attachCallback_;
70     CanvasCallbackFuncPairList detachCallback_;
71 };
72 
73 } // namespace OHOS::Ace::Framework
74 
75 #endif // FOUNDATION_ACE_FRAMEWORK_JAVASCRIPT_BRIDGE_JS_VIEW_JS_RENDERING_CONTEXT_H
76