1 /*
2  * Copyright Samsung Electronics Co.,LTD.  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __HARDWARE_EXYNOS_ACRYLIC_H__
18 #define __HARDWARE_EXYNOS_ACRYLIC_H__
19 
20 #include <vector>
21 #include <cstdint>
22 #include <unistd.h>
23 #include <system/graphics.h>
24 #include <hardware/hwcomposer.h>
25 
26 /* basic primitives */
27 
28 #define MAX_HW2D_PLANES 4
29 
30 /*
31  * Structure to express 2-dimensional coordinates.
32  * Invented to pass or to return two values in a single word
33  */
34 typedef struct hw2d_coord {
35     int16_t hori;
36     int16_t vert;
swaphw2d_coord37     void swap()
38     {
39         int16_t tmp = hori;
40         hori = vert;
41         vert = tmp;
42     }
43 } hw2d_coord_t;
44 
45 /*
46  * Structure to express a rectangle in a 2-dimensional grid.
47  * Invented to pass or to return four values in a single 64-bit word
48  */
49 typedef struct hw2d_rect {
50     hw2d_coord_t pos;
51     hw2d_coord_t size;
52 } hw2d_rect_t;
53 
54 /*
55  * Structure to express the capability of HW 2D Accelerators
56  */
57 struct stHW2DCapability {
58     /* maximum magnification with filter applied */
59     hw2d_coord_t max_upsampling_num;
60     /* maximum minification factor with filter applied */
61     hw2d_coord_t max_downsampling_factor;
62     /* maximum maginifcation without filter applied */
63     hw2d_coord_t max_upsizing_num;
64     /* maximum minification factor without filter applied */
65     hw2d_coord_t max_downsizing_factor;
66     /* the smallest source image size the HW can process */
67     hw2d_coord_t min_src_dimension;
68     /* the largest source image size the HW can process */
69     hw2d_coord_t max_src_dimension;
70     /* the smallest output image size the HW can process */
71     hw2d_coord_t min_dst_dimension;
72     /* the largest output image size the HW can process */
73     hw2d_coord_t max_dst_dimension;
74     /*
75      * the restriction of the alignments of the numbers of pixels
76      * in both of horizontal and vertical direction
77      */
78     hw2d_coord_t min_pix_align;
79     /*
80      * The number of repeats of rescaling that the compositor supports. Note
81      * that this is not the capability of the driver and H/W but the capability.
82      * of the compositor(Acrylic).
83      * If the driver and H/W supports rescaling, it is 0. If the compositor is
84      * capable of running the H/W twice to overcome the minification restriction,
85      * it is then 1.
86      */
87     int16_t rescaling_count;
88     /*
89      * the compositing mode that the HW 2D supports.
90      * should be combination of the values of HW2DCapability::blend_ops_t.
91      */
92     uint32_t compositing_mode;
93     /*
94      * the geometric transformation in the 2-dimensional coordinate.
95      * should be combination of the values of HW2DCapability::transform_t.
96      */
97     uint32_t transform_type;
98     /*
99      * the capabilities of HW. The value is the combination of the values of
100      * HW2DCapability::feature_t.
101      */
102     uint32_t auxiliary_feature;
103     /*
104      * the number of color formats supported by HW 2D.
105      * it is also the number of elements in pixformats array.
106      */
107     unsigned int num_formats;
108     /*
109      * the number of color spaces supported by HW 2D.
110      * it is also the number of elements in dataspaces array.
111      */
112     unsigned int num_dataspaces;
113     /*
114      * the number of source images that HW 2D can process.
115      */
116     unsigned int max_layers;
117     /*
118      * the array of color formats that are supported by HW 2D. the values should
119      * be selected from the definitions in <system/graphics.h> and exynos_formats.h.
120      */
121     uint32_t *pixformats;
122     /*
123      * the array of color spaces that are supported by HW 2D. the values should
124      * be selected from the definitions in <system/graphics.h>.
125      */
126     int *dataspaces;
127     /*
128      * the restriction of the alignments of the base addresses of buffers of images.
129      */
130     size_t base_align;
131 };
132 
133 /*
134  * HW2DCapability - Description of the capability of HW 2D.
135  */
136 class HW2DCapability {
137 public:
138     /*
139      * the values to indicate the capabilities of geometric transformations
140      * supported by HW 2D accelerators.
141      */
142     enum transform_t {
143         TRANSFORM_FLIP_H = 1,   /* horizontal flip: (symmetrical displacement by x = -x, y = y) */
144         TRANSFORM_FLIP_V = 2,   /* vertical flip: (symmetrical displacement by x = x, y = -y) */
145         TRANSFORM_FLIP_HV = 3,  /* horizontal flip then vertical flip */
146         TRANSFORM_ROT_90 = 4,   /* clockwise rotation by 90 degree */
147         TRANSFORM_ROT_180 = 8,  /* clockwise rotation by 180 degree */
148         TRANSFORM_ROT_270 = 16, /* clockwise rotation by 270 degree */
149         TRANSFORM_ROT_ALL = 28, /* bitmask to select rotation degrees */
150         TRANSFORM_ALL = 31      /* bitmask to select the correct bits */
151     };
152     /*
153      * the values to indicate the capabilities of layer compositing
154      * supported by HW 2D accelerators.
155      * Sc: Source color, Dc: Destination(background) color,
156      * Sa: Source alpha, Pa: Plane alpha
157      */
158     enum blend_ops_t {
159         BLEND_NONE = 1,     /* Sc * 1 + Dc * 0 */
160         BLEND_SRC_COPY = 2, /* Sc * 1 * Pa + Dc * (1 - Sa) */
161         BLEND_SRC_OVER = 4, /* Sc * Sa * Pa + Dc * (1 - Sa) */
162     };
163 
164     /*
165      * The values to indicate the capabilities of 2D H/W. The capabilities
166      * listed here shows if a function is supported or not.
167      */
168     enum feature_t {
169         FEATURE_PLANE_ALPHA  = 1 << 0,
170         FEATURE_AFBC_ENCODE  = 1 << 4,
171         FEATURE_AFBC_DECODE  = 1 << 5,
172         FEATURE_UORDER_READ  = 1 << 6,
173         FEATURE_UORDER_WRITE = 1 << 7,
174         FEATURE_OTF_READ     = 1 << 8,
175         FEATURE_OTF_WRITE    = 1 << 9,
176         FEATURE_SOLIDCOLOR  = 1 << 10,
177     };
178 
179     enum { RESAMPLING_FRACTION_BITS = 20 };
180 
HW2DCapability(const stHW2DCapability & cap)181     HW2DCapability(const stHW2DCapability &cap): mCap(cap) { }
182 
183     /*
184      * returns the bitmask of supporting flip directions
185      * the returned value is the combination of the values of transform_t.
186      */
supportedFlip()187     transform_t supportedFlip() const { return static_cast<transform_t>(mCap.transform_type & TRANSFORM_FLIP_HV); }
188     /*
189      * returns the bitmask of supporting rotation degrees
190      * the returned value is the combination of the values of transform_t.
191      */
supportedRotation()192     transform_t supportedRotation() const { return static_cast<transform_t>(mCap.transform_type & TRANSFORM_ROT_ALL); }
193     /*
194      * returns the pair of the maximum maginifcation
195      * in the horizontal and vertical direction with filter applied
196      */
supportedMaxMagnification()197     hw2d_coord_t supportedMaxMagnification() const { return mCap.max_upsampling_num;}
198     /*
199      * returns the pair of the maximum minifcation factor
200      * in the horizontal and vertical direction with filter applied
201      */
supportedMinMinification()202     hw2d_coord_t supportedMinMinification() const { return mCap.max_downsampling_factor;}
203     /*
204      * returns the pair of the maximum maginifcation
205      * in the horizontal and vertical direction without filter applied
206      */
supportedMaxEnlarging()207     hw2d_coord_t supportedMaxEnlarging() const { return mCap.max_upsizing_num;}
208     /*
209      * returns the pair of the maximum minifcation factor
210      * in the horizontal and vertical direction without filter applied
211      */
supportedMinDecimation()212     hw2d_coord_t supportedMinDecimation() const { return mCap.max_downsizing_factor;}
213     /*
214      * returns the bitmask of supporting compositing modes
215      * The returned value is the combination of the value sof blend_ops_t.
216      */
supportedCompositingMode()217     uint32_t supportedCompositingMode() const { return mCap.compositing_mode; }
218     /*
219      * returns the alignment restriction of HW 2D in the base memory address.
220      */
supportedBaseAlign()221     size_t supportedBaseAlign() const { return mCap.base_align; }
222     /*
223      * returns the maximum number of horizontal and vertical pixels
224      * of the source images that HW 2D can process
225      */
supportedMaxSrcDimension()226     hw2d_coord_t supportedMaxSrcDimension() const { return mCap.max_src_dimension; }
227     /*
228      * returns the minimum number of horizontal and vertical pixels
229      * of the source images that HW 2D can process
230      */
supportedMinSrcDimension()231     hw2d_coord_t supportedMinSrcDimension() const { return mCap.min_src_dimension; }
232     /*
233      * returns the maximum number of horizontal and vertical pixels
234      * of the output image that HW 2D can process
235      */
supportedMaxDstDimension()236     hw2d_coord_t supportedMaxDstDimension() const { return mCap.max_dst_dimension; }
237     /*
238      * returns the minimum number of horizontal and vertical pixels
239      * of the output image that HW 2D can process
240      */
supportedMinDstDimension()241     hw2d_coord_t supportedMinDstDimension() const { return mCap.min_dst_dimension; }
242     /*
243      * returns the alignment restriction of the number of horizontal
244      * and vertical pixels that HW 2D can process
245      */
supportedDimensionAlign()246     hw2d_coord_t supportedDimensionAlign() const { return mCap.min_pix_align; }
247     /*
248      * returns the number of source images that HW 2D can composit
249      */
maxLayerCount()250     unsigned int maxLayerCount() const { return mCap.max_layers; }
251     /*
252      * study if the given format is supported by the HW 2D
253      */
isFormatSupported(uint32_t fmt)254     bool isFormatSupported(uint32_t fmt) const {
255         for (unsigned int i = 0; i < mCap.num_formats; i++)
256             if (mCap.pixformats[i] == fmt)
257                 return true;
258         return false;
259     }
260     /*
261      * study if the given colorspace is supported by HW 2D
262      */
isDataspaceSupported(int dataspace)263     bool isDataspaceSupported(int dataspace) const {
264         // discard transfer function information because it is required during display
265         dataspace &= ~HAL_DATASPACE_TRANSFER_MASK;
266 
267         for (unsigned int i = 0; i < mCap.num_dataspaces; i++)
268             if (mCap.dataspaces[i] == dataspace)
269                 return true;
270         return false;
271     }
272     /*
273      * convert the transformation mask in transform_type into the values
274      * that Android Graphics HAL understands.
275      */
getHWCTransformMask()276     uint32_t getHWCTransformMask() const {
277         uint32_t mask = 0;
278         if (mCap.transform_type & TRANSFORM_FLIP_H)
279             mask |= HAL_TRANSFORM_FLIP_H;
280         if (mCap.transform_type & TRANSFORM_FLIP_V)
281             mask |= HAL_TRANSFORM_FLIP_V;
282         if (mCap.transform_type & TRANSFORM_ROT_90)
283             mask |= HAL_TRANSFORM_ROT_90;
284         if (mCap.transform_type & TRANSFORM_ROT_180)
285             mask |= HAL_TRANSFORM_ROT_180;
286         if (mCap.transform_type & TRANSFORM_ROT_270)
287             mask |= HAL_TRANSFORM_ROT_270;
288         return mask;
289     }
290     /*
291      * discover if the given feature that is defined in feature_t is supported.
292      */
isFeatureSupported(uint32_t feature)293     bool isFeatureSupported(uint32_t feature) const { return !!(mCap.auxiliary_feature & feature); }
294     /*
295      * study if the given horizontal resolution change from @from into @to is
296      * supported by HW 2D
297      */
298     bool supportedHResampling(int16_t from, int16_t to, int16_t resamping_count = 1) const
299     {
300         return supportedResampling(from, to,
301                                    mCap.max_upsampling_num.hori * resamping_count,
302                                    mCap.max_downsampling_factor.hori * resamping_count);
303     }
304     /*
305      * study if the given vertical resolution change from @from into @to is
306      * supported by HW 2D
307      */
308     bool supportedVResampling(int16_t from, int16_t to, int16_t resamping_count = 1) const
309     {
310         return supportedResampling(from, to,
311                                    mCap.max_upsampling_num.vert * resamping_count,
312                                    mCap.max_downsampling_factor.vert * resamping_count);
313     }
314     /*
315      * study if the given resampling from @from to @to is supported by HW 2D compositor
316      * implementation.
317      */
supportedResampling(hw2d_coord_t from,hw2d_coord_t to,uint32_t transform)318     bool supportedResampling(hw2d_coord_t from, hw2d_coord_t to, uint32_t transform) const
319     {
320         int count = supportedRescalingCount();
321 
322         if (!!(transform & HAL_TRANSFORM_ROT_90))
323             to.swap();
324 
325         return supportedHResampling(from.hori, to.hori, count) &&
326                supportedVResampling(from.vert, to.vert, count);
327     }
328     /*
329      * study if the given resampling from @from to @to is supported by HW 2D
330      */
supportedHWResampling(hw2d_coord_t from,hw2d_coord_t to,uint32_t transform)331     bool supportedHWResampling(hw2d_coord_t from, hw2d_coord_t to, uint32_t transform) const
332     {
333         if (!!(transform & HAL_TRANSFORM_ROT_90))
334             to.swap();
335 
336         return supportedHResampling(from.hori, to.hori, 1) &&
337                supportedVResampling(from.vert, to.vert, 1);
338     }
339     /*
340      * study if the given number of horizontal pixels (@from) can be changed
341      * to @to by HW 2D
342      */
supportedHResizing(int16_t from,int16_t to)343     bool supportedHResizing(int16_t from, int16_t to) const
344     {
345         return supportedResizing(from, to,
346                 mCap.max_upsizing_num.hori, mCap.max_downsizing_factor.hori);
347     }
348     /*
349      * study if the given number of vertical pixels (@from) can be changed
350      * to @to by HW 2D
351      */
supportedVResizing(int16_t from,int16_t to)352     bool supportedVResizing(int16_t from, int16_t to) const
353     {
354         return supportedResizing(from, to,
355                 mCap.max_upsizing_num.vert, mCap.max_downsizing_factor.vert);
356     }
357     /*
358      * study if the given resizing from @from to @to is supported by HW 2D compositor
359      * implementation.
360      */
supportedResizing(hw2d_coord_t from,hw2d_coord_t to,uint32_t transform)361     bool supportedResizing(hw2d_coord_t from, hw2d_coord_t to, uint32_t transform) const
362     {
363         if (!!(transform & HAL_TRANSFORM_ROT_90))
364             to.swap();
365 
366         return supportedHResizing(from.hori, to.hori) && supportedVResizing(from.vert, to.vert);
367     }
368     /*
369      * study the number rescaling by the compositor for a layer
370      * 1 if the compositor does not repeat the scaling. 2 if the compositor
371      * repeat the scaling once.
372      * Note that the return value of supportedRescalingCount() is the one
373      * incremented from the struct stHW2DCapability.rescaling_count.
374      */
supportedRescalingCount()375     int supportedRescalingCount() const
376     {
377         return mCap.rescaling_count + 1;
378     }
379 private:
supportedResampling(int16_t from,int16_t to,int16_t upfactor,int16_t downfactor)380     bool supportedResampling(int16_t from, int16_t to, int16_t upfactor, int16_t downfactor) const
381     {
382         int64_t factor = static_cast<int64_t>(from);
383 
384         factor <<= RESAMPLING_FRACTION_BITS;
385         factor /= to;
386 
387         if (factor > (static_cast<int64_t>(downfactor) << RESAMPLING_FRACTION_BITS))
388             return false;
389 
390         // @upfactor is a reciprocal number of an upsampling factor
391         if (factor < ((1 << RESAMPLING_FRACTION_BITS) / static_cast<int64_t>(upfactor)))
392             return false;
393 
394         return true;
395     }
396 
supportedResizing(int16_t from,int16_t to,int16_t upfactor,int16_t downfactor)397     bool supportedResizing(int16_t from, int16_t to, int16_t upfactor, int16_t downfactor) const
398     {
399         if ((from < to) && (upfactor == 0))
400             return true;
401 
402         if ((from > to) && (downfactor == 0))
403             return true;
404 
405         return supportedResampling(from, to, upfactor, downfactor);
406     }
407     const stHW2DCapability &mCap;
408 };
409 
410 class Acrylic;
411 
412 /*
413  * AcrylicCanvas - Description of an image and its buffers
414  *
415  * It includes the entire image area and how the image is stored in the memory,
416  * where it is stored and the buffer synchronization objects called Fence.
417  * AcrylicCanvas contains the following attributes:
418  * - image dimension: number of horizontal and vertical pixels
419  * - color format of the image
420  * - colorspace of the image
421  * - the identifiers of the image buffers (userptr or dmabuf)
422  *
423  * Creation of AcrylicCanvas by new operator is prohibited. The only way to
424  * create an instance of AcrylicCavans is to call Acrylic::createLayer().
425  */
426 class AcrylicCanvas {
427     friend class Acrylic;
428 public:
429     /*
430      * The attributes of the image described by AcrylicCanvas:
431      * The attributes are bit masks and therefore combination of them may be
432      * also required.
433      * - ATTR_NONE: indicates no special attributes are specified
434      * - ATTR_PROTECTED: the buffer of the image is protected.  Reading or
435      *                   writing to the buffer of the image is prohibited.
436      * - ATTR_COMPRESSED: the image is or is to be stored in a compressed form.
437      *                    e.g. AFBC
438      * - ATTR_UORDER: the image data is written in U-order instead of raster-scan order.
439      *                U-order memory access by GPU helps the BUS efficiency.
440      * - ATTR_OTF: The image buffer is hard-wired. If this attribute is given, libacryl
441      *             ignores the buffer configuration to the canvas.
442      * - ATTR_SOLIDCOLOR : The image buffer is empty and should be filled with one RGBA value by H/W.
443      */
444     enum layer_attr_t {
445         ATTR_NONE       = 0,
446         ATTR_PROTECTED  = 1,
447         ATTR_COMPRESSED = 2,
448         ATTR_UORDER     = 4,
449         ATTR_OTF        = 8,
450         ATTR_SOLIDCOLOR = 16,
451         ATTR_ALL_MASK   = 0x1F
452     };
453     /*
454      * Describes how the buffer of the image is identified.
455      * - MT_DMABUF: the buffer is identified by an open file descriptor that is
456      *              exported by dmabuf.
457      * - MT_USERPTR: the buffer is identified by a memory address that is valid
458      *               in the current process. Usally it is a bug if mAttributes
459      *               has ATTR_PROTECTED whlie mMemoryType is MT_USERPTR.
460      * - MT_EMTPY : the buffer is empty such as hare-wired buffer or colorfill layer.
461      */
462     enum memory_type { MT_DMABUF = 1, MT_USERPTR = 2, MT_EMPTY = 3 };
463     /*
464      * Indicates the configured or modified settings
465      * - SETTING_TYPE: Image format and color space information is configured by users.
466      * - SETTING_BUFFER: Image buffer information is configured by users.
467      * - SETTING_DIMENSION: Image dimension information is configured by users.
468      * - SETTING_TYPE_MODIFIED: Image format and color space information is configured
469      *                          by users and it is not applied to HW yet.
470      * - SETTING_BUFFER_MODIFIED: Image buffer information is configured by users and
471      *                            it is not applied to HW yet.
472      * - SETTING_DIMENSION_MODIFIED: Image dimension information is configured by users
473      *                               and it is not applied to HW yet.
474      */
475     enum setting_check_t {
476         SETTING_TYPE = 1,
477         SETTING_BUFFER = 2,
478         SETTING_DIMENSION = 4,
479         SETTING_MASK = SETTING_TYPE | SETTING_BUFFER | SETTING_DIMENSION,
480         SETTING_TYPE_MODIFIED = 16,
481         SETTING_BUFFER_MODIFIED = 32,
482         SETTING_DIMENSION_MODIFIED = 64,
483         SETTIMG_MODIFIED_MASK = SETTING_TYPE_MODIFIED | SETTING_BUFFER_MODIFIED | SETTING_DIMENSION_MODIFIED,
484     };
485 
486     /*
487      * MEMBER FUNCTIONS FOR THE USERS
488      */
489 
490     /*
491      * Configure the dimension of the image
492      */
493     virtual bool setImageDimension(int32_t width, int32_t height);
494     /*
495      * Find the dimension of the image configured to AcyrilicCanvas
496      */
getImageDimension()497     hw2d_coord_t getImageDimension() { return mImageDimension; }
498     /*
499      * Configure the image color format and color space
500      * You should configure the correct colorspace if you want to change the
501      * the image is YCbCr and the target image is RGB because the brightness
502      * of the pixel data encoded in YCbCr is generated from various colorspaces
503      * including BT.601, BT.709(sRGB) and BT.2020. If the YCbCr image is a frame
504      * of a moving picture, we should likely consider the 1/16 headroom and
505      * footroom. Generally BT.601, BT.709 and BT.2020 have footroom and headroom
506      * while JFIF(JPEG, SRGB) does not have.
507      */
508     bool setImageType(uint32_t fmt, int dataspace);
509     /*
510      * Configure color fill layer that fill only one RGBA color without actual buffer.
511      * Note that this successes if the compositor supports FEATURE_SOLIDCOLOR.
512      */
513     bool setImageBuffer(int a, int r, int g, int b, uint32_t attr = ATTR_NONE);
514     /*
515      * Configure the image buffer of dmabuf type.
516      */
517     bool setImageBuffer(int fd[MAX_HW2D_PLANES], size_t len[MAX_HW2D_PLANES], off_t offset[MAX_HW2D_PLANES],
518                         int num_buffers, int fence = -1, uint32_t attr = ATTR_NONE);
519     /*
520      * Configure the image buffer of dmabuf type.
521      * This does not receives a parameter 'offset' compared to the above
522      * setImageBuffer(). The latter type of setImageBuffer() is provided to
523      * provide convenience to the users because offsets are mostly zero.
524      */
525     bool setImageBuffer(int fd[MAX_HW2D_PLANES], size_t len[MAX_HW2D_PLANES], int num_buffers,
526                         int fence = -1, uint32_t attr = ATTR_NONE)
527     {
528         off_t offset[MAX_HW2D_PLANES] = {0, 0, 0};
529         return setImageBuffer(fd, len, offset, num_buffers, fence, attr);
530     }
531     /*
532      * Configure the image buffer of userptr type.
533      * Note that you cannot pass a fence if the buffer type is userptr because
534      * userptr buffer is not shareable between processes and devices.
535      */
536     bool setImageBuffer(void *addr[MAX_HW2D_PLANES], size_t len[MAX_HW2D_PLANES],
537                         int num_buffers, uint32_t attr = ATTR_NONE);
538     /*
539      * Configure the image buffer is hard-wired which means that the buffer is always
540      * prepared in the compositor H/W.
541      * Note that this successes if the compositor supports FEATURE_OTF_READ or FEATURE_OTF_WRITE.
542      */
543     bool setImageOTFBuffer(uint32_t attr = AcrylicCanvas::ATTR_NONE);
544     /*
545      * Configure the fence. setFence() overwrites the fence specified by
546      * setImageBuffer().
547      */
548     void setFence(int fence);
549     /*
550      * Called by the implementations of Acyrlic to determine if all required
551      * configurations are completed.
552      */
isSettingOkay()553     bool isSettingOkay()
554     {
555         return (mSettingFlags & SETTING_MASK) == (SETTING_TYPE | SETTING_BUFFER | SETTING_DIMENSION);
556     }
557 
558     /*
559      * MEMBER FUNCTIONS FOR THE IMPLEMENTATIONS of Acrylic
560      */
561 
562     /*
563      * Determine if the buffer is protected. If the buffer or the image is
564      * protected, the driver of HW 2D should specially care the buffer and the
565      * processing involving the buffer.
566      */
isProtected()567     bool isProtected() { return !!(mAttributes & ATTR_PROTECTED); }
568     /*
569      * Determine if the image in the buffer is or should be in a compressed form.
570      */
isCompressed()571     bool isCompressed() { return !!(mAttributes & ATTR_COMPRESSED); }
572     /*
573      * Study if the image is or should be written in U-Order for accelerated
574      * graphic processing instead of raster-scan order.
575      */
isUOrder()576     bool isUOrder() { return !!(mAttributes & ATTR_UORDER); }
577     /*
578      * Study if the canvas buffer is hard-wired.
579      */
isOTF()580     bool isOTF() { return !!(mAttributes & ATTR_OTF); }
581     /*
582      * Study if the image is filled with solid color.
583      */
isSolidColor()584     bool isSolidColor() { return !!(mAttributes & ATTR_SOLIDCOLOR); }
585     /*
586      * Obtain the acquire fence of the buffer.
587      */
getFence()588     int getFence() const { return mFence; }
589     /*
590      * Invalidate the confgured acquire fence without destryoing(close()) it
591      */
clearFence()592     void clearFence() { mFence = -1; }
593     /*
594      * Obtain the image color format
595      */
getFormat()596     uint32_t getFormat() { return mPixFormat; }
597     /*
598      * Obtain the color space information
599      */
getDataspace()600     int getDataspace() { return mDataSpace; }
601     /*
602      * Obtain the buffer type whether the buffer is MT_USERPTR or MT_DMABUF
603      */
getBufferType()604     memory_type getBufferType() { return mMemoryType; }
605     /*
606      * Obtain the number of buffers.
607      * The parameter 'index' of the following member functions including
608      * getDmabuf(), getUserptr(), getOffset(), getBufferLength() should be
609      * smaller than the return value of getBufferCount(). Otherwise, you will
610      * get invalid memory area referenced.
611      */
getBufferCount()612     unsigned int getBufferCount() { return mNumBuffers; }
613     /*
614      * Obtain the open file descriptor exported by dmabuf if the buffer type is
615      * MT_DMABUF
616      */
getDmabuf(unsigned int index)617     int getDmabuf(unsigned int index)
618     {
619         return (mMemoryType == MT_DMABUF) ? m.mBufferFd[index] : -1;
620     }
621     /*
622      * Obtain the buffer address if the buffer type is MT_USERPTR
623      */
getUserptr(unsigned int index)624     void *getUserptr(unsigned int index)
625     {
626         return (mMemoryType == MT_USERPTR) ? m.mBufferAddr[index] : NULL;
627     }
628     /*
629      * Obtain the offset where the image is stored in the buffer if the buffer
630      * type is MT_DMABUF
631      */
getOffset(unsigned int index)632     uint32_t getOffset(unsigned int index)
633     {
634         return (mMemoryType == MT_DMABUF) ? mBufferOffset[index] : 0;
635     }
636     /*
637      * Obtain the length of the buffer
638      */
getBufferLength(unsigned int index)639     uint32_t getBufferLength(unsigned int index)
640     {
641         return mBufferLength[index];
642     }
643     /*
644      * Clear all modified states. It is called by the Acrylic implementations after
645      * they executes their HWs
646      */
clearSettingModified()647     void clearSettingModified()
648     {
649         unset(SETTING_TYPE_MODIFIED |
650               SETTING_BUFFER_MODIFIED |
651               SETTING_DIMENSION_MODIFIED);
652     }
653     /*
654      * Obtain the flags that indicates the configuration status
655      */
getSettingFlags()656     uint32_t getSettingFlags() { return mSettingFlags; }
657     /*
658      * Obtain the solid color combined by 8bit of A, R, G, B
659      */
getSolidColor()660     uint32_t getSolidColor() { return mSolidColor; }
661 protected:
662     enum canvas_type_t {
663         CANVAS_SOURCE,
664         CANVAS_TARGET,
665     };
666 
667     Acrylic *mCompositor;
668 
669     AcrylicCanvas(Acrylic *compositor, canvas_type_t type = CANVAS_SOURCE);
670     virtual ~AcrylicCanvas();
671 
getCompositor()672     Acrylic *getCompositor() { return mCompositor; }
673 
unset(uint32_t flag)674     void unset(uint32_t flag) { mSettingFlags &= ~flag; }
set(uint32_t flag)675     void set(uint32_t flag) { mSettingFlags |= flag; }
676 private:
677     /*
678      * called when Acrylic is being destroyed to inform AcrylicCanvas
679      * that no Acrylic has a reference to it.
680      */
disconnectLayer()681     void disconnectLayer() { mCompositor = NULL; }
682 
683     hw2d_coord_t mImageDimension;
684     uint32_t mPixFormat;
685     int mDataSpace;
686     memory_type mMemoryType;
687     union {
688         void *mBufferAddr[MAX_HW2D_PLANES];
689         int mBufferFd[MAX_HW2D_PLANES];
690     } m;
691     size_t mBufferLength[MAX_HW2D_PLANES];
692     uint32_t mBufferOffset[MAX_HW2D_PLANES];
693     int mNumBuffers;
694     int mFence; // NOTE: this should be reset to -1 after Acrylic::execute()
695     uint32_t mAttributes;
696     uint32_t mSettingFlags;
697     uint32_t mSolidColor; // [32:0] ARGB order.
698     canvas_type_t mCanvasType;
699 };
700 
701 /*
702  * AcrylicLayer - Description of compositing properties
703  *                as well as an image and its buffers
704  *
705  * AcrylicLayer extends the properties of AcrylicCanvas for compositing purpose.
706  * It adds the following propertes to AcrylicCanvas:
707  * - The rectangle region of interest in the image. It is usually called 'crop'
708  * - The rectangle region to map the crop area in the target dimension. It is
709  *   usually called 'window'. If the width or the height of the window is
710  *   different from crop, resampling or resizing is invloved.
711  * - How the input images are composited. Currently Acrylic supports only the
712  *   following opertions:
713  *   .HWC_BLENDING_NONE:    The alpha channel in the image should be ignored
714  *   .HWC_BLENDING_PREMULT: The color brightness values are regardted as they
715  *                          are multiplied with its alpha channel value
716  *   .HWC_BLENDING_COVERAGE: The alpha value of each pixel is multiplied with
717  *                           the brighness of each pixel during compositing.
718  * - How to transfrom the image into window: flip, rotation
719  * - The order of input images to be stacked on the background
720  * - The transparent level of the image. It is multiplied to every pixel
721  *   brightness values when compositing. The transparent level of the entire
722  *   image is also called 'plane alpha' or 'global alpha'.
723  *
724  * The default properties are:
725  * - mImageRect: the entire image dimension.
726  * - mTargetRect: the entire target dimension
727  * - mBlendingMode: HWC_BLENDING_NONE
728  * - mTransform: 0 (no flip, no rotation)
729  * - mZOrder: 0
730  * - mPlaneAlpha: 1.0(255)
731  *
732  * Creation of AcrylicLayer by new operator is prohibited. The only way to
733  * create an instance of AcrylicLayer is to call Acrylic::createLayer().
734  */
735 class AcrylicLayer: public AcrylicCanvas {
736     friend class Acrylic;
737 public:
738     /*
739      * The various/special attributes to the source images
740      * - ATTR_NORESAMPLING: Force resize the image speicifed to the layer even
741      *                      though the target image size is too small to apply
742      *                      the interpolation filter correctly.
743      */
744     enum composit_attr_t { ATTR_NORESAMPLING = 1, ATTR_ALL_MASK = 1};
745 
746     virtual ~AcrylicLayer();
747 
748     /*
749      * MEMBER FUNCTIONS FOR THE USERS
750      */
751 
752     /*
753      * Configure the compositing mode including the compositing operations,
754      * plane alpha and z-order
755      */
756     bool setCompositMode(uint32_t mode, uint8_t alpha = 0xFF, int z_order = 0);
757     /*
758      * Configure the crop, window, transformation and special compositing attributes.
759      */
760     bool setCompositArea(hwc_rect_t &src_area, hwc_rect_t &out_area, uint32_t transform = 0, uint32_t attr = 0);
761     /*
762      * Configure the crop and transformation information. The window rect is
763      * is regarded as the entire target dimension.
764      */
765     bool setCompositArea(hwc_rect_t &src_area, uint32_t transform = 0, uint32_t attr = 0)
766     {
767         hwc_rect_t out_area = {0, 0, 0, 0};
768         return setCompositArea(src_area, out_area, transform, attr);
769     }
770     /*
771      * Configure the dimension of the image. This function overrides
772      * AcrylicCanvas::setImageDimension(). Once this function is called, the
773      * crop rect, mImageRect reset to the entire image dimension configured
774      * by this function. Note that the window, mTargetRect is not reset because
775      * it is not dependent upon the image dimension.
776      */
777     virtual bool setImageDimension(int32_t width, int32_t height);
778     /*
779      * Configure the source image area(crop) with the target rect and transform
780      * untouched while setCompositArea() modifies source rect, target rect and
781      * transform with the given arguments.
782      */
setImageRect(hwc_rect_t & src_area)783     bool setImageRect(hwc_rect_t &src_area)
784     {
785         hwc_rect_t out_area = {
786             mTargetRect.pos.hori,
787             mTargetRect.pos.vert,
788             mTargetRect.pos.hori + mTargetRect.size.hori,
789             mTargetRect.pos.vert + mTargetRect.size.vert
790         };
791         return setCompositArea(src_area, out_area, mTransform);
792     }
793     /*
794      * Configure the minimum liminance and the maximum luminance of the display
795      * used in the HDR video mastering environment. It is spepcified in the
796      * metatata of the HDR video contents.
797      * @min: minimum luminance in 0.0001 cd/m^2 unit
798      * @max: maximum luminance in 1 cd/m^2 unit
799      *
800      * The followings are the default values:
801      * @min: 0
802      * @max: 100 (SDR)
803      *
804      * NOTE:
805      * The configured video is treated as SDR until setMasterDisplayLuminance()
806      * is called. It means that the target image is to be SDR.
807      */
808     void setMasterDisplayLuminance(uint16_t min, uint16_t max);
809 
810     /*
811      * Configure an opaque data associated to a layer
812      * @data: pointer to data associated to this layer
813      * @data_len: number of effective bytes pointed by @data
814      *
815      * The configured data is opaque to libacryl. It should be handled by the
816      * implementations. How to specify and how to understand the data is just
817      * contract between the user of a specific implementation and the implemtation.
818      * The configured data is effective until the data is cleared by invoking
819      * clearLayerData().
820      * The second argument @data_lan can be used for checking the contract. The
821      * implementation and the users should decide the data structure delivered by
822      * setLayerData(). If the data structure has fixed length, the implementation
823      * can determine if the delivered data is correctly configured with comparing
824      * @data_len and the expected size.
825      */
setLayerData(void * data,size_t data_len)826     void setLayerData(void *data, size_t data_len) {
827         mLayerData = data;
828         mLayerDataLen = data_len;
829     }
830 
831     /*
832      * Clears the configured layer data.
833      */
clearLayerData()834     void clearLayerData() {
835         mLayerData = nullptr;
836         mLayerDataLen = 0;
837     }
838 
839     /*
840      * MEMBER FUNCTIONS FOR THE IMPLEMENTATIONS of Acrylic
841      */
842     /*
843      * Optain the configured compositing mode
844      */
getCompositingMode()845     uint32_t getCompositingMode() { return mBlendingMode; }
846     /*
847      * Obtain the z-order of the layer
848      */
getZOrder()849     int32_t getZOrder() { return mZOrder; }
850     /*
851      * Obtain the plane alpha value of the layer
852      */
getPlaneAlpha()853     uint8_t getPlaneAlpha() { return mPlaneAlpha; }
854     /*
855      * Obtain the crop region of the layer
856      */
getImageRect()857     hw2d_rect_t getImageRect() { return mImageRect; }
858     /*
859      * Obtain the window region onto the background. If all four values are
860      * zero, the window region should be regarded as the entire background
861      * dimension.
862      */
getTargetRect()863     hw2d_rect_t getTargetRect() { return mTargetRect; }
864     /*
865      * Obtain the transform property of the layer
866      */
getTransform()867     uint32_t getTransform() { return mTransform; }
868     /*
869      * Obtain the special compositing attribute flags
870      */
getCompositAttr()871     uint32_t getCompositAttr() { return mCompositAttr; }
872     /*
873      * Obtain the minimum luminance of the display of the mastering environment
874      * of the video layer in the unit of 0.0001 cd/m^2.
875      */
getMinMasteringLuminance()876     uint16_t getMinMasteringLuminance() { return mMinLuminance; }
877     /*
878      * Obtain the maximum luminance of the display of the mastering environment
879      * of the video layer in the unit of 1 cd/m^2.
880      */
getMaxMasteringLuminance()881     uint16_t getMaxMasteringLuminance() { return mMaxLuminance; }
882     /*
883      * Copy the contents of @layer into the callee instance.
884      * Note that the file descriptors of @layer are not duplicated. The caller
885      * should care about that. Note also that importLayer() invalidates the
886      * acquire fence of @layer because it is transferred to the callee.
887      */
888     void importLayer(AcrylicLayer &layer, bool inherit_transform);
889     /*
890      * Store the given transit data to the instance. It is generated by the
891      * implementations of Acrylic class.
892      */
storeTransit(void * transitData)893     void storeTransit(void *transitData) { mTransitData = transitData; }
894     /*
895      * Retrieve the transit data from the instance.
896      */
getTransit()897     void *getTransit() { return mTransitData; }
898     /*
899      * Retrieve the layer opaque data.
900      */
getLayerData()901     void *getLayerData() { return mLayerData; }
getLayerDataLength()902     size_t getLayerDataLength() { return mLayerDataLen; }
903 private:
904     AcrylicLayer(Acrylic *compositor);
905 
906     void *mTransitData;
907     void *mLayerData;
908     size_t mLayerDataLen;
909     hw2d_rect_t mImageRect;
910     hw2d_rect_t mTargetRect;
911     uint32_t mBlendingMode;
912     uint32_t mTransform;
913     int32_t mZOrder;
914     uint32_t mCompositAttr;
915     uint16_t mMaxLuminance; // in nit
916     uint16_t mMinLuminance; // in 0.0001 nit
917     uint8_t mPlaneAlpha;
918 };
919 
920 class AcrylicPerformanceRequest;
921 
922 /*
923  * DEPRECATED:
924  * AcrylicFactory works as it did for now but it will be removed in the future
925  * because new factory methods to create an instance of Acrylic is introduced:
926  * use the followings instead of AcrylicFactory:
927  * - Acrylic::createInstance() for a specific Acrylic backend
928  * - Acrylic::createCompositor() to create the "default_compositor"
929  * - Acrylic::createScaler() to create the "default_scaler"
930  *
931  * AcrylicFactory - The creator of Acrylic
932  *
933  * Users are prohibited to create an instance of Acrylic because the does not
934  * have enough knowledge how to create. Instead, users should ask the factory
935  * to create an instance of Acrylic with a special 'name'(spec) to identify a
936  * specific HW 2D.
937  */
938 class AcrylicFactory {
939 public:
AcrylicFactory()940     AcrylicFactory() { };
~AcrylicFactory()941     ~AcrylicFactory() { };
942     /*
943      * Creator of Acrylic instance
944      */
945     static Acrylic *createAcrylic(const char *spec);
946 };
947 
948 /*
949  * Acrylic - The type of the object for 2D compositing with HW 2D
950  *
951  * Acrylic executes HW 2D with the image information specified on AcrylicLayers
952  * and AcrylicCanvas that are created by Acrylic.
953  * To create an instance of Acrylic, you should use AcrylicFactory.
954  */
955 class Acrylic {
956 public:
957     /*
958      * Factory methods of an instance of Acrylic subclasses
959      * createInstance() - create the instance exactly specified by @spec
960      * createCompositor() - create an instance of HW 2D compositor defined in board definition
961      * createScaler() - create an instance of image post processor defined in board definition
962      * createBlter() - create an instance of H/W accelerator of bit block transfer defined in board definition
963      */
964     static Acrylic *createInstance(const char *spec);
965     static Acrylic *createCompositor();
966     static Acrylic *createScaler();
967     static Acrylic *createBlter();
968 
969     Acrylic(const HW2DCapability &capability);
970     virtual ~Acrylic();
971     /*
972      * Create a new instance of AcrylicLayer. If the number of created
973      * AcrylicLayer is already mCapability.maxLayerCount(), createLayer()
974      * fails.
975      * The caller of createLayer() should destroy the instance of AcrylicLayer
976      * by itself. The instance of AcrylicLayer is only available while the
977      * Acrylic that created it lives. If the Acrylic that created AcrylicLayer
978      * has been destroyed, all configuration to AcrylicLayer have no effect.
979      */
980     AcrylicLayer *createLayer();
981     /*
982      * Obtain HW2DCapability object to study the capability fo HW 2D that the
983      * Acrylic handles.
984      */
getCapabilities()985     const HW2DCapability &getCapabilities() { return mCapability; }
986     /*
987      * Configure the image dimension of the background. The background image
988      * is not the input image with the lowest z-order but the target image in
989      * this context.
990      */
setCanvasDimension(int32_t width,int32_t height)991     bool setCanvasDimension(int32_t width, int32_t height)
992     {
993         return mCanvas.setImageDimension(width, height);
994     }
995     /*
996      * Configure the image color format and the address space of the target image
997      */
setCanvasImageType(uint32_t fmt,int dataspace)998     bool setCanvasImageType(uint32_t fmt, int dataspace)
999     {
1000         return mCanvas.setImageType(fmt, dataspace);
1001     }
1002     /*
1003      * Configure the buffers where to write the result (target) image. The
1004      * identifier of the buffer is a open file descriptor exported by dmabuf.
1005      * Users can also configure if the result image is in the compressed form
1006      * or to be protected from access by non-previliged users.
1007      */
1008     bool setCanvasBuffer(int fd[MAX_HW2D_PLANES], size_t len[MAX_HW2D_PLANES], off_t offset[MAX_HW2D_PLANES],
1009                          int num_buffers, int fence = -1, uint32_t attr = AcrylicCanvas::ATTR_NONE)
1010     {
1011         return mCanvas.setImageBuffer(fd, len, offset, num_buffers, fence, attr);
1012     }
1013     /*
1014      * Configure the buffers where to write the result (target) image.
1015      * This version ov setCanvasBuffer() is the same as the above version except
1016      * that this version does not require @offset as its argument.
1017      */
1018     bool setCanvasBuffer(int fd[MAX_HW2D_PLANES], size_t len[MAX_HW2D_PLANES], int num_buffers,
1019                          int fence = -1, uint32_t attr = AcrylicCanvas::ATTR_NONE)
1020     {
1021         return mCanvas.setImageBuffer(fd, len, num_buffers, fence, attr);
1022     }
1023     /*
1024      * Configure the buffers where to write the result (target) image. The
1025      * identifier of the buffer is a valid memory address in the address space
1026      * of the caller. AcrylicCanvas::ATTR_PROTECTED to @attr is not allowed
1027      * because userptr buffer should not be protected.
1028      */
1029     bool setCanvasBuffer(void *addr[MAX_HW2D_PLANES], size_t len[MAX_HW2D_PLANES],
1030                          int num_buffers, uint32_t attr = AcrylicCanvas::ATTR_NONE)
1031     {
1032         return mCanvas.setImageBuffer(addr, len, num_buffers, attr);
1033     }
1034     /*
1035      * Configure the target buffers as hard-wired.
1036      */
1037     bool setCanvasOTF(uint32_t attr = AcrylicCanvas::ATTR_NONE)
1038     {
1039         return mCanvas.setImageOTFBuffer(attr);
1040     }
1041     /*
1042      * Configure the default background color and the opacity. It is effecitve
1043      * when the source images are not opaque or the source images does not fill
1044      * the entire target image buffer.
1045      * Configuring the default color makes the result image more deterministic.
1046      * NOTE that the value(brightness) of each color components and the opacity
1047      * are 16-bit wide. If your system supports 8-bit brightness, you should
1048      * shift the brightness value to the left in the amount of 8 bits. Again,
1049      * you should multiply the value of each color components and the opacity
1050      * with 256 (2^8) if your color system supports 8-bit brightness levels.
1051      */
setDefaultColor(uint16_t red,uint16_t green,uint16_t blue,uint16_t alpha)1052     void setDefaultColor(uint16_t red, uint16_t green, uint16_t blue, uint16_t alpha)
1053     {
1054         mBackgroundColor.R = red;
1055         mBackgroundColor.G = green;
1056         mBackgroundColor.B = blue;
1057         mBackgroundColor.A = alpha;
1058         mHasBackgroundColor = true;
1059     }
1060     /*
1061      * Cancel the configured default color values.
1062      */
clearDefaultColor()1063     void clearDefaultColor()
1064     {
1065         mHasBackgroundColor = false;
1066     }
1067     /*
1068      * Configures cofficients the tone mapper if the user of Acrylic wants to
1069      * overrides the default coefficients of the tone mapper for HDR display.
1070      * The coefficients are a two dimensional array of coefficients of x-axis
1071      * and y-axis. If no coefficient is configured, the default function of the
1072      * implemntation of Acrylic is used. It may be ether of gamma 2.2 for SDR,
1073      * gamma 2.6 for HDR.
1074      * The configured coefficients are not enabled if no layer has HDR video
1075      * because HDR processing is not enabled.
1076      */
1077     virtual bool setHDRToneMapCoefficients(uint32_t *matrix[2], int num_elements);
1078     /*
1079      * Configure the minimum luminance and the maximum luminance of the target
1080      * display.
1081      * @min: minimum luminance in 0.0001 cd/m^2 unit
1082      * @max: maximum luminance in 1 cd/m^2 unit
1083      *
1084      * The following sare the default values:
1085      * @min: 0
1086      * @max 100 (SDR)
1087      *
1088      * NOTE:
1089      * How to handle the target display luminance depends on the implementation
1090      * of Acrylic. An implementation does not refer to the configured target
1091      * display luminance while another implementation may us the taret display
1092      * luminance to choose its HDR conversion logic.
1093      */
setTargetDisplayLuminance(uint16_t min,uint16_t max)1094     inline void setTargetDisplayLuminance(uint16_t min, uint16_t max)
1095     {
1096         mMaxTargetLuminance = max;
1097         mMinTargetLuminance = min;
1098     }
1099     /*
1100      * Configure information of target display device.
1101      */
setTargetDisplayInfo(void * data)1102     inline void setTargetDisplayInfo(void *data)
1103     {
1104         mTargetDisplayInfo = data;
1105     }
1106     /*
1107      * Run HW 2D. If @fence is not NULL and num_fences is not zero, execute()
1108      * fills the release fences to the array of @fence. The number of fences
1109      * filled by execute() is min(num_fences, mLayers.size()). If num_fences is
1110      * larger than mLayers.size(), execute() fills -1 to the rest of the elements
1111      * of @fence.
1112      * execute() returns before HW 2D completes the processing, of course.
1113      */
1114     virtual bool execute(int fence[], unsigned int num_fences) = 0;
1115     /*
1116      * Run HW 2D. This version of execute() does not provides release fences.
1117      * If @handle is NULL, execute() does not return until HW 2D completes
1118      * the processng. If @handle is not NULL, execute() returns before HW 2D
1119      * completes and stores a value(handle) to @handle. Users can wait for HW 2D
1120      * to be finished with that handle. Users does not need to wait HW 2D. Then,
1121      * they sshould release the handle with releaseHandle().
1122      */
1123     virtual bool execute(int *handle = NULL) = 0;
1124     /*
1125      * Release @handle informed by execute()
1126      */
releaseHandle(int handle)1127     virtual void releaseHandle(int __attribute__((__unused__)) handle) { }
1128     /*
1129      * Wait HW 2D to finish the processing associated with @handle. The handle
1130      * is released after the wait completes.
1131      */
1132     virtual bool waitExecution(int handle) = 0;
1133     /*
1134      * Return the last execution time of the H/W in micro seconds.
1135      * It is only vaild when the last call to execute() succeeded.
1136      */
getLaptimeUSec()1137     virtual unsigned int getLaptimeUSec() { return 0; }
1138     /*
1139      * Configure the priority of the image processing tasks requested
1140      * to this compositor object. The default priority is -1 and the
1141      * highest priority is 15. If a user configure a priority outside
1142      * of the range between -1 and 15, the user will get failure.
1143      * How the priority to be applied is the implmentation specific.
1144      * prioritize() returns a negative value (-1) on failure. On successful,
1145      * prioritize() returns zero or a positive value. An implementation may
1146      * return a positive value to inform additional information.
1147      */
1148     virtual int prioritize(int priority = -1);
1149     /*
1150      * Configure a value for guaranteeing quality of service in terms
1151      * of image processing performance. The types and ranges of the
1152      * value is not defined by libacryl but the end users including
1153      * HWC and the driver. libacryl just deliver the value to the
1154      * driver that the implementation of Acrylic is talking with.
1155      * If the implementation does not implement requestPerformanceQoS(),
1156      * calling requestPerformanceQoS() effects nothing.
1157      * The types, the size and the number of the parameters are defined
1158      * as required. They should be defined in acrylic_soc.h.
1159      */
1160     virtual bool requestPerformanceQoS(AcrylicPerformanceRequest *request);
1161     /*
1162      * Called when an AcrylicLayer is being destroyed
1163      */
1164     void removeLayer(AcrylicLayer *layer);
1165     /*
1166      * Obtains the instance AcrylicCanvas of the taret image. It is called by
1167      * the implementations of Acrylic and the test modules of Acrylic. The other
1168      * users of Acrylic do not need to call getCanvas(). Instead, consider
1169      * setCanvasDimension(), setCanvasImageType() and setCanvasBuffer().
1170      */
getCanvas()1171     AcrylicCanvas &getCanvas() { return mCanvas; }
layerCount()1172     unsigned int layerCount() { return static_cast<unsigned int>(mLayers.size()); }
1173 protected:
1174     /*
1175      * Called when an AcrylicLayer is destroyed. Unlike removeLayer(),
1176      * removeTransitData() is called whenever an AcrylicLayer which is still
1177      * associated with an instance of Acrylic is destroyed. It means that
1178      * removeTransitData() is called even when an instance of Acrylic is
1179      * destroyed to remove all transit data installed in an AcrylicLayer instance.
1180      * If an implmentation of Acrylic may install a transit data to an instance of
1181      * AcrylicLayer, it should implement removeTransitData().
1182      */
removeTransitData(AcrylicLayer * layer)1183     virtual void removeTransitData(AcrylicLayer __attribute__((__unused__)) *layer) { }
1184     bool validateAllLayers();
1185     void sortLayers();
getLayer(unsigned int index)1186     AcrylicLayer *getLayer(unsigned int index)
1187     {
1188         return (index < mLayers.size()) ? mLayers[index] : nullptr;
1189     }
getBackgroundColor(uint16_t * red,uint16_t * green,uint16_t * blue,uint16_t * alpha)1190     void getBackgroundColor(uint16_t *red, uint16_t *green, uint16_t *blue,
1191                          uint16_t *alpha)
1192     {
1193         *red   = mBackgroundColor.R;
1194         *green = mBackgroundColor.G;
1195         *blue  = mBackgroundColor.B;
1196         *alpha = mBackgroundColor.A;
1197     }
hasBackgroundColor()1198     bool hasBackgroundColor() { return mHasBackgroundColor; }
getMaxTargetDisplayLuminance()1199     uint16_t getMaxTargetDisplayLuminance() { return mMaxTargetLuminance; }
getMinTargetDisplayLuminance()1200     uint16_t getMinTargetDisplayLuminance() { return mMinTargetLuminance; }
getTargetDisplayInfo()1201     void *getTargetDisplayInfo() { return mTargetDisplayInfo; }
1202 private:
1203     std::vector<AcrylicLayer *> mLayers;
1204     const HW2DCapability &mCapability;
1205     struct {
1206         uint16_t R;
1207         uint16_t G;
1208         uint16_t B;
1209         uint16_t A;
1210     } mBackgroundColor;
1211     bool mHasBackgroundColor;
1212     uint16_t mMaxTargetLuminance;
1213     uint16_t mMinTargetLuminance;
1214     void *mTargetDisplayInfo;
1215     AcrylicCanvas mCanvas;
1216 };
1217 
1218 struct AcrylicPerformanceRequestLayer {
1219     hw2d_coord_t    mSourceDimension;
1220     uint32_t        mPixFormat;
1221     hw2d_rect_t     mSourceRect;
1222     hw2d_rect_t     mTargetRect;
1223     uint32_t        mTransform;
1224     uint32_t        mAttribute;
1225 };
1226 
1227 struct AcrylicPerformanceRequestFrame {
1228     int             mNumLayers;
1229     int             mNumAllocLayers;
1230     int             mFrameRate;
1231     uint32_t        mTargetPixFormat;
1232     hw2d_coord_t    mTargetDimension;
1233     bool            mHasBackgroundLayer;
1234     struct AcrylicPerformanceRequestLayer *mLayers;
1235 
1236     AcrylicPerformanceRequestFrame();
1237     ~AcrylicPerformanceRequestFrame();
1238 
1239     bool reset(int num_layers = 0);
1240 
setSourceDimensionAcrylicPerformanceRequestFrame1241     void setSourceDimension(int layer, int width, int height, uint32_t fmt) {
1242         if (layer < mNumLayers) {
1243             mLayers[layer].mSourceDimension.hori = width;
1244             mLayers[layer].mSourceDimension.vert = height;
1245             mLayers[layer].mPixFormat = fmt;
1246         }
1247     }
1248 
setAttributeAcrylicPerformanceRequestFrame1249     void setAttribute(int layer, uint32_t attribute) {
1250         if (layer < mNumLayers)
1251             mLayers[layer].mAttribute = attribute;
1252     }
1253 
setTransferAcrylicPerformanceRequestFrame1254     void setTransfer(int layer, hwc_rect_t &src_area, hwc_rect_t &out_area, uint32_t transform) {
1255         if (layer < mNumLayers) {
1256             mLayers[layer].mSourceRect.pos.hori = src_area.left;
1257             mLayers[layer].mSourceRect.pos.vert = src_area.top;
1258             mLayers[layer].mSourceRect.size.hori = src_area.right - src_area.left;
1259             mLayers[layer].mSourceRect.size.vert = src_area.bottom - src_area.top;
1260             mLayers[layer].mTargetRect.pos.hori = out_area.left;
1261             mLayers[layer].mTargetRect.pos.vert = out_area.top;
1262             mLayers[layer].mTargetRect.size.hori = out_area.right - out_area.left;
1263             mLayers[layer].mTargetRect.size.vert = out_area.bottom - out_area.top;
1264             mLayers[layer].mTransform = transform;
1265         }
1266     }
1267 
setTargetDimensionAcrylicPerformanceRequestFrame1268     void setTargetDimension(int width, int height, uint32_t fmt, bool bBackground) {
1269         mTargetDimension.hori = width;
1270         mTargetDimension.vert = height;
1271         mTargetPixFormat = fmt;
1272         mHasBackgroundLayer = bBackground;
1273     }
1274 
setFrameRateAcrylicPerformanceRequestFrame1275     void setFrameRate(int rate) { mFrameRate = rate; }
1276 
getLayerCountAcrylicPerformanceRequestFrame1277     int getLayerCount() { return mNumLayers; }
1278 };
1279 
1280 class AcrylicPerformanceRequest {
1281 public:
1282     AcrylicPerformanceRequest();
1283     ~AcrylicPerformanceRequest();
1284 
1285     bool reset(int num_frames = 0);
1286 
getFrameCount()1287     int getFrameCount() { return mNumFrames; }
getFrame(int idx)1288     AcrylicPerformanceRequestFrame *getFrame(int idx) { return (idx < mNumFrames) ? &mFrames[idx] : NULL; }
1289 
1290 private:
1291     int mNumFrames;
1292     int mNumAllocFrames;
1293     AcrylicPerformanceRequestFrame *mFrames;
1294 };
1295 
1296 #endif /*__HARDWARE_EXYNOS_ACRYLIC_H__*/
1297