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 CANVAS_H 17 #define CANVAS_H 18 19 #include <iostream> 20 #include <string> 21 #include <vector> 22 23 #include "drawing/draw/core_canvas.h" 24 25 namespace OHOS { 26 namespace Rosen { 27 namespace Drawing { 28 class RecordCmd; 29 30 class AutoCanvasMatrixBrush { 31 public: 32 AutoCanvasMatrixBrush(Canvas* canvas, 33 const Matrix* matrix, const Brush* brush, const Rect& bounds); 34 ~AutoCanvasMatrixBrush(); 35 36 AutoCanvasMatrixBrush(AutoCanvasMatrixBrush&&) = delete; 37 AutoCanvasMatrixBrush(const AutoCanvasMatrixBrush&) = delete; 38 AutoCanvasMatrixBrush& operator=(AutoCanvasMatrixBrush&&) = delete; 39 AutoCanvasMatrixBrush& operator=(const AutoCanvasMatrixBrush&) = delete; 40 41 private: 42 Canvas* canvas_; 43 uint32_t saveCount_; 44 Paint paintPen_; 45 Paint paintBrush_; 46 }; 47 48 class DRAWING_API Canvas : public CoreCanvas { 49 public: Canvas()50 Canvas() {} Canvas(int32_t width,int32_t height)51 Canvas(int32_t width, int32_t height) : CoreCanvas(width, height) {} 52 53 virtual Canvas* GetRecordingCanvas() const; 54 55 void AddCanvas(Canvas* canvas); 56 57 void RemoveAll(); 58 // constructor adopt a raw canvas ptr, using for ArkUI, should remove after rosen modifier provide drawing Canvas. Canvas(void * rawCanvas)59 explicit Canvas(void* rawCanvas) : CoreCanvas(rawCanvas) {} 60 virtual ~Canvas(); 61 62 /* 63 * @brief Restores Canvas Matrix and clip value state to count. 64 * @param count Depth of state stack to restore. 65 */ 66 void RestoreToCount(uint32_t count); 67 68 /* 69 * @brief Draw recordcmd. 70 * @param recordCmd Record command. 71 * @param matrix Matrix to rotate, scale, translate, and so on; may be nullptr. 72 * @param brush Brush to apply transparency, filtering, and so on; must be nullptr now. 73 */ 74 virtual void DrawRecordCmd(const std::shared_ptr<RecordCmd> recordCmd, 75 const Matrix* matrix = nullptr, const Brush* brush = nullptr); 76 77 virtual bool GetRecordingState() const; 78 79 virtual void SetRecordingState(bool flag); 80 81 virtual void SetOffscreen(bool isOffscreen); 82 83 virtual bool GetOffscreen() const; 84 85 virtual void SetUICapture(bool isUICapture); 86 87 virtual bool GetUICapture() const; 88 protected: 89 std::vector<Canvas*> pCanvasList_; 90 bool recordingState_ = false; 91 bool isOffscreen_ = false; 92 bool isUICapture_ = false; 93 }; 94 95 class DRAWING_API OverDrawCanvas : public Canvas { 96 public: OverDrawCanvas(std::shared_ptr<Drawing::Canvas> canvas)97 OverDrawCanvas(std::shared_ptr<Drawing::Canvas> canvas) 98 { 99 BuildOverDraw(canvas); 100 } ~OverDrawCanvas()101 virtual ~OverDrawCanvas() {} GetDrawingType()102 virtual DrawingType GetDrawingType() const 103 { 104 return DrawingType::OVER_DRAW; 105 } 106 }; 107 108 class DRAWING_API NoDrawCanvas : public Canvas { 109 public: NoDrawCanvas(int32_t width,int32_t height)110 NoDrawCanvas(int32_t width, int32_t height) 111 { 112 BuildNoDraw(width, height); 113 } 114 ~NoDrawCanvas() override = default; GetDrawingType()115 DrawingType GetDrawingType() const override 116 { 117 return DrawingType::NO_DRAW; 118 } 119 }; 120 121 class AutoCanvasRestore { 122 public: AutoCanvasRestore(Canvas & canvas,bool doSave)123 AutoCanvasRestore(Canvas& canvas, bool doSave) : canvas_(canvas) 124 { 125 saveCount_ = canvas_.GetSaveCount(); 126 if (doSave) { 127 canvas_.Save(); 128 } 129 } ~AutoCanvasRestore()130 ~AutoCanvasRestore() 131 { 132 canvas_.RestoreToCount(saveCount_); 133 } 134 135 AutoCanvasRestore(AutoCanvasRestore&&) = delete; 136 AutoCanvasRestore(const AutoCanvasRestore&) = delete; 137 AutoCanvasRestore& operator=(AutoCanvasRestore&&) = delete; 138 AutoCanvasRestore& operator=(const AutoCanvasRestore&) = delete; 139 140 private: 141 Canvas& canvas_; 142 uint32_t saveCount_; 143 }; 144 } // namespace Drawing 145 } // namespace Rosen 146 } // namespace OHOS 147 #endif 148