1 /* 2 * Copyright (c) 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 DRAWING_SURFACE_H 17 #define DRAWING_SURFACE_H 18 19 #include "impl_interface/surface_impl.h" 20 21 #include "draw/canvas.h" 22 #include "image/bitmap.h" 23 #include "image/image.h" 24 #include "utils/drawing_macros.h" 25 26 namespace OHOS { 27 namespace Rosen { 28 namespace Drawing { 29 30 #ifdef RS_ENABLE_GPU 31 struct FrameBuffer { 32 int width; 33 int height; 34 int FBOID; 35 int Format; 36 ColorType colorType = Drawing::COLORTYPE_RGBA_8888; 37 std::shared_ptr<GPUContext> gpuContext; 38 std::shared_ptr<ColorSpace> colorSpace; 39 }; 40 #endif 41 42 struct FlushInfo { 43 bool backendSurfaceAccess = false; 44 size_t numSemaphores = 0; 45 void* backendSemaphore = nullptr; 46 void (*finishedProc)(void* finishedContext) = nullptr; 47 void* finishedContext = nullptr; 48 void (*submittedProc)(void* finishedContext, bool success) = nullptr; 49 void* submittedContext = nullptr; 50 }; 51 52 enum class BackendAccess { 53 FLUSH_READ, 54 FLUSH_WRITE, 55 DISCARD_WRITE 56 }; 57 58 enum class SemaphoresSubmited { 59 DRAWING_SUBMIT_NO, // Drawing flush callback not executd. 60 DRAWING_SUBMIT_YES, // Drawing flush callback executed. 61 DRAWING_ENGINE_SUBMIT_NO, // Engine flush has callback parameters and return values. 62 DRAWING_ENGINE_SUBMIT_YES, // Engine flush has callback parameters and return values. 63 DRAWING_ENGINE_YES // Engine flush has no callback parameters and return values. 64 }; 65 66 class DRAWING_API Surface { 67 public: 68 Surface(); ~Surface()69 ~Surface() {} 70 71 /** 72 * @brief Bind raster Surface. 73 * @param bitmap Raster pixel array. 74 * @return true if Bind success. 75 */ 76 bool Bind(const Bitmap& bitmap); 77 78 #ifdef RS_ENABLE_GPU 79 /** 80 * @brief Bind GPU texture Surface. 81 * @param image In GPU memory as a GPU texture. 82 * @return true if Bind success. 83 */ 84 bool Bind(const Image& image); 85 86 /** 87 * @brief Bind 88 * @param info FrameBuffer object info. 89 * @return true if Bind success. 90 */ 91 bool Bind(const FrameBuffer& frameBuffer); 92 93 #ifdef RS_ENABLE_VK 94 static std::shared_ptr<Surface> MakeFromBackendRenderTarget(GPUContext* gpuContext, const TextureInfo& info, 95 TextureOrigin origin, ColorType colorType, std::shared_ptr<ColorSpace> colorSpace, 96 void (*deleteVkImage)(void *), void* cleanHelper); 97 #endif 98 static std::shared_ptr<Surface> MakeFromBackendTexture(GPUContext* gpuContext, const TextureInfo& info, 99 TextureOrigin origin, int sampleCnt, ColorType colorType, 100 std::shared_ptr<ColorSpace> colorSpace, void (*deleteVkImage)(void *), void* cleanHelper); 101 102 /** 103 * @brief Create Surface from gpuContext and imageInfo. 104 * @param gpuContext GPU texture. 105 * @param budgeted Texture count. 106 * @param imageInfo image Info. 107 * @return A shared pointer to Surface. 108 */ 109 static std::shared_ptr<Surface> MakeRenderTarget(GPUContext* gpuContext, bool budgeted, const ImageInfo& imageInfo); 110 #endif 111 112 /** 113 * @brief Allocates raster Surface. 114 * @param imageInfo image info. 115 * @return A shared pointer to Surface. 116 */ 117 static std::shared_ptr<Surface> MakeRaster(const ImageInfo& imageInfo); 118 119 /** 120 * @brief Allocates raster direct Surface. 121 * @param imageInfo image info. 122 * @param pixels Pointer to destination pixels buffer. 123 * @param rowBytes Interval from one Surface row to the next. 124 * @return A shared pointer to Surface. 125 */ 126 static std::shared_ptr<Surface> MakeRasterDirect(const ImageInfo& imageInfo, void* pixels, size_t rowBytes); 127 128 /** 129 * @brief Create Surface using width and height. 130 * @param width Pixel column count. 131 * @param height Pixel row count. 132 * @return A shared pointer to Surface. 133 */ 134 static std::shared_ptr<Surface> MakeRasterN32Premul(int32_t width, int32_t height); 135 136 /** 137 * @brief Gets Canvas that draws into Surface. 138 */ 139 std::shared_ptr<Canvas> GetCanvas(); 140 141 /** 142 * @brief Gets Image capturing Surface contents. 143 * @return A shared pointer to Image 144 */ 145 std::shared_ptr<Image> GetImageSnapshot() const; 146 147 /** 148 * @brief Gets Image capturing Surface contents. 149 * @param bounds Bounds. 150 * If bounds extends beyond the Surface, it will be trimmed to just the intersection of it 151 * and the Surface. 152 * If bounds does not intersect the surface, then this returns nullptr. 153 * If bounds == the surface, then this is the same as calling the no-parameter variant. 154 * @param allowRefCache Whether to use cache. 155 Use False, if you want to change the effect directly on this Snapshot 156 * @return A shared pointer to Image 157 */ 158 std::shared_ptr<Image> GetImageSnapshot(const RectI& bounds, bool allowRefCache = true) const; 159 160 /** 161 * @brief Returns a compatible Surface, with the specified width and height. 162 * @param width surface width 163 * @param height surface height 164 * @return A shared pointer to Surface 165 */ 166 std::shared_ptr<Surface> MakeSurface(int width, int height) const; 167 168 /** 169 * @brief Returns a compatible Surface, with the specified width and height. 170 * @param imageinfo surface imageinfo 171 * @return A shared pointer to Surface 172 */ 173 std::shared_ptr<Surface> MakeSurface(const ImageInfo& imageinfo) const; 174 175 /** 176 * @brief Gets ImageInfo of Surface. 177 * @return ImageInfo 178 */ 179 ImageInfo GetImageInfo(); 180 181 /** 182 * @brief Gets BackendTexture of Surface. 183 * @param access backend access mode, default as FLUSH_READ 184 * @return BackendTexture 185 */ 186 BackendTexture GetBackendTexture(BackendAccess access = BackendAccess::FLUSH_READ) const; 187 188 /** 189 * @brief Call to ensure all reads/writes of surface have been issue to the underlying 3D API. 190 */ 191 void FlushAndSubmit(bool syncCpu = false); 192 193 /** 194 * @brief Call to ensure all reads/writes of surface have been issue to the underlying 3D API. 195 */ 196 SemaphoresSubmited Flush(FlushInfo *drawingflushInfo = nullptr); 197 198 int Width() const; 199 int Height() const; 200 201 template<typename T> GetImpl()202 T* GetImpl() const 203 { 204 return impl_->DowncastingTo<T>(); 205 } 206 207 #ifdef RS_ENABLE_GL 208 void Wait(const std::vector<GrGLsync>& syncs); 209 #endif 210 211 #ifdef RS_ENABLE_VK 212 void Wait(int32_t time, const VkSemaphore& semaphore); 213 void SetDrawingArea(const std::vector<RectI>& rects); 214 void ClearDrawingArea(); 215 #endif 216 217 /** 218 * @brief Set headroom for backendTexture of Surface. 219 * @param headroom headroom value 220 */ 221 void SetHeadroom(float headroom); 222 223 /** 224 * @brief Get headroom for backendTexture of Surface. 225 * @return headroom value 226 */ 227 float GetHeadroom() const; 228 229 private: 230 std::shared_ptr<SurfaceImpl> impl_; 231 std::shared_ptr<Canvas> cachedCanvas_; 232 }; 233 } // namespace Drawing 234 } // namespace Rosen 235 } // namespace OHOS 236 #endif 237