Merge "Fix comments of encoded image buffer" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / native-image-source-impl.h
1 #ifndef DALI_INTERNAL_NATIVE_IMAGE_SOURCE_IMPL_H
2 #define DALI_INTERNAL_NATIVE_IMAGE_SOURCE_IMPL_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/bitmap-saver.h>
23 #include <dali/public-api/adaptor-framework/native-image-source.h>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29 namespace Adaptor
30 {
31 /**
32  * Dali internal NativeImageSource.
33  */
34 class NativeImageSource
35 {
36 public:
37   static constexpr uint32_t DEFAULT_QUALITY = 100;
38
39   /**
40    * Create a new NativeImageSource internally.
41    * Depending on hardware the width and height may have to be a power of two.
42    * @param[in] width The width of the image.
43    * @param[in] height The height of the image.
44    * @param[in] depth color depth of the image.
45    * @param[in] nativeImageSource contains either: pixmap of type X11 Pixmap , a Ecore_X_Pixmap or is empty
46    * @return A smart-pointer to a newly allocated image.
47    */
48   static NativeImageSource* New(uint32_t                            width,
49                                 uint32_t                            height,
50                                 Dali::NativeImageSource::ColorDepth depth,
51                                 Any                                 nativeImageSource);
52   /**
53    * @copydoc Dali::NativeImageSource::GetNativeImageSource()
54    */
55   virtual Any GetNativeImageSource() const = 0;
56
57   /**
58    * @copydoc Dali::NativeImageSource::GetPixels()
59    */
60   virtual bool GetPixels(std::vector<unsigned char>& pixbuf, uint32_t& width, uint32_t& height, Pixel::Format& pixelFormat) const = 0;
61
62   /**
63    * @copydoc Dali::NativeImageSource::SetSource( Any source )
64    */
65   virtual void SetSource(Any source) = 0;
66
67   /**
68    * @copydoc Dali::NativeImageSource::IsColorDepthSupported( ColorDepth colorDepth )
69    */
70   virtual bool IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth) = 0;
71
72   /**
73    * destructor
74    */
75   virtual ~NativeImageSource() = default;
76
77   /**
78    * @copydoc Dali::NativeImageSource::CreateResource()
79    */
80   virtual bool CreateResource() = 0;
81
82   /**
83    * @copydoc Dali::NativeImageSource::DestroyResource()
84    */
85   virtual void DestroyResource() = 0;
86
87   /**
88    * @copydoc Dali::NativeImageSource::TargetTexture()
89    */
90   virtual uint32_t TargetTexture() = 0;
91
92   /**
93    * @copydoc Dali::NativeImageSource::PrepareTexture()
94    */
95   virtual void PrepareTexture() = 0;
96
97   /**
98    * @copydoc Dali::NativeImageSource::GetWidth()
99    */
100   virtual uint32_t GetWidth() const = 0;
101
102   /**
103    * @copydoc Dali::NativeImageSource::GetHeight()
104    */
105   virtual uint32_t GetHeight() const = 0;
106
107   /**
108    * @copydoc Dali::NativeImageSource::RequiresBlending()
109    */
110   virtual bool RequiresBlending() const = 0;
111
112   /**
113    * @copydoc Dali::NativeImageSource::GetTextureTarget()
114    */
115   virtual int GetTextureTarget() const = 0;
116
117   /**
118    * @copydoc Dali::NativeImageSource::GetCustomFragmentPrefix()
119    */
120   virtual const char* GetCustomFragmentPrefix() const = 0;
121
122   /**
123    * @copydoc Dali::NativeImageSource::ApplyNativeFragmentShader()
124    */
125   virtual bool ApplyNativeFragmentShader(std::string& shader) = 0;
126
127   /**
128    * @copydoc Dali::NativeImageSource::GetCustomSamplerTypename()
129    */
130   virtual const char* GetCustomSamplerTypename() const = 0;
131
132   /**
133    * @copydoc Dali::NativeImageSource::GetNativeImageHandle()
134    */
135   virtual Any GetNativeImageHandle() const = 0;
136
137   /**
138    * @copydoc Dali::NativeImageSource::SourceChanged()
139    */
140   virtual bool SourceChanged() const = 0;
141
142   /**
143    * @copydoc Dali::NativeImageInterface::GetExtension()
144    */
145   virtual NativeImageInterface::Extension* GetNativeImageInterfaceExtension() = 0;
146
147   /**
148    * @brief Dali::DevelNativeImageSource::AcquireBuffer()
149    */
150   virtual uint8_t* AcquireBuffer(uint16_t& width, uint16_t& height, uint16_t& stride) = 0;
151
152   /**
153    * @brief Dali::DevelNativeImageSource::ReleaseBuffer()
154    */
155   virtual bool ReleaseBuffer() = 0;
156
157   /**
158    * @copydoc Dali::NativeImageSource::EncodeToFile(const std::string& )
159    */
160   inline bool EncodeToFile(const std::string& filename) const
161   {
162     return EncodeToFile(filename, DEFAULT_QUALITY);
163   }
164
165   /**
166    * @brief Converts the current pixel contents to either a JPEG or PNG format
167    * and write that to the filesystem.
168    *
169    * @param[in] filename Identify the filesystem location at which to write the encoded image.
170    *                     The extension determines the encoding used.
171    *                     The two valid encoding are (".jpeg"|".jpg") and ".png".
172    * @param[in] quality The quality of encoded jpeg image
173    * @return    @c true if the pixels were written, and @c false otherwise
174    */
175   inline bool EncodeToFile(const std::string& filename, const uint32_t quality) const
176   {
177     std::vector<uint8_t> pixbuf;
178     uint32_t             width(0), height(0);
179     Pixel::Format        pixelFormat;
180
181     if(GetPixels(pixbuf, width, height, pixelFormat))
182     {
183       return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height, quality);
184     }
185     return false;
186   }
187
188 public:
189   inline static Internal::Adaptor::NativeImageSource& GetImplementation(Dali::NativeImageSource& image)
190   {
191     return *image.mImpl;
192   }
193 };
194
195 } // namespace Adaptor
196
197 } // namespace Internal
198
199 } // namespace Dali
200
201 #endif // DALI_INTERNAL_NATIVE_IMAGE_SOURCE_IMPL_H