• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "core_canvas.h"
17 
18 #include "config/DrawingConfig.h"
19 #include "impl_factory.h"
20 #include "utils/log.h"
21 
22 #ifdef USE_M133_SKIA
23 #include "src/base/SkUTF.h"
24 #else
25 #include "src/utils/SkUTF.h"
26 #endif
27 namespace OHOS {
28 namespace Rosen {
29 #define DRAW_API_WITH_PAINT(func, ...)                                                     \
30     do {                                                                                   \
31         bool brushValid = paintBrush_.IsValid();                                           \
32         bool penValid = paintPen_.IsValid();                                               \
33         if (!brushValid && !penValid) {                                                    \
34             impl_->func(__VA_ARGS__, defaultPaint_);                                       \
35             return;                                                                        \
36         }                                                                                  \
37         if (brushValid && penValid && Paint::CanCombinePaint(paintBrush_, paintPen_)) {    \
38             paintPen_.SetStyle(Paint::PaintStyle::PAINT_FILL_STROKE);                      \
39             impl_->func(__VA_ARGS__, paintPen_);                                           \
40             paintPen_.SetStyle(Paint::PaintStyle::PAINT_STROKE);                           \
41             return;                                                                        \
42         }                                                                                  \
43         if (brushValid) {                                                                  \
44             impl_->func(__VA_ARGS__, paintBrush_);                                         \
45         }                                                                                  \
46         if (penValid) {                                                                    \
47             impl_->func(__VA_ARGS__, paintPen_);                                           \
48         }                                                                                  \
49     } while (0)
50 
51 
52 #define DRAW_API_WITH_PAINT_LOOPER(func, ...)                                              \
53 do {                                                                                       \
54     bool brushValid = paintBrush_.IsValid();                                               \
55     bool penValid = paintPen_.IsValid();                                                   \
56     if (!brushValid && !penValid) {                                                        \
57         impl_->func(__VA_ARGS__, defaultPaint_);                                           \
58         return;                                                                            \
59     }                                                                                      \
60     if (brushValid && penValid && Paint::CanCombinePaint(paintBrush_, paintPen_)) {        \
61         paintPen_.SetStyle(Paint::PaintStyle::PAINT_FILL_STROKE);                          \
62         std::shared_ptr looper = paintPen_.GetLooper();                                    \
63         if (looper != nullptr) {                                                           \
64             Paint looperPaint;                                                             \
65             GetLooperPaint(paintPen_, looperPaint);                                        \
66             impl_->Save();                                                                 \
67             impl_->Translate(looper->GetXOffset(), looper->GetYOffset());                  \
68             impl_->func(__VA_ARGS__, looperPaint);                                         \
69             impl_->Restore();                                                              \
70         }                                                                                  \
71         impl_->func(__VA_ARGS__, paintPen_);                                               \
72         paintPen_.SetStyle(Paint::PaintStyle::PAINT_STROKE);                               \
73         return;                                                                            \
74     }                                                                                      \
75     if (brushValid) {                                                                      \
76         std::shared_ptr looper = paintBrush_.GetLooper();                                  \
77         if (looper != nullptr) {                                                           \
78             Paint looperPaint;                                                             \
79             GetLooperPaint(paintBrush_, looperPaint);                                      \
80             impl_->Save();                                                                 \
81             impl_->Translate(looper->GetXOffset(), looper->GetYOffset());                  \
82             impl_->func(__VA_ARGS__, looperPaint);                                         \
83             impl_->Restore();                                                              \
84         }                                                                                  \
85         impl_->func(__VA_ARGS__, paintBrush_);                                             \
86     }                                                                                      \
87     if (penValid) {                                                                        \
88         std::shared_ptr looper = paintPen_.GetLooper();                                    \
89         if (looper != nullptr) {                                                           \
90             Paint looperPaint;                                                             \
91             GetLooperPaint(paintPen_, looperPaint);                                        \
92             impl_->Save();                                                                 \
93             impl_->Translate(looper->GetXOffset(), looper->GetYOffset());                  \
94             impl_->func(__VA_ARGS__, looperPaint);                                         \
95             impl_->Restore();                                                              \
96         }                                                                                  \
97         impl_->func(__VA_ARGS__, paintPen_);                                               \
98     }                                                                                      \
99 } while (0)
100 
101 namespace Drawing {
GetLooperPaint(const Paint & paint,Paint & looperPaint)102 static void GetLooperPaint(const Paint& paint, Paint& looperPaint)
103 {
104     looperPaint = paint;
105     std::shared_ptr looper = paint.GetLooper();
106     looperPaint.SetColor(looper->GetColor());
107     Filter filter = looperPaint.GetFilter();
108     filter.SetMaskFilter(looper->GetMaskFilter());
109     looperPaint.SetFilter(filter);
110 }
111 
CoreCanvas()112 CoreCanvas::CoreCanvas() : impl_(ImplFactory::CreateCoreCanvasImpl())
113 {
114     defaultPaint_.SetAntiAlias(true);
115     defaultPaint_.SetStyle(Drawing::Paint::PaintStyle::PAINT_FILL);
116 }
117 
CoreCanvas(DrawingType type)118 CoreCanvas::CoreCanvas(DrawingType type) : impl_(ImplFactory::CreateCoreCanvasImpl(type))
119 {
120     defaultPaint_.SetAntiAlias(true);
121     defaultPaint_.SetStyle(Drawing::Paint::PaintStyle::PAINT_FILL);
122 }
123 
CoreCanvas(int32_t width,int32_t height)124 CoreCanvas::CoreCanvas(int32_t width, int32_t height) : impl_(ImplFactory::CreateCoreCanvasImpl(width, height))
125 {
126     defaultPaint_.SetAntiAlias(true);
127     defaultPaint_.SetStyle(Drawing::Paint::PaintStyle::PAINT_FILL);
128 }
129 
Bind(const Bitmap & bitmap)130 void CoreCanvas::Bind(const Bitmap& bitmap)
131 {
132     impl_->Bind(bitmap);
133 }
134 
GetTotalMatrix() const135 Matrix CoreCanvas::GetTotalMatrix() const
136 {
137     return impl_->GetTotalMatrix();
138 }
139 
GetLocalClipBounds() const140 Rect CoreCanvas::GetLocalClipBounds() const
141 {
142     return impl_->GetLocalClipBounds();
143 }
144 
GetDeviceClipBounds() const145 RectI CoreCanvas::GetDeviceClipBounds() const
146 {
147     return impl_->GetDeviceClipBounds();
148 }
149 
RecordState(Canvas * canvas)150 void CoreCanvas::RecordState(Canvas* canvas)
151 {
152     impl_->RecordState(canvas);
153 }
154 
GetRoundInDeviceClipBounds() const155 RectI CoreCanvas::GetRoundInDeviceClipBounds() const
156 {
157     return impl_->GetRoundInDeviceClipBounds();
158 }
159 
160 #ifdef RS_ENABLE_GPU
GetGPUContext()161 std::shared_ptr<GPUContext> CoreCanvas::GetGPUContext()
162 {
163     if (!gpuContext_) {
164         gpuContext_ = impl_->GetGPUContext();
165     }
166     return gpuContext_;
167 }
168 #endif
169 
GetWidth() const170 int32_t CoreCanvas::GetWidth() const
171 {
172     return impl_->GetWidth();
173 }
174 
GetHeight() const175 int32_t CoreCanvas::GetHeight() const
176 {
177     return impl_->GetHeight();
178 }
179 
GetImageInfo()180 ImageInfo CoreCanvas::GetImageInfo()
181 {
182     return impl_->GetImageInfo();
183 }
184 
ReadPixels(const ImageInfo & dstInfo,void * dstPixels,size_t dstRowBytes,int srcX,int srcY)185 bool CoreCanvas::ReadPixels(const ImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
186     int srcX, int srcY)
187 {
188     return impl_->ReadPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY);
189 }
190 
ReadPixels(const Bitmap & dstBitmap,int srcX,int srcY)191 bool CoreCanvas::ReadPixels(const Bitmap& dstBitmap, int srcX, int srcY)
192 {
193     return impl_->ReadPixels(dstBitmap, srcX, srcY);
194 }
195 
DrawPoint(const Point & point)196 void CoreCanvas::DrawPoint(const Point& point)
197 {
198 #ifdef DRAWING_DISABLE_API
199     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_POINT)) {
200         return;
201     }
202 #endif
203     DRAW_API_WITH_PAINT(DrawPoint, point);
204 }
205 
DrawSdf(const SDFShapeBase & shape)206 void CoreCanvas::DrawSdf(const SDFShapeBase& shape)
207 {
208 #ifdef DRAWING_DISABLE_API
209     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_SDF)) {
210         return;
211     }
212 #endif
213     impl_->DrawSdf(shape);
214 }
215 
DrawPoints(PointMode mode,size_t count,const Point pts[])216 void CoreCanvas::DrawPoints(PointMode mode, size_t count, const Point pts[])
217 {
218 #ifdef DRAWING_DISABLE_API
219     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_POINT)) {
220         return;
221     }
222 #endif
223     DRAW_API_WITH_PAINT(DrawPoints, mode, count, pts);
224 }
225 
DrawLine(const Point & startPt,const Point & endPt)226 void CoreCanvas::DrawLine(const Point& startPt, const Point& endPt)
227 {
228 #ifdef DRAWING_DISABLE_API
229     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_LINE)) {
230         return;
231     }
232 #endif
233     DRAW_API_WITH_PAINT(DrawLine, startPt, endPt);
234 }
235 
DrawRect(const Rect & rect)236 void CoreCanvas::DrawRect(const Rect& rect)
237 {
238 #ifdef DRAWING_DISABLE_API
239     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_RECT)) {
240         return;
241     }
242 #endif
243     DRAW_API_WITH_PAINT(DrawRect, rect);
244 }
245 
DrawRoundRect(const RoundRect & roundRect)246 void CoreCanvas::DrawRoundRect(const RoundRect& roundRect)
247 {
248 #ifdef DRAWING_DISABLE_API
249     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_RRECT)) {
250         return;
251     }
252 #endif
253     DRAW_API_WITH_PAINT(DrawRoundRect, roundRect);
254 }
255 
DrawNestedRoundRect(const RoundRect & outer,const RoundRect & inner)256 void CoreCanvas::DrawNestedRoundRect(const RoundRect& outer, const RoundRect& inner)
257 {
258 #ifdef DRAWING_DISABLE_API
259     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_NESTED_RRECT)) {
260         return;
261     }
262 #endif
263     DRAW_API_WITH_PAINT(DrawNestedRoundRect, outer, inner);
264 }
265 
DrawArc(const Rect & oval,scalar startAngle,scalar sweepAngle)266 void CoreCanvas::DrawArc(const Rect& oval, scalar startAngle, scalar sweepAngle)
267 {
268 #ifdef DRAWING_DISABLE_API
269     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_ARC)) {
270         return;
271     }
272 #endif
273     DRAW_API_WITH_PAINT(DrawArc, oval, startAngle, sweepAngle);
274 }
275 
DrawPie(const Rect & oval,scalar startAngle,scalar sweepAngle)276 void CoreCanvas::DrawPie(const Rect& oval, scalar startAngle, scalar sweepAngle)
277 {
278 #ifdef DRAWING_DISABLE_API
279     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_PIE)) {
280         return;
281     }
282 #endif
283     DRAW_API_WITH_PAINT(DrawPie, oval, startAngle, sweepAngle);
284 }
285 
DrawOval(const Rect & oval)286 void CoreCanvas::DrawOval(const Rect& oval)
287 {
288 #ifdef DRAWING_DISABLE_API
289     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_OVAL)) {
290         return;
291     }
292 #endif
293     DRAW_API_WITH_PAINT(DrawOval, oval);
294 }
295 
DrawCircle(const Point & centerPt,scalar radius)296 void CoreCanvas::DrawCircle(const Point& centerPt, scalar radius)
297 {
298 #ifdef DRAWING_DISABLE_API
299     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_CIRCLE)) {
300         return;
301     }
302 #endif
303     DRAW_API_WITH_PAINT(DrawCircle, centerPt, radius);
304 }
305 
DrawPath(const Path & path)306 void CoreCanvas::DrawPath(const Path& path)
307 {
308 #ifdef DRAWING_DISABLE_API
309     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_PATH)) {
310         return;
311     }
312 #endif
313     DRAW_API_WITH_PAINT(DrawPath, path);
314 }
315 
DrawPathWithStencil(const Path & path,uint32_t stencilVal)316 void CoreCanvas::DrawPathWithStencil(const Path& path, uint32_t stencilVal)
317 {
318 #ifdef DRAWING_DISABLE_API
319     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_PATH)) {
320         return;
321     }
322 #endif
323     DRAW_API_WITH_PAINT(DrawPathWithStencil, path, stencilVal);
324 }
325 
DrawBackground(const Brush & brush)326 void CoreCanvas::DrawBackground(const Brush& brush)
327 {
328 #ifdef DRAWING_DISABLE_API
329     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_BACKGROUND)) {
330         return;
331     }
332 #endif
333     impl_->DrawBackground(brush);
334 }
335 
DrawShadow(const Path & path,const Point3 & planeParams,const Point3 & devLightPos,scalar lightRadius,Color ambientColor,Color spotColor,ShadowFlags flag)336 void CoreCanvas::DrawShadow(const Path& path, const Point3& planeParams, const Point3& devLightPos, scalar lightRadius,
337     Color ambientColor, Color spotColor, ShadowFlags flag)
338 {
339 #ifdef DRAWING_DISABLE_API
340     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_SHADOW)) {
341         return;
342     }
343 #endif
344     impl_->DrawShadow(path, planeParams, devLightPos, lightRadius, ambientColor, spotColor, flag);
345 }
346 
DrawShadowStyle(const Path & path,const Point3 & planeParams,const Point3 & devLightPos,scalar lightRadius,Color ambientColor,Color spotColor,ShadowFlags flag,bool isLimitElevation)347 void CoreCanvas::DrawShadowStyle(const Path& path, const Point3& planeParams, const Point3& devLightPos,
348     scalar lightRadius, Color ambientColor, Color spotColor, ShadowFlags flag, bool isLimitElevation)
349 {
350 #ifdef DRAWING_DISABLE_API
351     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_SHADOW_STYLE)) {
352         return;
353     }
354 #endif
355     impl_->DrawShadowStyle(
356         path, planeParams, devLightPos, lightRadius, ambientColor, spotColor, flag, isLimitElevation);
357 }
358 
DrawColor(ColorQuad color,BlendMode mode)359 void CoreCanvas::DrawColor(ColorQuad color, BlendMode mode)
360 {
361 #ifdef DRAWING_DISABLE_API
362     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_COLOR)) {
363         return;
364     }
365 #endif
366     impl_->DrawColor(color, mode);
367 }
368 
DrawRegion(const Region & region)369 void CoreCanvas::DrawRegion(const Region& region)
370 {
371 #ifdef DRAWING_DISABLE_API
372     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_REGION)) {
373         return;
374     }
375 #endif
376     DRAW_API_WITH_PAINT(DrawRegion, region);
377 }
378 
DrawPatch(const Point cubics[12],const ColorQuad colors[4],const Point texCoords[4],BlendMode mode)379 void CoreCanvas::DrawPatch(const Point cubics[12], const ColorQuad colors[4], const Point texCoords[4], BlendMode mode)
380 {
381 #ifdef DRAWING_DISABLE_API
382     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_PATCH)) {
383         return;
384     }
385 #endif
386     DRAW_API_WITH_PAINT(DrawPatch, cubics, colors, texCoords, mode);
387 }
388 
DrawVertices(const Vertices & vertices,BlendMode mode)389 void CoreCanvas::DrawVertices(const Vertices& vertices, BlendMode mode)
390 {
391 #ifdef DRAWING_DISABLE_API
392     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_VERTICES)) {
393         return;
394     }
395 #endif
396     DRAW_API_WITH_PAINT(DrawVertices, vertices, mode);
397 }
398 
OpCalculateBefore(const Matrix & matrix)399 bool CoreCanvas::OpCalculateBefore(const Matrix& matrix)
400 {
401     return impl_->OpCalculateBefore(matrix);
402 }
403 
OpCalculateAfter(const Rect & bound)404 std::shared_ptr<Drawing::OpListHandle> CoreCanvas::OpCalculateAfter(const Rect& bound)
405 {
406     return impl_->OpCalculateAfter(bound);
407 }
408 
DrawAtlas(const Image * atlas,const RSXform xform[],const Rect tex[],const ColorQuad colors[],int count,BlendMode mode,const SamplingOptions & sampling,const Rect * cullRect)409 void CoreCanvas::DrawAtlas(const Image* atlas, const RSXform xform[], const Rect tex[], const ColorQuad colors[],
410     int count, BlendMode mode, const SamplingOptions& sampling, const Rect* cullRect)
411 {
412 #ifdef DRAWING_DISABLE_API
413     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_ATLAS)) {
414         return;
415     }
416 #endif
417     DRAW_API_WITH_PAINT(DrawAtlas, atlas, xform, tex, colors, count, mode, sampling, cullRect);
418 }
419 
DrawBitmap(const Bitmap & bitmap,const scalar px,const scalar py)420 void CoreCanvas::DrawBitmap(const Bitmap& bitmap, const scalar px, const scalar py)
421 {
422 #ifdef DRAWING_DISABLE_API
423     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_BITMAP)) {
424         return;
425     }
426 #endif
427     DRAW_API_WITH_PAINT(DrawBitmap, bitmap, px, py);
428 }
429 
DrawImageNine(const Image * image,const RectI & center,const Rect & dst,FilterMode filter,const Brush * brush)430 void CoreCanvas::DrawImageNine(const Image* image, const RectI& center, const Rect& dst,
431     FilterMode filter, const Brush* brush)
432 {
433 #ifdef DRAWING_DISABLE_API
434     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_IMAGE_NINE)) {
435         return;
436     }
437 #endif
438     impl_->DrawImageNine(image, center, dst, filter, brush);
439 }
440 
DrawImageLattice(const Image * image,const Lattice & lattice,const Rect & dst,FilterMode filter)441 void CoreCanvas::DrawImageLattice(const Image* image, const Lattice& lattice, const Rect& dst,
442     FilterMode filter)
443 {
444 #ifdef DRAWING_DISABLE_API
445     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_IMAGE_LATTICE)) {
446         return;
447     }
448 #endif
449     DRAW_API_WITH_PAINT(DrawImageLattice, image, lattice, dst, filter);
450 }
451 
DrawImage(const Image & image,const scalar px,const scalar py,const SamplingOptions & sampling)452 void CoreCanvas::DrawImage(const Image& image, const scalar px, const scalar py, const SamplingOptions& sampling)
453 {
454 #ifdef DRAWING_DISABLE_API
455     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_IMAGE)) {
456         return;
457     }
458 #endif
459     DRAW_API_WITH_PAINT(DrawImage, image, px, py, sampling);
460 }
461 
DrawImageWithStencil(const Image & image,const scalar px,const scalar py,const SamplingOptions & sampling,uint32_t stencilVal)462 void CoreCanvas::DrawImageWithStencil(const Image& image, const scalar px, const scalar py,
463     const SamplingOptions& sampling, uint32_t stencilVal)
464 {
465 #ifdef DRAWING_DISABLE_API
466     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_IMAGE)) {
467         return;
468     }
469 #endif
470     DRAW_API_WITH_PAINT(DrawImageWithStencil, image, px, py, sampling, stencilVal);
471 }
472 
DrawImageRect(const Image & image,const Rect & src,const Rect & dst,const SamplingOptions & sampling,SrcRectConstraint constraint)473 void CoreCanvas::DrawImageRect(
474     const Image& image, const Rect& src, const Rect& dst, const SamplingOptions& sampling, SrcRectConstraint constraint)
475 {
476 #ifdef DRAWING_DISABLE_API
477     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_IMAGE_RECT)) {
478         return;
479     }
480 #endif
481     DRAW_API_WITH_PAINT(DrawImageRect, image, src, dst, sampling, constraint);
482 }
483 
DrawImageRect(const Image & image,const Rect & dst,const SamplingOptions & sampling)484 void CoreCanvas::DrawImageRect(const Image& image, const Rect& dst, const SamplingOptions& sampling)
485 {
486 #ifdef DRAWING_DISABLE_API
487     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_IMAGE_RECT)) {
488         return;
489     }
490 #endif
491     DRAW_API_WITH_PAINT(DrawImageRect, image, dst, sampling);
492 }
493 
DrawPicture(const Picture & picture)494 void CoreCanvas::DrawPicture(const Picture& picture)
495 {
496 #ifdef DRAWING_DISABLE_API
497     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_PICTURE)) {
498         return;
499     }
500 #endif
501     impl_->DrawPicture(picture);
502 }
503 
DrawSVGDOM(const sk_sp<SkSVGDOM> & svgDom)504 void CoreCanvas::DrawSVGDOM(const sk_sp<SkSVGDOM>& svgDom)
505 {
506 #ifdef DRAWING_DISABLE_API
507     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_SVGDOM)) {
508         return;
509     }
510 #endif
511     impl_->DrawSVGDOM(svgDom);
512 }
513 
DrawTextBlob(const TextBlob * blob,const scalar x,const scalar y)514 void CoreCanvas::DrawTextBlob(const TextBlob* blob, const scalar x, const scalar y)
515 {
516 #ifdef DRAWING_DISABLE_API
517     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_TEXTBLOB)) {
518         return;
519     }
520 #endif
521     DRAW_API_WITH_PAINT_LOOPER(DrawTextBlob, blob, x, y);
522 }
523 
DrawSingleCharacter(int32_t unicode,const Font & font,scalar x,scalar y)524 void CoreCanvas::DrawSingleCharacter(int32_t unicode, const Font& font, scalar x, scalar y)
525 {
526     std::function<void(int, const Font&)> drawSingleCharacterProc = [&](int currentGlyph, const Font& currentFont) {
527         TextBlobBuilder textBlobBuilder;
528         const TextBlobBuilder::RunBuffer& runBuffer = textBlobBuilder.AllocRunPos(currentFont, 1);
529         runBuffer.glyphs[0] = currentGlyph;
530         runBuffer.pos[0] = 0;
531         runBuffer.pos[1] = 0;
532         std::shared_ptr<TextBlob> textBlob = textBlobBuilder.Make();
533         DrawTextBlob(textBlob.get(), x, y);
534     };
535     uint16_t glyph = font.UnicharToGlyph(unicode);
536     if (glyph != 0) {
537         drawSingleCharacterProc(glyph, font);
538     } else {
539         std::shared_ptr<Font> fallbackFont = font.GetFallbackFont(unicode);
540         if (fallbackFont) {
541             uint16_t fallbackGlyph = fallbackFont->UnicharToGlyph(unicode);
542             drawSingleCharacterProc(fallbackGlyph, *fallbackFont);
543         }
544     }
545 }
546 
DrawSingleCharacterWithFeatures(const char * str,const Font & font,scalar x,scalar y,std::shared_ptr<Drawing::DrawingFontFeatures> fontFeatures)547 void CoreCanvas::DrawSingleCharacterWithFeatures(const char* str, const Font& font, scalar x, scalar y,
548     std::shared_ptr<Drawing::DrawingFontFeatures> fontFeatures)
549 {
550     std::function<void(int, const Font&)> drawSingleCharacterProc = [&](int currentGlyph, const Font& currentFont) {
551         TextBlobBuilder textBlobBuilder;
552         const TextBlobBuilder::RunBuffer& runBuffer = textBlobBuilder.AllocRunPos(currentFont, 1);
553         runBuffer.glyphs[0] = currentGlyph;
554         runBuffer.pos[0] = 0;
555         runBuffer.pos[1] = 0;
556         std::shared_ptr<TextBlob> textBlob = textBlobBuilder.Make();
557         DrawTextBlob(textBlob.get(), x, y);
558     };
559 
560     uint16_t glyph = font.UnicharToGlyphWithFeatures(str, fontFeatures);
561     if (glyph != 0) {
562         drawSingleCharacterProc(glyph, font);
563     } else {
564         const char* currentStr = str;
565         int32_t unicode = SkUTF::NextUTF8(&currentStr, str + strlen(str));
566         std::shared_ptr<Font> fallbackFont = font.GetFallbackFont(unicode);
567         if (fallbackFont) {
568             uint16_t fallbackGlyph = fallbackFont->UnicharToGlyphWithFeatures(str, fontFeatures);
569             drawSingleCharacterProc(fallbackGlyph, *fallbackFont);
570         }
571     }
572 }
573 
DrawSymbol(const DrawingHMSymbolData & symbol,Point locate)574 void CoreCanvas::DrawSymbol(const DrawingHMSymbolData& symbol, Point locate)
575 {
576 #ifdef DRAWING_DISABLE_API
577     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_SYMBOL)) {
578         return;
579     }
580 #endif
581     DRAW_API_WITH_PAINT(DrawSymbol, symbol, locate);
582 }
583 
ClearStencil(const RectI & rect,uint32_t stencilVal)584 void CoreCanvas::ClearStencil(const RectI& rect, uint32_t stencilVal)
585 {
586     impl_->ClearStencil(rect, stencilVal);
587 }
588 
ClipRect(const Rect & rect,ClipOp op,bool doAntiAlias)589 void CoreCanvas::ClipRect(const Rect& rect, ClipOp op, bool doAntiAlias)
590 {
591 #ifdef DRAWING_DISABLE_API
592     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_CLIP_RECT)) {
593         return;
594     }
595 #endif
596     impl_->ClipRect(rect, op, doAntiAlias);
597 }
598 
ClipIRect(const RectI & rect,ClipOp op)599 void CoreCanvas::ClipIRect(const RectI& rect, ClipOp op)
600 {
601 #ifdef DRAWING_DISABLE_API
602     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_CLIP_IRECT)) {
603         return;
604     }
605 #endif
606     impl_->ClipIRect(rect, op);
607 }
608 
ClipRoundRect(const RoundRect & roundRect,ClipOp op,bool doAntiAlias)609 void CoreCanvas::ClipRoundRect(const RoundRect& roundRect, ClipOp op, bool doAntiAlias)
610 {
611 #ifdef DRAWING_DISABLE_API
612     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_CLIP_RRECT)) {
613         return;
614     }
615 #endif
616     impl_->ClipRoundRect(roundRect, op, doAntiAlias);
617 }
618 
ClipRoundRect(const Rect & rect,std::vector<Point> & pts,bool doAntiAlias)619 void CoreCanvas::ClipRoundRect(const Rect& rect, std::vector<Point>& pts, bool doAntiAlias)
620 {
621 #ifdef DRAWING_DISABLE_API
622     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_CLIP_RRECT)) {
623         return;
624     }
625 #endif
626     impl_->ClipRoundRect(rect, pts, doAntiAlias);
627 }
628 
ClipPath(const Path & path,ClipOp op,bool doAntiAlias)629 void CoreCanvas::ClipPath(const Path& path, ClipOp op, bool doAntiAlias)
630 {
631 #ifdef DRAWING_DISABLE_API
632     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_CLIP_PATH)) {
633         return;
634     }
635 #endif
636     impl_->ClipPath(path, op, doAntiAlias);
637 }
638 
ClipRegion(const Region & region,ClipOp op)639 void CoreCanvas::ClipRegion(const Region& region, ClipOp op)
640 {
641 #ifdef DRAWING_DISABLE_API
642     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_CLIP_REGION)) {
643         return;
644     }
645 #endif
646     impl_->ClipRegion(region, op);
647 }
648 
IsClipEmpty()649 bool CoreCanvas::IsClipEmpty()
650 {
651     return impl_->IsClipEmpty();
652 }
653 
IsClipRect()654 bool CoreCanvas::IsClipRect()
655 {
656     return impl_->IsClipRect();
657 }
658 
ResetClip()659 void CoreCanvas::ResetClip()
660 {
661     return impl_->ResetClip();
662 }
663 
QuickReject(const Path & path)664 bool CoreCanvas::QuickReject(const Path& path)
665 {
666     return impl_->QuickReject(path);
667 }
668 
QuickReject(const Rect & rect)669 bool CoreCanvas::QuickReject(const Rect& rect)
670 {
671     return impl_->QuickReject(rect);
672 }
673 
SetMatrix(const Matrix & matrix)674 void CoreCanvas::SetMatrix(const Matrix& matrix)
675 {
676     impl_->SetMatrix(matrix);
677 }
678 
ResetMatrix()679 void CoreCanvas::ResetMatrix()
680 {
681     impl_->ResetMatrix();
682 }
683 
ConcatMatrix(const Matrix & matrix)684 void CoreCanvas::ConcatMatrix(const Matrix& matrix)
685 {
686     impl_->ConcatMatrix(matrix);
687 }
688 
Translate(scalar dx,scalar dy)689 void CoreCanvas::Translate(scalar dx, scalar dy)
690 {
691     impl_->Translate(dx, dy);
692 }
693 
Scale(scalar sx,scalar sy)694 void CoreCanvas::Scale(scalar sx, scalar sy)
695 {
696     impl_->Scale(sx, sy);
697 }
698 
Rotate(scalar deg,scalar sx,scalar sy)699 void CoreCanvas::Rotate(scalar deg, scalar sx, scalar sy)
700 {
701     impl_->Rotate(deg, sx, sy);
702 }
703 
Shear(scalar sx,scalar sy)704 void CoreCanvas::Shear(scalar sx, scalar sy)
705 {
706     impl_->Shear(sx, sy);
707 }
708 
Flush()709 void CoreCanvas::Flush()
710 {
711     impl_->Flush();
712 }
713 
Clear(ColorQuad color)714 void CoreCanvas::Clear(ColorQuad color)
715 {
716     impl_->Clear(color);
717 }
718 
Save()719 uint32_t CoreCanvas::Save()
720 {
721     return impl_->Save();
722 }
723 
SaveLayer(const SaveLayerOps & saveLayerOps)724 void CoreCanvas::SaveLayer(const SaveLayerOps& saveLayerOps)
725 {
726     impl_->SaveLayer(saveLayerOps);
727 }
728 
Restore()729 void CoreCanvas::Restore()
730 {
731     impl_->Restore();
732 }
733 
GetSaveCount() const734 uint32_t CoreCanvas::GetSaveCount() const
735 {
736     return impl_->GetSaveCount();
737 }
738 
Discard()739 void CoreCanvas::Discard()
740 {
741     return impl_->Discard();
742 }
743 
AttachPen(const Pen & pen)744 CoreCanvas& CoreCanvas::AttachPen(const Pen& pen)
745 {
746     paintPen_.AttachPen(pen);
747     return *this;
748 }
749 
AttachBrush(const Brush & brush)750 CoreCanvas& CoreCanvas::AttachBrush(const Brush& brush)
751 {
752     paintBrush_.AttachBrush(brush);
753     return *this;
754 }
755 
AttachPaint(const Paint & paint)756 CoreCanvas& CoreCanvas::AttachPaint(const Paint& paint)
757 {
758     paintBrush_.Disable();
759     paintPen_ = paint;
760 #ifdef RS_ENABLE_GPU
761     auto shaderEffect = paintPen_.GetShaderEffect();
762     if (shaderEffect != nullptr) {
763         shaderEffect->SetGPUContext(GetGPUContext());
764     }
765 #endif
766     return *this;
767 }
768 
DetachPen()769 CoreCanvas& CoreCanvas::DetachPen()
770 {
771     paintPen_.Disable();
772     return *this;
773 }
774 
DetachBrush()775 CoreCanvas& CoreCanvas::DetachBrush()
776 {
777     paintBrush_.Disable();
778     return *this;
779 }
780 
DetachPaint()781 CoreCanvas& CoreCanvas::DetachPaint()
782 {
783     paintPen_.Disable();
784     return *this;
785 }
786 
GetCanvasData() const787 std::shared_ptr<CoreCanvasImpl> CoreCanvas::GetCanvasData() const
788 {
789     return impl_;
790 }
791 
GetEnvForegroundColor() const792 ColorQuad CoreCanvas::GetEnvForegroundColor() const
793 {
794     return Color::COLOR_BLACK;
795 }
796 
isHighContrastEnabled() const797 bool CoreCanvas::isHighContrastEnabled() const
798 {
799     return false;
800 }
801 
GetCacheType() const802 Drawing::CacheType CoreCanvas::GetCacheType() const
803 {
804     return Drawing::CacheType::UNDEFINED;
805 }
806 
GetSurface() const807 Drawing::Surface* CoreCanvas::GetSurface() const
808 {
809     return nullptr;
810 }
811 
GetAlpha() const812 float CoreCanvas::GetAlpha() const
813 {
814     return 1.0f;
815 }
816 
GetAlphaSaveCount() const817 int CoreCanvas::GetAlphaSaveCount() const
818 {
819     return 0;
820 }
821 
BuildOverDraw(std::shared_ptr<Canvas> canvas)822 void CoreCanvas::BuildOverDraw(std::shared_ptr<Canvas> canvas)
823 {
824     if (impl_ && canvas) {
825         impl_->BuildOverDraw(canvas);
826     }
827 }
828 
BuildNoDraw(int32_t width,int32_t height)829 void CoreCanvas::BuildNoDraw(int32_t width, int32_t height)
830 {
831     impl_->BuildNoDraw(width, height);
832 }
833 
BuildStateRecord(int32_t width,int32_t height)834 void CoreCanvas::BuildStateRecord(int32_t width, int32_t height)
835 {
836     impl_->BuildStateRecord(width, height);
837 }
838 
BuildStateInherite(int32_t width,int32_t height)839 void CoreCanvas::BuildStateInherite(int32_t width, int32_t height)
840 {
841     impl_->BuildStateInherite(width, height);
842 }
843 
Reset(int32_t width,int32_t height)844 void CoreCanvas::Reset(int32_t width, int32_t height)
845 {
846     impl_->Reset(width, height);
847 }
848 
InheriteState(Canvas * canvas)849 void CoreCanvas::InheriteState(Canvas* canvas)
850 {
851     impl_->InheriteState(canvas);
852 }
853 
DrawBlurImage(const Image & image,const HpsBlurParameter & blurParams)854 bool CoreCanvas::DrawBlurImage(const Image& image, const HpsBlurParameter& blurParams)
855 {
856 #ifdef DRAWING_DISABLE_API
857     if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_BLUR_IMAGE)) {
858         SamplingOptions sampling;
859         Paint paint;
860         impl_->DrawImageRect(image, blurParams.src, blurParams.dst, sampling,
861             SrcRectConstraint::FAST_SRC_RECT_CONSTRAINT, paint);
862         return true;
863     }
864 #endif
865     return impl_->DrawBlurImage(image, blurParams);
866 }
867 
DrawImageEffectHPS(const Image & image,const std::vector<std::shared_ptr<Drawing::HpsEffectParameter>> & hpsEffectParams)868 bool CoreCanvas::DrawImageEffectHPS(const Image& image,
869     const std::vector<std::shared_ptr<Drawing::HpsEffectParameter>>& hpsEffectParams)
870 {
871     return impl_->DrawImageEffectHPS(image, hpsEffectParams);
872 }
873 
SetParallelRender(bool parallelEnable)874 void CoreCanvas::SetParallelRender(bool parallelEnable)
875 {
876     impl_->SetParallelRender(parallelEnable);
877 }
878 
CalcHpsBluredImageDimension(const Drawing::HpsBlurParameter & blurParams)879 std::array<int, 2> CoreCanvas::CalcHpsBluredImageDimension(const Drawing::HpsBlurParameter& blurParams)
880 {
881     return impl_->CalcHpsBluredImageDimension(blurParams);
882 }
883 } // namespace Drawing
884 } // namespace Rosen
885 } // namespace OHOS
886