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 "core/components_ng/pattern/shape/shape_container_modifier.h"
17
18 #include "base/geometry/ng/image_mesh.h"
19 #include "base/utils/utils.h"
20 #include "core/components_ng/render/adapter/drawing_painter.h"
21 namespace OHOS::Ace::NG {
22 namespace {
CheckNeedMesh(const RefPtr<PropertyInt> & propertyRow,const RefPtr<PropertyInt> & propertyColumn,const RefPtr<PropertyVectorFloat> & propertyMesh)23 std::tuple<bool, int32_t, int32_t> CheckNeedMesh(const RefPtr<PropertyInt>& propertyRow,
24 const RefPtr<PropertyInt>& propertyColumn, const RefPtr<PropertyVectorFloat>& propertyMesh)
25 {
26 std::tuple<bool, int32_t, int32_t> noMeshRet = { false, 1, 1 };
27 CHECK_NULL_RETURN(propertyRow, noMeshRet);
28 CHECK_NULL_RETURN(propertyColumn, noMeshRet);
29 CHECK_NULL_RETURN(propertyMesh, noMeshRet);
30 auto row = propertyRow->Get();
31 auto column = propertyColumn->Get();
32 auto verticesSize = propertyMesh->Get().size();
33 int64_t tempVerticesSize = (static_cast<int64_t>(column) + 1) * (row + 1) * 2;
34 if (tempVerticesSize != static_cast<int64_t>(verticesSize)) {
35 return noMeshRet;
36 }
37 return { true, row, column };
38 }
39 } // namespace
40
ShapeContainerModifier()41 ShapeContainerModifier::ShapeContainerModifier()
42 {
43 row_ = AceType::MakeRefPtr<PropertyInt>(0);
44 column_ = AceType::MakeRefPtr<PropertyInt>(0);
45 mesh_ = AceType::MakeRefPtr<PropertyVectorFloat>(LinearVector<float>(0));
46 flag_ = AceType::MakeRefPtr<PropertyBool>(false);
47 AttachProperty(row_);
48 AttachProperty(column_);
49 AttachProperty(mesh_);
50 AttachProperty(flag_);
51 }
52
UpdatePixelMap(const RefPtr<PixelMap> & pixelMap)53 void ShapeContainerModifier::UpdatePixelMap(const RefPtr<PixelMap>& pixelMap)
54 {
55 if (((!pixelMap_) != (!pixelMap)) || (pixelMap_ && pixelMap && pixelMap_->GetPixels() != pixelMap->GetPixels())) {
56 CHECK_NULL_VOID(flag_);
57 flag_->Set(!flag_->Get());
58 }
59 pixelMap_ = pixelMap;
60 }
61
onDraw(DrawingContext & context)62 void ShapeContainerModifier::onDraw(DrawingContext& context)
63 {
64 CHECK_NULL_VOID(pixelMap_);
65 RSCanvas& canvas = context.canvas;
66 RSBrush brush;
67 canvas.AttachBrush(brush);
68 const auto& [needMesh, row, column] = CheckNeedMesh(row_, column_, mesh_);
69 std::vector<float> mesh;
70 if (mesh_ && needMesh) {
71 mesh = mesh_->Get();
72 } else {
73 const float height = static_cast<float>(pixelMap_->GetHeight());
74 const float width = static_cast<float>(pixelMap_->GetWidth());
75 mesh = { 0, 0, width, 0, 0, height, width, height };
76 }
77 DrawingPainter::DrawPixelMapMesh(canvas, row, column, mesh, pixelMap_);
78 canvas.DetachBrush();
79 }
80 } // namespace OHOS::Ace::NG
81