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 #include "image/gpu_context.h" 25 26 namespace OHOS { 27 namespace Rosen { 28 namespace Drawing { 29 class RecordCmd; 30 31 class AutoCanvasMatrixBrush { 32 public: 33 AutoCanvasMatrixBrush(Canvas* canvas, 34 const Matrix* matrix, const Brush* brush, const Rect& bounds); 35 ~AutoCanvasMatrixBrush(); 36 37 AutoCanvasMatrixBrush(AutoCanvasMatrixBrush&&) = delete; 38 AutoCanvasMatrixBrush(const AutoCanvasMatrixBrush&) = delete; 39 AutoCanvasMatrixBrush& operator=(AutoCanvasMatrixBrush&&) = delete; 40 AutoCanvasMatrixBrush& operator=(const AutoCanvasMatrixBrush&) = delete; 41 42 private: 43 Canvas* canvas_; 44 uint32_t saveCount_; 45 Paint paintPen_; 46 Paint paintBrush_; 47 }; 48 49 class DRAWING_API Canvas : public CoreCanvas { 50 public: 51 static constexpr int64_t INVALID_STENCIL_VAL{-1}; 52 Canvas()53 Canvas() {} Canvas(DrawingType type)54 Canvas(DrawingType type) : CoreCanvas(type) {} Canvas(int32_t width,int32_t height)55 Canvas(int32_t width, int32_t height) : CoreCanvas(width, height) {} 56 57 virtual Canvas* GetRecordingCanvas() const; 58 59 void AddCanvas(Canvas* canvas); 60 61 void RemoveAll(); 62 virtual ~Canvas(); 63 64 /* 65 * @brief Restores Canvas Matrix and clip value state to count. 66 * @param count Depth of state stack to restore. 67 */ 68 void RestoreToCount(uint32_t count); 69 70 /* 71 * @brief Draw recordcmd. 72 * @param recordCmd Record command. 73 * @param matrix Matrix to rotate, scale, translate, and so on; may be nullptr. 74 * @param brush Brush to apply transparency, filtering, and so on; must be nullptr now. 75 */ 76 virtual void DrawRecordCmd(const std::shared_ptr<RecordCmd> recordCmd, 77 const Matrix* matrix = nullptr, const Brush* brush = nullptr); 78 79 virtual bool GetRecordingState() const; 80 81 virtual void SetRecordingState(bool flag); 82 83 virtual void SetOffscreen(bool isOffscreen); 84 85 virtual bool GetOffscreen() const; 86 87 virtual void SetUICapture(bool isUICapture); 88 89 virtual bool GetUICapture() const; 90 GetStencilVal()91 inline int64_t GetStencilVal() const noexcept 92 { 93 return stencilVal_; 94 } 95 SetStencilVal(int64_t stencilVal)96 inline void SetStencilVal(int64_t stencilVal) noexcept 97 { 98 stencilVal_ = stencilVal; 99 } 100 GetMaxStencilVal()101 inline int64_t GetMaxStencilVal() const noexcept 102 { 103 return maxStencilVal_; 104 } 105 SetMaxStencilVal(int64_t maxStencilVal)106 inline void SetMaxStencilVal(int64_t maxStencilVal) noexcept 107 { 108 maxStencilVal_ = maxStencilVal; 109 } 110 protected: 111 std::vector<Canvas*> pCanvasList_; 112 bool recordingState_ = false; 113 bool isOffscreen_ = false; 114 bool isUICapture_ = false; 115 int64_t stencilVal_ = INVALID_STENCIL_VAL; 116 int64_t maxStencilVal_ = 0; 117 }; 118 119 class DRAWING_API OverDrawCanvas : public Canvas { 120 public: OverDrawCanvas(std::shared_ptr<Drawing::Canvas> canvas)121 OverDrawCanvas(std::shared_ptr<Drawing::Canvas> canvas) : Canvas(DrawingType::OVER_DRAW) 122 { 123 BuildOverDraw(canvas); 124 } ~OverDrawCanvas()125 virtual ~OverDrawCanvas() {} GetDrawingType()126 virtual DrawingType GetDrawingType() const override 127 { 128 return DrawingType::OVER_DRAW; 129 } SetGrContext(std::shared_ptr<GPUContext> gpuContext)130 void SetGrContext(std::shared_ptr<GPUContext> gpuContext) 131 { 132 gpuContext_ = gpuContext; 133 } 134 #ifdef RS_ENABLE_GPU GetGPUContext()135 std::shared_ptr<GPUContext> GetGPUContext() override 136 { 137 return gpuContext_; 138 } 139 #endif 140 private: 141 std::shared_ptr<GPUContext> gpuContext_ = nullptr; 142 }; 143 144 class DRAWING_API NoDrawCanvas : public Canvas { 145 public: NoDrawCanvas(int32_t width,int32_t height)146 NoDrawCanvas(int32_t width, int32_t height) : Canvas(DrawingType::NO_DRAW) 147 { 148 BuildNoDraw(width, height); 149 } 150 ~NoDrawCanvas() override = default; GetDrawingType()151 DrawingType GetDrawingType() const override 152 { 153 return DrawingType::NO_DRAW; 154 } 155 }; 156 157 class AutoCanvasRestore { 158 public: AutoCanvasRestore(Canvas & canvas,bool doSave)159 AutoCanvasRestore(Canvas& canvas, bool doSave) : canvas_(canvas) 160 { 161 saveCount_ = canvas_.GetSaveCount(); 162 if (doSave) { 163 canvas_.Save(); 164 } 165 } ~AutoCanvasRestore()166 ~AutoCanvasRestore() 167 { 168 canvas_.RestoreToCount(saveCount_); 169 } 170 171 AutoCanvasRestore(AutoCanvasRestore&&) = delete; 172 AutoCanvasRestore(const AutoCanvasRestore&) = delete; 173 AutoCanvasRestore& operator=(AutoCanvasRestore&&) = delete; 174 AutoCanvasRestore& operator=(const AutoCanvasRestore&) = delete; 175 176 private: 177 Canvas& canvas_; 178 uint32_t saveCount_; 179 }; 180 } // namespace Drawing 181 } // namespace Rosen 182 } // namespace OHOS 183 #endif 184