• 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_rendering_context.h"
17 
18 #include <mutex>
19 #include <utility>
20 
21 #include "ffi_remote_data.h"
22 
23 #include "base/memory/ace_type.h"
24 #include "base/utils/utils.h"
25 #include "bridge/cj_frontend/cppview/render_image.h"
26 #include "bridge/declarative_frontend/jsview/models/canvas/offscreen_canvas_rendering_context_2d_model_impl.h"
27 #include "core/components_ng/pattern/canvas/offscreen_canvas_rendering_context_2d_model_ng.h"
28 
29 using namespace OHOS;
30 
31 namespace OHOS::Ace::Framework {
32 std::mutex CJOffscreenRenderingContext::mutex_;
33 std::unordered_map<uint32_t, RefPtr<AceType>> CJOffscreenRenderingContext::offscreenPatternMap_;
34 uint32_t CJOffscreenRenderingContext::offscreenPatternCount_ = 0;
35 
CJOffscreenRenderingContext(double width,double height,bool antialias,int32_t unit)36 CJOffscreenRenderingContext::CJOffscreenRenderingContext(double width, double height, bool antialias, int32_t unit)
37     : NativeCanvasRenderer(antialias)
38 {
39     renderingContext2DModel_ = AceType::MakeRefPtr<NG::OffscreenCanvasRenderingContext2DModelNG>();
40     id_ = offscreenPatternCount_;
41 
42     double density = this->GetDensity();
43     this->SetHeight(height * density);
44     this->SetWidth(width * density);
45     auto renderingContext =
46         AceType::DynamicCast<NG::OffscreenCanvasRenderingContext2DModelNG>(this->renderingContext2DModel_);
47     auto offscreenPattern = renderingContext->CreateOffscreenPattern(round(width * density), round(height * density));
48     this->SetOffscreenPattern(offscreenPattern);
49     std::lock_guard<std::mutex> lock(mutex_);
50     offscreenPatternMap_[offscreenPatternCount_++] = offscreenPattern;
51 
52     if ((static_cast<CanvasUnit>(unit) == CanvasUnit::PX)) {
53         this->SetUnit(CanvasUnit::PX);
54     }
55     this->SetAntiAlias();
56     this->SetDensity();
57 }
58 
CJOffscreenRenderingContext()59 CJOffscreenRenderingContext::CJOffscreenRenderingContext()
60 {
61     id_ = offscreenPatternCount_;
62     renderingContext2DModel_ = AceType::MakeRefPtr<NG::OffscreenCanvasRenderingContext2DModelNG>();
63 }
64 
~CJOffscreenRenderingContext()65 CJOffscreenRenderingContext::~CJOffscreenRenderingContext()
66 {
67     LOGI("Native OffscreeCanvasRenderer Destroyed: %{public}" PRId64, GetID());
68 }
69 
CJTransferToImageBitmap()70 int64_t CJOffscreenRenderingContext::CJTransferToImageBitmap()
71 {
72     auto offscreenCanvasPattern = AceType::DynamicCast<NG::OffscreenCanvasPattern>(GetOffscreenPattern(id_));
73     auto pixelMap = offscreenCanvasPattern->TransferToImageBitmap();
74     auto ret = FFIData::Create<OHOS::Media::PixelMapImpl>(pixelMap->GetPixelMapSharedPtr());
75     auto cjImage = FFI::FFIData::Create<CJRenderImage>();
76     cjImage->InitCJRenderImage(pixelMap);
77     cjImage->SetUnit(GetUnit());
78     cjImage->SetWidth(GetWidth());
79     cjImage->SetHeight(GetHeight());
80     cjImage->SetPixelMapId(ret->GetID());
81 #ifndef PIXEL_MAP_SUPPORTED
82     auto imageData = offscreenCanvasPattern->GetImageData(0, 0, width_, height_);
83     cjImage->SetImageData(std::move(imageData));
84 #endif
85     return cjImage->GetID();
86 }
87 } // namespace OHOS::Ace::Framework