1 /*
2 * Copyright (c) 2025 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 "render/rs_pixel_map_shader_obj.h"
17
18 #include "effect/shader_effect_lazy.h"
19 #include "render/rs_pixel_map_util.h"
20 #include "utils/log.h"
21
22 #ifdef ROSEN_OHOS
23 #include "transaction/rs_marshalling_helper.h"
24 #include "utils/object_helper.h"
25 #endif
26
27 namespace OHOS {
28 namespace Rosen {
29
RSPixelMapShaderObj()30 RSPixelMapShaderObj::RSPixelMapShaderObj()
31 : Drawing::ShaderEffectObj(static_cast<int32_t>(Drawing::ShaderEffect::ShaderEffectType::IMAGE))
32 {
33 }
34
CreateForUnmarshalling()35 std::shared_ptr<RSPixelMapShaderObj> RSPixelMapShaderObj::CreateForUnmarshalling()
36 {
37 return std::shared_ptr<RSPixelMapShaderObj>(new RSPixelMapShaderObj());
38 }
39
CreatePixelMapShader(std::shared_ptr<Media::PixelMap> pixelMap,Drawing::TileMode tileX,Drawing::TileMode tileY,const Drawing::SamplingOptions & sampling,const Drawing::Matrix & matrix)40 std::shared_ptr<Drawing::ShaderEffectLazy> RSPixelMapShaderObj::CreatePixelMapShader(
41 std::shared_ptr<Media::PixelMap> pixelMap,
42 Drawing::TileMode tileX,
43 Drawing::TileMode tileY,
44 const Drawing::SamplingOptions& sampling,
45 const Drawing::Matrix& matrix)
46 {
47 if (!pixelMap) {
48 return nullptr;
49 }
50
51 // Create RSPixelMapShaderObj
52 auto shaderObj = std::shared_ptr<RSPixelMapShaderObj>(new RSPixelMapShaderObj(pixelMap, tileX, tileY,
53 sampling, matrix));
54
55 // Create ShaderEffectLazy wrapping the ShaderObj
56 return Drawing::ShaderEffectLazy::CreateFromShaderEffectObj(shaderObj);
57 }
58
RSPixelMapShaderObj(std::shared_ptr<Media::PixelMap> pixelMap,Drawing::TileMode tileX,Drawing::TileMode tileY,const Drawing::SamplingOptions & sampling,const Drawing::Matrix & matrix)59 RSPixelMapShaderObj::RSPixelMapShaderObj(
60 std::shared_ptr<Media::PixelMap> pixelMap,
61 Drawing::TileMode tileX,
62 Drawing::TileMode tileY,
63 const Drawing::SamplingOptions& sampling,
64 const Drawing::Matrix& matrix)
65 : Drawing::ShaderEffectObj(static_cast<int32_t>(Drawing::ShaderEffect::ShaderEffectType::IMAGE)),
66 pixelMap_(pixelMap),
67 tileX_(tileX),
68 tileY_(tileY),
69 sampling_(sampling),
70 matrix_(matrix)
71 {
72 }
73
GenerateBaseObject()74 std::shared_ptr<void> RSPixelMapShaderObj::GenerateBaseObject()
75 {
76 if (!pixelMap_) {
77 return nullptr;
78 }
79
80 // Convert PixelMap to Drawing::Image
81 auto image = RSPixelMapUtil::ExtractDrawingImage(pixelMap_);
82 if (!image) {
83 return nullptr;
84 }
85
86 // Create ImageShader using Drawing::Image
87 auto imageShader = Drawing::ShaderEffect::CreateImageShader(
88 *image, tileX_, tileY_, sampling_, matrix_);
89
90 return std::static_pointer_cast<void>(imageShader);
91 }
92
93 #ifdef ROSEN_OHOS
Marshalling(Parcel & parcel)94 bool RSPixelMapShaderObj::Marshalling(Parcel& parcel)
95 {
96 // Return failure if pixelMap is null
97 if (!pixelMap_) {
98 return false;
99 }
100
101 // Serialize PixelMap parameters (type and subType handled externally)
102
103 // Use RSMarshallingHelper for consistent parameter serialization
104 return RSMarshallingHelper::Marshalling(parcel, pixelMap_) &&
105 RSMarshallingHelper::Marshalling(parcel, tileX_) &&
106 RSMarshallingHelper::Marshalling(parcel, tileY_) &&
107 RSMarshallingHelper::Marshalling(parcel, sampling_) &&
108 RSMarshallingHelper::Marshalling(parcel, matrix_);
109 }
110
Unmarshalling(Parcel & parcel,bool & isValid,int32_t depth)111 bool RSPixelMapShaderObj::Unmarshalling(Parcel& parcel, bool& isValid, int32_t depth)
112 {
113 // Use RSMarshallingHelper for consistent parameter deserialization
114 return RSMarshallingHelper::Unmarshalling(parcel, pixelMap_) &&
115 RSMarshallingHelper::Unmarshalling(parcel, tileX_) &&
116 RSMarshallingHelper::Unmarshalling(parcel, tileY_) &&
117 RSMarshallingHelper::Unmarshalling(parcel, sampling_) &&
118 RSMarshallingHelper::Unmarshalling(parcel, matrix_);
119 }
120
121 // Register RSPixelMapShaderObj to ObjectHelper using macro
122 OBJECT_UNMARSHALLING_REGISTER(RSPixelMapShaderObj,
123 static_cast<int32_t>(Drawing::Object::ObjectType::SHADER_EFFECT),
124 static_cast<int32_t>(Drawing::ShaderEffect::ShaderEffectType::IMAGE));
125
126 #endif
127 } // namespace Rosen
128 } // namespace OHOS