• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "recording/draw_cmd.h"
17 
18 #include "platform/common/rs_system_properties.h"
19 #include "recording/cmd_list_helper.h"
20 #include "recording/draw_cmd_list.h"
21 #include "recording/mem_allocator.h"
22 #include "recording/op_item.h"
23 
24 #include "draw/brush.h"
25 #include "draw/path.h"
26 #include "draw/surface.h"
27 #include "effect/color_filter.h"
28 #include "effect/color_space.h"
29 #include "effect/image_filter.h"
30 #include "effect/mask_filter.h"
31 #include "effect/path_effect.h"
32 #include "effect/shader_effect.h"
33 #include "utils/log.h"
34 #include "utils/scalar.h"
35 #include "utils/system_properties.h"
36 
37 namespace OHOS {
38 namespace Rosen {
39 namespace Drawing {
40 namespace {
41 constexpr int TEXT_BLOB_CACHE_MARGIN = 10;
GetOffScreenSurfaceAndCanvas(const Canvas & canvas,std::shared_ptr<Drawing::Surface> & offScreenSurface,std::shared_ptr<Canvas> & offScreenCanvas)42 bool GetOffScreenSurfaceAndCanvas(const Canvas& canvas,
43     std::shared_ptr<Drawing::Surface>& offScreenSurface, std::shared_ptr<Canvas>& offScreenCanvas)
44 {
45     auto surface = canvas.GetSurface();
46     if (!surface) {
47         return false;
48     }
49     offScreenSurface = surface->MakeSurface(surface->Width(), surface->Height());
50     if (!offScreenSurface) {
51         return false;
52     }
53     offScreenCanvas = offScreenSurface->GetCanvas();
54     return true;
55 }
56 }
57 
58 std::function<void (std::shared_ptr<Drawing::Image> image)> DrawOpItem::holdDrawingImagefunc_ = nullptr;
SetBaseCallback(std::function<void (std::shared_ptr<Drawing::Image> image)> holdDrawingImagefunc)59 void DrawOpItem::SetBaseCallback(
60     std::function<void (std::shared_ptr<Drawing::Image> image)> holdDrawingImagefunc)
61 {
62     holdDrawingImagefunc_ = holdDrawingImagefunc;
63 }
64 
BrushHandleToBrush(const BrushHandle & brushHandle,const DrawCmdList & cmdList,Brush & brush)65 void DrawOpItem::BrushHandleToBrush(const BrushHandle& brushHandle, const DrawCmdList& cmdList, Brush& brush)
66 {
67     brush.SetBlendMode(brushHandle.mode);
68     brush.SetAntiAlias(brushHandle.isAntiAlias);
69 
70     if (brushHandle.colorSpaceHandle.size) {
71         auto colorSpace = CmdListHelper::GetColorSpaceFromCmdList(cmdList, brushHandle.colorSpaceHandle);
72         const Color4f color4f = { brushHandle.color.GetRedF(), brushHandle.color.GetGreenF(),
73                                   brushHandle.color.GetBlueF(), brushHandle.color.GetAlphaF() };
74         brush.SetColor(color4f, colorSpace);
75     } else {
76         brush.SetColor(brushHandle.color);
77     }
78 
79     if (brushHandle.shaderEffectHandle.size) {
80         auto shaderEffect = CmdListHelper::GetShaderEffectFromCmdList(cmdList, brushHandle.shaderEffectHandle);
81         brush.SetShaderEffect(shaderEffect);
82     }
83 
84     Filter filter;
85     bool hasFilter = false;
86     if (brushHandle.colorFilterHandle.size) {
87         auto colorFilter = CmdListHelper::GetColorFilterFromCmdList(cmdList, brushHandle.colorFilterHandle);
88         filter.SetColorFilter(colorFilter);
89         hasFilter = true;
90     }
91     if (brushHandle.imageFilterHandle.size) {
92         auto imageFilter = CmdListHelper::GetImageFilterFromCmdList(cmdList, brushHandle.imageFilterHandle);
93         filter.SetImageFilter(imageFilter);
94         hasFilter = true;
95     }
96     if (brushHandle.maskFilterHandle.size) {
97         auto maskFilter = CmdListHelper::GetMaskFilterFromCmdList(cmdList, brushHandle.maskFilterHandle);
98         filter.SetMaskFilter(maskFilter);
99         hasFilter = true;
100     }
101 
102     if (hasFilter) {
103         filter.SetFilterQuality(brushHandle.filterQuality);
104         brush.SetFilter(filter);
105     }
106 }
107 
BrushToBrushHandle(const Brush & brush,DrawCmdList & cmdList,BrushHandle & brushHandle)108 void DrawOpItem::BrushToBrushHandle(const Brush& brush, DrawCmdList& cmdList, BrushHandle& brushHandle)
109 {
110     const Filter& filter = brush.GetFilter();
111     brushHandle.color = brush.GetColor();
112     brushHandle.mode = brush.GetBlendMode();
113     brushHandle.isAntiAlias = brush.IsAntiAlias();
114     brushHandle.filterQuality = filter.GetFilterQuality();
115     brushHandle.colorSpaceHandle = CmdListHelper::AddColorSpaceToCmdList(cmdList, brush.GetColorSpace());
116     brushHandle.shaderEffectHandle = CmdListHelper::AddShaderEffectToCmdList(cmdList, brush.GetShaderEffect());
117     brushHandle.colorFilterHandle = CmdListHelper::AddColorFilterToCmdList(cmdList, filter.GetColorFilter());
118     brushHandle.imageFilterHandle = CmdListHelper::AddImageFilterToCmdList(cmdList, filter.GetImageFilter());
119     brushHandle.maskFilterHandle = CmdListHelper::AddMaskFilterToCmdList(cmdList, filter.GetMaskFilter());
120 }
121 
GeneratePaintFromHandle(const PaintHandle & paintHandle,const DrawCmdList & cmdList,Paint & paint)122 void DrawOpItem::GeneratePaintFromHandle(const PaintHandle& paintHandle, const DrawCmdList& cmdList, Paint& paint)
123 {
124     paint.SetBlendMode(paintHandle.mode);
125     paint.SetAntiAlias(paintHandle.isAntiAlias);
126     paint.SetStyle(paintHandle.style);
127 
128     if (paintHandle.colorSpaceHandle.size) {
129         auto colorSpace = CmdListHelper::GetColorSpaceFromCmdList(cmdList, paintHandle.colorSpaceHandle);
130         const Color4f color4f = { paintHandle.color.GetRedF(), paintHandle.color.GetGreenF(),
131                                   paintHandle.color.GetBlueF(), paintHandle.color.GetAlphaF() };
132         paint.SetColor(color4f, colorSpace);
133     } else {
134         paint.SetColor(paintHandle.color);
135     }
136 
137     if (paintHandle.shaderEffectHandle.size) {
138         auto shaderEffect = CmdListHelper::GetShaderEffectFromCmdList(cmdList, paintHandle.shaderEffectHandle);
139         paint.SetShaderEffect(shaderEffect);
140     }
141 
142     Filter filter;
143     bool hasFilter = false;
144     if (paintHandle.colorFilterHandle.size) {
145         auto colorFilter = CmdListHelper::GetColorFilterFromCmdList(cmdList, paintHandle.colorFilterHandle);
146         filter.SetColorFilter(colorFilter);
147         hasFilter = true;
148     }
149     if (paintHandle.imageFilterHandle.size) {
150         auto imageFilter = CmdListHelper::GetImageFilterFromCmdList(cmdList, paintHandle.imageFilterHandle);
151         filter.SetImageFilter(imageFilter);
152         hasFilter = true;
153     }
154     if (paintHandle.maskFilterHandle.size) {
155         auto maskFilter = CmdListHelper::GetMaskFilterFromCmdList(cmdList, paintHandle.maskFilterHandle);
156         filter.SetMaskFilter(maskFilter);
157         hasFilter = true;
158     }
159 
160     if (hasFilter) {
161         filter.SetFilterQuality(paintHandle.filterQuality);
162         paint.SetFilter(filter);
163     }
164 
165     if (!paint.HasStrokeStyle()) {
166         return;
167     }
168 
169     paint.SetWidth(paintHandle.width);
170     paint.SetMiterLimit(paintHandle.miterLimit);
171     paint.SetCapStyle(paintHandle.capStyle);
172     paint.SetJoinStyle(paintHandle.joinStyle);
173     if (paintHandle.pathEffectHandle.size) {
174         auto pathEffect = CmdListHelper::GetPathEffectFromCmdList(cmdList, paintHandle.pathEffectHandle);
175         paint.SetPathEffect(pathEffect);
176     }
177 }
178 
GenerateHandleFromPaint(CmdList & cmdList,const Paint & paint,PaintHandle & paintHandle)179 void DrawOpItem::GenerateHandleFromPaint(CmdList& cmdList, const Paint& paint, PaintHandle& paintHandle)
180 {
181     paintHandle.isAntiAlias = paint.IsAntiAlias();
182     paintHandle.style = paint.GetStyle();
183     paintHandle.color = paint.GetColor();
184     paintHandle.mode = paint.GetBlendMode();
185 
186     if (paint.HasFilter()) {
187         const Filter& filter = paint.GetFilter();
188         paintHandle.filterQuality = filter.GetFilterQuality();
189         paintHandle.colorFilterHandle = CmdListHelper::AddColorFilterToCmdList(cmdList, filter.GetColorFilter());
190         paintHandle.imageFilterHandle = CmdListHelper::AddImageFilterToCmdList(cmdList, filter.GetImageFilter());
191         paintHandle.maskFilterHandle = CmdListHelper::AddMaskFilterToCmdList(cmdList, filter.GetMaskFilter());
192     }
193 
194     if (paint.GetColorSpace()) {
195         paintHandle.colorSpaceHandle = CmdListHelper::AddColorSpaceToCmdList(cmdList, paint.GetColorSpace());
196     }
197 
198     if (paint.GetShaderEffect()) {
199         paintHandle.shaderEffectHandle = CmdListHelper::AddShaderEffectToCmdList(cmdList, paint.GetShaderEffect());
200     }
201 
202     if (!paint.HasStrokeStyle()) {
203         return;
204     }
205 
206     paintHandle.width = paint.GetWidth();
207     paintHandle.miterLimit = paint.GetMiterLimit();
208     paintHandle.capStyle = paint.GetCapStyle();
209     paintHandle.joinStyle = paint.GetJoinStyle();
210     if (paint.GetPathEffect()) {
211         paintHandle.pathEffectHandle = CmdListHelper::AddPathEffectToCmdList(cmdList, paint.GetPathEffect());
212     }
213 }
214 
GenerateCachedOpItemPlayer(DrawCmdList & cmdList,Canvas * canvas,const Rect * rect)215 GenerateCachedOpItemPlayer::GenerateCachedOpItemPlayer(DrawCmdList &cmdList, Canvas* canvas, const Rect* rect)
216     : canvas_(canvas), rect_(rect), cmdList_(cmdList) {}
217 
GenerateCachedOpItem(uint32_t type,void * handle)218 bool GenerateCachedOpItemPlayer::GenerateCachedOpItem(uint32_t type, void* handle)
219 {
220     if (handle == nullptr) {
221         return false;
222     }
223 
224     if (type == DrawOpItem::TEXT_BLOB_OPITEM) {
225         auto* op = static_cast<DrawTextBlobOpItem::ConstructorHandle*>(handle);
226         return op->GenerateCachedOpItem(cmdList_, canvas_);
227     }
228     return false;
229 }
230 
231 /* UnmarshallingPlayer */
232 std::unordered_map<uint32_t, UnmarshallingPlayer::UnmarshallingFunc>*
233     UnmarshallingPlayer::opUnmarshallingFuncLUT_ = nullptr;
234 
RegisterUnmarshallingFunc(uint32_t type,UnmarshallingPlayer::UnmarshallingFunc func)235 bool UnmarshallingPlayer::RegisterUnmarshallingFunc(uint32_t type, UnmarshallingPlayer::UnmarshallingFunc func)
236 {
237     if (!opUnmarshallingFuncLUT_) {
238         static std::unordered_map<uint32_t, UnmarshallingPlayer::UnmarshallingFunc> opUnmarshallingFuncLUT = {};
239         opUnmarshallingFuncLUT_ = &opUnmarshallingFuncLUT;
240     }
241     return opUnmarshallingFuncLUT_->emplace(type, func).second;
242 }
243 
UnmarshallingPlayer(const DrawCmdList & cmdList)244 UnmarshallingPlayer::UnmarshallingPlayer(const DrawCmdList& cmdList) : cmdList_(cmdList) {}
245 
Unmarshalling(uint32_t type,void * handle)246 std::shared_ptr<DrawOpItem> UnmarshallingPlayer::Unmarshalling(uint32_t type, void* handle)
247 {
248     if (type == DrawOpItem::OPITEM_HEAD || !opUnmarshallingFuncLUT_) {
249         return nullptr;
250     }
251 
252     auto it = opUnmarshallingFuncLUT_->find(type);
253     if (it == opUnmarshallingFuncLUT_->end() || it->second == nullptr) {
254         return nullptr;
255     }
256 
257     auto func = it->second;
258     return (*func)(this->cmdList_, handle);
259 }
260 
261 /* DrawWithPaintOpItem */
DrawWithPaintOpItem(const DrawCmdList & cmdList,const PaintHandle & paintHandle,uint32_t type)262 DrawWithPaintOpItem::DrawWithPaintOpItem(const DrawCmdList& cmdList, const PaintHandle& paintHandle, uint32_t type)
263     : DrawOpItem(type)
264 {
265     GeneratePaintFromHandle(paintHandle, cmdList, paint_);
266 }
267 
268 /* DrawPointOpItem */
269 REGISTER_UNMARSHALLING_FUNC(DrawPoint, DrawOpItem::POINT_OPITEM, DrawPointOpItem::Unmarshalling);
270 
DrawPointOpItem(const DrawCmdList & cmdList,DrawPointOpItem::ConstructorHandle * handle)271 DrawPointOpItem::DrawPointOpItem(const DrawCmdList& cmdList, DrawPointOpItem::ConstructorHandle* handle)
272     : DrawWithPaintOpItem(cmdList, handle->paintHandle, POINT_OPITEM), point_(handle->point) {}
273 
Unmarshalling(const DrawCmdList & cmdList,void * handle)274 std::shared_ptr<DrawOpItem> DrawPointOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
275 {
276     return std::make_shared<DrawPointOpItem>(cmdList, static_cast<DrawPointOpItem::ConstructorHandle*>(handle));
277 }
278 
Marshalling(DrawCmdList & cmdList)279 void DrawPointOpItem::Marshalling(DrawCmdList& cmdList)
280 {
281     PaintHandle paintHandle;
282     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
283     cmdList.AddOp<ConstructorHandle>(point_, paintHandle);
284 }
285 
Playback(Canvas * canvas,const Rect * rect)286 void DrawPointOpItem::Playback(Canvas* canvas, const Rect* rect)
287 {
288     canvas->AttachPaint(paint_);
289     canvas->DrawPoint(point_);
290 }
291 
292 /* DrawPointsOpItem */
293 REGISTER_UNMARSHALLING_FUNC(DrawPoints, DrawOpItem::POINTS_OPITEM, DrawPointsOpItem::Unmarshalling);
294 
DrawPointsOpItem(const DrawCmdList & cmdList,DrawPointsOpItem::ConstructorHandle * handle)295 DrawPointsOpItem::DrawPointsOpItem(const DrawCmdList& cmdList, DrawPointsOpItem::ConstructorHandle* handle)
296     : DrawWithPaintOpItem(cmdList, handle->paintHandle, POINTS_OPITEM), mode_(handle->mode)
297 {
298     pts_ = CmdListHelper::GetVectorFromCmdList<Point>(cmdList, handle->pts);
299 }
300 
Unmarshalling(const DrawCmdList & cmdList,void * handle)301 std::shared_ptr<DrawOpItem> DrawPointsOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
302 {
303     return std::make_shared<DrawPointsOpItem>(cmdList, static_cast<DrawPointsOpItem::ConstructorHandle*>(handle));
304 }
305 
Marshalling(DrawCmdList & cmdList)306 void DrawPointsOpItem::Marshalling(DrawCmdList& cmdList)
307 {
308     PaintHandle paintHandle;
309     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
310     auto pointsData = CmdListHelper::AddVectorToCmdList<Point>(cmdList, pts_);
311     cmdList.AddOp<ConstructorHandle>(mode_, pointsData, paintHandle);
312 }
313 
Playback(Canvas * canvas,const Rect * rect)314 void DrawPointsOpItem::Playback(Canvas* canvas, const Rect* rect)
315 {
316     canvas->AttachPaint(paint_);
317     canvas->DrawPoints(mode_, pts_.size(), pts_.data());
318 }
319 
320 /* DrawLineOpItem */
321 REGISTER_UNMARSHALLING_FUNC(DrawLine, DrawOpItem::LINE_OPITEM, DrawLineOpItem::Unmarshalling);
322 
DrawLineOpItem(const DrawCmdList & cmdList,DrawLineOpItem::ConstructorHandle * handle)323 DrawLineOpItem::DrawLineOpItem(const DrawCmdList& cmdList, DrawLineOpItem::ConstructorHandle* handle)
324     : DrawWithPaintOpItem(cmdList, handle->paintHandle, LINE_OPITEM),
325       startPt_(handle->startPt), endPt_(handle->endPt) {}
326 
Unmarshalling(const DrawCmdList & cmdList,void * handle)327 std::shared_ptr<DrawOpItem> DrawLineOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
328 {
329     return std::make_shared<DrawLineOpItem>(cmdList, static_cast<DrawLineOpItem::ConstructorHandle*>(handle));
330 }
331 
Marshalling(DrawCmdList & cmdList)332 void DrawLineOpItem::Marshalling(DrawCmdList& cmdList)
333 {
334     PaintHandle paintHandle;
335     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
336     cmdList.AddOp<ConstructorHandle>(startPt_, endPt_, paintHandle);
337 }
338 
Playback(Canvas * canvas,const Rect * rect)339 void DrawLineOpItem::Playback(Canvas* canvas, const Rect* rect)
340 {
341     canvas->AttachPaint(paint_);
342     canvas->DrawLine(startPt_, endPt_);
343 }
344 
345 /* DrawRectOpItem */
346 REGISTER_UNMARSHALLING_FUNC(DrawRect, DrawOpItem::RECT_OPITEM, DrawRectOpItem::Unmarshalling);
347 
DrawRectOpItem(const DrawCmdList & cmdList,DrawRectOpItem::ConstructorHandle * handle)348 DrawRectOpItem::DrawRectOpItem(const DrawCmdList& cmdList, DrawRectOpItem::ConstructorHandle* handle)
349     : DrawWithPaintOpItem(cmdList, handle->paintHandle, RECT_OPITEM), rect_(handle->rect) {}
350 
Unmarshalling(const DrawCmdList & cmdList,void * handle)351 std::shared_ptr<DrawOpItem> DrawRectOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
352 {
353     return std::make_shared<DrawRectOpItem>(cmdList, static_cast<DrawRectOpItem::ConstructorHandle*>(handle));
354 }
355 
Marshalling(DrawCmdList & cmdList)356 void DrawRectOpItem::Marshalling(DrawCmdList& cmdList)
357 {
358     PaintHandle paintHandle;
359     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
360     cmdList.AddOp<ConstructorHandle>(rect_, paintHandle);
361 }
362 
Playback(Canvas * canvas,const Rect * rect)363 void DrawRectOpItem::Playback(Canvas* canvas, const Rect* rect)
364 {
365     canvas->AttachPaint(paint_);
366     canvas->DrawRect(rect_);
367 }
368 
369 /* DrawRoundRectOpItem */
370 REGISTER_UNMARSHALLING_FUNC(DrawRoundRect, DrawOpItem::ROUND_RECT_OPITEM, DrawRoundRectOpItem::Unmarshalling);
371 
DrawRoundRectOpItem(const DrawCmdList & cmdList,DrawRoundRectOpItem::ConstructorHandle * handle)372 DrawRoundRectOpItem::DrawRoundRectOpItem(const DrawCmdList& cmdList, DrawRoundRectOpItem::ConstructorHandle* handle)
373     : DrawWithPaintOpItem(cmdList, handle->paintHandle, ROUND_RECT_OPITEM), rrect_(handle->rrect) {}
374 
Unmarshalling(const DrawCmdList & cmdList,void * handle)375 std::shared_ptr<DrawOpItem> DrawRoundRectOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
376 {
377     return std::make_shared<DrawRoundRectOpItem>(cmdList, static_cast<DrawRoundRectOpItem::ConstructorHandle*>(handle));
378 }
379 
Marshalling(DrawCmdList & cmdList)380 void DrawRoundRectOpItem::Marshalling(DrawCmdList& cmdList)
381 {
382     PaintHandle paintHandle;
383     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
384     cmdList.AddOp<ConstructorHandle>(rrect_, paintHandle);
385 }
386 
Playback(Canvas * canvas,const Rect * rect)387 void DrawRoundRectOpItem::Playback(Canvas* canvas, const Rect* rect)
388 {
389     canvas->AttachPaint(paint_);
390     canvas->DrawRoundRect(rrect_);
391 }
392 
393 /* DrawNestedRoundRectOpItem */
394 REGISTER_UNMARSHALLING_FUNC(
395     DrawNestedRoundRect, DrawOpItem::NESTED_ROUND_RECT_OPITEM, DrawNestedRoundRectOpItem::Unmarshalling);
396 
DrawNestedRoundRectOpItem(const DrawCmdList & cmdList,DrawNestedRoundRectOpItem::ConstructorHandle * handle)397 DrawNestedRoundRectOpItem::DrawNestedRoundRectOpItem(
398     const DrawCmdList& cmdList, DrawNestedRoundRectOpItem::ConstructorHandle* handle)
399     : DrawWithPaintOpItem(cmdList, handle->paintHandle, NESTED_ROUND_RECT_OPITEM),
400       outerRRect_(handle->outerRRect), innerRRect_(handle->innerRRect) {}
401 
Unmarshalling(const DrawCmdList & cmdList,void * handle)402 std::shared_ptr<DrawOpItem> DrawNestedRoundRectOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
403 {
404     return std::make_shared<DrawNestedRoundRectOpItem>(
405         cmdList, static_cast<DrawNestedRoundRectOpItem::ConstructorHandle*>(handle));
406 }
407 
Marshalling(DrawCmdList & cmdList)408 void DrawNestedRoundRectOpItem::Marshalling(DrawCmdList& cmdList)
409 {
410     PaintHandle paintHandle;
411     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
412     cmdList.AddOp<ConstructorHandle>(outerRRect_, innerRRect_, paintHandle);
413 }
414 
Playback(Canvas * canvas,const Rect * rect)415 void DrawNestedRoundRectOpItem::Playback(Canvas* canvas, const Rect* rect)
416 {
417     canvas->AttachPaint(paint_);
418     canvas->DrawNestedRoundRect(outerRRect_, innerRRect_);
419 }
420 
421 /* DrawArcOpItem */
422 REGISTER_UNMARSHALLING_FUNC(DrawArc, DrawOpItem::ARC_OPITEM, DrawArcOpItem::Unmarshalling);
423 
DrawArcOpItem(const DrawCmdList & cmdList,DrawArcOpItem::ConstructorHandle * handle)424 DrawArcOpItem::DrawArcOpItem(const DrawCmdList& cmdList, DrawArcOpItem::ConstructorHandle* handle)
425     : DrawWithPaintOpItem(cmdList, handle->paintHandle, ARC_OPITEM), rect_(handle->rect),
426       startAngle_(handle->startAngle), sweepAngle_(handle->sweepAngle) {}
427 
Unmarshalling(const DrawCmdList & cmdList,void * handle)428 std::shared_ptr<DrawOpItem> DrawArcOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
429 {
430     return std::make_shared<DrawArcOpItem>(cmdList, static_cast<DrawArcOpItem::ConstructorHandle*>(handle));
431 }
432 
Marshalling(DrawCmdList & cmdList)433 void DrawArcOpItem::Marshalling(DrawCmdList& cmdList)
434 {
435     PaintHandle paintHandle;
436     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
437     cmdList.AddOp<ConstructorHandle>(rect_, startAngle_, sweepAngle_, paintHandle);
438 }
439 
Playback(Canvas * canvas,const Rect * rect)440 void DrawArcOpItem::Playback(Canvas* canvas, const Rect* rect)
441 {
442     canvas->AttachPaint(paint_);
443     canvas->DrawArc(rect_, startAngle_, sweepAngle_);
444 }
445 
446 /* DrawPieOpItem */
447 REGISTER_UNMARSHALLING_FUNC(DrawPie, DrawOpItem::PIE_OPITEM, DrawPieOpItem::Unmarshalling);
448 
DrawPieOpItem(const DrawCmdList & cmdList,DrawPieOpItem::ConstructorHandle * handle)449 DrawPieOpItem::DrawPieOpItem(const DrawCmdList& cmdList, DrawPieOpItem::ConstructorHandle* handle)
450     : DrawWithPaintOpItem(cmdList, handle->paintHandle, PIE_OPITEM), rect_(handle->rect),
451       startAngle_(handle->startAngle), sweepAngle_(handle->sweepAngle) {}
452 
Unmarshalling(const DrawCmdList & cmdList,void * handle)453 std::shared_ptr<DrawOpItem> DrawPieOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
454 {
455     return std::make_shared<DrawPieOpItem>(cmdList, static_cast<DrawPieOpItem::ConstructorHandle*>(handle));
456 }
457 
Marshalling(DrawCmdList & cmdList)458 void DrawPieOpItem::Marshalling(DrawCmdList& cmdList)
459 {
460     PaintHandle paintHandle;
461     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
462     cmdList.AddOp<ConstructorHandle>(rect_, startAngle_, sweepAngle_, paintHandle);
463 }
464 
Playback(Canvas * canvas,const Rect * rect)465 void DrawPieOpItem::Playback(Canvas* canvas, const Rect* rect)
466 {
467     canvas->AttachPaint(paint_);
468     canvas->DrawPie(rect_, startAngle_, sweepAngle_);
469 }
470 
471 /* DrawOvalOpItem */
472 REGISTER_UNMARSHALLING_FUNC(DrawOval, DrawOpItem::OVAL_OPITEM, DrawOvalOpItem::Unmarshalling);
473 
DrawOvalOpItem(const DrawCmdList & cmdList,DrawOvalOpItem::ConstructorHandle * handle)474 DrawOvalOpItem::DrawOvalOpItem(const DrawCmdList& cmdList, DrawOvalOpItem::ConstructorHandle* handle)
475     : DrawWithPaintOpItem(cmdList, handle->paintHandle, OVAL_OPITEM), rect_(handle->rect) {}
476 
Unmarshalling(const DrawCmdList & cmdList,void * handle)477 std::shared_ptr<DrawOpItem> DrawOvalOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
478 {
479     return std::make_shared<DrawOvalOpItem>(cmdList, static_cast<DrawOvalOpItem::ConstructorHandle*>(handle));
480 }
481 
Marshalling(DrawCmdList & cmdList)482 void DrawOvalOpItem::Marshalling(DrawCmdList& cmdList)
483 {
484     PaintHandle paintHandle;
485     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
486     cmdList.AddOp<ConstructorHandle>(rect_, paintHandle);
487 }
488 
Playback(Canvas * canvas,const Rect * rect)489 void DrawOvalOpItem::Playback(Canvas* canvas, const Rect* rect)
490 {
491     canvas->AttachPaint(paint_);
492     canvas->DrawOval(rect_);
493 }
494 
495 /* DrawCircleOpItem */
496 REGISTER_UNMARSHALLING_FUNC(DrawCircle, DrawOpItem::CIRCLE_OPITEM, DrawCircleOpItem::Unmarshalling);
497 
DrawCircleOpItem(const DrawCmdList & cmdList,DrawCircleOpItem::ConstructorHandle * handle)498 DrawCircleOpItem::DrawCircleOpItem(const DrawCmdList& cmdList, DrawCircleOpItem::ConstructorHandle* handle)
499     : DrawWithPaintOpItem(cmdList, handle->paintHandle, CIRCLE_OPITEM),
500       centerPt_(handle->centerPt), radius_(handle->radius) {}
501 
Unmarshalling(const DrawCmdList & cmdList,void * handle)502 std::shared_ptr<DrawOpItem> DrawCircleOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
503 {
504     return std::make_shared<DrawCircleOpItem>(cmdList, static_cast<DrawCircleOpItem::ConstructorHandle*>(handle));
505 }
506 
Marshalling(DrawCmdList & cmdList)507 void DrawCircleOpItem::Marshalling(DrawCmdList& cmdList)
508 {
509     PaintHandle paintHandle;
510     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
511     cmdList.AddOp<ConstructorHandle>(centerPt_, radius_, paintHandle);
512 }
513 
Playback(Canvas * canvas,const Rect * rect)514 void DrawCircleOpItem::Playback(Canvas* canvas, const Rect* rect)
515 {
516     canvas->AttachPaint(paint_);
517     canvas->DrawCircle(centerPt_, radius_);
518 }
519 
520 /* DrawPathOpItem */
521 REGISTER_UNMARSHALLING_FUNC(DrawPath, DrawOpItem::PATH_OPITEM, DrawPathOpItem::Unmarshalling);
522 
DrawPathOpItem(const DrawCmdList & cmdList,DrawPathOpItem::ConstructorHandle * handle)523 DrawPathOpItem::DrawPathOpItem(const DrawCmdList& cmdList, DrawPathOpItem::ConstructorHandle* handle)
524     : DrawWithPaintOpItem(cmdList, handle->paintHandle, PATH_OPITEM)
525 {
526     path_ = CmdListHelper::GetPathFromCmdList(cmdList, handle->path);
527 }
528 
Unmarshalling(const DrawCmdList & cmdList,void * handle)529 std::shared_ptr<DrawOpItem> DrawPathOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
530 {
531     return std::make_shared<DrawPathOpItem>(cmdList, static_cast<DrawPathOpItem::ConstructorHandle*>(handle));
532 }
533 
Marshalling(DrawCmdList & cmdList)534 void DrawPathOpItem::Marshalling(DrawCmdList& cmdList)
535 {
536     PaintHandle paintHandle;
537     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
538     auto pathHandle = CmdListHelper::AddPathToCmdList(cmdList, *path_);
539     cmdList.AddOp<ConstructorHandle>(pathHandle, paintHandle);
540 }
541 
Playback(Canvas * canvas,const Rect * rect)542 void DrawPathOpItem::Playback(Canvas* canvas, const Rect* rect)
543 {
544     if (path_ == nullptr) {
545         LOGD("DrawPathOpItem path is null!");
546         return;
547     }
548     canvas->AttachPaint(paint_);
549     canvas->DrawPath(*path_);
550 }
551 
552 /* DrawBackgroundOpItem */
553 REGISTER_UNMARSHALLING_FUNC(DrawBackground, DrawOpItem::BACKGROUND_OPITEM, DrawBackgroundOpItem::Unmarshalling);
554 
DrawBackgroundOpItem(const DrawCmdList & cmdList,DrawBackgroundOpItem::ConstructorHandle * handle)555 DrawBackgroundOpItem::DrawBackgroundOpItem(const DrawCmdList& cmdList, DrawBackgroundOpItem::ConstructorHandle* handle)
556     : DrawOpItem(BACKGROUND_OPITEM)
557 {
558     BrushHandleToBrush(handle->brushHandle, cmdList, brush_);
559 }
560 
Unmarshalling(const DrawCmdList & cmdList,void * handle)561 std::shared_ptr<DrawOpItem> DrawBackgroundOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
562 {
563     return std::make_shared<DrawBackgroundOpItem>(
564         cmdList, static_cast<DrawBackgroundOpItem::ConstructorHandle*>(handle));
565 }
566 
Marshalling(DrawCmdList & cmdList)567 void DrawBackgroundOpItem::Marshalling(DrawCmdList& cmdList)
568 {
569     BrushHandle brushHandle;
570     BrushToBrushHandle(brush_, cmdList, brushHandle);
571     cmdList.AddOp<ConstructorHandle>(brushHandle);
572 }
573 
Playback(Canvas * canvas,const Rect * rect)574 void DrawBackgroundOpItem::Playback(Canvas* canvas, const Rect* rect)
575 {
576     canvas->DrawBackground(brush_);
577 }
578 
579 /* DrawShadowOpItem */
580 REGISTER_UNMARSHALLING_FUNC(DrawShadow, DrawOpItem::SHADOW_OPITEM, DrawShadowOpItem::Unmarshalling);
581 
DrawShadowOpItem(const DrawCmdList & cmdList,DrawShadowOpItem::ConstructorHandle * handle)582 DrawShadowOpItem::DrawShadowOpItem(const DrawCmdList& cmdList, DrawShadowOpItem::ConstructorHandle* handle)
583     : DrawOpItem(SHADOW_OPITEM), planeParams_(handle->planeParams), devLightPos_(handle->devLightPos),
584     lightRadius_(handle->lightRadius), ambientColor_(handle->ambientColor),
585     spotColor_(handle->spotColor), flag_(handle->flag)
586 {
587     path_ = CmdListHelper::GetPathFromCmdList(cmdList, handle->path);
588 }
589 
Unmarshalling(const DrawCmdList & cmdList,void * handle)590 std::shared_ptr<DrawOpItem> DrawShadowOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
591 {
592     return std::make_shared<DrawShadowOpItem>(cmdList, static_cast<DrawShadowOpItem::ConstructorHandle*>(handle));
593 }
594 
Marshalling(DrawCmdList & cmdList)595 void DrawShadowOpItem::Marshalling(DrawCmdList& cmdList)
596 {
597     auto pathHandle = CmdListHelper::AddPathToCmdList(cmdList, *path_);
598     cmdList.AddOp<ConstructorHandle>(
599         pathHandle, planeParams_, devLightPos_, lightRadius_, ambientColor_, spotColor_, flag_);
600 }
601 
Playback(Canvas * canvas,const Rect * rect)602 void DrawShadowOpItem::Playback(Canvas* canvas, const Rect* rect)
603 {
604     if (path_ == nullptr) {
605         LOGD("DrawShadowOpItem path is null!");
606         return;
607     }
608     canvas->DrawShadow(*path_, planeParams_, devLightPos_, lightRadius_,
609                        ambientColor_, spotColor_, flag_);
610 }
611 
612 /* DrawRegionOpItem */
613 REGISTER_UNMARSHALLING_FUNC(DrawRegion, DrawOpItem::REGION_OPITEM, DrawRegionOpItem::Unmarshalling);
614 
DrawRegionOpItem(const DrawCmdList & cmdList,DrawRegionOpItem::ConstructorHandle * handle)615 DrawRegionOpItem::DrawRegionOpItem(const DrawCmdList& cmdList, DrawRegionOpItem::ConstructorHandle* handle)
616     : DrawWithPaintOpItem(cmdList, handle->paintHandle, REGION_OPITEM)
617 {
618     region_ = CmdListHelper::GetRegionFromCmdList(cmdList, handle->region);
619 }
620 
Unmarshalling(const DrawCmdList & cmdList,void * handle)621 std::shared_ptr<DrawOpItem> DrawRegionOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
622 {
623     return std::make_shared<DrawRegionOpItem>(cmdList, static_cast<DrawRegionOpItem::ConstructorHandle*>(handle));
624 }
625 
Marshalling(DrawCmdList & cmdList)626 void DrawRegionOpItem::Marshalling(DrawCmdList& cmdList)
627 {
628     PaintHandle paintHandle;
629     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
630     auto regionHandle = CmdListHelper::AddRegionToCmdList(cmdList, *region_);
631     cmdList.AddOp<ConstructorHandle>(regionHandle, paintHandle);
632 }
633 
Playback(Canvas * canvas,const Rect * rect)634 void DrawRegionOpItem::Playback(Canvas* canvas, const Rect* rect)
635 {
636     if (region_ == nullptr) {
637         LOGD("DrawRegionOpItem region is nullptr!");
638         return;
639     }
640     canvas->AttachPaint(paint_);
641     canvas->DrawRegion(*region_);
642 }
643 
644 /* DrawVerticesOpItem */
645 REGISTER_UNMARSHALLING_FUNC(DrawVertices, DrawOpItem::VERTICES_OPITEM, DrawVerticesOpItem::Unmarshalling);
646 
DrawVerticesOpItem(const DrawCmdList & cmdList,DrawVerticesOpItem::ConstructorHandle * handle)647 DrawVerticesOpItem::DrawVerticesOpItem(const DrawCmdList& cmdList, DrawVerticesOpItem::ConstructorHandle* handle)
648     : DrawWithPaintOpItem(cmdList, handle->paintHandle, VERTICES_OPITEM), mode_(handle->mode)
649 {
650     vertices_ = CmdListHelper::GetVerticesFromCmdList(cmdList, handle->vertices);
651 }
652 
Unmarshalling(const DrawCmdList & cmdList,void * handle)653 std::shared_ptr<DrawOpItem> DrawVerticesOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
654 {
655     return std::make_shared<DrawVerticesOpItem>(cmdList, static_cast<DrawVerticesOpItem::ConstructorHandle*>(handle));
656 }
657 
Marshalling(DrawCmdList & cmdList)658 void DrawVerticesOpItem::Marshalling(DrawCmdList& cmdList)
659 {
660     PaintHandle paintHandle;
661     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
662     auto opDataHandle = CmdListHelper::AddVerticesToCmdList(cmdList, *vertices_);
663     cmdList.AddOp<ConstructorHandle>(opDataHandle, mode_, paintHandle);
664 }
665 
Playback(Canvas * canvas,const Rect * rect)666 void DrawVerticesOpItem::Playback(Canvas* canvas, const Rect* rect)
667 {
668     if (vertices_ == nullptr) {
669         LOGD("DrawVerticesOpItem vertices is null");
670         return;
671     }
672     canvas->AttachPaint(paint_);
673     canvas->DrawVertices(*vertices_, mode_);
674 }
675 
676 /* DrawColorOpItem */
677 REGISTER_UNMARSHALLING_FUNC(DrawColor, DrawOpItem::COLOR_OPITEM, DrawColorOpItem::Unmarshalling);
678 
DrawColorOpItem(DrawColorOpItem::ConstructorHandle * handle)679 DrawColorOpItem::DrawColorOpItem(DrawColorOpItem::ConstructorHandle* handle)
680     : DrawOpItem(COLOR_OPITEM), color_(handle->color), mode_(handle->mode) {}
681 
Unmarshalling(const DrawCmdList & cmdList,void * handle)682 std::shared_ptr<DrawOpItem> DrawColorOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
683 {
684     return std::make_shared<DrawColorOpItem>(static_cast<DrawColorOpItem::ConstructorHandle*>(handle));
685 }
686 
Marshalling(DrawCmdList & cmdList)687 void DrawColorOpItem::Marshalling(DrawCmdList& cmdList)
688 {
689     cmdList.AddOp<ConstructorHandle>(color_, mode_);
690 }
691 
Playback(Canvas * canvas,const Rect * rect)692 void DrawColorOpItem::Playback(Canvas* canvas, const Rect* rect)
693 {
694     canvas->DrawColor(color_, mode_);
695 }
696 
697 /* DrawImageNineOpItem */
698 REGISTER_UNMARSHALLING_FUNC(DrawImageNine, DrawOpItem::IMAGE_NINE_OPITEM, DrawImageNineOpItem::Unmarshalling);
699 
DrawImageNineOpItem(const DrawCmdList & cmdList,DrawImageNineOpItem::ConstructorHandle * handle)700 DrawImageNineOpItem::DrawImageNineOpItem(const DrawCmdList& cmdList, DrawImageNineOpItem::ConstructorHandle* handle)
701     : DrawOpItem(IMAGE_NINE_OPITEM), center_(handle->center), dst_(handle->dst), filter_(handle->filter),
702     hasBrush_(handle->hasBrush)
703 {
704     image_ = CmdListHelper::GetImageFromCmdList(cmdList, handle->image);
705     if (DrawOpItem::holdDrawingImagefunc_) {
706         DrawOpItem::holdDrawingImagefunc_(image_);
707     }
708     if (hasBrush_) {
709         BrushHandleToBrush(handle->brushHandle, cmdList, brush_);
710     }
711 }
712 
Unmarshalling(const DrawCmdList & cmdList,void * handle)713 std::shared_ptr<DrawOpItem> DrawImageNineOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
714 {
715     return std::make_shared<DrawImageNineOpItem>(cmdList, static_cast<DrawImageNineOpItem::ConstructorHandle*>(handle));
716 }
717 
Marshalling(DrawCmdList & cmdList)718 void DrawImageNineOpItem::Marshalling(DrawCmdList& cmdList)
719 {
720     auto imageHandle = CmdListHelper::AddImageToCmdList(cmdList, *image_);
721     BrushHandle brushHandle;
722     if (hasBrush_) {
723         BrushToBrushHandle(brush_, cmdList, brushHandle);
724     }
725 
726     cmdList.AddOp<ConstructorHandle>(imageHandle, center_, dst_, filter_, brushHandle, hasBrush_);
727 }
728 
Playback(Canvas * canvas,const Rect * rect)729 void DrawImageNineOpItem::Playback(Canvas* canvas, const Rect* rect)
730 {
731     if (image_ == nullptr) {
732         LOGD("DrawImageNineOpItem image is null");
733         return;
734     }
735     Brush* brushPtr = hasBrush_ ? &brush_ : nullptr;
736     canvas->DrawImageNine(image_.get(), center_, dst_, filter_, brushPtr);
737 }
738 
739 /* DrawImageLatticeOpItem */
740 REGISTER_UNMARSHALLING_FUNC(DrawImageLattice, DrawOpItem::IMAGE_LATTICE_OPITEM, DrawImageLatticeOpItem::Unmarshalling);
741 
DrawImageLatticeOpItem(const DrawCmdList & cmdList,DrawImageLatticeOpItem::ConstructorHandle * handle)742 DrawImageLatticeOpItem::DrawImageLatticeOpItem(
743     const DrawCmdList& cmdList, DrawImageLatticeOpItem::ConstructorHandle* handle)
744     : DrawOpItem(IMAGE_LATTICE_OPITEM), lattice_(handle->lattice), dst_(handle->dst), filter_(handle->filter),
745     hasBrush_(handle->hasBrush)
746 {
747     image_ = CmdListHelper::GetImageFromCmdList(cmdList, handle->image);
748     if (DrawOpItem::holdDrawingImagefunc_) {
749         DrawOpItem::holdDrawingImagefunc_(image_);
750     }
751     if (hasBrush_) {
752         BrushHandleToBrush(handle->brushHandle, cmdList, brush_);
753     }
754 }
755 
Unmarshalling(const DrawCmdList & cmdList,void * handle)756 std::shared_ptr<DrawOpItem> DrawImageLatticeOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
757 {
758     return std::make_shared<DrawImageLatticeOpItem>(
759         cmdList, static_cast<DrawImageLatticeOpItem::ConstructorHandle*>(handle));
760 }
761 
Marshalling(DrawCmdList & cmdList)762 void DrawImageLatticeOpItem::Marshalling(DrawCmdList& cmdList)
763 {
764     auto imageHandle = CmdListHelper::AddImageToCmdList(cmdList, *image_);
765     BrushHandle brushHandle;
766     if (hasBrush_) {
767         BrushToBrushHandle(brush_, cmdList, brushHandle);
768     }
769 
770     cmdList.AddOp<ConstructorHandle>(imageHandle, lattice_, dst_, filter_, brushHandle, hasBrush_);
771 }
772 
Playback(Canvas * canvas,const Rect * rect)773 void DrawImageLatticeOpItem::Playback(Canvas* canvas, const Rect* rect)
774 {
775     if (image_ == nullptr) {
776         LOGD("DrawImageNineOpItem image is null");
777         return;
778     }
779     Brush* brushPtr = hasBrush_ ? &brush_ : nullptr;
780     canvas->DrawImageLattice(image_.get(), lattice_, dst_, filter_, brushPtr);
781 }
782 
783 /* DrawBitmapOpItem */
784 REGISTER_UNMARSHALLING_FUNC(DrawBitmap, DrawOpItem::BITMAP_OPITEM, DrawBitmapOpItem::Unmarshalling);
785 
DrawBitmapOpItem(const DrawCmdList & cmdList,DrawBitmapOpItem::ConstructorHandle * handle)786 DrawBitmapOpItem::DrawBitmapOpItem(const DrawCmdList& cmdList, DrawBitmapOpItem::ConstructorHandle* handle)
787     : DrawWithPaintOpItem(cmdList, handle->paintHandle, BITMAP_OPITEM), px_(handle->px), py_(handle->py)
788 {
789     bitmap_ = CmdListHelper::GetBitmapFromCmdList(cmdList, handle->bitmap);
790 }
791 
Unmarshalling(const DrawCmdList & cmdList,void * handle)792 std::shared_ptr<DrawOpItem> DrawBitmapOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
793 {
794     return std::make_shared<DrawBitmapOpItem>(cmdList, static_cast<DrawBitmapOpItem::ConstructorHandle*>(handle));
795 }
796 
Marshalling(DrawCmdList & cmdList)797 void DrawBitmapOpItem::Marshalling(DrawCmdList& cmdList)
798 {
799     PaintHandle paintHandle;
800     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
801     auto bitmapHandle = CmdListHelper::AddBitmapToCmdList(cmdList, *bitmap_);
802     cmdList.AddOp<ConstructorHandle>(bitmapHandle, px_, py_, paintHandle);
803 }
804 
Playback(Canvas * canvas,const Rect * rect)805 void DrawBitmapOpItem::Playback(Canvas* canvas, const Rect* rect)
806 {
807     if (bitmap_ == nullptr) {
808         LOGD("DrawBitmapOpItem bitmap is null");
809         return;
810     }
811     canvas->AttachPaint(paint_);
812     canvas->DrawBitmap(*bitmap_, px_, py_);
813 }
814 
815 /* DrawImageOpItem */
816 REGISTER_UNMARSHALLING_FUNC(DrawImage, DrawOpItem::IMAGE_OPITEM, DrawImageOpItem::Unmarshalling);
817 
DrawImageOpItem(const DrawCmdList & cmdList,DrawImageOpItem::ConstructorHandle * handle)818 DrawImageOpItem::DrawImageOpItem(const DrawCmdList& cmdList, DrawImageOpItem::ConstructorHandle* handle)
819     : DrawWithPaintOpItem(cmdList, handle->paintHandle, IMAGE_OPITEM), px_(handle->px), py_(handle->py),
820       samplingOptions_(handle->samplingOptions)
821 {
822     image_ = CmdListHelper::GetImageFromCmdList(cmdList, handle->image);
823     if (DrawOpItem::holdDrawingImagefunc_) {
824         DrawOpItem::holdDrawingImagefunc_(image_);
825     }
826 }
827 
Unmarshalling(const DrawCmdList & cmdList,void * handle)828 std::shared_ptr<DrawOpItem> DrawImageOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
829 {
830     return std::make_shared<DrawImageOpItem>(cmdList, static_cast<DrawImageOpItem::ConstructorHandle*>(handle));
831 }
832 
Marshalling(DrawCmdList & cmdList)833 void DrawImageOpItem::Marshalling(DrawCmdList& cmdList)
834 {
835     PaintHandle paintHandle;
836     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
837     auto imageHandle = CmdListHelper::AddImageToCmdList(cmdList, *image_);
838     cmdList.AddOp<ConstructorHandle>(imageHandle, px_, py_, samplingOptions_, paintHandle);
839 }
840 
Playback(Canvas * canvas,const Rect * rect)841 void DrawImageOpItem::Playback(Canvas* canvas, const Rect* rect)
842 {
843     if (image_ == nullptr) {
844         LOGD("DrawImageOpItem image is null");
845         return;
846     }
847     canvas->AttachPaint(paint_);
848     canvas->DrawImage(*image_, px_, py_, samplingOptions_);
849 }
850 
851 /* DrawImageRectOpItem */
852 REGISTER_UNMARSHALLING_FUNC(DrawImageRect, DrawOpItem::IMAGE_RECT_OPITEM, DrawImageRectOpItem::Unmarshalling);
853 
DrawImageRectOpItem(const DrawCmdList & cmdList,DrawImageRectOpItem::ConstructorHandle * handle)854 DrawImageRectOpItem::DrawImageRectOpItem(const DrawCmdList& cmdList, DrawImageRectOpItem::ConstructorHandle* handle)
855     : DrawWithPaintOpItem(cmdList, handle->paintHandle, IMAGE_RECT_OPITEM), src_(handle->src), dst_(handle->dst),
856       sampling_(handle->sampling), constraint_(handle->constraint), isForeground_(handle->isForeground)
857 {
858     image_ = CmdListHelper::GetImageFromCmdList(cmdList, handle->image);
859     if (DrawOpItem::holdDrawingImagefunc_) {
860         DrawOpItem::holdDrawingImagefunc_(image_);
861     }
862 }
863 
DrawImageRectOpItem(const Image & image,const Rect & src,const Rect & dst,const SamplingOptions & sampling,SrcRectConstraint constraint,const Paint & paint,bool isForeground)864 DrawImageRectOpItem::DrawImageRectOpItem(const Image& image, const Rect& src,
865     const Rect& dst, const SamplingOptions& sampling,
866     SrcRectConstraint constraint, const Paint& paint, bool isForeground)
867     : DrawWithPaintOpItem(paint, DrawOpItem::IMAGE_RECT_OPITEM), src_(src), dst_(dst), sampling_(sampling),
868     constraint_(constraint), image_(std::make_shared<Image>(image)), isForeground_(isForeground)
869 {
870     if (DrawOpItem::holdDrawingImagefunc_) {
871         DrawOpItem::holdDrawingImagefunc_(image_);
872     }
873 }
874 
Unmarshalling(const DrawCmdList & cmdList,void * handle)875 std::shared_ptr<DrawOpItem> DrawImageRectOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
876 {
877     return std::make_shared<DrawImageRectOpItem>(cmdList, static_cast<DrawImageRectOpItem::ConstructorHandle*>(handle));
878 }
879 
Marshalling(DrawCmdList & cmdList)880 void DrawImageRectOpItem::Marshalling(DrawCmdList& cmdList)
881 {
882     PaintHandle paintHandle;
883     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
884     auto imageHandle = CmdListHelper::AddImageToCmdList(cmdList, *image_);
885     cmdList.AddOp<ConstructorHandle>(imageHandle, src_, dst_, sampling_, constraint_, paintHandle);
886 }
887 
Playback(Canvas * canvas,const Rect * rect)888 void DrawImageRectOpItem::Playback(Canvas* canvas, const Rect* rect)
889 {
890     if (image_ == nullptr) {
891         LOGD("DrawImageRectOpItem image is null");
892         return;
893     }
894     if (isForeground_) {
895         AutoCanvasRestore acr(*canvas, false);
896         SaveLayerOps ops;
897         canvas->SaveLayer(ops);
898         canvas->AttachPaint(paint_);
899         canvas->DrawImageRect(*image_, src_, dst_, sampling_, constraint_);
900         Brush brush;
901         brush.SetColor(canvas->GetEnvForegroundColor());
902         brush.SetBlendMode(Drawing::BlendMode::SRC_IN);
903         canvas->DrawBackground(brush);
904         return;
905     }
906     canvas->AttachPaint(paint_);
907     canvas->DrawImageRect(*image_, src_, dst_, sampling_, constraint_);
908 }
909 
910 /* DrawPictureOpItem */
911 REGISTER_UNMARSHALLING_FUNC(DrawPicture, DrawOpItem::PICTURE_OPITEM, DrawPictureOpItem::Unmarshalling);
912 
DrawPictureOpItem(const DrawCmdList & cmdList,DrawPictureOpItem::ConstructorHandle * handle)913 DrawPictureOpItem::DrawPictureOpItem(const DrawCmdList& cmdList, DrawPictureOpItem::ConstructorHandle* handle)
914     : DrawOpItem(PICTURE_OPITEM)
915 {
916     picture_ = CmdListHelper::GetPictureFromCmdList(cmdList, handle->picture);
917 }
918 
Unmarshalling(const DrawCmdList & cmdList,void * handle)919 std::shared_ptr<DrawOpItem> DrawPictureOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
920 {
921     return std::make_shared<DrawPictureOpItem>(cmdList, static_cast<DrawPictureOpItem::ConstructorHandle*>(handle));
922 }
923 
Marshalling(DrawCmdList & cmdList)924 void DrawPictureOpItem::Marshalling(DrawCmdList& cmdList)
925 {
926     auto pictureHandle = CmdListHelper::AddPictureToCmdList(cmdList, *picture_);
927     cmdList.AddOp<ConstructorHandle>(pictureHandle);
928 }
929 
Playback(Canvas * canvas,const Rect * rect)930 void DrawPictureOpItem::Playback(Canvas* canvas, const Rect* rect)
931 {
932     if (picture_ == nullptr) {
933         LOGD("DrawPictureOpItem picture is null");
934         return;
935     }
936     canvas->DrawPicture(*picture_);
937 }
938 
939 /* DrawTextBlobOpItem */
940 REGISTER_UNMARSHALLING_FUNC(DrawTextBlob, DrawOpItem::TEXT_BLOB_OPITEM, DrawTextBlobOpItem::Unmarshalling);
941 
SimplifyPaint(ColorQuad colorQuad,Paint & paint)942 void SimplifyPaint(ColorQuad colorQuad, Paint& paint)
943 {
944     Color color{colorQuad};
945     paint.SetColor(color);
946     paint.SetShaderEffect(nullptr);
947     if (paint.HasFilter()) {
948         Filter filter = paint.GetFilter();
949         if (filter.GetColorFilter() != nullptr) {
950             filter.SetColorFilter(nullptr);
951             paint.SetFilter(filter);
952         }
953     }
954     paint.SetWidth(1.04); // 1.04 is empirical value
955     paint.SetJoinStyle(Pen::JoinStyle::ROUND_JOIN);
956 }
957 
DrawTextBlobOpItem(const DrawCmdList & cmdList,DrawTextBlobOpItem::ConstructorHandle * handle)958 DrawTextBlobOpItem::DrawTextBlobOpItem(const DrawCmdList& cmdList, DrawTextBlobOpItem::ConstructorHandle* handle)
959     : DrawWithPaintOpItem(cmdList, handle->paintHandle, TEXT_BLOB_OPITEM), x_(handle->x), y_(handle->y)
960 {
961     textBlob_ = CmdListHelper::GetTextBlobFromCmdList(cmdList, handle->textBlob);
962 }
963 
Unmarshalling(const DrawCmdList & cmdList,void * handle)964 std::shared_ptr<DrawOpItem> DrawTextBlobOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
965 {
966     return std::make_shared<DrawTextBlobOpItem>(cmdList, static_cast<DrawTextBlobOpItem::ConstructorHandle*>(handle));
967 }
968 
Marshalling(DrawCmdList & cmdList)969 void DrawTextBlobOpItem::Marshalling(DrawCmdList& cmdList)
970 {
971     PaintHandle paintHandle;
972     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
973     auto textBlobHandle = CmdListHelper::AddTextBlobToCmdList(cmdList, textBlob_.get());
974     cmdList.AddOp<ConstructorHandle>(textBlobHandle, x_, y_, paintHandle);
975 }
976 
Playback(Canvas * canvas,const Rect * rect)977 void DrawTextBlobOpItem::Playback(Canvas* canvas, const Rect* rect)
978 {
979     if (textBlob_ == nullptr) {
980         LOGD("DrawTextBlobOpItem textBlob is null");
981         return;
982     }
983     Drawing::RectI globalClipBounds = canvas->GetDeviceClipBounds();
984     if (globalClipBounds.GetWidth() == 1 && !callFromCacheFunc_) {
985         // if the ClipBound's width == 1, the textblob will draw outside of the clip,
986         // this is a workround for this case
987         if (!cacheImage_) {
988             cacheImage_ = GenerateCachedOpItem(canvas);
989         }
990         if (cacheImage_) {
991             cacheImage_->Playback(canvas, rect);
992         }
993         return;
994     }
995     if (canvas->isHighContrastEnabled()) {
996         LOGD("DrawTextBlobOpItem::Playback highContrastEnabled, %{public}s, %{public}d", __FUNCTION__, __LINE__);
997         ColorQuad colorQuad = paint_.GetColor().CastToColorQuad();
998         if (Color::ColorQuadGetA(colorQuad) == 0 || paint_.HasFilter()) {
999             canvas->AttachPaint(paint_);
1000             canvas->DrawTextBlob(textBlob_.get(), x_, y_);
1001             return;
1002         }
1003         if (canvas->GetAlphaSaveCount() > 0 && canvas->GetAlpha() < 1.0f) {
1004             std::shared_ptr<Drawing::Surface> offScreenSurface;
1005             std::shared_ptr<Canvas> offScreenCanvas;
1006             if (GetOffScreenSurfaceAndCanvas(*canvas, offScreenSurface, offScreenCanvas)) {
1007                 DrawHighContrast(offScreenCanvas.get());
1008                 offScreenCanvas->Flush();
1009                 Drawing::Brush paint;
1010                 paint.SetAntiAlias(true);
1011                 canvas->AttachBrush(paint);
1012                 Drawing::SamplingOptions sampling =
1013                     Drawing::SamplingOptions(Drawing::FilterMode::NEAREST, Drawing::MipmapMode::NEAREST);
1014                 canvas->DrawImage(*offScreenSurface->GetImageSnapshot().get(), 0, 0, sampling);
1015                 canvas->DetachBrush();
1016                 return;
1017             }
1018         }
1019         DrawHighContrast(canvas);
1020     } else {
1021         canvas->AttachPaint(paint_);
1022         canvas->DrawTextBlob(textBlob_.get(), x_, y_);
1023     }
1024 }
1025 
DrawHighContrast(Canvas * canvas) const1026 void DrawTextBlobOpItem::DrawHighContrast(Canvas* canvas) const
1027 {
1028     ColorQuad colorQuad = paint_.GetColor().CastToColorQuad();
1029     uint32_t channelSum = Color::ColorQuadGetR(colorQuad) + Color::ColorQuadGetG(colorQuad) +
1030         Color::ColorQuadGetB(colorQuad);
1031     bool flag = channelSum < 594; // 594 is empirical value
1032 
1033     Paint outlinePaint(paint_);
1034     SimplifyPaint(flag ? Color::COLOR_WHITE : Color::COLOR_BLACK, outlinePaint);
1035     outlinePaint.SetStyle(Paint::PAINT_FILL_STROKE);
1036     canvas->AttachPaint(outlinePaint);
1037     canvas->DrawTextBlob(textBlob_.get(), x_, y_);
1038 
1039     Paint innerPaint(paint_);
1040     SimplifyPaint(flag ? Color::COLOR_BLACK : Color::COLOR_WHITE, innerPaint);
1041     innerPaint.SetStyle(Paint::PAINT_FILL);
1042     canvas->AttachPaint(innerPaint);
1043     canvas->DrawTextBlob(textBlob_.get(), x_, y_);
1044 }
1045 
GenerateCachedOpItem(DrawCmdList & cmdList,const TextBlob * textBlob,scalar x,scalar y,Paint & p)1046 bool DrawTextBlobOpItem::ConstructorHandle::GenerateCachedOpItem(
1047     DrawCmdList& cmdList, const TextBlob* textBlob, scalar x, scalar y, Paint& p)
1048 {
1049     if (!textBlob) {
1050         LOGD("textBlob nullptr, %{public}s, %{public}d", __FUNCTION__, __LINE__);
1051         return false;
1052     }
1053 
1054     auto bounds = textBlob->Bounds();
1055     if (!bounds || !bounds->IsValid()) {
1056         return false;
1057     }
1058     bounds->Offset(x, y);
1059     bounds->MakeOutset(TEXT_BLOB_CACHE_MARGIN, TEXT_BLOB_CACHE_MARGIN);
1060     // create CPU raster surface
1061     Drawing::ImageInfo offscreenInfo { bounds->GetWidth(), bounds->GetHeight(),
1062         Drawing::COLORTYPE_RGBA_8888, Drawing::ALPHATYPE_PREMUL, nullptr};
1063     std::shared_ptr<Surface> offscreenSurface = Surface::MakeRaster(offscreenInfo);
1064     if (offscreenSurface == nullptr) {
1065         return false;
1066     }
1067     auto offscreenCanvas = offscreenSurface->GetCanvas();
1068     if (offscreenCanvas == nullptr) {
1069         return false;
1070     }
1071     if (bounds->GetLeft() != 0 || bounds->GetTop() != 0) {
1072         offscreenCanvas->Translate(-bounds->GetLeft(), -bounds->GetTop());
1073     }
1074 
1075     //OffscreenCanvas used once, detach is unnecessary, FakeBrush/Pen avoid affecting ImageRectOp, attach is necessary.
1076     bool isForeground = false;
1077     if (p.GetColor() == Drawing::Color::COLOR_FOREGROUND) {
1078         isForeground = true;
1079         p.SetColor(Drawing::Color::COLOR_BLACK);
1080     }
1081     offscreenCanvas->AttachPaint(p);
1082     offscreenCanvas->DrawTextBlob(textBlob, x, y);
1083 
1084     std::shared_ptr<Image> image = offscreenSurface->GetImageSnapshot();
1085     Drawing::Rect src(0, 0, image->GetWidth(), image->GetHeight());
1086     Drawing::Rect dst(bounds->GetLeft(), bounds->GetTop(),
1087         bounds->GetLeft() + image->GetWidth(), bounds->GetTop() + image->GetHeight());
1088     SamplingOptions sampling;
1089     auto imageHandle = CmdListHelper::AddImageToCmdList(cmdList, image);
1090     PaintHandle fakePaintHandle;
1091     fakePaintHandle.isAntiAlias = true;
1092     fakePaintHandle.style = Paint::PaintStyle::PAINT_FILL;
1093     cmdList.AddOp<DrawImageRectOpItem::ConstructorHandle>(
1094         imageHandle, src, dst, sampling, SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT, fakePaintHandle, isForeground);
1095     return true;
1096 }
1097 
GenerateCachedOpItem(DrawCmdList & cmdList,Canvas * canvas)1098 bool DrawTextBlobOpItem::ConstructorHandle::GenerateCachedOpItem(DrawCmdList& cmdList, Canvas* canvas)
1099 {
1100     std::shared_ptr<TextBlob> textBlob_ = CmdListHelper::GetTextBlobFromCmdList(cmdList, textBlob);
1101     if (!textBlob_) {
1102         LOGD("textBlob nullptr, %{public}s, %{public}d", __FUNCTION__, __LINE__);
1103         return false;
1104     }
1105 
1106     auto bounds = textBlob_->Bounds();
1107     if (!bounds || !bounds->IsValid()) {
1108         return false;
1109     }
1110     bounds->Offset(x, y);
1111 
1112     std::shared_ptr<Surface> offscreenSurface = nullptr;
1113 
1114     if (auto surface = canvas != nullptr ? canvas->GetSurface() : nullptr) {
1115         // create GPU accelerated surface if possible
1116         offscreenSurface = surface->MakeSurface(bounds->GetWidth(), bounds->GetHeight());
1117     } else {
1118         // create CPU raster surface
1119         Drawing::ImageInfo offscreenInfo { bounds->GetWidth(), bounds->GetHeight(),
1120             Drawing::COLORTYPE_RGBA_8888, Drawing::ALPHATYPE_PREMUL, nullptr};
1121         offscreenSurface = Surface::MakeRaster(offscreenInfo);
1122     }
1123     if (offscreenSurface == nullptr) {
1124         return false;
1125     }
1126 
1127     Canvas* offscreenCanvas = offscreenSurface->GetCanvas().get();
1128 
1129     // align draw op to [0, 0]
1130     if (bounds->GetLeft() != 0 || bounds->GetTop() != 0) {
1131         offscreenCanvas->Translate(-bounds->GetLeft(), -bounds->GetTop());
1132     }
1133 
1134     Paint p;
1135     GeneratePaintFromHandle(paintHandle, cmdList, p);
1136     offscreenCanvas->AttachPaint(p);
1137     offscreenCanvas->DrawTextBlob(textBlob_.get(), x, y);
1138 
1139     std::shared_ptr<Image> image = offscreenSurface->GetImageSnapshot();
1140     Drawing::Rect src(0, 0, image->GetWidth(), image->GetHeight());
1141     Drawing::Rect dst(bounds->GetLeft(), bounds->GetTop(),
1142         bounds->GetLeft() + image->GetWidth(), bounds->GetTop() + image->GetHeight());
1143     SamplingOptions sampling;
1144     auto imageHandle = CmdListHelper::AddImageToCmdList(cmdList, image);
1145     PaintHandle fakePaintHandle;
1146     fakePaintHandle.isAntiAlias = true;
1147     fakePaintHandle.style = Paint::PaintStyle::PAINT_FILL;
1148     cmdList.AddOp<DrawImageRectOpItem::ConstructorHandle>(imageHandle, src, dst, sampling,
1149         SrcRectConstraint::FAST_SRC_RECT_CONSTRAINT, fakePaintHandle);
1150     return true;
1151 }
1152 
GenerateCachedOpItem(Canvas * canvas)1153 std::shared_ptr<DrawImageRectOpItem> DrawTextBlobOpItem::GenerateCachedOpItem(Canvas* canvas)
1154 {
1155     if (!textBlob_) {
1156         LOGD("textBlob nullptr, %{public}s, %{public}d", __FUNCTION__, __LINE__);
1157         return nullptr;
1158     }
1159 
1160     auto bounds = textBlob_->Bounds();
1161     if (!bounds || !bounds->IsValid()) {
1162         return nullptr;
1163     }
1164     bounds->Offset(x_, y_);
1165 
1166     std::shared_ptr<Surface> offscreenSurface = nullptr;
1167 
1168     if (auto surface = canvas != nullptr ? canvas->GetSurface() : nullptr) {
1169         // create GPU accelerated surface if possible
1170         offscreenSurface = surface->MakeSurface(bounds->GetWidth(), bounds->GetHeight());
1171     } else {
1172         // create CPU raster surface
1173         Drawing::ImageInfo offscreenInfo { bounds->GetWidth(), bounds->GetHeight(),
1174             Drawing::COLORTYPE_RGBA_8888, Drawing::ALPHATYPE_PREMUL, nullptr};
1175         offscreenSurface = Surface::MakeRaster(offscreenInfo);
1176     }
1177     if (offscreenSurface == nullptr) {
1178         return nullptr;
1179     }
1180 
1181     Canvas* offscreenCanvas = offscreenSurface->GetCanvas().get();
1182 
1183     // align draw op to [0, 0]
1184     if (bounds->GetLeft() != 0 || bounds->GetTop() != 0) {
1185         offscreenCanvas->Translate(-bounds->GetLeft(), -bounds->GetTop());
1186     }
1187 
1188     callFromCacheFunc_ = true;
1189     Playback(offscreenCanvas, nullptr);
1190     callFromCacheFunc_ = false;
1191 
1192     std::shared_ptr<Image> image = offscreenSurface->GetImageSnapshot();
1193     Drawing::Rect src(0, 0, image->GetWidth(), image->GetHeight());
1194     Drawing::Rect dst(bounds->GetLeft(), bounds->GetTop(), bounds->GetRight(), bounds->GetBottom());
1195     Paint fakePaint;
1196     fakePaint.SetStyle(Paint::PaintStyle::PAINT_FILL);
1197     fakePaint.SetAntiAlias(true);
1198     return std::make_shared<DrawImageRectOpItem>(*image, src, dst, SamplingOptions(),
1199         SrcRectConstraint::FAST_SRC_RECT_CONSTRAINT, fakePaint);
1200 }
1201 
1202 /* DrawSymbolOpItem */
1203 REGISTER_UNMARSHALLING_FUNC(DrawSymbol, DrawOpItem::SYMBOL_OPITEM, DrawSymbolOpItem::Unmarshalling);
1204 
DrawSymbolOpItem(const DrawCmdList & cmdList,DrawSymbolOpItem::ConstructorHandle * handle)1205 DrawSymbolOpItem::DrawSymbolOpItem(const DrawCmdList& cmdList, DrawSymbolOpItem::ConstructorHandle* handle)
1206     : DrawWithPaintOpItem(cmdList, handle->paintHandle, SYMBOL_OPITEM), locate_(handle->locate)
1207 {
1208     symbol_ = CmdListHelper::GetSymbolFromCmdList(cmdList, handle->symbolHandle);
1209 }
1210 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1211 std::shared_ptr<DrawOpItem> DrawSymbolOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1212 {
1213     return std::make_shared<DrawSymbolOpItem>(cmdList, static_cast<DrawSymbolOpItem::ConstructorHandle*>(handle));
1214 }
1215 
SetSymbol()1216 void DrawSymbolOpItem::SetSymbol()
1217 {
1218     if (symbol_.symbolInfo_.effect == DrawingEffectStrategy::HIERARCHICAL) {
1219         if (!startAnimation_) {
1220             InitialVariableColor();
1221         }
1222         for (size_t i = 0; i < animation_.size(); i++) {
1223             SetVariableColor(i);
1224         }
1225     }
1226 }
1227 
InitialScale()1228 void DrawSymbolOpItem::InitialScale()
1229 {
1230     DrawSymbolAnimation animation;
1231     animation.startValue = 0; // 0 means scale start value
1232     animation.curValue = 0; // 0 means scale current value
1233     animation.endValue = 0.5; // 0.5 means scale end value
1234     animation.speedValue = 0.05; // 0.05 means scale change step
1235     animation.number = 0; // 0 means number of times that the animation to be played
1236     animation.curTime = std::chrono::duration_cast<
1237         std::chrono::milliseconds>(
1238             std::chrono::system_clock::now().time_since_epoch()); //time ms
1239     animation_.push_back(animation);
1240     startAnimation_ = true;
1241 }
1242 
InitialVariableColor()1243 void DrawSymbolOpItem::InitialVariableColor()
1244 {
1245     LOGD("SetSymbol groups %{public}d", static_cast<int>(symbol_.symbolInfo_.renderGroups.size()));
1246 
1247     long long standStartDuration = 299;
1248     std::chrono::milliseconds standStartTime = std::chrono::duration_cast<
1249         std::chrono::milliseconds>(
1250             std::chrono::system_clock::now().time_since_epoch());
1251 
1252     for (size_t j = 0; j < symbol_.symbolInfo_.renderGroups.size(); j++) {
1253         DrawSymbolAnimation animation;
1254         animation.startValue = 0.4; // 0.4 means alpha start value
1255         animation.curValue = 0.4; // 0.4 means alpha current value
1256         animation.endValue = 1; // 1 means alpha end value
1257         animation.speedValue = 0.08; // 0.08 means alpha change step
1258         animation.number = 0; // 0 means number of times that the animation to be played
1259         animation.startDuration = standStartDuration - static_cast<long long>(100 * j); //100 is start time duration
1260         animation.curTime = standStartTime; // every group have same start timestamp
1261         animation_.push_back(animation);
1262         symbol_.symbolInfo_.renderGroups[j].color.a = animation.startValue;
1263     }
1264     startAnimation_ = true;
1265 }
1266 
SetScale(size_t index)1267 void DrawSymbolOpItem::SetScale(size_t index)
1268 {
1269     if (animation_.size() < index || animation_[index].number >= number_) {
1270         LOGD("SymbolOpItem::symbol scale animation is false!");
1271         return;
1272     }
1273     DrawSymbolAnimation animation = animation_[index];
1274     if (animation.number >= number_ || animation.startValue == animation.endValue) {
1275         return;
1276     }
1277     if (animation.number == 0) {
1278         LOGD("SymbolOpItem::symbol scale animation is start!");
1279     }
1280 
1281     if (abs(animation.curValue - animation.endValue) < animation.speedValue) {
1282         double temp = animation.startValue;
1283         animation.startValue = animation.endValue;
1284         animation.endValue = temp;
1285         animation.number++;
1286     }
1287     if (animation.number == number_) {
1288         LOGD("SymbolOpItem::symbol scale animation is end!");
1289         return;
1290     }
1291     if (animation.endValue > animation.startValue) {
1292         animation.curValue = animation.curValue + animation.speedValue;
1293     } else {
1294         animation.curValue = animation.curValue - animation.speedValue;
1295     }
1296     animation_[index] = animation;
1297 }
1298 
SetVariableColor(size_t index)1299 void DrawSymbolOpItem::SetVariableColor(size_t index)
1300 {
1301     if (animation_.size() < index || animation_[index].number >= number_) {
1302         return;
1303     }
1304 
1305     DrawSymbolAnimation animation = animation_[index];
1306 
1307     auto curTime = std::chrono::duration_cast<
1308         std::chrono::milliseconds>(
1309             std::chrono::system_clock::now().time_since_epoch());
1310 
1311     long long duration = (curTime - animation.curTime).count(); // ms
1312     animation.curTime = curTime;
1313     animation.startDuration = animation.startDuration - duration;
1314     if (animation.startValue == animation.endValue ||
1315         animation.startDuration > 0) {
1316         animation_[index] = animation;
1317         return;
1318     }
1319 
1320     // cal step
1321     float calSpeed = 1.2 / 700 * duration; //700 and 1.2 is duration
1322 
1323     if (abs(animation.curValue - animation.endValue) < calSpeed) {
1324         double stemp = animation.startValue;
1325         animation.startValue = animation.endValue;
1326         animation.endValue = stemp;
1327         animation.number++;
1328     }
1329 
1330     if (animation.endValue > animation.startValue) {
1331         animation.curValue = animation.curValue + calSpeed;
1332     } else {
1333         animation.curValue = animation.curValue - calSpeed;
1334     }
1335 
1336     UpdataVariableColor(animation.curValue, index);
1337     animation_[index] = animation;
1338 }
1339 
UpdateScale(const double cur,Path & path)1340 void DrawSymbolOpItem::UpdateScale(const double cur, Path& path)
1341 {
1342     LOGD("SymbolOpItem::animation cur %{public}f", static_cast<float>(cur));
1343     //set symbol
1344     Rect rect = path.GetBounds();
1345     float y = static_cast<float>(rect.GetWidth()) / 2;
1346     float x = static_cast<float>(rect.GetHeight()) / 2;
1347     Matrix matrix;
1348     matrix.Translate(-x, -y);
1349     path.Transform(matrix);
1350     Matrix matrix1;
1351     matrix1.SetScale(1.0f + cur, 1.0f+ cur);
1352     path.Transform(matrix1);
1353     Matrix matrix2;
1354     matrix2.Translate(x, y);
1355     path.Transform(matrix2);
1356 }
1357 
UpdataVariableColor(const double cur,size_t index)1358 void DrawSymbolOpItem::UpdataVariableColor(const double cur, size_t index)
1359 {
1360     symbol_.symbolInfo_.renderGroups[index].color.a = fmin(1, fmax(0, cur));
1361 }
1362 
Marshalling(DrawCmdList & cmdList)1363 void DrawSymbolOpItem::Marshalling(DrawCmdList& cmdList)
1364 {
1365     PaintHandle paintHandle;
1366     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
1367     auto symbolHandle = CmdListHelper::AddSymbolToCmdList(cmdList, symbol_);
1368     cmdList.AddOp<ConstructorHandle>(symbolHandle, locate_, paintHandle);
1369 }
1370 
Playback(Canvas * canvas,const Rect * rect)1371 void DrawSymbolOpItem::Playback(Canvas* canvas, const Rect* rect)
1372 {
1373     if (!canvas) {
1374         LOGD("SymbolOpItem::Playback failed cause by canvas is nullptr");
1375         return;
1376     }
1377     SetSymbol();
1378     Path path(symbol_.path_);
1379 
1380     // 1.0 move path
1381     path.Offset(locate_.GetX(), locate_.GetY());
1382 
1383     // 2.0 split path
1384     std::vector<Path> paths;
1385     DrawingHMSymbol::PathOutlineDecompose(path, paths);
1386     std::vector<Path> pathLayers;
1387     DrawingHMSymbol::MultilayerPath(symbol_.symbolInfo_.layers, paths, pathLayers);
1388 
1389     // 3.0 set paint
1390     Paint paintCopy = paint_;
1391     paintCopy.SetAntiAlias(true);
1392     paintCopy.SetStyle(Paint::PaintStyle::PAINT_FILL_STROKE);
1393     paintCopy.SetWidth(0.0f);
1394     paintCopy.SetJoinStyle(Pen::JoinStyle::ROUND_JOIN);
1395 
1396     // draw path
1397     std::vector<DrawingRenderGroup> groups = symbol_.symbolInfo_.renderGroups;
1398     LOGD("SymbolOpItem::Draw RenderGroup size %{public}d", static_cast<int>(groups.size()));
1399     if (groups.size() == 0) {
1400         canvas->AttachPaint(paintCopy);
1401         canvas->DrawPath(path);
1402     }
1403     for (auto group : groups) {
1404         Path multPath;
1405         MergeDrawingPath(multPath, group, pathLayers);
1406         // color
1407         paintCopy.SetColor(Color::ColorQuadSetARGB(0xFF, group.color.r, group.color.g, group.color.b));
1408         paintCopy.SetAlphaF(group.color.a);
1409         canvas->AttachPaint(paintCopy);
1410         canvas->DrawPath(multPath);
1411     }
1412 }
1413 
MergeDrawingPath(Drawing::Path & multPath,Drawing::DrawingRenderGroup & group,std::vector<Drawing::Path> & pathLayers)1414 void DrawSymbolOpItem::MergeDrawingPath(
1415     Drawing::Path& multPath, Drawing::DrawingRenderGroup& group, std::vector<Drawing::Path>& pathLayers)
1416 {
1417     for (auto groupInfo : group.groupInfos) {
1418         Drawing::Path pathTemp;
1419         for (auto k : groupInfo.layerIndexes) {
1420             if (k >= pathLayers.size()) {
1421                 continue;
1422             }
1423             pathTemp.AddPath(pathLayers[k]);
1424         }
1425         for (size_t h : groupInfo.maskIndexes) {
1426             if (h >= pathLayers.size()) {
1427                 continue;
1428             }
1429             Drawing::Path outPath;
1430             auto isOk = outPath.Op(pathTemp, pathLayers[h], Drawing::PathOp::DIFFERENCE);
1431             if (isOk) {
1432                 pathTemp = outPath;
1433             }
1434         }
1435         multPath.AddPath(pathTemp);
1436     }
1437 }
1438 
1439 /* ClipRectOpItem */
1440 REGISTER_UNMARSHALLING_FUNC(ClipRect, DrawOpItem::CLIP_RECT_OPITEM, ClipRectOpItem::Unmarshalling);
1441 
ClipRectOpItem(ClipRectOpItem::ConstructorHandle * handle)1442 ClipRectOpItem::ClipRectOpItem(ClipRectOpItem::ConstructorHandle* handle)
1443     : DrawOpItem(CLIP_RECT_OPITEM), rect_(handle->rect), clipOp_(handle->clipOp), doAntiAlias_(handle->doAntiAlias) {}
1444 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1445 std::shared_ptr<DrawOpItem> ClipRectOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1446 {
1447     return std::make_shared<ClipRectOpItem>(static_cast<ClipRectOpItem::ConstructorHandle*>(handle));
1448 }
1449 
Marshalling(DrawCmdList & cmdList)1450 void ClipRectOpItem::Marshalling(DrawCmdList& cmdList)
1451 {
1452     cmdList.AddOp<ConstructorHandle>(rect_, clipOp_, doAntiAlias_);
1453 }
1454 
Playback(Canvas * canvas,const Rect * rect)1455 void ClipRectOpItem::Playback(Canvas* canvas, const Rect* rect)
1456 {
1457     canvas->ClipRect(rect_, clipOp_, doAntiAlias_);
1458 }
1459 
1460 /* ClipIRectOpItem */
1461 REGISTER_UNMARSHALLING_FUNC(ClipIRect, DrawOpItem::CLIP_IRECT_OPITEM, ClipIRectOpItem::Unmarshalling);
1462 
ClipIRectOpItem(ClipIRectOpItem::ConstructorHandle * handle)1463 ClipIRectOpItem::ClipIRectOpItem(ClipIRectOpItem::ConstructorHandle* handle)
1464     : DrawOpItem(CLIP_IRECT_OPITEM), rect_(handle->rect), clipOp_(handle->clipOp) {}
1465 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1466 std::shared_ptr<DrawOpItem> ClipIRectOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1467 {
1468     return std::make_shared<ClipIRectOpItem>(static_cast<ClipIRectOpItem::ConstructorHandle*>(handle));
1469 }
1470 
Marshalling(DrawCmdList & cmdList)1471 void ClipIRectOpItem::Marshalling(DrawCmdList& cmdList)
1472 {
1473     cmdList.AddOp<ConstructorHandle>(rect_, clipOp_);
1474 }
1475 
Playback(Canvas * canvas,const Rect * rect)1476 void ClipIRectOpItem::Playback(Canvas* canvas, const Rect* rect)
1477 {
1478     canvas->ClipIRect(rect_, clipOp_);
1479 }
1480 
1481 /* ClipRoundRectOpItem */
1482 REGISTER_UNMARSHALLING_FUNC(ClipRoundRect, DrawOpItem::CLIP_ROUND_RECT_OPITEM, ClipRoundRectOpItem::Unmarshalling);
1483 
ClipRoundRectOpItem(ClipRoundRectOpItem::ConstructorHandle * handle)1484 ClipRoundRectOpItem::ClipRoundRectOpItem(ClipRoundRectOpItem::ConstructorHandle* handle)
1485     : DrawOpItem(CLIP_ROUND_RECT_OPITEM), rrect_(handle->rrect), clipOp_(handle->clipOp),
1486     doAntiAlias_(handle->doAntiAlias) {}
1487 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1488 std::shared_ptr<DrawOpItem> ClipRoundRectOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1489 {
1490     return std::make_shared<ClipRoundRectOpItem>(static_cast<ClipRoundRectOpItem::ConstructorHandle*>(handle));
1491 }
1492 
Marshalling(DrawCmdList & cmdList)1493 void ClipRoundRectOpItem::Marshalling(DrawCmdList& cmdList)
1494 {
1495     cmdList.AddOp<ConstructorHandle>(rrect_, clipOp_, doAntiAlias_);
1496 }
1497 
Playback(Canvas * canvas,const Rect * rect)1498 void ClipRoundRectOpItem::Playback(Canvas* canvas, const Rect* rect)
1499 {
1500     canvas->ClipRoundRect(rrect_, clipOp_, doAntiAlias_);
1501 }
1502 
1503 /* ClipPathOpItem */
1504 REGISTER_UNMARSHALLING_FUNC(ClipPath, DrawOpItem::CLIP_PATH_OPITEM, ClipPathOpItem::Unmarshalling);
1505 
ClipPathOpItem(const DrawCmdList & cmdList,ClipPathOpItem::ConstructorHandle * handle)1506 ClipPathOpItem::ClipPathOpItem(const DrawCmdList& cmdList, ClipPathOpItem::ConstructorHandle* handle)
1507     : DrawOpItem(CLIP_PATH_OPITEM), clipOp_(handle->clipOp), doAntiAlias_(handle->doAntiAlias)
1508 {
1509     path_ = CmdListHelper::GetPathFromCmdList(cmdList, handle->path);
1510 }
1511 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1512 std::shared_ptr<DrawOpItem> ClipPathOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1513 {
1514     return std::make_shared<ClipPathOpItem>(cmdList, static_cast<ClipPathOpItem::ConstructorHandle*>(handle));
1515 }
1516 
Marshalling(DrawCmdList & cmdList)1517 void ClipPathOpItem::Marshalling(DrawCmdList& cmdList)
1518 {
1519     auto pathHandle = CmdListHelper::AddPathToCmdList(cmdList, *path_);
1520     cmdList.AddOp<ConstructorHandle>(pathHandle, clipOp_, doAntiAlias_);
1521 }
1522 
Playback(Canvas * canvas,const Rect * rect)1523 void ClipPathOpItem::Playback(Canvas* canvas, const Rect* rect)
1524 {
1525     if (path_ == nullptr) {
1526         LOGD("ClipPathOpItem path is null!");
1527         return;
1528     }
1529     canvas->ClipPath(*path_, clipOp_, doAntiAlias_);
1530 }
1531 
1532 /* ClipRegionOpItem */
1533 REGISTER_UNMARSHALLING_FUNC(ClipRegion, DrawOpItem::CLIP_REGION_OPITEM, ClipRegionOpItem::Unmarshalling);
1534 
ClipRegionOpItem(const DrawCmdList & cmdList,ClipRegionOpItem::ConstructorHandle * handle)1535 ClipRegionOpItem::ClipRegionOpItem(const DrawCmdList& cmdList, ClipRegionOpItem::ConstructorHandle* handle)
1536     : DrawOpItem(CLIP_REGION_OPITEM), clipOp_(handle->clipOp)
1537 {
1538     region_ = CmdListHelper::GetRegionFromCmdList(cmdList, handle->region);
1539 }
1540 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1541 std::shared_ptr<DrawOpItem> ClipRegionOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1542 {
1543     return std::make_shared<ClipRegionOpItem>(cmdList, static_cast<ClipRegionOpItem::ConstructorHandle*>(handle));
1544 }
1545 
Marshalling(DrawCmdList & cmdList)1546 void ClipRegionOpItem::Marshalling(DrawCmdList& cmdList)
1547 {
1548     auto regionHandle = CmdListHelper::AddRegionToCmdList(cmdList, *region_);
1549     cmdList.AddOp<ConstructorHandle>(regionHandle, clipOp_);
1550 }
1551 
Playback(Canvas * canvas,const Rect * rect)1552 void ClipRegionOpItem::Playback(Canvas* canvas, const Rect* rect)
1553 {
1554     if (region_ == nullptr) {
1555         LOGD("ClipRegionOpItem region is null!");
1556         return;
1557     }
1558     canvas->ClipRegion(*region_, clipOp_);
1559 }
1560 
1561 /* SetMatrixOpItem */
1562 REGISTER_UNMARSHALLING_FUNC(SetMatrix, DrawOpItem::SET_MATRIX_OPITEM, SetMatrixOpItem::Unmarshalling);
1563 
SetMatrixOpItem(SetMatrixOpItem::ConstructorHandle * handle)1564 SetMatrixOpItem::SetMatrixOpItem(SetMatrixOpItem::ConstructorHandle* handle) : DrawOpItem(SET_MATRIX_OPITEM)
1565 {
1566     matrix_.SetAll(handle->matrixBuffer);
1567 }
1568 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1569 std::shared_ptr<DrawOpItem> SetMatrixOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1570 {
1571     return std::make_shared<SetMatrixOpItem>(static_cast<SetMatrixOpItem::ConstructorHandle*>(handle));
1572 }
1573 
Marshalling(DrawCmdList & cmdList)1574 void SetMatrixOpItem::Marshalling(DrawCmdList& cmdList)
1575 {
1576     Matrix::Buffer matrixBuffer;
1577     matrix_.GetAll(matrixBuffer);
1578     cmdList.AddOp<ConstructorHandle>(matrixBuffer);
1579 }
1580 
Playback(Canvas * canvas,const Rect * rect)1581 void SetMatrixOpItem::Playback(Canvas* canvas, const Rect* rect)
1582 {
1583     canvas->SetMatrix(matrix_);
1584 }
1585 
1586 /* ResetMatrixOpItem */
1587 REGISTER_UNMARSHALLING_FUNC(ResetMatrix, DrawOpItem::RESET_MATRIX_OPITEM, ResetMatrixOpItem::Unmarshalling);
1588 
ResetMatrixOpItem()1589 ResetMatrixOpItem::ResetMatrixOpItem() : DrawOpItem(RESET_MATRIX_OPITEM) {}
1590 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1591 std::shared_ptr<DrawOpItem> ResetMatrixOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1592 {
1593     return std::make_shared<ResetMatrixOpItem>();
1594 }
1595 
Marshalling(DrawCmdList & cmdList)1596 void ResetMatrixOpItem::Marshalling(DrawCmdList& cmdList)
1597 {
1598     cmdList.AddOp<ConstructorHandle>();
1599 }
1600 
Playback(Canvas * canvas,const Rect * rect)1601 void ResetMatrixOpItem::Playback(Canvas* canvas, const Rect* rect)
1602 {
1603     canvas->ResetMatrix();
1604 }
1605 
1606 /* ConcatMatrixOpItem */
1607 REGISTER_UNMARSHALLING_FUNC(ConcatMatrix, DrawOpItem::CONCAT_MATRIX_OPITEM, ConcatMatrixOpItem::Unmarshalling);
1608 
ConcatMatrixOpItem(ConcatMatrixOpItem::ConstructorHandle * handle)1609 ConcatMatrixOpItem::ConcatMatrixOpItem(ConcatMatrixOpItem::ConstructorHandle* handle) : DrawOpItem(CONCAT_MATRIX_OPITEM)
1610 {
1611     matrix_.SetAll(handle->matrixBuffer);
1612 }
1613 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1614 std::shared_ptr<DrawOpItem> ConcatMatrixOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1615 {
1616     return std::make_shared<ConcatMatrixOpItem>(static_cast<ConcatMatrixOpItem::ConstructorHandle*>(handle));
1617 }
1618 
Marshalling(DrawCmdList & cmdList)1619 void ConcatMatrixOpItem::Marshalling(DrawCmdList& cmdList)
1620 {
1621     Matrix::Buffer matrixBuffer;
1622     matrix_.GetAll(matrixBuffer);
1623     cmdList.AddOp<ConstructorHandle>(matrixBuffer);
1624 }
1625 
Playback(Canvas * canvas,const Rect * rect)1626 void ConcatMatrixOpItem::Playback(Canvas* canvas, const Rect* rect)
1627 {
1628     canvas->ConcatMatrix(matrix_);
1629 }
1630 
1631 /* TranslateOpItem */
1632 REGISTER_UNMARSHALLING_FUNC(Translate, DrawOpItem::TRANSLATE_OPITEM, TranslateOpItem::Unmarshalling);
1633 
TranslateOpItem(TranslateOpItem::ConstructorHandle * handle)1634 TranslateOpItem::TranslateOpItem(TranslateOpItem::ConstructorHandle* handle)
1635     : DrawOpItem(TRANSLATE_OPITEM), dx_(handle->dx), dy_(handle->dy) {}
1636 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1637 std::shared_ptr<DrawOpItem> TranslateOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1638 {
1639     return std::make_shared<TranslateOpItem>(static_cast<TranslateOpItem::ConstructorHandle*>(handle));
1640 }
1641 
Marshalling(DrawCmdList & cmdList)1642 void TranslateOpItem::Marshalling(DrawCmdList& cmdList)
1643 {
1644     cmdList.AddOp<ConstructorHandle>(dx_, dy_);
1645 }
1646 
Playback(Canvas * canvas,const Rect * rect)1647 void TranslateOpItem::Playback(Canvas* canvas, const Rect* rect)
1648 {
1649     canvas->Translate(dx_, dy_);
1650 }
1651 
1652 /* ScaleOpItem */
1653 REGISTER_UNMARSHALLING_FUNC(Scale, DrawOpItem::SCALE_OPITEM, ScaleOpItem::Unmarshalling);
1654 
ScaleOpItem(ScaleOpItem::ConstructorHandle * handle)1655 ScaleOpItem::ScaleOpItem(ScaleOpItem::ConstructorHandle* handle)
1656     : DrawOpItem(SCALE_OPITEM), sx_(handle->sx), sy_(handle->sy) {}
1657 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1658 std::shared_ptr<DrawOpItem> ScaleOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1659 {
1660     return std::make_shared<ScaleOpItem>(static_cast<ScaleOpItem::ConstructorHandle*>(handle));
1661 }
1662 
Marshalling(DrawCmdList & cmdList)1663 void ScaleOpItem::Marshalling(DrawCmdList& cmdList)
1664 {
1665     cmdList.AddOp<ConstructorHandle>(sx_, sy_);
1666 }
1667 
Playback(Canvas * canvas,const Rect * rect)1668 void ScaleOpItem::Playback(Canvas* canvas, const Rect* rect)
1669 {
1670     canvas->Scale(sx_, sy_);
1671 }
1672 
1673 /* RotateOpItem */
1674 REGISTER_UNMARSHALLING_FUNC(Rotate, DrawOpItem::ROTATE_OPITEM, RotateOpItem::Unmarshalling);
1675 
RotateOpItem(RotateOpItem::ConstructorHandle * handle)1676 RotateOpItem::RotateOpItem(RotateOpItem::ConstructorHandle* handle)
1677     : DrawOpItem(ROTATE_OPITEM), deg_(handle->deg), sx_(handle->sx), sy_(handle->sy) {}
1678 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1679 std::shared_ptr<DrawOpItem> RotateOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1680 {
1681     return std::make_shared<RotateOpItem>(static_cast<RotateOpItem::ConstructorHandle*>(handle));
1682 }
1683 
Marshalling(DrawCmdList & cmdList)1684 void RotateOpItem::Marshalling(DrawCmdList& cmdList)
1685 {
1686     cmdList.AddOp<ConstructorHandle>(deg_, sx_, sy_);
1687 }
1688 
Playback(Canvas * canvas,const Rect * rect)1689 void RotateOpItem::Playback(Canvas* canvas, const Rect* rect)
1690 {
1691     canvas->Rotate(deg_, sx_, sy_);
1692 }
1693 
1694 /* ShearOpItem */
1695 REGISTER_UNMARSHALLING_FUNC(Shear, DrawOpItem::SHEAR_OPITEM, ShearOpItem::Unmarshalling);
1696 
ShearOpItem(ShearOpItem::ConstructorHandle * handle)1697 ShearOpItem::ShearOpItem(ShearOpItem::ConstructorHandle* handle)
1698     : DrawOpItem(SHEAR_OPITEM), sx_(handle->sx), sy_(handle->sy) {}
1699 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1700 std::shared_ptr<DrawOpItem> ShearOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1701 {
1702     return std::make_shared<ShearOpItem>(static_cast<ShearOpItem::ConstructorHandle*>(handle));
1703 }
1704 
Marshalling(DrawCmdList & cmdList)1705 void ShearOpItem::Marshalling(DrawCmdList& cmdList)
1706 {
1707     cmdList.AddOp<ConstructorHandle>(sx_, sy_);
1708 }
1709 
Playback(Canvas * canvas,const Rect * rect)1710 void ShearOpItem::Playback(Canvas* canvas, const Rect* rect)
1711 {
1712     canvas->Shear(sx_, sy_);
1713 }
1714 
1715 /* FlushOpItem */
1716 REGISTER_UNMARSHALLING_FUNC(Flush, DrawOpItem::FLUSH_OPITEM, FlushOpItem::Unmarshalling);
1717 
FlushOpItem()1718 FlushOpItem::FlushOpItem() : DrawOpItem(FLUSH_OPITEM) {}
1719 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1720 std::shared_ptr<DrawOpItem> FlushOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1721 {
1722     return std::make_shared<FlushOpItem>();
1723 }
1724 
Marshalling(DrawCmdList & cmdList)1725 void FlushOpItem::Marshalling(DrawCmdList& cmdList)
1726 {
1727     cmdList.AddOp<ConstructorHandle>();
1728 }
1729 
Playback(Canvas * canvas,const Rect * rect)1730 void FlushOpItem::Playback(Canvas* canvas, const Rect* rect)
1731 {
1732     canvas->Flush();
1733 }
1734 
1735 /* ClearOpItem */
1736 REGISTER_UNMARSHALLING_FUNC(Clear, DrawOpItem::CLEAR_OPITEM, ClearOpItem::Unmarshalling);
1737 
ClearOpItem(ClearOpItem::ConstructorHandle * handle)1738 ClearOpItem::ClearOpItem(ClearOpItem::ConstructorHandle* handle)
1739     : DrawOpItem(CLEAR_OPITEM), color_(handle->color) {}
1740 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1741 std::shared_ptr<DrawOpItem> ClearOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1742 {
1743     return std::make_shared<ClearOpItem>(static_cast<ClearOpItem::ConstructorHandle*>(handle));
1744 }
1745 
Marshalling(DrawCmdList & cmdList)1746 void ClearOpItem::Marshalling(DrawCmdList& cmdList)
1747 {
1748     cmdList.AddOp<ConstructorHandle>(color_);
1749 }
1750 
Playback(Canvas * canvas,const Rect * rect)1751 void ClearOpItem::Playback(Canvas* canvas, const Rect* rect)
1752 {
1753     canvas->Clear(color_);
1754 }
1755 
1756 /* SaveOpItem */
1757 REGISTER_UNMARSHALLING_FUNC(Save, DrawOpItem::SAVE_OPITEM, SaveOpItem::Unmarshalling);
1758 
SaveOpItem()1759 SaveOpItem::SaveOpItem() : DrawOpItem(SAVE_OPITEM) {}
1760 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1761 std::shared_ptr<DrawOpItem> SaveOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1762 {
1763     return std::make_shared<SaveOpItem>();
1764 }
1765 
Marshalling(DrawCmdList & cmdList)1766 void SaveOpItem::Marshalling(DrawCmdList& cmdList)
1767 {
1768     cmdList.AddOp<ConstructorHandle>();
1769 }
1770 
Playback(Canvas * canvas,const Rect * rect)1771 void SaveOpItem::Playback(Canvas* canvas, const Rect* rect)
1772 {
1773     canvas->Save();
1774 }
1775 
1776 /* SaveLayerOpItem */
1777 REGISTER_UNMARSHALLING_FUNC(SaveLayer, DrawOpItem::SAVE_LAYER_OPITEM, SaveLayerOpItem::Unmarshalling);
1778 
SaveLayerOpItem(const DrawCmdList & cmdList,SaveLayerOpItem::ConstructorHandle * handle)1779 SaveLayerOpItem::SaveLayerOpItem(const DrawCmdList& cmdList, SaveLayerOpItem::ConstructorHandle* handle)
1780     : DrawOpItem(SAVE_LAYER_OPITEM), saveLayerFlags_(handle->saveLayerFlags), rect_(handle->rect),
1781     hasBrush_(handle->hasBrush)
1782 {
1783     if (hasBrush_) {
1784         BrushHandleToBrush(handle->brushHandle, cmdList, brush_);
1785     }
1786 }
1787 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1788 std::shared_ptr<DrawOpItem> SaveLayerOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1789 {
1790     return std::make_shared<SaveLayerOpItem>(cmdList, static_cast<SaveLayerOpItem::ConstructorHandle*>(handle));
1791 }
1792 
Marshalling(DrawCmdList & cmdList)1793 void SaveLayerOpItem::Marshalling(DrawCmdList& cmdList)
1794 {
1795     BrushHandle brushHandle;
1796     if (hasBrush_) {
1797         BrushToBrushHandle(brush_, cmdList, brushHandle);
1798     }
1799     cmdList.AddOp<ConstructorHandle>(rect_, hasBrush_, brushHandle, saveLayerFlags_);
1800 }
1801 
Playback(Canvas * canvas,const Rect * rect)1802 void SaveLayerOpItem::Playback(Canvas* canvas, const Rect* rect)
1803 {
1804     const Rect* rectPtr = nullptr;
1805     if (rect_.IsValid()) {
1806         rectPtr = &rect_;
1807     }
1808     Brush* brushPtr = hasBrush_ ? &brush_ : nullptr;
1809     SaveLayerOps slo(rectPtr, brushPtr, saveLayerFlags_);
1810     canvas->SaveLayer(slo);
1811 }
1812 
1813 /* RestoreOpItem */
1814 REGISTER_UNMARSHALLING_FUNC(Restore, DrawOpItem::RESTORE_OPITEM, RestoreOpItem::Unmarshalling);
1815 
RestoreOpItem()1816 RestoreOpItem::RestoreOpItem() : DrawOpItem(RESTORE_OPITEM) {}
1817 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1818 std::shared_ptr<DrawOpItem> RestoreOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1819 {
1820     return std::make_shared<RestoreOpItem>();
1821 }
1822 
Marshalling(DrawCmdList & cmdList)1823 void RestoreOpItem::Marshalling(DrawCmdList& cmdList)
1824 {
1825     cmdList.AddOp<ConstructorHandle>();
1826 }
1827 
Playback(Canvas * canvas,const Rect * rect)1828 void RestoreOpItem::Playback(Canvas* canvas, const Rect* rect)
1829 {
1830     canvas->Restore();
1831 }
1832 
1833 /* DiscardOpItem */
1834 REGISTER_UNMARSHALLING_FUNC(Discard, DrawOpItem::DISCARD_OPITEM, DiscardOpItem::Unmarshalling);
1835 
DiscardOpItem()1836 DiscardOpItem::DiscardOpItem() : DrawOpItem(DISCARD_OPITEM) {}
1837 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1838 std::shared_ptr<DrawOpItem> DiscardOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1839 {
1840     return std::make_shared<DiscardOpItem>();
1841 }
1842 
Marshalling(DrawCmdList & cmdList)1843 void DiscardOpItem::Marshalling(DrawCmdList& cmdList)
1844 {
1845     cmdList.AddOp<ConstructorHandle>();
1846 }
1847 
Playback(Canvas * canvas,const Rect * rect)1848 void DiscardOpItem::Playback(Canvas* canvas, const Rect* rect)
1849 {
1850     canvas->Discard();
1851 }
1852 
1853 /* ClipAdaptiveRoundRectOpItem */
1854 REGISTER_UNMARSHALLING_FUNC(
1855     ClipAdaptiveRoundRect, DrawOpItem::CLIP_ADAPTIVE_ROUND_RECT_OPITEM, ClipAdaptiveRoundRectOpItem::Unmarshalling);
1856 
ClipAdaptiveRoundRectOpItem(const DrawCmdList & cmdList,ClipAdaptiveRoundRectOpItem::ConstructorHandle * handle)1857 ClipAdaptiveRoundRectOpItem::ClipAdaptiveRoundRectOpItem(
1858     const DrawCmdList& cmdList, ClipAdaptiveRoundRectOpItem::ConstructorHandle* handle)
1859     : DrawOpItem(CLIP_ADAPTIVE_ROUND_RECT_OPITEM)
1860 {
1861     radiusData_ = CmdListHelper::GetVectorFromCmdList<Point>(cmdList, handle->radiusData);
1862 }
1863 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1864 std::shared_ptr<DrawOpItem> ClipAdaptiveRoundRectOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1865 {
1866     return std::make_shared<ClipAdaptiveRoundRectOpItem>(
1867         cmdList, static_cast<ClipAdaptiveRoundRectOpItem::ConstructorHandle*>(handle));
1868 }
1869 
Marshalling(DrawCmdList & cmdList)1870 void ClipAdaptiveRoundRectOpItem::Marshalling(DrawCmdList& cmdList)
1871 {
1872     auto radiusData = CmdListHelper::AddVectorToCmdList<Point>(cmdList, radiusData_);
1873     cmdList.AddOp<ConstructorHandle>(radiusData);
1874 }
1875 
Playback(Canvas * canvas,const Rect * rect)1876 void ClipAdaptiveRoundRectOpItem::Playback(Canvas* canvas, const Rect* rect)
1877 {
1878     canvas->ClipRoundRect(*rect, radiusData_, true);
1879 }
1880 
1881 /* DrawAdaptiveImageOpItem */
1882 REGISTER_UNMARSHALLING_FUNC(
1883     DrawAdaptiveImage, DrawOpItem::ADAPTIVE_IMAGE_OPITEM, DrawAdaptiveImageOpItem::Unmarshalling);
1884 
DrawAdaptiveImageOpItem(const DrawCmdList & cmdList,DrawAdaptiveImageOpItem::ConstructorHandle * handle)1885 DrawAdaptiveImageOpItem::DrawAdaptiveImageOpItem(
1886     const DrawCmdList& cmdList, DrawAdaptiveImageOpItem::ConstructorHandle* handle)
1887     : DrawWithPaintOpItem(cmdList, handle->paintHandle, ADAPTIVE_IMAGE_OPITEM),
1888       rsImageInfo_(handle->rsImageInfo), sampling_(handle->sampling), isImage_(handle->isImage)
1889 {
1890     if (isImage_) {
1891         image_ = CmdListHelper::GetImageFromCmdList(cmdList, handle->image);
1892         if (DrawOpItem::holdDrawingImagefunc_) {
1893             DrawOpItem::holdDrawingImagefunc_(image_);
1894         }
1895     } else {
1896         data_ = CmdListHelper::GetCompressDataFromCmdList(cmdList, handle->image);
1897     }
1898 }
1899 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1900 std::shared_ptr<DrawOpItem> DrawAdaptiveImageOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1901 {
1902     return std::make_shared<DrawAdaptiveImageOpItem>(
1903         cmdList, static_cast<DrawAdaptiveImageOpItem::ConstructorHandle*>(handle));
1904 }
1905 
Marshalling(DrawCmdList & cmdList)1906 void DrawAdaptiveImageOpItem::Marshalling(DrawCmdList& cmdList)
1907 {
1908     PaintHandle paintHandle;
1909     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
1910     OpDataHandle imageHandle;
1911     if (!isImage_) {
1912         imageHandle = CmdListHelper::AddCompressDataToCmdList(cmdList, data_);
1913     } else {
1914         imageHandle = CmdListHelper::AddImageToCmdList(cmdList, image_);
1915     }
1916     cmdList.AddOp<ConstructorHandle>(imageHandle, rsImageInfo_, sampling_, isImage_, paintHandle);
1917 }
1918 
Playback(Canvas * canvas,const Rect * rect)1919 void DrawAdaptiveImageOpItem::Playback(Canvas* canvas, const Rect* rect)
1920 {
1921     if (isImage_ && image_ != nullptr) {
1922         canvas->AttachPaint(paint_);
1923         AdaptiveImageHelper::DrawImage(*canvas, *rect, image_, rsImageInfo_, sampling_);
1924         return;
1925     }
1926     if (!isImage_ && data_ != nullptr) {
1927         canvas->AttachPaint(paint_);
1928         AdaptiveImageHelper::DrawImage(*canvas, *rect, data_, rsImageInfo_, sampling_);
1929     }
1930 }
1931 
1932 /* DrawAdaptivePixelMapOpItem */
1933 REGISTER_UNMARSHALLING_FUNC(
1934     DrawAdaptivePixelMap, DrawOpItem::ADAPTIVE_PIXELMAP_OPITEM, DrawAdaptivePixelMapOpItem::Unmarshalling);
1935 
DrawAdaptivePixelMapOpItem(const DrawCmdList & cmdList,DrawAdaptivePixelMapOpItem::ConstructorHandle * handle)1936 DrawAdaptivePixelMapOpItem::DrawAdaptivePixelMapOpItem(
1937     const DrawCmdList& cmdList, DrawAdaptivePixelMapOpItem::ConstructorHandle* handle)
1938     : DrawWithPaintOpItem(cmdList, handle->paintHandle, ADAPTIVE_PIXELMAP_OPITEM),
1939       imageInfo_(handle->imageInfo), sampling_(handle->sampling)
1940 {
1941     pixelMap_ = CmdListHelper::GetPixelMapFromCmdList(cmdList, handle->pixelMap);
1942 }
1943 
Unmarshalling(const DrawCmdList & cmdList,void * handle)1944 std::shared_ptr<DrawOpItem> DrawAdaptivePixelMapOpItem::Unmarshalling(const DrawCmdList& cmdList, void* handle)
1945 {
1946     return std::make_shared<DrawAdaptivePixelMapOpItem>(
1947         cmdList, static_cast<DrawAdaptivePixelMapOpItem::ConstructorHandle*>(handle));
1948 }
1949 
Marshalling(DrawCmdList & cmdList)1950 void DrawAdaptivePixelMapOpItem::Marshalling(DrawCmdList& cmdList)
1951 {
1952     PaintHandle paintHandle;
1953     GenerateHandleFromPaint(cmdList, paint_, paintHandle);
1954     auto pixelmapHandle = CmdListHelper::AddPixelMapToCmdList(cmdList, pixelMap_);
1955     cmdList.AddOp<ConstructorHandle>(pixelmapHandle, imageInfo_, sampling_, paintHandle);
1956 }
1957 
Playback(Canvas * canvas,const Rect * rect)1958 void DrawAdaptivePixelMapOpItem::Playback(Canvas* canvas, const Rect* rect)
1959 {
1960     if (pixelMap_ == nullptr) {
1961         LOGD("DrawAdaptivePixelMapOpItem pixelMap is null!");
1962         return;
1963     }
1964     canvas->AttachPaint(paint_);
1965     AdaptiveImageHelper::DrawPixelMap(*canvas, *rect, pixelMap_, imageInfo_, sampling_);
1966 }
1967 } // namespace Drawing
1968 } // namespace Rosen
1969 } // namespace OHOS
1970