• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_PAINT_FILTER_CANVAS_H
17 #define RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_PAINT_FILTER_CANVAS_H
18 
19 #include <limits>
20 #include <optional>
21 #include <stack>
22 #include <vector>
23 
24 #include "common/rs_color.h"
25 #include "common/rs_macros.h"
26 #include "common/rs_occlusion_region.h"
27 #include "common/rs_rect.h"
28 #include "draw/canvas.h"
29 #include "draw/surface.h"
30 #include "screen_manager/screen_types.h"
31 #include "surface_type.h"
32 #include "utils/region.h"
33 
34 namespace OHOS {
35 namespace Rosen {
36 class RSFilter;
37 
38 class RSB_EXPORT RSPaintFilterCanvasBase : public Drawing::Canvas {
39 public:
40     RSPaintFilterCanvasBase(Drawing::Canvas* canvas);
41     ~RSPaintFilterCanvasBase() override = default;
42 
43     void SetParallelRender(bool parallelEnable) override;
44     Drawing::Matrix GetTotalMatrix() const override;
45 
46     Drawing::Rect GetLocalClipBounds() const override;
47 
48     Drawing::RectI GetDeviceClipBounds() const override;
49 
50     Drawing::RectI GetRoundInDeviceClipBounds() const override;
51 
52     uint32_t GetSaveCount() const override;
53 
54 #ifdef RS_ENABLE_GPU
55     std::shared_ptr<Drawing::GPUContext> GetGPUContext() override;
56 #endif
57 
58     void DrawSdf(const Drawing::SDFShapeBase& shape) override;
59     void DrawPoint(const Drawing::Point& point) override;
60     void DrawPoints(Drawing::PointMode mode, size_t count, const Drawing::Point pts[]) override;
61     void DrawLine(const Drawing::Point& startPt, const Drawing::Point& endPt) override;
62     void DrawRect(const Drawing::Rect& rect) override;
63     void DrawRoundRect(const Drawing::RoundRect& roundRect) override;
64     void DrawNestedRoundRect(const Drawing::RoundRect& outer, const Drawing::RoundRect& inner) override;
65     void DrawArc(const Drawing::Rect& oval, Drawing::scalar startAngle, Drawing::scalar sweepAngle) override;
66     void DrawPie(const Drawing::Rect& oval, Drawing::scalar startAngle, Drawing::scalar sweepAngle) override;
67     void DrawOval(const Drawing::Rect& oval) override;
68     void DrawCircle(const Drawing::Point& centerPt, Drawing::scalar radius) override;
69     void DrawPath(const Drawing::Path& path) override;
70     void DrawPathWithStencil(const Drawing::Path& path, uint32_t stencilVal) override;
71     void DrawBackground(const Drawing::Brush& brush) override;
72     void DrawShadow(const Drawing::Path& path, const Drawing::Point3& planeParams,
73         const Drawing::Point3& devLightPos, Drawing::scalar lightRadius,
74         Drawing::Color ambientColor, Drawing::Color spotColor, Drawing::ShadowFlags flag) override;
75     void DrawShadowStyle(const Drawing::Path& path, const Drawing::Point3& planeParams,
76         const Drawing::Point3& devLightPos, Drawing::scalar lightRadius,
77         Drawing::Color ambientColor, Drawing::Color spotColor, Drawing::ShadowFlags flag,
78         bool isLimitElevation) override;
79     void DrawColor(Drawing::ColorQuad color, Drawing::BlendMode mode = Drawing::BlendMode::SRC_OVER) override;
80     void DrawRegion(const Drawing::Region& region) override;
81     void DrawPatch(const Drawing::Point cubics[12], const Drawing::ColorQuad colors[4],
82         const Drawing::Point texCoords[4], Drawing::BlendMode mode) override;
83     void DrawVertices(const Drawing::Vertices& vertices, Drawing::BlendMode mode) override;
84 
85     void DrawImageNine(const Drawing::Image* image, const Drawing::RectI& center, const Drawing::Rect& dst,
86         Drawing::FilterMode filter, const Drawing::Brush* brush = nullptr) override;
87     void DrawImageLattice(const Drawing::Image* image, const Drawing::Lattice& lattice, const Drawing::Rect& dst,
88         Drawing::FilterMode filter) override;
89 
90     bool OpCalculateBefore(const Drawing::Matrix& matrix) override;
91     std::shared_ptr<Drawing::OpListHandle> OpCalculateAfter(const Drawing::Rect& bound) override;
92 
93     void DrawAtlas(const Drawing::Image* atlas, const Drawing::RSXform xform[], const Drawing::Rect tex[],
94         const Drawing::ColorQuad colors[], int count, Drawing::BlendMode mode,
95         const Drawing::SamplingOptions& sampling, const Drawing::Rect* cullRect) override;
96     void DrawBitmap(const Drawing::Bitmap& bitmap, const Drawing::scalar px, const Drawing::scalar py) override;
97     void DrawImage(const Drawing::Image& image,
98         const Drawing::scalar px, const Drawing::scalar py, const Drawing::SamplingOptions& sampling) override;
99     void DrawImageWithStencil(const Drawing::Image& image, const Drawing::scalar px, const Drawing::scalar py,
100         const Drawing::SamplingOptions& sampling, uint32_t stencilVal) override;
101     void DrawImageRect(const Drawing::Image& image, const Drawing::Rect& src, const Drawing::Rect& dst,
102         const Drawing::SamplingOptions& sampling, Drawing::SrcRectConstraint constraint) override;
103     void DrawImageRect(const Drawing::Image& image,
104         const Drawing::Rect& dst, const Drawing::SamplingOptions& sampling) override;
105     void DrawPicture(const Drawing::Picture& picture) override;
106     void DrawTextBlob(const Drawing::TextBlob* blob, const Drawing::scalar x, const Drawing::scalar y) override;
107 
108     void ClearStencil(const Drawing::RectI& rect, uint32_t stencilVal) override;
109     void ClipRect(const Drawing::Rect& rect, Drawing::ClipOp op = Drawing::ClipOp::INTERSECT,
110         bool doAntiAlias = false) override;
111     void ClipIRect(const Drawing::RectI& rect, Drawing::ClipOp op = Drawing::ClipOp::INTERSECT) override;
112     void ClipRoundRect(const Drawing::RoundRect& roundRect, Drawing::ClipOp op = Drawing::ClipOp::INTERSECT,
113         bool doAntiAlias = false) override;
114     void ClipRoundRect(const Drawing::Rect& rect, std::vector<Drawing::Point>& pts, bool doAntiAlias = false) override;
115     void ClipPath(const Drawing::Path& path, Drawing::ClipOp op = Drawing::ClipOp::INTERSECT,
116         bool doAntiAlias = false) override;
117     void ClipRegion(const Drawing::Region& region, Drawing::ClipOp op = Drawing::ClipOp::INTERSECT) override;
118     void ResetClip() override;
119 
120     void SetMatrix(const Drawing::Matrix& matrix) override;
121     void ResetMatrix() override;
122     void ConcatMatrix(const Drawing::Matrix& matrix) override;
123     void Translate(Drawing::scalar dx, Drawing::scalar dy) override;
124     void Scale(Drawing::scalar sx, Drawing::scalar sy) override;
125     void Rotate(Drawing::scalar deg, Drawing::scalar sx, Drawing::scalar sy) override;
126     void Shear(Drawing::scalar sx, Drawing::scalar sy) override;
127 
128     void Flush() override;
129     void Clear(Drawing::ColorQuad color) override;
130     uint32_t Save() override;
131     void SaveLayer(const Drawing::SaveLayerOps& saveLayerOps) override;
132     void Restore() override;
133     void Discard() override;
134 
135     CoreCanvas& AttachPen(const Drawing::Pen& pen) override;
136     CoreCanvas& AttachBrush(const Drawing::Brush& brush) override;
137     CoreCanvas& AttachPaint(const Drawing::Paint& paint) override;
138     CoreCanvas& DetachPen() override;
139     CoreCanvas& DetachBrush() override;
140     CoreCanvas& DetachPaint() override;
141 
142     /**
143      * @brief DDK Draw HPS Effect on image
144      *
145      * DDK provides an interface for high-performance implementation of certain 2D visual effects
146      *
147      * @param image                 the image needed to draw
148      * @param hpsEffectParams       the params of different effects needed to excute on image
149      * @return true if DDK excute HPS Effect success; false when failed.
150      */
151     bool DrawImageEffectHPS(const Drawing::Image& image,
152         const std::vector<std::shared_ptr<Drawing::HpsEffectParameter>>& hpsEffectParams) override;
153     /**
154      * @brief DDK Draw Gaussian Blur on image
155      *
156      * DDK provides an interface for high-performance implementation of Gaussian Blur
157      *
158      * @param image                 the image needed to draw
159      * @param blurParams            the params of Gaussian Blur which will excute on image
160      * @return true if DDK excute Blur success; false when failed.
161      */
162     bool DrawBlurImage(const Drawing::Image& image, const Drawing::HpsBlurParameter& blurParams) override;
163     /**
164      * @brief DDK calculate the size of the off-screen canvas that Blur Effect excute on
165      *
166      * DDK will excute Blur Effect on an off-screen canvas, the size of the off-screen canvas
167      *  will be calculated in DDK.
168      *
169      * @param blurParams            the params of Gaussian Blur which will excute on image
170      * @return size of the off-screen canvas.
171      */
172     std::array<int, 2> CalcHpsBluredImageDimension(const Drawing::HpsBlurParameter& blurParams) override;
173 
174     bool IsClipRect() override;
175 
176 protected:
177     virtual bool OnFilter() const = 0;
178     virtual bool OnFilterWithBrush(Drawing::Brush& brush) const = 0;
179     virtual Drawing::Brush* GetFilteredBrush() const = 0;
180     Drawing::Canvas* canvas_ = nullptr;
181 };
182 
183 // This class is used to filter the paint before drawing. currently, it is used to filter the alpha and foreground
184 // color.
185 class RSB_EXPORT RSPaintFilterCanvas : public RSPaintFilterCanvasBase {
186 public:
187     RSPaintFilterCanvas(Drawing::Canvas* canvas, float alpha = 1.0f);
188     RSPaintFilterCanvas(Drawing::Surface* surface, float alpha = 1.0f);
189     ~RSPaintFilterCanvas() override = default;
190 
191     void CopyConfigurationToOffscreenCanvas(const RSPaintFilterCanvas& other);
192     void PushDirtyRegion(Drawing::Region& resultRegion);
193     void PopDirtyRegion();
194     bool IsDirtyRegionStackEmpty();
195     Drawing::Region& GetCurDirtyRegion();
196 
197     // alpha related
198     void MultiplyAlpha(float alpha);
199     void SetAlpha(float alpha);
200     float GetAlpha() const override;
201     int SaveAlpha();
202     void RestoreAlpha();
203     int GetAlphaSaveCount() const override;
204     void RestoreAlphaToCount(int count);
205 
206     // env related
207     void SetEnvForegroundColor(Color color);
208     Drawing::ColorQuad GetEnvForegroundColor() const override;
209     int SaveEnv();
210     void RestoreEnv();
211     int GetEnvSaveCount() const;
212     void RestoreEnvToCount(int count);
213 
214     // blendmode and blender related
215     void SaveLayer(const Drawing::SaveLayerOps& saveLayerOps) override;
216     void SetBlendMode(std::optional<int> blendMode);
217     void SetBlender(std::shared_ptr<Drawing::Blender>);
218     bool HasOffscreenLayer() const;
219 
220     // save/restore utils
221     struct SaveStatus {
222         int canvasSaveCount = -1;
223         int alphaSaveCount = -1;
224         int envSaveCount = -1;
225     };
226 
227     enum SubTreeStatus : uint8_t {
228         DEFAULT_STATE = 0x00,
229         SUBTREE_PARALLEL_STATE = 0x01,
230         SUBTREE_QUICK_DRAW_STATE = 0x02
231     };
232 
233     enum class ScreenshotType {
234         NON_SHOT = 0,
235         SDR_SCREENSHOT,
236         SDR_WINDOWSHOT,
237         HDR_SCREENSHOT,
238         HDR_WINDOWSHOT,
239     };
240 
241     struct HDRProperties {
242         bool isHDREnabledVirtualScreen = false;
243         float hdrBrightness = 1.0f; // Default 1.0f means max available headroom
244         ScreenshotType screenshotType = ScreenshotType::NON_SHOT;
245     };
246 
247     enum SaveType : uint8_t {
248         kNone           = 0x0,
249         kCanvas         = 0x1,
250         kAlpha          = 0x2,
251         kEnv            = 0x4,
252         kCanvasAndAlpha = kCanvas | kAlpha,
253         kAll            = kCanvas | kAlpha | kEnv,
254     };
255 
256     ScreenshotType GetScreenshotType() const;
257     void SetScreenshotType(ScreenshotType type);
258     SaveStatus SaveAllStatus(SaveType type = kAll);
259     SaveStatus GetSaveStatus() const;
260     void RestoreStatus(const SaveStatus& status);
261 
262     Drawing::Surface* GetSurface() const override;
263 
264     // high contrast
265     void SetHighContrast(bool enabled);
266     bool isHighContrastEnabled() const override;
267 
268     using CacheType = Drawing::CacheType;
269     // cache
270     void SetCacheType(CacheType type);
271     Drawing::CacheType GetCacheType() const override;
272 
273     // visible rect
274     void SetVisibleRect(Drawing::Rect visibleRect);
275     Drawing::Rect GetVisibleRect() const;
276 
277     static std::optional<Drawing::Rect> GetLocalClipBounds(const Drawing::Canvas& canvas,
278         const Drawing::RectI* clipBounds = nullptr);
279 
280     CoreCanvas& AttachPen(const Drawing::Pen& pen) override;
281     CoreCanvas& AttachBrush(const Drawing::Brush& brush) override;
282     CoreCanvas& AttachPaint(const Drawing::Paint& paint) override;
283 
284     void SetParallelThreadIdx(uint32_t idx);
285     uint32_t GetParallelThreadIdx() const;
286     uint32_t GetParallelThreadId();
287     void SetParallelThreadId(uint32_t idx);
288     void SetIsParallelCanvas(bool isParallel);
289     bool GetIsParallelCanvas() const;
290 
291     void RecordState(const RSPaintFilterCanvas& other);
292     std::weak_ptr<Drawing::Surface> GetWeakSurface();
293     // just used in SubTree, used for check whether a new Surface needs to be created in the SubTree thread.
294     void SetWeakSurface(std::shared_ptr<Drawing::Surface> surface);
SetSubTreeParallelState(SubTreeStatus state)295     inline void SetSubTreeParallelState(SubTreeStatus state)
296     {
297         subTreeDrawStatus_ = state;
298     }
IsQuickGetDrawState()299     inline bool IsQuickGetDrawState() const
300     {
301         return subTreeDrawStatus_ == SUBTREE_QUICK_DRAW_STATE;
302     }
IsSubTreeInParallel()303     inline bool IsSubTreeInParallel() const
304     {
305         return subTreeDrawStatus_ != DEFAULT_STATE;
306     }
307 
308     void SetDisableFilterCache(bool disable);
309     bool GetDisableFilterCache() const;
310 
311     void SetRecordDrawable(bool enable);
312     bool GetRecordDrawable() const;
313 
314     // effect cache data relate
315     struct CachedEffectData {
316         CachedEffectData() = default;
317         CachedEffectData(std::shared_ptr<Drawing::Image>&& image, const Drawing::RectI& rect);
318         CachedEffectData(const std::shared_ptr<Drawing::Image>& image, const Drawing::RectI& rect);
319         ~CachedEffectData() = default;
320         std::string GetInfo() const;
321         std::shared_ptr<Drawing::Image> cachedImage_ = nullptr;
322         Drawing::RectI cachedRect_ = {};
323         Drawing::Matrix cachedMatrix_ = Drawing::Matrix();
324     };
325     void SetEffectData(const std::shared_ptr<CachedEffectData>& effectData);
326     const std::shared_ptr<CachedEffectData>& GetEffectData() const;
327     void SetDrawnRegion(const Occlusion::Region& region);
328     const Occlusion::Region& GetDrawnRegion() const;
329     // behind window cache relate
330     void SetBehindWindowData(const std::shared_ptr<CachedEffectData>& behindWindowData);
331     const std::shared_ptr<CachedEffectData>& GetBehindWindowData() const;
332 
333     // for foregroundFilter to store offscreen canvas & surface
334     struct OffscreenData {
335         std::shared_ptr<Drawing::Surface> offscreenSurface_ = nullptr;
336         std::shared_ptr<RSPaintFilterCanvas> offscreenCanvas_ = nullptr;
337     };
338     // for foregroundFilter to store and restore offscreen canvas & surface
339     void ReplaceMainScreenData(std::shared_ptr<Drawing::Surface>& offscreenSurface,
340         std::shared_ptr<RSPaintFilterCanvas>& offscreenCanvas);
341     void SwapBackMainScreenData();
342     void SavePCanvasList();
343     void RestorePCanvasList();
344 
345     // canvas status relate
346     struct CanvasStatus {
347         float alpha_;
348         Drawing::Matrix matrix_;
349         std::shared_ptr<CachedEffectData> effectData_;
350     };
351     CanvasStatus GetCanvasStatus() const;
352     void SetCanvasStatus(const CanvasStatus& status);
353     Drawing::Canvas* GetRecordingCanvas() const override;
354     bool GetRecordingState() const override;
355     void SetRecordingState(bool flag) override;
356 
GetOffscreenDataList()357     const std::stack<OffscreenData>& GetOffscreenDataList() const
358     {
359         return offscreenDataList_;
360     }
361 
StoreCanvas()362     void StoreCanvas()
363     {
364         if (storeMainCanvas_ == nullptr) {
365             storeMainCanvas_ = canvas_;
366         }
367     }
368 
GetOriginalCanvas()369     Drawing::Canvas* GetOriginalCanvas()
370     {
371         return storeMainCanvas_;
372     }
373 
GetDrawingType()374     Drawing::DrawingType GetDrawingType() const override
375     {
376         return Drawing::DrawingType::PAINT_FILTER;
377     }
378     bool IsOnMultipleScreen() const;
379     void SetOnMultipleScreen(bool multipleScreen);
380     ScreenId GetScreenId() const;
381     void SetScreenId(ScreenId screenId);
382     GraphicColorGamut GetTargetColorGamut() const;
383     void SetTargetColorGamut(GraphicColorGamut colorGamut);
384     float GetHDRBrightness() const;
385     void SetHDRBrightness(float hdrBrightness);
386     float GetBrightnessRatio() const;
387     void SetBrightnessRatio(float brightnessRatio);
388     void CopyHDRConfiguration(const RSPaintFilterCanvas& other);
389     bool GetHdrOn() const;
390     void SetHdrOn(bool isHdrOn);
391     bool GetHDREnabledVirtualScreen() const;
392     void SetHDREnabledVirtualScreen(bool isHDREnabledVirtualScreen);
393     const HDRProperties& GetHDRProperties() const;
394     bool GetIsWindowFreezeCapture() const;
395     void SetIsWindowFreezeCapture(bool isWindowFreezeCapture);
396     bool GetIsDrawingCache() const;
397     void SetIsDrawingCache(bool isDrawingCache);
398     void SetEffectIntersectWithDRM(bool intersect);
399     bool GetEffectIntersectWithDRM() const;
400     void SetDarkColorMode(bool isDark);
401     bool GetDarkColorMode() const;
402 
403     struct CacheBehindWindowData {
404         CacheBehindWindowData() = default;
405         CacheBehindWindowData(std::shared_ptr<RSFilter> filter, const Drawing::Rect rect);
406         ~CacheBehindWindowData() = default;
407         std::shared_ptr<RSFilter> filter_ = nullptr;
408         Drawing::Rect rect_ = {};
409     };
410     void SetCacheBehindWindowData(const std::shared_ptr<CacheBehindWindowData>& data);
411     const std::shared_ptr<CacheBehindWindowData>& GetCacheBehindWindowData() const;
412 
413     // Set culled nodes for control-level occlusion culling
SetCulledNodes(std::unordered_set<NodeId> && culledNodes)414     void SetCulledNodes(std::unordered_set<NodeId>&& culledNodes)
415     {
416         culledNodes_ = std::move(culledNodes);
417     }
418 
419     // Get culled nodes for control-level occlusion culling
GetCulledNodes()420     const std::unordered_set<NodeId>& GetCulledNodes() const
421     {
422         return culledNodes_;
423     }
424 
425     // Set culled entire subtree for control-level occlusion culling
SetCulledEntireSubtree(std::unordered_set<NodeId> && culledEntireSubtree)426     void SetCulledEntireSubtree(std::unordered_set<NodeId>&& culledEntireSubtree)
427     {
428         culledEntireSubtree_ = std::move(culledEntireSubtree);
429     }
430 
431     // Get culled entire subtree for control-level occlusion culling
GetCulledEntireSubtree()432     const std::unordered_set<NodeId>& GetCulledEntireSubtree() const
433     {
434         return culledEntireSubtree_;
435     }
436 
437 protected:
438     using Env = struct {
439         Color envForegroundColor_;
440         std::shared_ptr<CachedEffectData> effectData_;
441         std::shared_ptr<CachedEffectData> behindWindowData_;
442         std::shared_ptr<Drawing::Blender> blender_;
443         bool hasOffscreenLayer_;
444     };
445 
446     bool OnFilter() const override;
OnFilterWithBrush(Drawing::Brush & brush)447     inline bool OnFilterWithBrush(Drawing::Brush& brush) const override
448     {
449         if (IsQuickGetDrawState()) {
450             return false;
451         }
452         float alpha = alphaStack_.top();
453         // foreground color and foreground color strategy identification
454         if (brush.GetColor().CastToColorQuad() == 0x00000001) {
455             brush.SetColor(envStack_.top().envForegroundColor_.AsArgbInt());
456         }
457 
458         // use alphaStack_.top() to multiply alpha
459         if (alpha < 1 && alpha > 0) {
460             brush.SetAlpha(brush.GetAlpha() * alpha);
461         }
462         return alpha > 0.f;
463     }
GetFilteredBrush()464     inline Drawing::Brush* GetFilteredBrush() const override
465     {
466         static Drawing::Brush brush;
467         float alpha = alphaStack_.top();
468         if (alpha >= 1) {
469             return nullptr;
470         }
471         brush.SetAlphaF(alpha);
472         return &brush;
473     }
474 
475 private:
476     bool isParallelCanvas_ = false;
477     bool disableFilterCache_ = false;
478     bool recordingState_ = false;
479     bool recordDrawable_ = false;
480     bool multipleScreen_ = false;
481     bool isHdrOn_ = false;
482     bool isWindowFreezeCapture_ = false;
483     // Drawing window cache or uifirst cache
484     bool isDrawingCache_ = false;
485     bool isIntersectWithDRM_ = false;
486     bool isDarkColorMode_ = false;
487     CacheType cacheType_ { RSPaintFilterCanvas::CacheType::UNDEFINED };
488     std::atomic_bool isHighContrastEnabled_ { false };
489     GraphicColorGamut targetColorGamut_ = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB;
490     float brightnessRatio_ = 1.0f; // Default 1.0f means no discount
491     ScreenId screenId_ = INVALID_SCREEN_ID;
492     uint32_t threadIndex_ = UNI_RENDER_THREAD_INDEX; // default
493     HDRProperties hdrProperties_;
494     Drawing::Surface* surface_ = nullptr;
495     Drawing::Canvas* storeMainCanvas_ = nullptr; // store main canvas
496     Drawing::Rect visibleRect_ = Drawing::Rect();
497     std::unordered_set<NodeId> culledNodes_; // store culled nodes for control-level occlusion culling
498     std::unordered_set<NodeId> culledEntireSubtree_; // store culled entire subtree for control-level occlusion culling
499 
500     std::stack<float> alphaStack_;
501     std::stack<Env> envStack_;
502 
503     // save every dirty region of the current surface for quick reject
504     std::stack<Drawing::Region> dirtyRegionStack_;
505 
506     // greater than 0 indicates canvas currently is drawing on a new layer created offscreen blendmode
507     // std::stack<bool> blendOffscreenStack_;
508 
509     // foregroundFilter related
510     std::vector<std::vector<Canvas*>> storedPCanvasList_; // store pCanvasList_
511     std::stack<OffscreenData> offscreenDataList_; // store offscreen canvas & surface
512     std::stack<Drawing::Surface*> storeMainScreenSurface_; // store surface_
513     std::stack<Drawing::Canvas*> storeMainScreenCanvas_; // store canvas_
514 
515     std::shared_ptr<CacheBehindWindowData> cacheBehindWindowData_ = nullptr;
516     Occlusion::Region drawnRegion_;
517     uint32_t threadId_;
518     std::weak_ptr<Drawing::Surface> weakSurface_;
519     uint8_t subTreeDrawStatus_ = DEFAULT_STATE;
520 };
521 
522 #ifdef RS_ENABLE_VK
523 class RSHybridRenderPaintFilterCanvas : public RSPaintFilterCanvas {
524 public:
RSPaintFilterCanvas(canvas,alpha)525     RSHybridRenderPaintFilterCanvas(Drawing::Canvas* canvas, float alpha = 1.0f) : RSPaintFilterCanvas(canvas, alpha)
526     {}
527 
RSPaintFilterCanvas(surface,alpha)528     RSHybridRenderPaintFilterCanvas(Drawing::Surface* surface, float alpha = 1.0f) : RSPaintFilterCanvas(surface, alpha)
529     {}
530 
531     //Override the AttachPaint method
532     CoreCanvas& AttachPaint(const Drawing::Paint& paint) override;
533 
IsRenderWithForegroundColor()534     bool IsRenderWithForegroundColor() const
535     {
536         return isRenderWithForegroundColor;
537     }
538 
SetRenderWithForegroundColor(bool renderFilterStatus)539     void SetRenderWithForegroundColor(bool renderFilterStatus)
540     {
541         isRenderWithForegroundColor = renderFilterStatus;
542     }
543 private:
544     bool isRenderWithForegroundColor = false;
545 };
546 #endif
547 
548 // Helper class similar to SkAutoCanvasRestore, but also restores alpha and/or env
549 class RSB_EXPORT RSAutoCanvasRestore {
550 public:
551     /** Preserves canvas save count. Optionally call SkCanvas::save() and/or RSPaintFilterCanvas::SaveAlpha() and/or
552        RSPaintFilterCanvas::SaveEnv().
553         @param canvas     RSPaintFilterCanvas to guard
554         @param saveCanvas call SkCanvas::save()
555         @param saveAlpha  call RSPaintFilterCanvas::SaveAlpha()
556         @return           utility to restore RSPaintFilterCanvas state on destructor
557     */
558     RSAutoCanvasRestore(
559         RSPaintFilterCanvas* canvas, RSPaintFilterCanvas::SaveType type = RSPaintFilterCanvas::SaveType::kAll)
canvas_(canvas)560         : canvas_(canvas), saveCount_(canvas ? canvas->SaveAllStatus(type) : RSPaintFilterCanvas::SaveStatus())
561     {}
562 
563     /** Allow RSAutoCanvasRestore to be used with std::unique_ptr and std::shared_ptr */
564     RSAutoCanvasRestore(const std::unique_ptr<RSPaintFilterCanvas>& canvas,
565         RSPaintFilterCanvas::SaveType type = RSPaintFilterCanvas::SaveType::kAll)
566         : RSAutoCanvasRestore(canvas.get(), type)
567     {}
568     RSAutoCanvasRestore(const std::shared_ptr<RSPaintFilterCanvas>& canvas,
569         RSPaintFilterCanvas::SaveType type = RSPaintFilterCanvas::SaveType::kAll)
570         : RSAutoCanvasRestore(canvas.get(), type)
571     {}
572 
573     RSAutoCanvasRestore(RSAutoCanvasRestore&&) = delete;
574     RSAutoCanvasRestore(const RSAutoCanvasRestore&) = delete;
575     RSAutoCanvasRestore& operator=(RSAutoCanvasRestore&&) = delete;
576     RSAutoCanvasRestore& operator=(const RSAutoCanvasRestore&) = delete;
577 
578     /** Restores RSPaintFilterCanvas to saved state. Destructor is called when container goes out of
579         scope.
580     */
~RSAutoCanvasRestore()581     ~RSAutoCanvasRestore()
582     {
583         if (canvas_) {
584             canvas_->RestoreStatus(saveCount_);
585         }
586     }
587 
588     /** Restores RSPaintFilterCanvas to saved state immediately. Subsequent calls and
589         ~RSAutoCanvasRestore() have no effect.
590     */
restore()591     void restore()
592     {
593         if (canvas_) {
594             canvas_->RestoreStatus(saveCount_);
595             canvas_ = nullptr;
596         }
597     }
598 
599 private:
600     RSPaintFilterCanvas* canvas_ = nullptr;
601     RSPaintFilterCanvas::SaveStatus saveCount_;
602 };
603 } // namespace Rosen
604 } // namespace OHOS
605 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_PAINT_FILTER_CANVAS_H
606