1 /* 2 * Copyright (c) 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 FOUNDATION_ACE_FRAMEWORKS_BASE_IMAGE_ACE_PIXEL_MAP_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_IMAGE_ACE_PIXEL_MAP_H 18 19 #include <chrono> 20 #include <fstream> 21 #include <string> 22 #include <vector> 23 #include <memory> 24 25 #include "base/geometry/dimension.h" 26 #include "base/geometry/rect.h" 27 #include "base/geometry/ng/size_t.h" 28 #include "base/memory/ace_type.h" 29 #include "core/common/resource/resource_object.h" 30 31 namespace OHOS { 32 33 namespace Media { 34 class PixelMap; 35 } 36 37 namespace Ace { 38 39 enum class PixelFormat : int32_t { 40 UNKNOWN = 0, 41 ARGB_8888 = 1, // Each pixel is stored on 4 bytes. 42 RGB_565 = 2, // Each pixel is stored on 2 bytes 43 RGBA_8888 = 3, 44 BGRA_8888 = 4, 45 RGB_888 = 5, 46 ALPHA_8 = 6, 47 RGBA_F16 = 7, 48 NV21 = 8, // Each pixel is stored on 3/2 bytes. 49 NV12 = 9, 50 CMYK = 10, 51 YCBCR_P010 = 11, 52 YCRCB_P010 = 12, 53 RGBA_1010102 = 14, 54 }; 55 56 enum class AlphaType : int32_t { 57 IMAGE_ALPHA_TYPE_UNKNOWN = 0, 58 IMAGE_ALPHA_TYPE_OPAQUE = 1, // image pixels are stored as opaque. 59 IMAGE_ALPHA_TYPE_PREMUL = 2, // image have alpha component, and all pixels have premultiplied by alpha value. 60 IMAGE_ALPHA_TYPE_UNPREMUL = 3, // image have alpha component, and all pixels stored without premultiply alpha value. 61 }; 62 63 enum class AllocatorType : int32_t { 64 // keep same with java AllocatorType 65 DEFAULT = 0, 66 HEAP_ALLOC = 1, 67 SHARE_MEM_ALLOC = 2, 68 CUSTOM_ALLOC = 3, // external 69 DMA_ALLOC = 4, // SurfaceBuffer 70 }; 71 72 enum class ResizableOption { 73 LEFT, 74 RIGHT, 75 TOP, 76 BOTTOM, 77 }; 78 79 struct ImageResizableSlice { 80 Dimension left; 81 Dimension right; 82 Dimension top; 83 Dimension bottom; 84 struct ResourceUpdater { 85 RefPtr<ResourceObject> obj; 86 std::function<void(const RefPtr<ResourceObject>&, ImageResizableSlice&)> updateFunc; 87 }; 88 std::unordered_map<std::string, ResourceUpdater> resMap_; ToStringImageResizableSlice89 std::string ToString() const 90 { 91 std::string result; 92 result.append("ImageResizableSlice: {"); 93 result.append("left: "); 94 result.append(left.ToString()); 95 result.append(", right: "); 96 result.append(right.ToString()); 97 result.append(", top: "); 98 result.append(top.ToString()); 99 result.append(", bottom: "); 100 result.append(bottom.ToString()); 101 result.append("}"); 102 return result; 103 } 104 bool operator==(const ImageResizableSlice& slice) const 105 { 106 return left == slice.left && right == slice.right && top == slice.top && bottom == slice.bottom; 107 } ValidImageResizableSlice108 bool Valid() const 109 { 110 return left.IsValid() || right.IsValid() || top.IsValid() || bottom.IsValid(); 111 } SetResizableLeftImageResizableSlice112 void SetResizableLeft(const Dimension& sliceDimension) 113 { 114 left = sliceDimension; 115 } SetResizableRightImageResizableSlice116 void SetResizableRight(const Dimension& sliceDimension) 117 { 118 right = sliceDimension; 119 } SetResizableBottomImageResizableSlice120 void SetResizableBottom(const Dimension& sliceDimension) 121 { 122 bottom = sliceDimension; 123 } SetResizableTopImageResizableSlice124 void SetResizableTop(const Dimension& sliceDimension) 125 { 126 top = sliceDimension; 127 } SetEdgeSliceImageResizableSlice128 void SetEdgeSlice(ResizableOption direction, const Dimension& sliceDimension) 129 { 130 switch (direction) { 131 case ResizableOption::TOP: 132 SetResizableTop(sliceDimension); 133 break; 134 case ResizableOption::BOTTOM: 135 SetResizableBottom(sliceDimension); 136 break; 137 case ResizableOption::LEFT: 138 SetResizableLeft(sliceDimension); 139 break; 140 case ResizableOption::RIGHT: 141 SetResizableRight(sliceDimension); 142 break; 143 default: 144 break; 145 } 146 } 147 AddResourceImageResizableSlice148 void AddResource( 149 const std::string& key, 150 const RefPtr<ResourceObject>& resObj, 151 std::function<void(const RefPtr<ResourceObject>&, ImageResizableSlice&)>&& updateFunc) 152 { 153 if (resObj && updateFunc) { 154 resMap_[key] = { resObj, std::move(updateFunc) }; 155 } 156 } 157 RemoveResourceImageResizableSlice158 void RemoveResource(const std::string& key) 159 { 160 auto iter = resMap_.find(key); 161 if (iter != resMap_.end()) { 162 resMap_.erase(iter); 163 } 164 } 165 ReloadResourcesImageResizableSlice166 void ReloadResources() 167 { 168 for (const auto& [key, resourceUpdater] : resMap_) { 169 resourceUpdater.updateFunc(resourceUpdater.obj, *this); 170 } 171 } 172 }; 173 174 enum class AceAntiAliasingOption : int32_t { 175 NONE = 0, 176 LOW = 1, 177 MEDIUM = 2, 178 HIGH = 3, 179 }; 180 181 enum class ScaleMode : int32_t { 182 FIT_TARGET_SIZE = 0, 183 CENTER_CROP = 1, 184 }; 185 186 struct InitializationOptions { 187 NG::SizeT<int32_t> size; 188 PixelFormat srcPixelFormat = PixelFormat::BGRA_8888; 189 PixelFormat pixelFormat = PixelFormat::UNKNOWN; 190 AlphaType alphaType = AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN; 191 ScaleMode scaleMode = ScaleMode::FIT_TARGET_SIZE; 192 int32_t srcRowStride = 0; 193 bool editable = false; 194 bool useSourceIfMatch = false; 195 bool useDMA = false; 196 }; 197 198 struct WritePixelsOptions { 199 const uint8_t* source = nullptr; 200 uint64_t bufferSize = 0; 201 uint32_t offset = 0; 202 uint32_t stride = 0; 203 Rect region; 204 PixelFormat srcPixelFormat = PixelFormat::BGRA_8888; 205 }; 206 207 class ACE_FORCE_EXPORT PixelMap : public AceType { 208 DECLARE_ACE_TYPE(PixelMap, AceType); 209 210 public: 211 #if defined(ACE_STATIC) 212 /** 213 * @description: only for 1.2 214 * @param opts initialize options 215 * @return refptr pixelmap 216 */ 217 static RefPtr<PixelMap> Create(const std::shared_ptr<Media::PixelMap>& pixmap); 218 #endif 219 static RefPtr<PixelMap> Create(std::unique_ptr<Media::PixelMap>&& pixmap); 220 static RefPtr<PixelMap> Create(const InitializationOptions& opts); 221 static RefPtr<PixelMap> CreatePixelMap(void* sptrAddr); 222 static RefPtr<PixelMap> CopyPixelMap(const RefPtr<PixelMap>& pixelMap); 223 static RefPtr<PixelMap> DecodeTlv(std::vector<uint8_t>& buff); 224 225 /** 226 * @param ptr: drawable pointer of type Napi::DrawableDescriptor& 227 */ 228 static RefPtr<PixelMap> GetFromDrawable(void* ptr); 229 static bool GetPxielMapListFromAnimatedDrawable(void* ptr, std::vector<RefPtr<PixelMap>>& pixelMaps, 230 int32_t& duration, int32_t& iterations); 231 static RefPtr<PixelMap> CreatePixelMapFromDataAbility(void* uniquePtr); 232 static RefPtr<PixelMap> ConvertSkImageToPixmap( 233 const uint32_t* colors, uint32_t colorLength, int32_t width, int32_t height); 234 virtual int32_t GetWidth() const = 0; 235 virtual int32_t GetHeight() const = 0; 236 virtual bool GetPixelsVec(std::vector<uint8_t>& data) const = 0; 237 virtual const uint8_t* GetPixels() const = 0; 238 virtual PixelFormat GetPixelFormat() const = 0; 239 virtual AlphaType GetAlphaType() const = 0; 240 virtual int32_t GetRowStride() const = 0; 241 virtual int32_t GetRowBytes() const = 0; 242 virtual int32_t GetByteCount() const = 0; 243 virtual AllocatorType GetAllocatorType() const = 0; 244 virtual bool IsHdr() const = 0; 245 virtual void* GetPixelManager() const = 0; 246 virtual void* GetRawPixelMapPtr() const = 0; 247 virtual std::string GetId() = 0; 248 virtual std::string GetModifyId() = 0; 249 virtual std::shared_ptr<Media::PixelMap> GetPixelMapSharedPtr() = 0; 250 virtual void* GetWritablePixels() const = 0; 251 virtual void Scale(float xAxis, float yAxis) = 0; 252 virtual void Scale(float xAxis, float yAxis, const AceAntiAliasingOption &option) = 0; 253 254 static void* GetReleaseContext(const RefPtr<PixelMap>& pixelMap); 255 // passed to SkImage to release PixelMap shared_ptr 256 static void ReleaseProc(const void* /* pixels */, void* context); 257 virtual void SavePixelMapToFile(const std::string& dst) const = 0; 258 virtual RefPtr<PixelMap> GetCropPixelMap(const Rect& srcRect) = 0; 259 virtual bool EncodeTlv(std::vector<uint8_t>& buff) = 0; 260 virtual uint32_t WritePixels(const WritePixelsOptions& opts) = 0; 261 virtual uint32_t GetInnerColorGamut() const = 0; 262 virtual void SetMemoryName(std::string pixelMapName) const = 0; 263 }; 264 265 } // namespace Ace 266 } // namespace OHOS 267 268 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_IMAGE_ACE_PIXEL_MAP_H 269