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 DRAWING_API Canvas : public CoreCanvas { 29 public: Canvas()30 Canvas() {} Canvas(int32_t width,int32_t height)31 Canvas(int32_t width, int32_t height) : CoreCanvas(width, height) {} 32 GetRecordingCanvas()33 virtual Canvas* GetRecordingCanvas() const 34 { 35 return nullptr; 36 } 37 AddCanvas(Canvas * canvas)38 void AddCanvas(Canvas* canvas) 39 { 40 if (canvas != nullptr) { 41 pCanvasList_.push_back(canvas); 42 } 43 } 44 RemoveAll()45 void RemoveAll() 46 { 47 pCanvasList_.clear(); 48 } 49 // constructor adopt a raw canvas ptr, using for ArkUI, should remove after rosen modifier provide drawing Canvas. Canvas(void * rawCanvas)50 explicit Canvas(void* rawCanvas) : CoreCanvas(rawCanvas) {} ~Canvas()51 virtual ~Canvas() { 52 RemoveAll(); 53 } 54 55 /* 56 * @brief Restores Canvas Matrix and clip value state to count. 57 * @param count Depth of state stack to restore. 58 */ RestoreToCount(uint32_t count)59 void RestoreToCount(uint32_t count) 60 { 61 for (uint32_t i = this->GetSaveCount(); i > count; i--) { 62 this->Restore(); 63 } 64 } 65 GetRecordingState()66 virtual bool GetRecordingState() const 67 { 68 return recordingState_; 69 } 70 SetRecordingState(bool flag)71 virtual void SetRecordingState(bool flag) 72 { 73 recordingState_ = flag; 74 } 75 protected: 76 std::vector<Canvas*> pCanvasList_; 77 bool recordingState_ = false; 78 }; 79 80 class DRAWING_API OverDrawCanvas : public Canvas { 81 public: OverDrawCanvas(std::shared_ptr<Drawing::Canvas> canvas)82 OverDrawCanvas(std::shared_ptr<Drawing::Canvas> canvas) 83 { 84 BuildOverDraw(canvas); 85 } ~OverDrawCanvas()86 virtual ~OverDrawCanvas() {} GetDrawingType()87 virtual DrawingType GetDrawingType() const 88 { 89 return DrawingType::OVER_DRAW; 90 } 91 }; 92 93 class AutoCanvasRestore { 94 public: AutoCanvasRestore(Canvas & canvas,bool doSave)95 AutoCanvasRestore(Canvas& canvas, bool doSave) : canvas_(canvas) 96 { 97 saveCount_ = canvas_.GetSaveCount(); 98 if (doSave) { 99 canvas_.Save(); 100 } 101 } ~AutoCanvasRestore()102 ~AutoCanvasRestore() 103 { 104 canvas_.RestoreToCount(saveCount_); 105 } 106 107 AutoCanvasRestore(AutoCanvasRestore&&) = delete; 108 AutoCanvasRestore(const AutoCanvasRestore&) = delete; 109 AutoCanvasRestore& operator=(AutoCanvasRestore&&) = delete; 110 AutoCanvasRestore& operator=(const AutoCanvasRestore&) = delete; 111 112 private: 113 Canvas& canvas_; 114 uint32_t saveCount_; 115 }; 116 } // namespace Drawing 117 } // namespace Rosen 118 } // namespace OHOS 119 #endif 120