1 /* 2 * Copyright (c) 2020-2021 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 GRAPHIC_LITE_GFX_ENGINE_MANAGER_H 17 #define GRAPHIC_LITE_GFX_ENGINE_MANAGER_H 18 19 #include "gfx_utils/geometry2d.h" 20 #include "gfx_utils/graphic_buffer.h" 21 #include "gfx_utils/graphic_math.h" 22 #include "gfx_utils/graphic_types.h" 23 #include "gfx_utils/heap_base.h" 24 #include "gfx_utils/style.h" 25 #include "gfx_utils/transform.h" 26 27 namespace OHOS { 28 class BaseGfxEngine; 29 enum BlendMode { 30 BLEND_MODE, /* no blending */ 31 BLEND_SRC, /* S */ 32 BLEND_DST, /* D */ 33 BLEND_SRC_OVER, /* S + (1 - Sa) * D */ 34 BLEND_DST_OVER, /* (1 - Da) * S + D */ 35 BLEND_SRC_IN, /* Da * S */ 36 BLEND_DST_IN, /* Sa * D */ 37 BLEND_SRC_OUT, /* S * (1 - Da) */ 38 BLEND_DST_OUT, /* D * (1 - Sa) */ 39 BLEND_SCREEN, /* S + D - S * D */ 40 BLEND_MULTIPLY, /* S * (1 - Da) + D * (1 - Sa) + S * D */ 41 BLEND_ADDITIVE, /* S + D */ 42 BLEND_SUBTRACT, /* D * (1 - S) */ 43 }; 44 45 #ifndef TRANSFORMOPTION 46 #define TRANSFORMOPTION 47 struct TransformOption { 48 TransformAlgorithm algorithm; 49 }; 50 #endif 51 52 struct BlendOption { 53 TransformMap transMap; 54 BlendMode mode; 55 TransformOption option; 56 OpacityType opacity; 57 }; 58 59 class Image; 60 struct ArcInfo { 61 Point center; 62 Point imgPos; 63 uint16_t radius; 64 int16_t startAngle; 65 int16_t endAngle; 66 const Image* imgSrc; 67 }; 68 69 struct TransformDataInfo { 70 ImageHeader header; 71 const uint8_t* data; 72 uint8_t pxSize; 73 BlurLevel blurLevel; 74 TransformAlgorithm algorithm; 75 }; 76 77 enum BufferInfoUsage { 78 BUFFER_FB_SURFACE, 79 BUFFER_MAP_SURFACE, 80 BUFFER_SNAPSHOT_SURFACE 81 }; 82 83 class BaseGfxEngine : public HeapBase { 84 public: 85 virtual void DrawArc(BufferInfo& dst, ArcInfo& arcInfo, const Rect& mask, 86 const Style& style, OpacityType opacity, uint8_t cap); 87 88 virtual void DrawLine(BufferInfo& dst, const Point& start, const Point& end, 89 const Rect& mask, int16_t width, ColorType color, OpacityType opacity); 90 91 virtual void DrawLetter(BufferInfo& gfxDstBuffer, 92 const uint8_t* fontMap, 93 const Rect& fontRect, 94 const Rect& subRect, 95 const uint8_t fontWeight, 96 const ColorType& color, 97 const OpacityType opa); 98 99 virtual void DrawCubicBezier(BufferInfo& dst, const Point& start, const Point& control1, 100 const Point& control2, const Point& end, const Rect& mask, 101 int16_t width, ColorType color, OpacityType opacity); 102 103 virtual void DrawRect(BufferInfo& dst, 104 const Rect& rect, 105 const Rect& dirtyRect, 106 const Style& style, 107 OpacityType opacity); 108 109 virtual void DrawTransform(BufferInfo& dst, 110 const Rect& mask, 111 const Point& position, 112 ColorType color, 113 OpacityType opacity, 114 const TransformMap& transMap, 115 const TransformDataInfo& dataInfo); 116 117 // x/y: center of a circle 118 virtual void ClipCircle(const ImageInfo* info, float x, float y, float radius); 119 120 virtual void Blit(BufferInfo& dst, 121 const Point& dstPos, 122 const BufferInfo& src, 123 const Rect& subRect, 124 const BlendOption& blendOption); 125 126 virtual void Fill(BufferInfo& dst, 127 const Rect& fillArea, 128 const ColorType color, 129 const OpacityType opacity); 130 131 virtual uint8_t* AllocBuffer(uint32_t size, uint32_t usage); 132 133 virtual void FreeBuffer(uint8_t* buffer); 134 GetFBBufferInfo()135 virtual BufferInfo* GetFBBufferInfo() 136 { 137 return nullptr; 138 } 139 Flush()140 virtual void Flush() {} 141 GetScreenWidth()142 virtual uint16_t GetScreenWidth() 143 { 144 return width_; 145 } 146 GetScreenHeight()147 virtual uint16_t GetScreenHeight() 148 { 149 return height_; 150 } 151 SetScreenShape(ScreenShape screenShape)152 virtual void SetScreenShape(ScreenShape screenShape) 153 { 154 screenShape_ = screenShape; 155 } 156 GetScreenShape()157 virtual ScreenShape GetScreenShape() 158 { 159 return screenShape_; 160 } 161 GetInstance()162 static BaseGfxEngine* GetInstance() 163 { 164 return baseEngine_; 165 } 166 167 static void InitGfxEngine(BaseGfxEngine* gfxEngine = nullptr) 168 { 169 if (gfxEngine == nullptr) { 170 static BaseGfxEngine localGfxEngine; 171 baseEngine_ = &localGfxEngine; 172 return; 173 } 174 baseEngine_ = gfxEngine; 175 } 176 protected: 177 static BaseGfxEngine* baseEngine_; 178 uint16_t width_ = HORIZONTAL_RESOLUTION; 179 uint16_t height_ = VERTICAL_RESOLUTION; 180 ScreenShape screenShape_ = RECTANGLE; 181 }; 182 } 183 184 #endif // GRAPHIC_LITE_GFX_ENGINE_MANAGER_H 185