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 class DRAWING_API Surface { 59 public: 60 Surface(); ~Surface()61 ~Surface() {} 62 63 /** 64 * @brief Bind raster Surface. 65 * @param bitmap Raster pixel array. 66 * @return true if Bind success. 67 */ 68 bool Bind(const Bitmap& bitmap); 69 70 #ifdef RS_ENABLE_GPU 71 /** 72 * @brief Bind GPU texture Surface. 73 * @param image In GPU memory as a GPU texture. 74 * @return true if Bind success. 75 */ 76 bool Bind(const Image& image); 77 78 /** 79 * @brief Bind 80 * @param info FrameBuffer object info. 81 * @return true if Bind success. 82 */ 83 bool Bind(const FrameBuffer& frameBuffer); 84 85 #ifdef RS_ENABLE_VK 86 static std::shared_ptr<Surface> MakeFromBackendRenderTarget(GPUContext* gpuContext, const TextureInfo& info, 87 TextureOrigin origin, ColorType colorType, std::shared_ptr<ColorSpace> colorSpace, 88 void (*deleteVkImage)(void *), void* cleanHelper); 89 #endif 90 static std::shared_ptr<Surface> MakeFromBackendTexture(GPUContext* gpuContext, const TextureInfo& info, 91 TextureOrigin origin, int sampleCnt, ColorType colorType, 92 std::shared_ptr<ColorSpace> colorSpace, void (*deleteVkImage)(void *), void* cleanHelper); 93 94 /** 95 * @brief Create Surface from gpuContext and imageInfo. 96 * @param gpuContext GPU texture. 97 * @param budgeted Texture count. 98 * @param imageInfo image Info. 99 * @return A shared pointer to Surface. 100 */ 101 static std::shared_ptr<Surface> MakeRenderTarget(GPUContext* gpuContext, bool budgeted, const ImageInfo& imageInfo); 102 #endif 103 104 /** 105 * @brief Allocates raster Surface. 106 * @param imageInfo image info. 107 * @return A shared pointer to Surface. 108 */ 109 static std::shared_ptr<Surface> MakeRaster(const ImageInfo& imageInfo); 110 111 /** 112 * @brief Allocates raster direct Surface. 113 * @param imageInfo image info. 114 * @param pixels Pointer to destination pixels buffer. 115 * @param rowBytes Interval from one Surface row to the next. 116 * @return A shared pointer to Surface. 117 */ 118 static std::shared_ptr<Surface> MakeRasterDirect(const ImageInfo& imageInfo, void* pixels, size_t rowBytes); 119 120 /** 121 * @brief Create Surface using width and height. 122 * @param width Pixel column count. 123 * @param height Pixel row count. 124 * @return A shared pointer to Surface. 125 */ 126 static std::shared_ptr<Surface> MakeRasterN32Premul(int32_t width, int32_t height); 127 128 /** 129 * @brief Gets Canvas that draws into Surface. 130 */ 131 std::shared_ptr<Canvas> GetCanvas(); 132 133 /** 134 * @brief Gets Image capturing Surface contents. 135 * @return A shared pointer to Image 136 */ 137 std::shared_ptr<Image> GetImageSnapshot() const; 138 139 /** 140 * @brief Gets Image capturing Surface contents. 141 * @param bounds Bounds. 142 * If bounds extends beyond the Surface, it will be trimmed to just the intersection of it 143 * and the Surface. 144 * If bounds does not intersect the surface, then this returns nullptr. 145 * If bounds == the surface, then this is the same as calling the no-parameter variant. 146 @param allowRefCache Whether to use cache. 147 Use False, if you want to change the effect directly on this Snapshot 148 * @return A shared pointer to Image 149 */ 150 std::shared_ptr<Image> GetImageSnapshot(const RectI& bounds, bool allowRefCache = true) const; 151 152 /** 153 * @brief Returns a compatible Surface, with the specified width and height. 154 * @param width surface width 155 * @param height surface height 156 * @return A shared pointer to Surface 157 */ 158 std::shared_ptr<Surface> MakeSurface(int width, int height) const; 159 160 /** 161 * @brief Returns a compatible Surface, with the specified width and height. 162 * @param imageinfo surface imageinfo 163 * @return A shared pointer to Surface 164 */ 165 std::shared_ptr<Surface> MakeSurface(const ImageInfo& imageinfo) const; 166 167 /** 168 * @brief Gets ImageInfo of Surface. 169 * @return ImageInfo 170 */ 171 ImageInfo GetImageInfo(); 172 173 /** 174 * @brief Gets BackendTexture of Surface. 175 * @param access backend access mode, default as FLUSH_READ 176 * @return BackendTexture 177 */ 178 BackendTexture GetBackendTexture(BackendAccess access = BackendAccess::FLUSH_READ) const; 179 180 /** 181 * @brief Call to ensure all reads/writes of surface have been issue to the underlying 3D API. 182 */ 183 void FlushAndSubmit(bool syncCpu = false); 184 185 /** 186 * @brief Call to ensure all reads/writes of surface have been issue to the underlying 3D API. 187 */ 188 void Flush(FlushInfo *drawingflushInfo = nullptr); 189 190 int Width() const; 191 int Height() const; 192 193 template<typename T> GetImpl()194 T* GetImpl() const 195 { 196 return impl_->DowncastingTo<T>(); 197 } 198 199 #ifdef RS_ENABLE_GL 200 void Wait(const std::vector<GrGLsync>& syncs); 201 #endif 202 203 #ifdef RS_ENABLE_VK 204 void Wait(int32_t time, const VkSemaphore& semaphore); 205 void SetDrawingArea(const std::vector<RectI>& rects); 206 void ClearDrawingArea(); 207 #endif 208 209 private: 210 std::shared_ptr<SurfaceImpl> impl_; 211 std::shared_ptr<Canvas> cachedCanvas_; 212 }; 213 } // namespace Drawing 214 } // namespace Rosen 215 } // namespace OHOS 216 #endif 217