• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 INTERFACES_INNERKITS_INCLUDE_PIXEL_MAP_H_
17 #define INTERFACES_INNERKITS_INCLUDE_PIXEL_MAP_H_
18 
19 #include <atomic>
20 #include <cstdint>
21 #include <memory>
22 #include <mutex>
23 #include <shared_mutex>
24 #ifdef IMAGE_COLORSPACE_FLAG
25 #include "color_space.h"
26 #endif
27 #include "image_type.h"
28 #include "parcel.h"
29 #ifdef IMAGE_PURGEABLE_PIXELMAP
30 #include "purgeable_mem_base.h"
31 #include "purgeable_mem_builder.h"
32 #endif
33 #if !defined(_WIN32) && !defined(_APPLE) && !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
34 #include "pixel_map_parcel.h"
35 #endif
36 
37 namespace OHOS::Rosen {
38 class PixelMapStorage;
39 class RSMarshallingHelper;
40 class RSProfiler;
41 class RSModifiersDraw;
42 };
43 
44 namespace OHOS {
45 namespace Media {
46 struct HdrMetadata;
47 enum class ImageHdrType : int32_t;
48 using TransColorProc = bool (*)(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount);
49 using CustomFreePixelMap = void (*)(void *addr, void *context, uint32_t size);
50 
51 typedef struct {
52     float scaleX;
53     float scaleY;
54     float rotateD;
55     float cropLeft;
56     float cropTop;
57     float cropWidth;
58     float cropHeight;
59     float translateX;
60     float translateY;
61     bool flipX;
62     bool flipY;
63 } TransformData;
64 
65 struct InitializationOptions {
66     Size size;
67     PixelFormat srcPixelFormat = PixelFormat::BGRA_8888;
68     PixelFormat pixelFormat = PixelFormat::UNKNOWN;
69     AlphaType alphaType = AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
70     ScaleMode scaleMode = ScaleMode::FIT_TARGET_SIZE;
71     YUVConvertColorSpaceDetails convertColorSpace;
72     int32_t srcRowStride = 0;
73     AllocatorType allocatorType = AllocatorType::DEFAULT;
74     bool editable = false;
75     bool useSourceIfMatch = false;
76     bool useDMA = false;
77 };
78 struct TransInfos;
79 
80 // Build ARGB_8888 pixel value
81 constexpr uint8_t ARGB_MASK = 0xFF;
82 constexpr uint8_t ARGB_A_SHIFT = 24;
83 constexpr uint8_t ARGB_R_SHIFT = 16;
84 constexpr uint8_t ARGB_G_SHIFT = 8;
85 constexpr uint8_t ARGB_B_SHIFT = 0;
86 // Define pixel map malloc max size 600MB
87 // Memory copy will be performed twice on heap memory during IPC, so the size has to be limited
88 constexpr int32_t PIXEL_MAP_MAX_RAM_SIZE = 600 * 1024 * 1024;
89 
90 typedef struct PixelMapError {
91     uint32_t errorCode = 0;
92     std::string errorInfo = "";
93 } PIXEL_MAP_ERR;
94 
95 typedef struct BuildParam {
96     int32_t offset_ = 0;
97     int32_t width_ = 0;
98     bool flag_ = true;
99 } BUILD_PARAM;
100 
101 struct PixelMemInfo {
102     uint8_t* base = nullptr;
103     void* context = nullptr;
104     int32_t bufferSize = 0;
105     AllocatorType allocatorType = AllocatorType::SHARE_MEM_ALLOC;
106     bool isAstc = false;
107     bool displayOnly = false;
108 };
109 
110 struct RWPixelsOptions {
111     const uint8_t* pixels = nullptr;
112     uint64_t bufferSize = 0;
113     uint32_t offset = 0;
114     uint32_t stride = 0;
115     Rect region;
116     PixelFormat pixelFormat = PixelFormat::BGRA_8888;
117 };
118 
119 class ExifMetadata;
120 class AbsMemory;
121 
122 #define PIXELMAP_VERSION_START (1<<16)
123 #define PIXELMAP_VERSION_DISPLAY_ONLY (PIXELMAP_VERSION_START + 1)
124 #define PIXELMAP_VERSION_LATEST PIXELMAP_VERSION_DISPLAY_ONLY
125 
126 class PixelMap : public Parcelable, public PIXEL_MAP_ERR {
127 public:
128 #if !defined(_WIN32) && !defined(_APPLE) && !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
129     friend class PixelMapRecordParcel;
130 #endif
131     static std::atomic<uint32_t> currentId;
PixelMap()132     PixelMap()
133     {
134         uniqueId_ = currentId.fetch_add(1, std::memory_order_relaxed);
135     }
136     virtual ~PixelMap();
137 
138     /**
139      * Create a PixelMap through pixel data.
140      *
141      * @param colors The pixel data.
142      * @param colorLength The length of the pixel data.
143      * @param opts Initialization Options.
144      * @return The PixelMap.
145      */
146     NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const uint32_t *colors, uint32_t colorLength,
147                                                          const InitializationOptions &opts);
148 
149     /**
150      * Create a PixelMap through pixel data.
151      *
152      * @param colors The pixel data.
153      * @param colorLength The length of the pixel data.
154      * @param offset The location of the pixel data.
155      * @param stride the stride.
156      * @param opts Initialization Options.
157      * @return The PixelMap.
158      */
159     NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const uint32_t *colors, uint32_t colorLength, int32_t offset,
160                                                          int32_t stride, const InitializationOptions &opts);
161     /**
162      * Create a PixelMap through pixel data.
163      *
164      * @param colors The pixel data.
165      * @param colorLength The length of the pixel data.
166      * @param offset The location of the pixel data.
167      * @param stride the stride.
168      * @param opts Initialization Options.
169      * @param useCustomFormat Use default value.
170      * @return The PixelMap.
171      */
172     NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const uint32_t *colors, uint32_t colorLength, int32_t offset,
173         int32_t stride, const InitializationOptions &opts, bool useCustomFormat);
174 
175     /**
176      * Create a PixelMap through pixel data.
177      *
178      * @param colors The pixel data.
179      * @param colorLength The length of the pixel data.
180      * @param info params.
181      * @param opts Initialization Options.
182      * @param errorCode error code.
183      * @return The PixelMap.
184      */
185     NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const uint32_t *colors, uint32_t colorLength,
186         BUILD_PARAM &info, const InitializationOptions &opts, int &errorCode);
187 
188     /**
189      * Create a PixelMap through InitializationOptions.
190      *
191      * @param opts Initialization Options.
192      * @return The PixelMap.
193      */
194     NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const InitializationOptions &opts);
195 
196     /**
197      * Create a new pixelmap using the pixelmap.
198      *
199      * @param source The source pixelmap.
200      * @param opts Initialization Options.
201      * @return The PixelMap.
202      */
203     NATIVEEXPORT static std::unique_ptr<PixelMap> Create(PixelMap &source, const InitializationOptions &opts);
204 
205     /**
206      * Create a new pixelmap using the pixelmap.
207      *
208      * @param source The source pixelmap.
209      * @param srcRect Pixel range.
210      * @param opts Initialization Options.
211      * @return The PixelMap.
212      */
213     NATIVEEXPORT static std::unique_ptr<PixelMap> Create(PixelMap &source, const Rect &srcRect,
214                                                          const InitializationOptions &opts);
215 
216     /**
217      * Create a new pixelmap using the pixelmap.
218      *
219      * @param source The source pixelmap.
220      * @param srcRect Pixel range.
221      * @param opts Initialization Options.
222      * @param errorCode error code.
223      * @return The PixelMap.
224      */
225     NATIVEEXPORT static std::unique_ptr<PixelMap> Create(PixelMap &source, const Rect &srcRect,
226         const InitializationOptions &opts, int32_t &errorCode);
227 
228     /**
229      * Create a new pixelmap using the astc.
230      *
231      * @param source The source pixelmap.
232      * @param errorCode error code.
233      * @param destFormat object format.
234      * @return The PixelMap.
235      */
236     NATIVEEXPORT static std::unique_ptr<PixelMap> ConvertFromAstc(PixelMap *source, uint32_t &errorCode,
237         PixelFormat destFormat);
238 
239     /**
240      * Set image information.
241      *
242      * @param info The objects that need to be set up.
243      * @return Success returns 0, failure returns error code.
244      */
245     NATIVEEXPORT virtual uint32_t SetImageInfo(ImageInfo &info);
246 
247     /**
248      * Set image information.
249      *
250      * @param info The objects that need to be set up.
251      * @param isReused Memory needs to be released.
252      * @return Success returns 0, failure returns error code.
253      */
254     NATIVEEXPORT virtual uint32_t SetImageInfo(ImageInfo &info, bool isReused);
255 
256     /**
257      * Obtain the pixel address through byte coordinates.
258      *
259      * @param x x-coordinate.
260      * @param y y-coordinate.
261      * @return Return to the destination address.
262      */
263     NATIVEEXPORT virtual const uint8_t *GetPixel(int32_t x, int32_t y);
264 
265     /**
266      * Obtain the pixel address through coordinates
267      *
268      * @param x x-coordinate.
269      * @param y y-coordinate.
270      * @return Return to the destination address.
271      */
272     NATIVEEXPORT virtual const uint8_t *GetPixel8(int32_t x, int32_t y);
273 
274     /**
275      * Obtain the pixel address through two-byte coordinates.
276      *
277      * @param x x-coordinate.
278      * @param y y-coordinate.
279      * @return Return to the destination address.
280      */
281     NATIVEEXPORT virtual const uint16_t *GetPixel16(int32_t x, int32_t y);
282 
283     /**
284      * Obtain the pixel address through four-byte coordinates.
285      *
286      * @param x x-coordinate.
287      * @param y y-coordinate.
288      * @return Return to the destination address.
289      */
290     NATIVEEXPORT virtual const uint32_t *GetPixel32(int32_t x, int32_t y);
291 
292     /**
293      * Get ARGB pixel points based on the coordinates.
294      *
295      * @param x x-coordinate.
296      * @param y y-coordinate.
297      * @param color The pixels that need to be obtained.
298      */
299     NATIVEEXPORT virtual bool GetARGB32Color(int32_t x, int32_t y, uint32_t &color);
300 
301     /**
302      * Get RGBA1010102 pixel points based on the coordinates.
303      *
304      * @param x x-coordinate.
305      * @param y y-coordinate.
306      * @param color The pixels that need to be obtained.
307      */
308     NATIVEEXPORT virtual bool GetRGBA1010102Color(int32_t x, int32_t y, uint32_t &color);
309 
310     /**
311      * Set pixel buffer information.
312      *
313      * @param addr Pixel address.
314      * @param context Buffer descriptor.
315      * @param size Pixel size.
316      * @param type Memory type.
317      * @param func Memory Reclaimer.
318      */
319     NATIVEEXPORT virtual void SetPixelsAddr(void *addr, void *context, uint32_t size, AllocatorType type,
320                                     CustomFreePixelMap func);
321 
322     /**
323      * Get pixel step size.
324      */
325     NATIVEEXPORT virtual int32_t GetPixelBytes();
326 
327     /**
328      * Get step length.
329      */
330     NATIVEEXPORT virtual int32_t GetRowBytes();
331 
332     /**
333      * Get the pixel length.
334      */
335     NATIVEEXPORT virtual int32_t GetByteCount();
336 
337     /**
338      * Obtain the size of the pixel buffer.
339      */
340     NATIVEEXPORT virtual uint32_t GetAllocationByteCount();
341 
342     /**
343      * Get the width of the bitmap.
344      */
345     NATIVEEXPORT virtual int32_t GetWidth();
346 
347     /**
348      * Get the height of the bitmap.
349      */
350     NATIVEEXPORT virtual int32_t GetHeight();
351 
352     /**
353      * Get the actual size of ASTC.
354      */
GetAstcRealSize(Size & size)355     NATIVEEXPORT void GetAstcRealSize(Size &size)
356     {
357         size = astcrealSize_;
358     }
359 
360     /**
361      * set the actual size of ASTC.
362      */
SetAstcRealSize(Size size)363     NATIVEEXPORT void SetAstcRealSize(Size size)
364     {
365         astcrealSize_ = size;
366     }
367 
368     /**
369      * Get the ASTC transform information.
370      */
371     NATIVEEXPORT void GetTransformData(TransformData &transformData);
372 
373     /**
374      * Set the ASTC transform information.
375      */
376     NATIVEEXPORT void SetTransformData(TransformData transformData);
377 
378     /**
379      * Get the baseDensity.
380      */
381     NATIVEEXPORT virtual int32_t GetBaseDensity();
382 
383     /**
384      * PixelMap zooming.
385      *
386      * @param xAxis X-axis scaling ratio.
387      * @param yAxis y-axis scaling ratio.
388      */
389     NATIVEEXPORT virtual void scale(float xAxis, float yAxis);
390 
391     /**
392      * PixelMap zooming.
393      *
394      * @param xAxis X-axis scaling ratio.
395      * @param yAxis y-axis scaling ratio.
396      * @param option Scaling algorithm type.
397      */
398     NATIVEEXPORT virtual void scale(float xAxis, float yAxis, const AntiAliasingOption &option);
399 
400     /**
401      * PixelMap zooming.
402      *
403      * @param xAxis X-axis scaling ratio.
404      * @param yAxis y-axis scaling ratio.
405      */
406     NATIVEEXPORT virtual bool resize(float xAxis, float yAxis);
407 
408     /**
409      * PixelMap traverse.
410      *
411      * @param xAxis X-axis scaling ratio.
412      * @param yAxis y-axis scaling ratio.
413      */
414     NATIVEEXPORT virtual void translate(float xAxis, float yAxis);
415 
416     /**
417      * PixelMap rotation.
418      *
419      * @param degrees rotation angle.
420      */
421     NATIVEEXPORT virtual void rotate(float degrees);
422 
423     /**
424      * PixelMap inversion.
425      *
426      * @param xAxis X-axis scaling ratio.
427      * @param yAxis y-axis scaling ratio.
428      */
429     NATIVEEXPORT virtual void flip(bool xAxis, bool yAxis);
430 
431     /**
432      * PixelMap crop.
433      *
434      * @param rect The area that has been cut off.
435      */
436     NATIVEEXPORT virtual uint32_t crop(const Rect &rect);
437 
438     /**
439      * Get pixelmap information.
440      */
441     NATIVEEXPORT virtual void GetImageInfo(ImageInfo &imageInfo);
442 
443     /**
444      * Get pixelmap format.
445      */
446     NATIVEEXPORT virtual PixelFormat GetPixelFormat();
447 
448     /**
449      * Get pixelmap colorspace.
450      */
451     NATIVEEXPORT virtual ColorSpace GetColorSpace();
452 
453     /**
454      * Get pixelmap alpha type.
455      */
456     NATIVEEXPORT virtual AlphaType GetAlphaType();
457 
458     /**
459      * Set pixelmap alpha.
460      */
461     NATIVEEXPORT virtual uint32_t SetAlpha(const float percent);
462 
463     /**
464      * Get the pixel address.
465      */
466     NATIVEEXPORT virtual const uint8_t *GetPixels();
467 
468     /**
469      * Obtain the A channel of the ARGB pixel point.
470      *
471      * @param color the pixel.
472      */
473     NATIVEEXPORT virtual uint8_t GetARGB32ColorA(uint32_t color);
474 
475     /**
476      * Obtain the R channel of the ARGB pixel point.
477      *
478      * @param color the pixel.
479      */
480     NATIVEEXPORT virtual uint8_t GetARGB32ColorR(uint32_t color);
481 
482     /**
483      * Obtain the G channel of the ARGB pixel point.
484      *
485      * @param color the pixel.
486      */
487     NATIVEEXPORT virtual uint8_t GetARGB32ColorG(uint32_t color);
488 
489     /**
490      * Obtain the B channel of the ARGB pixel point.
491      *
492      * @param color the pixel.
493      */
494     NATIVEEXPORT virtual uint8_t GetARGB32ColorB(uint32_t color);
495 
496     /**
497      * Pixelmap comparison function.
498      *
499      * @param other the pixel.
500      * @return Return true if they are the same, otherwise return false.
501      */
502     NATIVEEXPORT virtual bool IsSameImage(const PixelMap &other);
503 
504     /**
505      * Read the pixel buffer.
506      *
507      * @param opts RWPixelsOptions.
508      * @return Return 0 if successful, otherwise return errorcode.
509      */
510     NATIVEEXPORT virtual uint32_t ReadPixels(const RWPixelsOptions &opts);
511 
512     /**
513      * Read the pixel buffer.
514      *
515      * @param bufferSize buffer size.
516      * @param offset deviation position.
517      * @param stride stride.
518      * @param region region.
519      * @param dst To read the pixel buffer.
520      * @return Return 0 if successful, otherwise return errorcode.
521      */
522     NATIVEEXPORT virtual uint32_t ReadPixels(const uint64_t &bufferSize, const uint32_t &offset, const uint32_t &stride,
523         const Rect &region, uint8_t *dst);
524 
525     /**
526      * Read the pixel buffer.
527      *
528      * @param bufferSize buffer size.
529      * @param dst To read the pixel buffer.
530      * @return Return 0 if successful, otherwise return errorcode.
531      */
532     NATIVEEXPORT virtual uint32_t ReadPixels(const uint64_t &bufferSize, uint8_t *dst);
533 
534     /**
535      * Read the pixel information in the ARGB format.
536      *
537      * @param bufferSize buffer size.
538      * @param dst To read the pixel buffer.
539      * @return Return 0 if successful, otherwise return errorcode.
540      */
541     NATIVEEXPORT virtual uint32_t ReadARGBPixels(const uint64_t &bufferSize, uint8_t *dst);
542 
543     /**
544      * Read the pixel address at the target position.
545      *
546      * @param pos impact point.
547      * @param dst To read the pixel buffer.
548      * @return Return 0 if successful, otherwise return errorcode.
549      */
550     NATIVEEXPORT virtual uint32_t ReadPixel(const Position &pos, uint32_t &dst);
551 
552     /**
553      * ResetConfig.
554      *
555      * @param size buffer size.
556      * @param format pixel format.
557      * @return Return 0 if successful, otherwise return errorcode.
558      */
559     NATIVEEXPORT virtual uint32_t ResetConfig(const Size &size, const PixelFormat &format);
560 
561     /**
562      * Set alpha type.
563      *
564      * @param alphaType alpha type.
565      * @return Return true if successful, otherwise return false.
566      */
567     NATIVEEXPORT virtual bool SetAlphaType(const AlphaType &alphaType);
568 
569     /**
570      * Set whether to support opaque optimization.
571      *
572      * @param supportOpaqueOpt whether to support opaque optimization.
573      */
574     NATIVEEXPORT virtual void SetSupportOpaqueOpt(bool supportOpaqueOpt);
575 
576     /**
577      * Get whether to support opaque optimization.
578      *
579      * @return Return true if support opaque optimization, otherwise return false.
580      */
581     NATIVEEXPORT virtual bool GetSupportOpaqueOpt();
582 
583     /**
584      * Write pixel points at the target position.
585      *
586      * @param pos target location.
587      * @param color pixel.
588      * @return Return 0 if successful, otherwise return errorcode.
589      */
590     NATIVEEXPORT virtual uint32_t WritePixel(const Position &pos, const uint32_t &color);
591 
592     /**
593      * Write pixels at the target position.
594      *
595      * @param opts RWPixelsOptions.
596      * @return Return 0 if successful, otherwise return errorcode.
597      */
598     NATIVEEXPORT virtual uint32_t WritePixels(const RWPixelsOptions &opts);
599 
600     /**
601      * Write pixels at the target regin.
602      *
603      * @param source pixels addr.
604      * @param bufferSize bufer size.
605      * @param offset Offset point.
606      * @param stride stride.
607      * @param region region.
608      * @return Return 0 if successful, otherwise return errorcode.
609      */
610     NATIVEEXPORT virtual uint32_t WritePixels(const uint8_t *source, const uint64_t &bufferSize, const uint32_t &offset,
611                          const uint32_t &stride, const Rect &region);
612 
613     /**
614      * Write pixels.
615      *
616      * @param source pixels addr.
617      * @param bufferSize bufer size.
618      * @return Return 0 if successful, otherwise return errorcode.
619      */
620     NATIVEEXPORT virtual uint32_t WritePixels(const uint8_t *source, const uint64_t &bufferSize);
621     NATIVEEXPORT virtual bool WritePixels(const uint32_t &color);
622     /**
623      * Release the pixel buffer.
624      */
625     NATIVEEXPORT virtual void FreePixelMap();
626     /**
627      * IsStrideAlignment.
628      */
629     NATIVEEXPORT bool IsStrideAlignment();
630     /**
631      * Get memory type.
632      */
633     NATIVEEXPORT virtual AllocatorType GetAllocatorType();
634     /**
635      * Get file descriptor.
636      */
637     NATIVEEXPORT virtual void *GetFd() const;
638     NATIVEEXPORT virtual void SetFreePixelMapProc(CustomFreePixelMap func);
639     NATIVEEXPORT virtual void SetTransformered(bool isTransformered);
640     NATIVEEXPORT uint32_t ConvertAlphaFormat(PixelMap &wPixelMap, const bool isPremul);
641     NATIVEEXPORT bool AttachAddrBySurfaceBuffer();
SetPixelMapError(uint32_t code,const std::string & info)642     NATIVEEXPORT void SetPixelMapError(uint32_t code, const std::string &info)
643     {
644         errorCode = code;
645         errorInfo = info;
646     }
647 
ConstructPixelMapError(PIXEL_MAP_ERR & err,uint32_t code,const std::string & info)648     NATIVEEXPORT static void ConstructPixelMapError(PIXEL_MAP_ERR &err, uint32_t code, const std::string &info)
649     {
650         err.errorCode = code;
651         err.errorInfo = info;
652     }
653 
654     NATIVEEXPORT virtual void SetRowStride(uint32_t stride);
GetRowStride()655     NATIVEEXPORT virtual int32_t GetRowStride()
656     {
657         return rowStride_;
658     }
GetCapacity()659     NATIVEEXPORT virtual uint32_t GetCapacity()
660     {
661         return pixelsSize_;
662     }
663 
IsEditable()664     NATIVEEXPORT virtual bool IsEditable()
665     {
666         return editable_;
667     }
668 
SetModifiable(bool modifiable)669     NATIVEEXPORT virtual void SetModifiable(bool modifiable)
670     {
671         modifiable_ = modifiable;
672     }
IsModifiable()673     NATIVEEXPORT virtual bool IsModifiable()
674     {
675         return modifiable_;
676     }
677 
IsTransformered()678     NATIVEEXPORT virtual bool IsTransformered()
679     {
680         return isTransformered_;
681     }
682 
683     // judgement whether create pixelmap use source as result
IsSourceAsResponse()684     NATIVEEXPORT virtual bool IsSourceAsResponse()
685     {
686         return useSourceAsResponse_;
687     }
688 
GetWritablePixels()689     NATIVEEXPORT virtual void *GetWritablePixels() const
690     {
691         if (!const_cast<PixelMap*>(this)->AttachAddrBySurfaceBuffer()) {
692             return nullptr;
693         }
694         return static_cast<void *>(data_);
695     }
696 
GetUniqueId()697     NATIVEEXPORT virtual uint32_t GetUniqueId() const
698     {
699         return uniqueId_;
700     }
701 
702     /**
703      * Serialize the pixelmap into a parcel.
704      */
705     NATIVEEXPORT virtual bool Marshalling(Parcel &data) const override;
706     /**
707      * Deserialize the parcel to generate the pixelmap.
708      */
709     NATIVEEXPORT static PixelMap *UnmarshallingWithIsDisplay(Parcel &parcel,
710         std::function<int(Parcel &parcel, std::function<int(Parcel&)> readFdDefaultFunc)> readSafeFdFunc,
711         bool isDisplay = false);
712     NATIVEEXPORT static PixelMap *Unmarshalling(Parcel &data,
713         std::function<int(Parcel &parcel, std::function<int(Parcel&)> readFdDefaultFunc)> readSafeFdFunc = nullptr);
714     NATIVEEXPORT static PixelMap *Unmarshalling(Parcel &parcel, PIXEL_MAP_ERR &error,
715         std::function<int(Parcel &parcel, std::function<int(Parcel&)> readFdDefaultFunc)> readSafeFdFunc = nullptr,
716         bool isDisplay = false);
717     /**
718      * Serialize the pixelmap into a vector in TLV format.
719      */
720     NATIVEEXPORT virtual bool EncodeTlv(std::vector<uint8_t> &buff) const;
721     /**
722      * Deserialize the vector data in the form of TLV to generate a pixelmap.
723      */
724     NATIVEEXPORT static PixelMap *DecodeTlv(std::vector<uint8_t> &buff);
SetImageYUVInfo(YUVDataInfo & yuvinfo)725     NATIVEEXPORT virtual void SetImageYUVInfo(YUVDataInfo &yuvinfo)
726     {
727         yuvDataInfo_ = yuvinfo;
728     }
729     NATIVEEXPORT virtual void AssignYuvDataOnType(PixelFormat format, int32_t width, int32_t height);
730     NATIVEEXPORT virtual void UpdateYUVDataInfo(PixelFormat format, int32_t width, int32_t height,
731         YUVStrideInfo &strides);
GetImageYUVInfo(YUVDataInfo & yuvInfo)732     NATIVEEXPORT virtual void GetImageYUVInfo(YUVDataInfo &yuvInfo) const
733     {
734         yuvInfo = yuvDataInfo_;
735     }
736 #ifdef IMAGE_COLORSPACE_FLAG
737     // -------[inner api for ImageSource/ImagePacker codec] it will get a colorspace object pointer----begin----
738     NATIVEEXPORT void InnerSetColorSpace(const OHOS::ColorManager::ColorSpace &grColorSpace, bool direct = false);
739     NATIVEEXPORT OHOS::ColorManager::ColorSpace InnerGetGrColorSpace();
InnerGetGrColorSpacePtr()740     NATIVEEXPORT std::shared_ptr<OHOS::ColorManager::ColorSpace> InnerGetGrColorSpacePtr()
741     {
742         return grColorSpace_;
743     }
744     /**
745      * ApplyColorSpace.
746      */
747     NATIVEEXPORT virtual uint32_t ApplyColorSpace(const OHOS::ColorManager::ColorSpace &grColorSpace);
748     // -------[inner api for ImageSource/ImagePacker codec] it will get a colorspace object pointer----end-------
749 #endif
750 
751 #ifdef IMAGE_PURGEABLE_PIXELMAP
IsPurgeable()752     NATIVEEXPORT bool IsPurgeable() const
753     {
754         return purgeableMemPtr_ != nullptr;
755     }
756 
GetPurgeableMemPtr()757     NATIVEEXPORT std::shared_ptr<PurgeableMem::PurgeableMemBase> GetPurgeableMemPtr() const
758     {
759         return purgeableMemPtr_;
760     }
761 
SetPurgeableMemPtr(std::shared_ptr<PurgeableMem::PurgeableMemBase> pmPtr)762     NATIVEEXPORT void SetPurgeableMemPtr(std::shared_ptr<PurgeableMem::PurgeableMemBase> pmPtr)
763     {
764         purgeableMemPtr_ = pmPtr;
765     }
766 #endif
767 
IsAstc()768     NATIVEEXPORT bool IsAstc()
769     {
770         return isAstc_;
771     }
772 
SetAstc(bool isAstc)773     NATIVEEXPORT void SetAstc(bool isAstc)
774     {
775         isAstc_ = isAstc;
776     }
777 
GetExifMetadata()778     NATIVEEXPORT std::shared_ptr<ExifMetadata> GetExifMetadata()
779     {
780         return exifMetadata_;
781     }
782 
SetExifMetadata(std::shared_ptr<ExifMetadata> & ptr)783     NATIVEEXPORT void SetExifMetadata(std::shared_ptr<ExifMetadata> &ptr)
784     {
785         exifMetadata_ = ptr;
786     }
787 
788     NATIVEEXPORT uint32_t GetImagePropertyInt(const std::string &key, int32_t &value);
789     NATIVEEXPORT uint32_t GetImagePropertyString(const std::string &key, std::string &value);
790     NATIVEEXPORT uint32_t ModifyImageProperty(const std::string &key, const std::string &value);
791     NATIVEEXPORT uint32_t SetMemoryName(const std::string &pixelMapName);
792     NATIVEEXPORT virtual std::unique_ptr<PixelMap> Clone(int32_t &errorCode);
793 
794     NATIVEEXPORT bool IsHdr();
795     NATIVEEXPORT void SetAstcHdr(bool astcHdr);
796     NATIVEEXPORT uint32_t ToSdr();
797     // format support rgba8888, nv12, nv21. The default value is rgba8888
798     // If toSRGB is false, pixelmap will be converted to display_p3
799     NATIVEEXPORT uint32_t ToSdr(PixelFormat format, bool toSRGB);
800     // use for hdr pixelmap, If isSRGB is false, the colorspace is p3 when converting to SDR.
801     NATIVEEXPORT void SetToSdrColorSpaceIsSRGB(bool isSRGB);
802     NATIVEEXPORT bool GetToSdrColorSpaceIsSRGB();
803 
GetHdrMetadata()804     NATIVEEXPORT std::shared_ptr<HdrMetadata> GetHdrMetadata()
805     {
806         return hdrMetadata_;
807     }
808 
SetHdrMetadata(const std::shared_ptr<HdrMetadata> & metadata)809     NATIVEEXPORT void SetHdrMetadata(const std::shared_ptr<HdrMetadata> &metadata)
810     {
811         hdrMetadata_ = metadata;
812     }
813 
GetHdrType()814     NATIVEEXPORT ImageHdrType GetHdrType()
815     {
816         return hdrType_;
817     }
818 
SetHdrType(ImageHdrType hdrType)819     NATIVEEXPORT void SetHdrType(ImageHdrType hdrType)
820     {
821         hdrType_ = hdrType;
822     }
823 
824     // unmap方案, 减少RenderService内存占用
825     NATIVEEXPORT bool UnMap();
826     NATIVEEXPORT bool ReMap();
IsUnMap()827     NATIVEEXPORT bool IsUnMap()
828     {
829         std::lock_guard<std::mutex> lock(*unmapMutex_);
830         return isUnMap_;
831     }
IncreaseUseCount()832     NATIVEEXPORT void IncreaseUseCount()
833     {
834         std::lock_guard<std::mutex> lock(*unmapMutex_);
835         useCount_ += 1;
836     }
DecreaseUseCount()837     NATIVEEXPORT void DecreaseUseCount()
838     {
839         std::lock_guard<std::mutex> lock(*unmapMutex_);
840         if (useCount_ > 0) {
841             useCount_ -= 1;
842         }
843     }
GetUseCount()844     NATIVEEXPORT uint64_t GetUseCount()
845     {
846         std::lock_guard<std::mutex> lock(*unmapMutex_);
847         return useCount_;
848     }
849 
850     // pixelmap with DMA memory should be marked dirty when memory was changed
MarkDirty()851     NATIVEEXPORT void MarkDirty()
852     {
853         isMemoryDirty_ = true;
854     }
855 
IsMemoryDirty()856     NATIVEEXPORT bool IsMemoryDirty()
857     {
858         return isMemoryDirty_;
859     }
860 
SetEditable(bool editable)861     NATIVEEXPORT void SetEditable(bool editable)
862     {
863         editable_ = editable;
864     }
865 
866     static int32_t GetRGBxRowDataSize(const ImageInfo& info);
867     static int32_t GetRGBxByteCount(const ImageInfo& info);
868     static int32_t GetYUVByteCount(const ImageInfo& info);
869     static int32_t GetAllocatedByteCount(const ImageInfo& info);
870 
871     NATIVEEXPORT uint32_t GetVersionId();
872     NATIVEEXPORT void AddVersionId();
873     void UpdatePixelsAlphaType();
874     uint64_t GetNoPaddingUsage();
875 
876 protected:
877     static constexpr uint8_t TLV_VARINT_BITS = 7;
878     static constexpr uint8_t TLV_VARINT_MASK = 0x7F;
879     static constexpr uint8_t TLV_VARINT_MORE = 0x80;
880     static constexpr uint8_t TLV_END = 0x00;
881     static constexpr uint8_t TLV_IMAGE_WIDTH = 0x01;
882     static constexpr uint8_t TLV_IMAGE_HEIGHT = 0x02;
883     static constexpr uint8_t TLV_IMAGE_PIXELFORMAT = 0x03;
884     static constexpr uint8_t TLV_IMAGE_COLORSPACE = 0x04;
885     static constexpr uint8_t TLV_IMAGE_ALPHATYPE = 0x05;
886     static constexpr uint8_t TLV_IMAGE_BASEDENSITY = 0x06;
887     static constexpr uint8_t TLV_IMAGE_ALLOCATORTYPE = 0x07;
888     static constexpr uint8_t TLV_IMAGE_DATA = 0x08;
889     static constexpr size_t MAX_IMAGEDATA_SIZE = 128 * 1024 * 1024; // 128M
890     static constexpr size_t MIN_IMAGEDATA_SIZE = 32 * 1024;         // 32k
891     friend class ImageSource;
892     friend class OHOS::Rosen::PixelMapStorage;
893     friend class OHOS::Rosen::RSMarshallingHelper;
894     friend class OHOS::Rosen::RSProfiler;
895     static bool ALPHA8ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount);
896     static bool RGB565ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount);
897     static bool ARGB8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount);
898     static bool RGBA8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount);
899     static bool BGRA8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount);
900     static bool RGB888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount);
901     static bool CheckParams(const uint32_t *colors, uint32_t colorLength, int32_t offset, int32_t stride,
902         const InitializationOptions &opts);
903     static void UpdatePixelsAlpha(const AlphaType &alphaType, const PixelFormat &pixelFormat, uint8_t *dstPixels,
904         PixelMap &dstPixelMap);
905     static void InitDstImageInfo(const InitializationOptions &opts, const ImageInfo &srcImageInfo,
906         ImageInfo &dstImageInfo);
907     static bool CopyPixMapToDst(PixelMap &source, void* &dstPixels, int &fd, uint32_t bufferSize);
908     static bool CopyPixelMap(PixelMap &source, PixelMap &dstPixelMap, int32_t &error);
909     static bool CopyPixelMap(PixelMap &source, PixelMap &dstPixelMap);
910     static bool SourceCropAndConvert(PixelMap &source, const ImageInfo &srcImageInfo, const ImageInfo &dstImageInfo,
911                                      const Rect &srcRect, PixelMap &dstPixelMap);
912     static bool IsSameSize(const Size &src, const Size &dst);
913     static bool ScalePixelMap(const Size &targetSize, const Size &dstSize, const ScaleMode &scaleMode,
914                               PixelMap &dstPixelMap);
915     static bool IsYuvFormat(PixelFormat format);
916     bool GetPixelFormatDetail(const PixelFormat format);
917     uint32_t CheckAlphaFormatInput(PixelMap &wPixelMap, const bool isPremul);
918     bool CheckPixelsInput(const uint8_t *dst, const uint64_t &bufferSize, const uint32_t &offset,
919                           const uint32_t &stride, const Rect &region);
920     void ReleaseSharedMemory(void *addr, void *context, uint32_t size);
921     static void ReleaseBuffer(AllocatorType allocatorType, int fd, uint64_t dataSize, void **buffer);
922     static void *AllocSharedMemory(const uint64_t bufferSize, int &fd, uint32_t uniqueId);
923     bool WritePropertiesToParcel(Parcel &parcel) const;
924     static bool ReadPropertiesFromParcel(Parcel& parcel, PixelMap*& pixelMap, ImageInfo& imgInfo,
925         PixelMemInfo& memInfo);
926     bool ReadBufferSizeFromParcel(Parcel& parcel, const ImageInfo& imgInfo, PixelMemInfo& memInfo,
927         PIXEL_MAP_ERR& error);
928     bool WriteMemInfoToParcel(Parcel &parcel, const int32_t &bufferSize) const;
929     static bool ReadMemInfoFromParcel(Parcel &parcel, PixelMemInfo &pixelMemInfo, PIXEL_MAP_ERR &error,
930         std::function<int(Parcel &parcel, std::function<int(Parcel&)> readFdDefaultFunc)> readSafeFdFunc = nullptr,
931         bool isDisplay = false);
932     bool WriteTransformDataToParcel(Parcel &parcel) const;
933     bool ReadTransformData(Parcel &parcel, PixelMap *pixelMap);
934     bool WriteAstcInfoToParcel(Parcel &parcel) const;
935     bool ReadAstcInfo(Parcel &parcel, PixelMap *pixelMap);
936     bool WriteYuvDataInfoToParcel(Parcel &parcel) const;
937     bool ReadYuvDataInfoFromParcel(Parcel &parcel, PixelMap *pixelMap);
938     uint32_t SetRowDataSizeForImageInfo(ImageInfo info);
939 
ResetPixelMap()940     void ResetPixelMap()
941     {
942         rowDataSize_ = 0;
943         pixelBytes_ = 0;
944         colorProc_ = nullptr;
945     }
946 
CheckValidParam(int32_t x,int32_t y)947     bool CheckValidParam(int32_t x, int32_t y)
948     {
949         return isUnMap_ || data_ == nullptr || x >= imageInfo_.size.width || x < 0 || y >= imageInfo_.size.height ||
950             y < 0 || (pixelsSize_ < static_cast<uint64_t>(rowDataSize_) * imageInfo_.size.height) ? false : true;
951     }
952 
953     static PixelMap *StartUnmarshalling(Parcel &parcel, ImageInfo &imgInfo,
954         PixelMemInfo &pixelMemInfo, PIXEL_MAP_ERR &error);
955     static PixelMap *FinishUnmarshalling(PixelMap* pixelMap, Parcel &parcel,
956         ImageInfo &imgInfo, PixelMemInfo &pixelMemInfo, PIXEL_MAP_ERR &error);
957 
958     static void ReleaseMemory(AllocatorType allocType, void *addr, void *context, uint32_t size);
959     static bool UpdatePixelMapMemInfo(PixelMap *pixelMap, ImageInfo &imgInfo, PixelMemInfo &pixelMemInfo);
960     bool WriteImageData(Parcel &parcel, size_t size) const;
961     bool WriteAshmemDataToParcel(Parcel &parcel, size_t size) const;
962     static uint8_t *ReadImageData(Parcel &parcel, int32_t size,
963         std::function<int(Parcel &parcel, std::function<int(Parcel&)> readFdDefaultFunc)> readSafeFdFunc = nullptr);
964     static uint8_t *ReadHeapDataFromParcel(Parcel &parcel, int32_t bufferSize);
965     static uint8_t *ReadAshmemDataFromParcel(Parcel &parcel, int32_t bufferSize,
966         std::function<int(Parcel &parcel, std::function<int(Parcel&)> readFdDefaultFunc)> readSafeFdFunc = nullptr);
967     static int ReadFileDescriptor(Parcel &parcel);
968     static bool WriteFileDescriptor(Parcel &parcel, int fd);
969     static bool ReadImageInfo(Parcel &parcel, ImageInfo &imgInfo);
970     bool WriteImageInfo(Parcel &parcel) const;
971     void WriteUint8(std::vector<uint8_t> &buff, uint8_t value) const;
972     static uint8_t ReadUint8(std::vector<uint8_t> &buff, int32_t &cursor);
973     uint8_t GetVarintLen(int32_t value) const;
974     void WriteVarint(std::vector<uint8_t> &buff, int32_t value) const;
975     static int32_t ReadVarint(std::vector<uint8_t> &buff, int32_t &cursor);
976     void WriteData(std::vector<uint8_t> &buff, const uint8_t *data,
977         const int32_t &height, const int32_t &rowDataSize, const int32_t &rowStride) const;
978     static uint8_t *ReadData(std::vector<uint8_t> &buff, int32_t size, int32_t &cursor);
979     static bool ReadTlvAttr(std::vector<uint8_t> &buff, ImageInfo &info, int32_t &type, int32_t &size, uint8_t **data);
980     bool DoTranslation(TransInfos &infos, const AntiAliasingOption &option = AntiAliasingOption::NONE);
981     void UpdateImageInfo();
982     bool IsYuvFormat() const;
983     static int32_t ConvertPixelAlpha(const void *srcPixels, const int32_t srcLength, const ImageInfo &srcInfo,
984         void *dstPixels, const ImageInfo &dstInfo);
985     void CopySurfaceBufferInfo(void *data);
986     void SetVersionId(uint32_t versionId);
987     std::unique_ptr<AbsMemory> CreateSdrMemory(ImageInfo &imageInfo, PixelFormat format,
988                                                AllocatorType dstType, uint32_t &errorCode, bool toSRGB);
989     // used to close fd after mmap in RenderService when memory type is shared-mem or dma.
990     bool CloseFd();
991     uint32_t CheckPixelMapForWritePixels();
992 
993     uint8_t *data_ = nullptr;
994     // this info SHOULD be the final info for decoded pixelmap, not the original image info
995     ImageInfo imageInfo_;
996     int32_t rowDataSize_ = 0;
997     int32_t rowStride_ = 0;
998     int32_t pixelBytes_ = 0;
999     TransColorProc colorProc_ = nullptr;
1000     void *context_ = nullptr;
1001     CustomFreePixelMap custFreePixelMap_ = nullptr;
1002     CustomFreePixelMap freePixelMapProc_ = nullptr;
1003     AllocatorType allocatorType_ = AllocatorType::SHARE_MEM_ALLOC;
1004     uint32_t pixelsSize_ = 0;
1005     bool editable_ = false;
1006     bool modifiable_ = true; // If this is set to false, any modifications to the pixels data is not allowed
1007     bool useSourceAsResponse_ = false;
1008     bool isTransformered_ = false;
1009     std::shared_ptr<std::mutex> transformMutex_ = std::make_shared<std::mutex>();
1010 
1011     // only used by rosen backend
1012     uint32_t uniqueId_ = 0;
1013     bool isAstc_ = false;
1014     TransformData transformData_ = {1, 1, 0, 0, 0, 0, 0, 0, 0, false, false};
1015     Size astcrealSize_;
1016     std::shared_ptr<HdrMetadata> hdrMetadata_ = nullptr;
1017     ImageHdrType hdrType_ = static_cast<ImageHdrType>(0);
1018 
1019 #ifdef IMAGE_COLORSPACE_FLAG
1020     std::shared_ptr<OHOS::ColorManager::ColorSpace> grColorSpace_ = nullptr;
1021 #else
1022     std::shared_ptr<uint8_t> grColorSpace_ = nullptr;
1023 #endif
1024 
1025 #ifdef IMAGE_PURGEABLE_PIXELMAP
1026     std::shared_ptr<PurgeableMem::PurgeableMemBase> purgeableMemPtr_ = nullptr;
1027 #else
1028     std::shared_ptr<uint8_t> purgeableMemPtr_ = nullptr;
1029 #endif
1030     YUVDataInfo yuvDataInfo_;
1031     std::shared_ptr<ExifMetadata> exifMetadata_ = nullptr;
1032     std::shared_ptr<std::mutex> metadataMutex_ = std::make_shared<std::mutex>();
1033     std::shared_ptr<std::mutex> translationMutex_ = std::make_shared<std::mutex>();
1034     std::shared_ptr<std::shared_mutex> colorSpaceMutex_ = std::make_shared<std::shared_mutex>();
1035     bool toSdrColorIsSRGB_ = false;
1036     uint32_t versionId_ = 1;
1037     std::shared_ptr<std::shared_mutex> versionMutex_ = std::make_shared<std::shared_mutex>();
1038 private:
IsDisplayOnly()1039     NATIVEEXPORT bool IsDisplayOnly()
1040     {
1041         return displayOnly_;
1042     }
1043 
SetDisplayOnly(bool displayOnly)1044     NATIVEEXPORT void SetDisplayOnly(bool displayOnly)
1045     {
1046         displayOnly_ = displayOnly;
1047     }
1048 
SetReadVersion(int32_t version)1049     NATIVEEXPORT void SetReadVersion(int32_t version)
1050     {
1051         readVersion_ = version;
1052     }
1053 
GetReadVersion()1054     NATIVEEXPORT int32_t GetReadVersion()
1055     {
1056         return readVersion_;
1057     }
1058 
1059     // unmap方案, 减少RenderService内存占用
1060     bool isUnMap_ = false;
1061     uint64_t useCount_ = 0ULL;
1062     std::shared_ptr<std::mutex> unmapMutex_ = std::make_shared<std::mutex>();
1063 
1064     // used to mark whether DMA memory should be refreshed
1065     mutable bool isMemoryDirty_ = false;
1066 
1067     // pixelmap versioning added since 16th of April 2025
1068     int32_t readVersion_ = PIXELMAP_VERSION_LATEST;
1069     bool displayOnly_ = false;
1070     bool astcHdr_ = false;
1071 
1072     friend class OHOS::Rosen::RSModifiersDraw;
1073     bool supportOpaqueOpt_ = false;
1074 };
1075 } // namespace Media
1076 } // namespace OHOS
1077 
1078 #endif // INTERFACES_INNERKITS_INCLUDE_PIXEL_MAP_H_
1079