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 IMAGE_H 17 #define IMAGE_H 18 19 #include "drawing/engine_adapter/impl_interface/image_impl.h" 20 #include "include/core/SkImage.h" 21 #include "utils/drawing_macros.h" 22 #include "image/gpu_context.h" 23 #ifdef RS_ENABLE_VK 24 #include "vulkan/vulkan.h" 25 #endif 26 27 namespace OHOS { 28 namespace Rosen { 29 namespace Drawing { 30 enum class BitDepth { 31 KU8, 32 KF16, 33 }; 34 35 enum class CompressedType { 36 NoneType, 37 ETC2_RGB8_UNORM, // the same ad ETC1 38 BC1_RGB8_UNORM, 39 BC1_RGBA8_UNORM, 40 ASTC_RGBA8_4x4, 41 ASTC_RGBA8_6x6, 42 ASTC_RGBA8_8x8, 43 }; 44 45 enum class TextureOrigin { 46 TOP_LEFT, 47 BOTTOM_LEFT, 48 }; 49 50 class Surface; 51 52 #ifdef RS_ENABLE_VK 53 struct VKAlloc { 54 VkDeviceMemory memory = VK_NULL_HANDLE; 55 VkDeviceSize offset = 0; 56 VkDeviceSize size = 0; 57 uint32_t flags = 0; 58 }; 59 60 struct VKYcbcrConversionInfo { 61 VkFormat format = VK_FORMAT_UNDEFINED; 62 uint64_t externalFormat = 0; 63 VkSamplerYcbcrModelConversion ycbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY; 64 VkSamplerYcbcrRange ycbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL; 65 VkChromaLocation xChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN; 66 VkChromaLocation yChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN; 67 VkFilter chromaFilter = VK_FILTER_NEAREST; 68 VkBool32 forceExplicitReconstruction = false; 69 VkFormatFeatureFlags formatFeatures = 0; 70 }; 71 72 struct VKTextureInfo { 73 VkImage vkImage = VK_NULL_HANDLE; 74 VKAlloc vkAlloc; 75 VkImageTiling imageTiling = VK_IMAGE_TILING_OPTIMAL; 76 VkImageLayout imageLayout = VK_IMAGE_LAYOUT_UNDEFINED; 77 VkFormat format = VK_FORMAT_UNDEFINED; 78 VkImageUsageFlags imageUsageFlags = 0; 79 uint32_t sampleCount = 1; 80 uint32_t levelCount = 0; 81 uint32_t currentQueueFamily = VK_QUEUE_FAMILY_IGNORED; 82 bool vkProtected = false; 83 VKYcbcrConversionInfo ycbcrConversionInfo; 84 VkSharingMode sharingMode = VK_SHARING_MODE_EXCLUSIVE; 85 }; 86 #endif 87 88 class DRAWING_API TextureInfo { 89 public: 90 /* 91 * @brief Sets the width value of Texture. 92 */ SetWidth(int width)93 void SetWidth(int width) 94 { 95 width_ = width; 96 } 97 98 /* 99 * @brief Sets the height value of Texture. 100 */ SetHeight(int height)101 void SetHeight(int height) 102 { 103 height_ = height; 104 } 105 106 /* 107 * @brief Used to say whether a texture has mip levels allocated or not. 108 */ SetIsMipMapped(bool isMipMapped)109 void SetIsMipMapped(bool isMipMapped) 110 { 111 isMipMapped_ = isMipMapped; 112 } 113 114 /* 115 * @brief Sets the target type of texture to render. 116 * @param target The target type of texture. 117 */ SetTarget(unsigned int target)118 void SetTarget(unsigned int target) 119 { 120 target_ = target; 121 } 122 123 /* 124 * @brief Sets the Id of texture to render. 125 * @param id Texture Id value. 126 */ SetID(unsigned int id)127 void SetID(unsigned int id) 128 { 129 id_ = id; 130 } 131 132 /* 133 * @brief Set the format of texture. 134 * @param format The format of texture. 135 */ SetFormat(unsigned int format)136 void SetFormat(unsigned int format) 137 { 138 format_ = format; 139 } 140 141 /* 142 * @brief Gets the width of TextureInfo. 143 */ GetWidth()144 int GetWidth() const 145 { 146 return width_; 147 } 148 149 /* 150 * @brief Gets the height of TextureInfo. 151 */ GetHeight()152 int GetHeight() const 153 { 154 return height_; 155 } 156 157 /* 158 * @brief Gets whether the texture has mip levels allocated. 159 */ GetIsMipMapped()160 bool GetIsMipMapped() const 161 { 162 return isMipMapped_; 163 } 164 165 /* 166 * @brief Gets the target type of TextureInfo. 167 * @return The target type of TextureInfo. 168 */ GetTarget()169 unsigned int GetTarget() const 170 { 171 return target_; 172 } 173 174 /* 175 * @brief Gets the Id of TextureInfo. 176 * @return The Id of TextureInfo. 177 */ GetID()178 unsigned int GetID() const 179 { 180 return id_; 181 } 182 183 /* 184 * @brief Gets the format of TextureInfo. 185 * @return The target format of TextureInfo. 186 */ GetFormat()187 unsigned int GetFormat() const 188 { 189 return format_; 190 } 191 192 #ifdef RS_ENABLE_VK GetVKTextureInfo()193 std::shared_ptr<VKTextureInfo> GetVKTextureInfo() const 194 { 195 return vkTextureInfo_; 196 } SetVKTextureInfo(std::shared_ptr<VKTextureInfo> vkTextureInfo)197 void SetVKTextureInfo(std::shared_ptr<VKTextureInfo> vkTextureInfo) 198 { 199 vkTextureInfo_ = vkTextureInfo; 200 } 201 #endif 202 private: 203 int width_ = 0; 204 int height_ = 0; 205 bool isMipMapped_ = false; 206 unsigned int target_ = 0; 207 unsigned int id_ = 0; 208 unsigned int format_ = 0; 209 #ifdef RS_ENABLE_VK 210 std::shared_ptr<VKTextureInfo> vkTextureInfo_ = nullptr; 211 #endif 212 }; 213 214 class DRAWING_API BackendTexture { 215 public: 216 BackendTexture() noexcept; 217 BackendTexture(bool isValid) noexcept; ~BackendTexture()218 virtual ~BackendTexture() {}; 219 220 bool IsValid() const; 221 void SetTextureInfo(const TextureInfo& textureInfo); 222 const TextureInfo& GetTextureInfo() const; 223 224 private: 225 bool isValid_; 226 TextureInfo textureInfo_; 227 }; 228 229 class DRAWING_API Image { 230 public: 231 Image() noexcept; 232 // constructor adopt a raw image ptr, using for ArkUI, should remove after enable multi-media image decode. 233 explicit Image(void* rawImg) noexcept; 234 explicit Image(std::shared_ptr<ImageImpl> imageImpl); ~Image()235 virtual ~Image() {}; 236 bool BuildFromBitmap(const Bitmap& bitmap); 237 238 /* 239 * @brief Create Image from Pixmap. 240 * @param pixmap pixmap. 241 * @param rasterReleaseProc function called when pixels can be released; or nullptr. 242 * @param releaseContext state passed to rasterReleaseProc; or nullptr. 243 * @return Image sharing pixmap. 244 */ 245 static std::shared_ptr<Image> MakeFromRaster(const Pixmap& pixmap, 246 RasterReleaseProc rasterReleaseProc, ReleaseContext releaseContext); 247 248 /* 249 * @brief Create Image from ImageInfo, sharing pixels. 250 */ 251 static std::shared_ptr<Image> MakeRasterData(const ImageInfo& info, std::shared_ptr<Data> pixels, 252 size_t rowBytes); 253 #ifdef ACE_ENABLE_GPU 254 /* 255 * @brief Create Image from Bitmap. Image is uploaded to GPU back-end using context. 256 * @param gpuContext GPU context. 257 * @param bitmap BitmapInfo, pixel address and row bytes. 258 * @return True if Image is created successed. 259 */ 260 bool BuildFromBitmap(GPUContext& gpuContext, const Bitmap& bitmap); 261 262 bool MakeFromEncoded(const std::shared_ptr<Data>& data); 263 264 /* 265 * @brief Create a GPU-backed Image from compressed data. 266 * @param gpuContext GPU context. 267 * @param data Compressed data to store in Image. 268 * @param width Width of full Image. 269 * @param height Height of full Image. 270 * @param type Type of compression used. 271 * @return True if Image is created successed. 272 */ 273 bool BuildFromCompressed(GPUContext& gpuContext, const std::shared_ptr<Data>& data, int width, int height, 274 CompressedType type); 275 276 /* 277 * @brief Create Image from GPU texture associated with context. 278 * @param gpuContext GPU context. 279 * @param info Texture info. 280 * @param origin The origin of the texture space corresponds to the context pixel. 281 One of TextureOrigin::Top_Left, TextureOrigion::Bottom_Left. 282 * @param bitmapFormat It contains ColorType and AlphaType. 283 * @param colorSpace Range of colors, may be nullptr. 284 * @return True if Image is created successed. 285 */ 286 bool BuildFromTexture(GPUContext& gpuContext, const TextureInfo& info, TextureOrigin origin, 287 BitmapFormat bitmapFormat, const std::shared_ptr<ColorSpace>& colorSpace, 288 void (*deleteFunc)(void*) = nullptr, void* cleanupHelper = nullptr); 289 290 bool BuildFromSurface(GPUContext& gpuContext, Surface& surface, TextureOrigin origin, 291 BitmapFormat bitmapFormat, const std::shared_ptr<ColorSpace>& colorSpace); 292 293 bool BuildSubset(const std::shared_ptr<Image>& image, const RectI& rect, GPUContext& gpuContext); 294 295 BackendTexture GetBackendTexture(bool flushPendingGrContextIO, TextureOrigin* origin) const; 296 297 bool IsValid(GPUContext* context) const; 298 299 bool pinAsTexture(GPUContext& context); 300 #endif 301 302 /* 303 * @brief Creates raster Bitmap with same pixels as Image. 304 */ 305 bool AsLegacyBitmap(Bitmap& bitmap) const; 306 307 /* 308 * @brief Gets the width of Image. 309 */ 310 int GetWidth() const; 311 312 /* 313 * @brief Gets the height of Image. 314 */ 315 int GetHeight() const; 316 317 /* 318 * @brief Gets the color type of Image. 319 */ 320 ColorType GetColorType() const; 321 322 /* 323 * @brief Gets the alpha type of Image. 324 */ 325 AlphaType GetAlphaType() const; 326 327 /* 328 * @brief Gets the color space of Image. 329 */ 330 std::shared_ptr<ColorSpace> GetColorSpace() const; 331 332 /* 333 * @brief Gets the unique Id of Image. 334 */ 335 uint32_t GetUniqueID() const; 336 337 /* 338 * @brief Gets the ImageInfo of Image. 339 */ 340 ImageInfo GetImageInfo(); 341 342 /* 343 * @brief Copies a Rect of pixels from Image to Bitmap. 344 * @param bitmap Destination Bitmap. 345 * @param x Column index whose absolute value is less than Image width. 346 * @param y Row index whose absolute value is less than Image height. 347 * @return True of pixels are copied to Bitmap. 348 */ 349 bool ReadPixels(Bitmap& bitmap, int x, int y); 350 351 bool ReadPixels(Pixmap& pixmap, int x, int y); 352 353 bool ReadPixels(const ImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, 354 int32_t srcX, int32_t srcY) const; 355 356 bool ScalePixels(const Bitmap& bitmap, const SamplingOptions& sampling, 357 bool allowCachingHint = true) const; 358 359 std::shared_ptr<Data> EncodeToData(EncodedImageFormat encodedImageFormat, int quality) const; 360 361 bool IsLazyGenerated() const; 362 363 /* 364 * @brief Get Bitmap by image's directContext, can call it if IsLazyGenerated return false. 365 */ 366 bool GetROPixels(Bitmap& bitmap) const; 367 368 std::shared_ptr<Image> MakeRasterImage() const; 369 370 bool CanPeekPixels() const; 371 372 /* 373 * @brief Returns true the contents of Image was created on or uploaded to GPU memory, 374 and is available as a GPU texture. 375 * @return True if Image is a GPU texture. 376 */ 377 bool IsTextureBacked() const; 378 379 bool IsOpaque() const; 380 381 template<typename T> GetImpl()382 T* GetImpl() const 383 { 384 return imageImplPtr->DowncastingTo<T>(); 385 } 386 387 // using for recording, should to remove after using shared memory 388 std::shared_ptr<Data> Serialize() const; 389 bool Deserialize(std::shared_ptr<Data> data); 390 391 const sk_sp<SkImage> ExportSkImage(); 392 393 private: 394 std::shared_ptr<ImageImpl> imageImplPtr; 395 }; 396 } // namespace Drawing 397 } // namespace Rosen 398 } // namespace OHOS 399 #endif 400