14ed2deddSopenharmony_ci/* 24ed2deddSopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 34ed2deddSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44ed2deddSopenharmony_ci * you may not use this file except in compliance with the License. 54ed2deddSopenharmony_ci * You may obtain a copy of the License at 64ed2deddSopenharmony_ci * 74ed2deddSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84ed2deddSopenharmony_ci * 94ed2deddSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104ed2deddSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114ed2deddSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124ed2deddSopenharmony_ci * See the License for the specific language governing permissions and 134ed2deddSopenharmony_ci * limitations under the License. 144ed2deddSopenharmony_ci */ 154ed2deddSopenharmony_ci 164ed2deddSopenharmony_ci/** 174ed2deddSopenharmony_ci * @addtogroup Surface 184ed2deddSopenharmony_ci * @{ 194ed2deddSopenharmony_ci * 204ed2deddSopenharmony_ci * @brief Provides the capabilities of applying for and releasing shared memory in multimedia and graphics scenarios. 214ed2deddSopenharmony_ci * 224ed2deddSopenharmony_ci * @since 1.0 234ed2deddSopenharmony_ci * @version 1.0 244ed2deddSopenharmony_ci */ 254ed2deddSopenharmony_ci 264ed2deddSopenharmony_ci/** 274ed2deddSopenharmony_ci * @file surface.h 284ed2deddSopenharmony_ci * 294ed2deddSopenharmony_ci * @brief Provides the capabilities of applying for and releasing shared memory in multimedia and graphics scenarios. 304ed2deddSopenharmony_ci * 314ed2deddSopenharmony_ci * By default, a surface can apply for a shared buffer. You can call {@link SetQueueSize} to set the maximum number 324ed2deddSopenharmony_ci * of buffers that can be applied for. \n 334ed2deddSopenharmony_ci * 344ed2deddSopenharmony_ci * @since 1.0 354ed2deddSopenharmony_ci * @version 1.0 364ed2deddSopenharmony_ci */ 374ed2deddSopenharmony_ci 384ed2deddSopenharmony_ci#ifndef GRAPHIC_LITE_SURFACE_H 394ed2deddSopenharmony_ci#define GRAPHIC_LITE_SURFACE_H 404ed2deddSopenharmony_ci 414ed2deddSopenharmony_ci#include "ibuffer_consumer_listener.h" 424ed2deddSopenharmony_ci#include "surface_buffer.h" 434ed2deddSopenharmony_ci#include "surface_type.h" 444ed2deddSopenharmony_ci 454ed2deddSopenharmony_cinamespace OHOS { 464ed2deddSopenharmony_ci/** 474ed2deddSopenharmony_ci * @brief Defines the consumer listener used to notify consumers when the surface status is updated. 484ed2deddSopenharmony_ci * 494ed2deddSopenharmony_ci * @since 1.0 504ed2deddSopenharmony_ci * @version 1.0 514ed2deddSopenharmony_ci */ 524ed2deddSopenharmony_ciclass Surface { 534ed2deddSopenharmony_cipublic: 544ed2deddSopenharmony_ci /** 554ed2deddSopenharmony_ci * @brief A constructor used to create a {@link Surface} object for consumers to use. 564ed2deddSopenharmony_ci * 574ed2deddSopenharmony_ci * In multi-process scenarios, this function is provided for consumers to obtain buffers generated by producers 584ed2deddSopenharmony_ci * for consumption. \n 594ed2deddSopenharmony_ci * In single-process scenarios, this function can be used by both consumers and producers. \n 604ed2deddSopenharmony_ci * 614ed2deddSopenharmony_ci * @since 1.0 624ed2deddSopenharmony_ci * @version 1.0 634ed2deddSopenharmony_ci */ 644ed2deddSopenharmony_ci static Surface* CreateSurface(); 654ed2deddSopenharmony_ci 664ed2deddSopenharmony_ci /** 674ed2deddSopenharmony_ci * @brief A destructor used to delete the <b>Surface</b> instance. 684ed2deddSopenharmony_ci * 694ed2deddSopenharmony_ci * This function releases the surface and all buffers applied for the surface. 704ed2deddSopenharmony_ci * 714ed2deddSopenharmony_ci * @since 1.0 724ed2deddSopenharmony_ci * @version 1.0 734ed2deddSopenharmony_ci */ 744ed2deddSopenharmony_ci virtual ~Surface(); 754ed2deddSopenharmony_ci 764ed2deddSopenharmony_ci /** 774ed2deddSopenharmony_ci * @brief Sets the number of buffers that can be allocated to the surface. 784ed2deddSopenharmony_ci * The default value is <b>1</b>. The value range is [1, 10]. 794ed2deddSopenharmony_ci * 804ed2deddSopenharmony_ci * @param queueSize Indicates the number of buffers to set. 814ed2deddSopenharmony_ci * @since 1.0 824ed2deddSopenharmony_ci * @version 1.0 834ed2deddSopenharmony_ci */ 844ed2deddSopenharmony_ci virtual void SetQueueSize(uint8_t queueSize) = 0; 854ed2deddSopenharmony_ci 864ed2deddSopenharmony_ci /** 874ed2deddSopenharmony_ci * @brief Obtains the number of surface buffers that can be allocated to the surface. 884ed2deddSopenharmony_ci * The default value is <b>1</b>. The value range is [1, 10]. 894ed2deddSopenharmony_ci * 904ed2deddSopenharmony_ci * @return Returns the number of buffers. 914ed2deddSopenharmony_ci * @since 1.0 924ed2deddSopenharmony_ci * @version 1.0 934ed2deddSopenharmony_ci */ 944ed2deddSopenharmony_ci virtual uint8_t GetQueueSize() = 0; 954ed2deddSopenharmony_ci 964ed2deddSopenharmony_ci /** 974ed2deddSopenharmony_ci * @brief Sets the width and height of the surface for calculating its stride and size. 984ed2deddSopenharmony_ci * The default value range of width and height is (0,7680]. 994ed2deddSopenharmony_ci * 1004ed2deddSopenharmony_ci * @param width Indicates the surface width, in pixels. 1014ed2deddSopenharmony_ci * @param height Indicates the surface height, in pixels. 1024ed2deddSopenharmony_ci * @since 1.0 1034ed2deddSopenharmony_ci * @version 1.0 1044ed2deddSopenharmony_ci */ 1054ed2deddSopenharmony_ci virtual void SetWidthAndHeight(uint32_t width, uint32_t height) = 0; 1064ed2deddSopenharmony_ci 1074ed2deddSopenharmony_ci /** 1084ed2deddSopenharmony_ci * @brief Obtains the width of the surface. 1094ed2deddSopenharmony_ci * 1104ed2deddSopenharmony_ci * @return Returns the surface width, in pixels. 1114ed2deddSopenharmony_ci * @since 1.0 1124ed2deddSopenharmony_ci * @version 1.0 */ 1134ed2deddSopenharmony_ci virtual uint32_t GetWidth() = 0; 1144ed2deddSopenharmony_ci 1154ed2deddSopenharmony_ci /** 1164ed2deddSopenharmony_ci * @brief Obtains the height of the surface. 1174ed2deddSopenharmony_ci * 1184ed2deddSopenharmony_ci * @return Returns the surface height, in pixels. 1194ed2deddSopenharmony_ci * @since 1.0 1204ed2deddSopenharmony_ci * @version 1.0 1214ed2deddSopenharmony_ci */ 1224ed2deddSopenharmony_ci virtual uint32_t GetHeight() = 0; 1234ed2deddSopenharmony_ci 1244ed2deddSopenharmony_ci /** 1254ed2deddSopenharmony_ci * @brief Sets the pixel format of the surface. For details, see {@link ImageFormat}. 1264ed2deddSopenharmony_ci * The default pixel format is {@link IMAGE_PIXEL_FORMAT_RGB565}. 1274ed2deddSopenharmony_ci * 1284ed2deddSopenharmony_ci * 1294ed2deddSopenharmony_ci * @param format Indicates the pixel format to be set. 1304ed2deddSopenharmony_ci * @since 1.0 1314ed2deddSopenharmony_ci * @version 1.0 1324ed2deddSopenharmony_ci */ 1334ed2deddSopenharmony_ci virtual void SetFormat(uint32_t format) = 0; 1344ed2deddSopenharmony_ci 1354ed2deddSopenharmony_ci /** 1364ed2deddSopenharmony_ci * @brief Obtains the pixel format of the surface. For details, see {@link ImageFormat}. 1374ed2deddSopenharmony_ci * The default pixel format is {@link IMAGE_PIXEL_FORMAT_RGB565}. 1384ed2deddSopenharmony_ci * 1394ed2deddSopenharmony_ci * @return Returns the pixel format. 1404ed2deddSopenharmony_ci * @since 1.0 1414ed2deddSopenharmony_ci * @version 1.0 1424ed2deddSopenharmony_ci */ 1434ed2deddSopenharmony_ci virtual uint32_t GetFormat() = 0; 1444ed2deddSopenharmony_ci 1454ed2deddSopenharmony_ci /** 1464ed2deddSopenharmony_ci * @brief Sets the number of bytes for stride alignment. 1474ed2deddSopenharmony_ci * 1484ed2deddSopenharmony_ci * By default, 4-byte aligned is used. The value range is [4,32]. 1494ed2deddSopenharmony_ci * 1504ed2deddSopenharmony_ci * @param strideAlignment Indicates the number of bytes for stride alignment. 1514ed2deddSopenharmony_ci * @since 1.0 1524ed2deddSopenharmony_ci * @version 1.0 1534ed2deddSopenharmony_ci */ 1544ed2deddSopenharmony_ci virtual void SetStrideAlignment(uint32_t strideAlignment) = 0; 1554ed2deddSopenharmony_ci 1564ed2deddSopenharmony_ci /** 1574ed2deddSopenharmony_ci * @brief Obtains the number of bytes for stride alignment. By default, 4-byte aligned is used. 1584ed2deddSopenharmony_ci * 1594ed2deddSopenharmony_ci * @return Returns the number of bytes for stride alignment. 1604ed2deddSopenharmony_ci * @since 1.0 1614ed2deddSopenharmony_ci * @version 1.0 1624ed2deddSopenharmony_ci */ 1634ed2deddSopenharmony_ci virtual uint32_t GetStrideAlignment() = 0; 1644ed2deddSopenharmony_ci 1654ed2deddSopenharmony_ci /** 1664ed2deddSopenharmony_ci * @brief Obtains the stride of the surface. 1674ed2deddSopenharmony_ci * 1684ed2deddSopenharmony_ci * @return Returns the stride. 1694ed2deddSopenharmony_ci * @since 1.0 1704ed2deddSopenharmony_ci * @version 1.0 1714ed2deddSopenharmony_ci */ 1724ed2deddSopenharmony_ci virtual uint32_t GetStride() = 0; 1734ed2deddSopenharmony_ci 1744ed2deddSopenharmony_ci /** 1754ed2deddSopenharmony_ci * @brief Sets the size of the shared memory to allocate. 1764ed2deddSopenharmony_ci * 1774ed2deddSopenharmony_ci * 1784ed2deddSopenharmony_ci * @param size Indicates the size of the shared memory. The value range is (0,58982400]. 1794ed2deddSopenharmony_ci * @since 1.0 1804ed2deddSopenharmony_ci * @version 1.0 1814ed2deddSopenharmony_ci */ 1824ed2deddSopenharmony_ci virtual void SetSize(uint32_t size) = 0; 1834ed2deddSopenharmony_ci 1844ed2deddSopenharmony_ci /** 1854ed2deddSopenharmony_ci * @brief Obtains the size of the shared memory to allocate. 1864ed2deddSopenharmony_ci * 1874ed2deddSopenharmony_ci * @return Returns the size of the shared memory. 1884ed2deddSopenharmony_ci * @since 1.0 1894ed2deddSopenharmony_ci * @version 1.0 1904ed2deddSopenharmony_ci */ 1914ed2deddSopenharmony_ci virtual uint32_t GetSize() = 0; 1924ed2deddSopenharmony_ci 1934ed2deddSopenharmony_ci /** 1944ed2deddSopenharmony_ci * @brief Sets the usage scenario of the buffer. Physically contiguous memory and virtual memory (by default) 1954ed2deddSopenharmony_ci * are supported. By default, virtual memory is allocated. 1964ed2deddSopenharmony_ci * 1974ed2deddSopenharmony_ci * @param usage Indicates the usage scenario of the buffer. For details, see {@link BUFFER_CONSUMER_USAGE}. 1984ed2deddSopenharmony_ci * @since 1.0 1994ed2deddSopenharmony_ci * @version 1.0 2004ed2deddSopenharmony_ci */ 2014ed2deddSopenharmony_ci virtual void SetUsage(uint32_t usage) = 0; 2024ed2deddSopenharmony_ci 2034ed2deddSopenharmony_ci /** 2044ed2deddSopenharmony_ci * @brief Obtains the usage scenario of the buffer. Physically contiguous memory and virtual memory are supported. 2054ed2deddSopenharmony_ci * 2064ed2deddSopenharmony_ci * @return Returns the usage scenario of the buffer. For details, see {@link BUFFER_CONSUMER_USAGE}. 2074ed2deddSopenharmony_ci * @since 1.0 2084ed2deddSopenharmony_ci * @version 1.0 2094ed2deddSopenharmony_ci */ 2104ed2deddSopenharmony_ci virtual uint32_t GetUsage() = 0; 2114ed2deddSopenharmony_ci 2124ed2deddSopenharmony_ci /** 2134ed2deddSopenharmony_ci * @brief Sets surface user data, which is stored in the format of <key, value>. 2144ed2deddSopenharmony_ci * 2154ed2deddSopenharmony_ci * @param key Indicates the key of a key-value pair to store. 2164ed2deddSopenharmony_ci * @param value Indicates the value of the key-value pair to store. 2174ed2deddSopenharmony_ci * @since 1.0 2184ed2deddSopenharmony_ci * @version 1.0 2194ed2deddSopenharmony_ci */ 2204ed2deddSopenharmony_ci virtual void SetUserData(const std::string& key, const std::string& value) = 0; 2214ed2deddSopenharmony_ci 2224ed2deddSopenharmony_ci /** 2234ed2deddSopenharmony_ci * @brief Obtains surface user data. 2244ed2deddSopenharmony_ci * 2254ed2deddSopenharmony_ci * @param key Indicates the key of a key-value pair for which the value is to be obtained. 2264ed2deddSopenharmony_ci * @return Returns the value of the key-value pair obtained. 2274ed2deddSopenharmony_ci * @since 1.0 2284ed2deddSopenharmony_ci * @version 1.0 2294ed2deddSopenharmony_ci */ 2304ed2deddSopenharmony_ci virtual std::string GetUserData(const std::string& key) = 0; 2314ed2deddSopenharmony_ci 2324ed2deddSopenharmony_ci /** 2334ed2deddSopenharmony_ci * @brief Obtains a buffer to write data. 2344ed2deddSopenharmony_ci * 2354ed2deddSopenharmony_ci * 2364ed2deddSopenharmony_ci * @param wait Specifies whether the function waits for an available buffer. If <b>wait</b> is <b>1</b>, 2374ed2deddSopenharmony_ci * the function waits until there is an available buffer in the free queue before returning a pointer. 2384ed2deddSopenharmony_ci * If the <b>wait</b> is <b>0</b>, the function does not wait and returns <b>nullptr</b> if there is no buffer 2394ed2deddSopenharmony_ci * in the free queue. The default value is <b>0</b>. 2404ed2deddSopenharmony_ci * @return Returns the pointer to the buffer if the operation is successful; returns <b>nullptr</b> otherwise. 2414ed2deddSopenharmony_ci * @since 1.0 2424ed2deddSopenharmony_ci * @version 1.0 2434ed2deddSopenharmony_ci */ 2444ed2deddSopenharmony_ci virtual SurfaceBuffer* RequestBuffer(uint8_t wait = 0) = 0; 2454ed2deddSopenharmony_ci 2464ed2deddSopenharmony_ci /** 2474ed2deddSopenharmony_ci * @brief Flushes a buffer to the dirty queue for consumers to use. 2484ed2deddSopenharmony_ci * 2494ed2deddSopenharmony_ci * 2504ed2deddSopenharmony_ci * 2514ed2deddSopenharmony_ci * @param SurfaceBuffer Indicates the pointer to the buffer flushed by producers. 2524ed2deddSopenharmony_ci * @return Returns <b>0</b> if the operation is successful; returns <b>-1</b> otherwise. 2534ed2deddSopenharmony_ci * @since 1.0 2544ed2deddSopenharmony_ci * @version 1.0 2554ed2deddSopenharmony_ci */ 2564ed2deddSopenharmony_ci virtual int32_t FlushBuffer(SurfaceBuffer* buffer) = 0; 2574ed2deddSopenharmony_ci 2584ed2deddSopenharmony_ci /** 2594ed2deddSopenharmony_ci * @brief Obtains a buffer. 2604ed2deddSopenharmony_ci * 2614ed2deddSopenharmony_ci * Consumers can use this function to obtain the buffer placed in the dirty queue by producers. 2624ed2deddSopenharmony_ci * If there is no buffer in the queue, <b>nullptr</b> is returned. 2634ed2deddSopenharmony_ci * 2644ed2deddSopenharmony_ci * @return Returns the pointer to the {@link SurfaceBuffer} object. 2654ed2deddSopenharmony_ci * @since 1.0 2664ed2deddSopenharmony_ci * @version 1.0 2674ed2deddSopenharmony_ci */ 2684ed2deddSopenharmony_ci virtual SurfaceBuffer* AcquireBuffer() = 0; 2694ed2deddSopenharmony_ci 2704ed2deddSopenharmony_ci /** 2714ed2deddSopenharmony_ci * @brief Releases the consumed buffer. 2724ed2deddSopenharmony_ci * 2734ed2deddSopenharmony_ci * After a consumer has used a {@link SurfaceBuffer} object, the consumer can release it through 2744ed2deddSopenharmony_ci * {@link ReleaseBuffer}. The released object is placed into the free queue so that producers can 2754ed2deddSopenharmony_ci * apply for the object. 2764ed2deddSopenharmony_ci * 2774ed2deddSopenharmony_ci * @param SurfaceBuffer Indicates the pointer to the buffer released. 2784ed2deddSopenharmony_ci * @return Returns <b>true</b> if the buffer is released; returns <b>false</b> otherwise. 2794ed2deddSopenharmony_ci * @since 1.0 2804ed2deddSopenharmony_ci * @version 1.0 2814ed2deddSopenharmony_ci */ 2824ed2deddSopenharmony_ci virtual bool ReleaseBuffer(SurfaceBuffer* buffer) = 0; 2834ed2deddSopenharmony_ci 2844ed2deddSopenharmony_ci /** 2854ed2deddSopenharmony_ci * @brief Releases a buffer to the free queue. 2864ed2deddSopenharmony_ci * 2874ed2deddSopenharmony_ci * 2884ed2deddSopenharmony_ci * @param SurfaceBuffer Indicates the pointer to the buffer to be released by producers. 2894ed2deddSopenharmony_ci * @since 1.0 2904ed2deddSopenharmony_ci * @version 1.0 2914ed2deddSopenharmony_ci */ 2924ed2deddSopenharmony_ci virtual void CancelBuffer(SurfaceBuffer* buffer) = 0; 2934ed2deddSopenharmony_ci 2944ed2deddSopenharmony_ci /** 2954ed2deddSopenharmony_ci * @brief Registers a consumer listener. 2964ed2deddSopenharmony_ci * 2974ed2deddSopenharmony_ci * When a buffer is placed in the dirty queue, {@link OnBufferAvailable} is called to notify consumers. 2984ed2deddSopenharmony_ci * If the listener is repeatedly registered, only the latest one is retained. 2994ed2deddSopenharmony_ci * 3004ed2deddSopenharmony_ci * @param IBufferConsumerListener Indicates the listener to register. 3014ed2deddSopenharmony_ci * @since 1.0 3024ed2deddSopenharmony_ci * @version 1.0 3034ed2deddSopenharmony_ci */ 3044ed2deddSopenharmony_ci virtual void RegisterConsumerListener(IBufferConsumerListener& listener) = 0; 3054ed2deddSopenharmony_ci 3064ed2deddSopenharmony_ci /** 3074ed2deddSopenharmony_ci * @brief Unregisters the consumer listener. 3084ed2deddSopenharmony_ci * 3094ed2deddSopenharmony_ci * After the listener is unregistered, no callback is triggered when a buffer is placed in the dirty queue. 3104ed2deddSopenharmony_ci * 3114ed2deddSopenharmony_ci * @since 1.0 3124ed2deddSopenharmony_ci * @version 1.0 3134ed2deddSopenharmony_ci */ 3144ed2deddSopenharmony_ci virtual void UnregisterConsumerListener() = 0; 3154ed2deddSopenharmony_ci 3164ed2deddSopenharmony_ciprotected: 3174ed2deddSopenharmony_ci Surface() {} 3184ed2deddSopenharmony_ci}; 3194ed2deddSopenharmony_ci} // end namespace 3204ed2deddSopenharmony_ci#endif 321