• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "bridge/cj_frontend/cppview/offscreen_canvas.h"
17 
18 #include "bridge/cj_frontend/cppview/offscreen_rendering_context.h"
19 #include "core/components/common/properties/paint_state.h"
20 
21 namespace OHOS::Ace::Framework {
NativeOffscreenCanvas(double height,double width,int32_t unit)22 NativeOffscreenCanvas::NativeOffscreenCanvas(double height, double width, int32_t unit) : FFIData()
23 {
24     SetUnit(static_cast<CanvasUnit>(unit));
25     double density = GetDensity();
26     SetHeight(height * density);
27     SetWidth(width * density);
28     offscreenCanvasPattern_ = AceType::MakeRefPtr<NG::OffscreenCanvasPattern>(
29         static_cast<int32_t>(this->width_), static_cast<int32_t>(this->height_));
30 }
31 
~NativeOffscreenCanvas()32 NativeOffscreenCanvas::~NativeOffscreenCanvas()
33 {
34     LOGI("Native Offscreen Canvas Destroyed: %{public}" PRId64, GetID());
35 }
36 
NativeSetHeihgt(double height)37 void NativeOffscreenCanvas::NativeSetHeihgt(double height)
38 {
39     double density = GetDensity();
40     height *= density;
41     if (height_ != height) {
42         height_ = height;
43         offscreenCanvasPattern_->UpdateSize(width_, height_);
44         if (offscreenCanvasContext_ != nullptr) {
45             offscreenCanvasContext_->SetHeight(height_);
46         }
47     }
48 }
49 
NativeSetWidth(double width)50 void NativeOffscreenCanvas::NativeSetWidth(double width)
51 {
52     double density = GetDensity();
53     width *= density;
54     if (width_ != width) {
55         width_ = width;
56         offscreenCanvasPattern_->UpdateSize(width_, width_);
57         if (offscreenCanvasContext_ != nullptr) {
58             offscreenCanvasContext_->SetWidth(width_);
59         }
60     }
61 }
62 
NativeGetHeihgt()63 double NativeOffscreenCanvas::NativeGetHeihgt()
64 {
65     double fHeight = GetHeight();
66     double density = Positive(GetDensity()) ? GetDensity() : 1;
67     fHeight /= density;
68     return fHeight;
69 }
70 
NativeGetWidth()71 double NativeOffscreenCanvas::NativeGetWidth()
72 {
73     double fWidth = GetWidth();
74     double density = Positive(GetDensity()) ? GetDensity() : 1;
75     fWidth /= density;
76     return fWidth;
77 }
78 
GetContext(int32_t contextType,bool option,double width,double height)79 int64_t NativeOffscreenCanvas::GetContext(int32_t contextType, bool option, double width, double height)
80 {
81     double density = GetDensity();
82     isGetContext_ = true;
83     width_ = width * density;
84     height_ = height * density;
85     if (!Container::IsCurrentUseNewPipeline()) {
86         return 0;
87     }
88     contextType_ = static_cast<ContextType>(contextType);
89     if (contextType_ == ContextType::CONTEXT_2D) {
90         offscreenCanvasContext_ = FFIData::Create<CJOffscreenRenderingContext>();
91         offscreenCanvasContext_->SetInstanceId(Container::CurrentId());
92         offscreenCanvasContext_->SetOffscreenPattern(offscreenCanvasPattern_);
93         offscreenCanvasContext_->AddOffscreenCanvasPattern(offscreenCanvasPattern_);
94         offscreenCanvasContext_->SetWidth(width_);
95         offscreenCanvasContext_->SetHeight(height_);
96         offscreenCanvasContext_->SetUnit(GetUnit());
97     }
98     offscreenCanvasContext_->SetDensity();
99     return offscreenCanvasContext_->GetID();
100 }
101 
TransferToImageBitmap()102 int64_t NativeOffscreenCanvas::TransferToImageBitmap()
103 {
104     if (offscreenCanvasPattern_ == nullptr || offscreenCanvasContext_ == nullptr) {
105         return 0;
106     }
107     auto pixelMap = offscreenCanvasPattern_->TransferToImageBitmap();
108     auto renderImage = FFIData::Create<CJRenderImage>();
109     renderImage->InitCJRenderImage(pixelMap);
110 
111 #ifndef PIXEL_MAP_SUPPORTED
112     auto imageData = offscreenCanvasPattern_->GetImageData(0, 0, width_, height_);
113     if (imageData == nullptr) {
114         return nullptr;
115     }
116     renderImage->SetImageData(std::make_shared<Ace::ImageData>(*imageData));
117 #endif
118     renderImage->SetUnit(GetUnit());
119     renderImage->SetWidth(GetWidth());
120     renderImage->SetHeight(GetHeight());
121     return renderImage->GetID();
122 }
123 
124 } // namespace OHOS::Ace::Framework