summaryrefslogtreecommitdiffstats
path: root/exynos4/hal/libgralloc_ump/alloc_device.cpp
blob: 61be7c574adfb8e80fbc3a2419092b5d95178978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/*
 * Copyright (C) 2010 ARM Limited. All rights reserved.
 *
 * Portions of this code have been modified from the original.
 * These modifications are:
 *    * includes
 *    * gralloc_alloc_buffer()
 *    * gralloc_alloc_framebuffer_locked()
 *    * gralloc_alloc_framebuffer()
 *    * alloc_device_alloc()
 *    * alloc_device_free()
 *    * alloc_device_close()
 *    * alloc_device_open()
 *
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#include <string.h>
#include <errno.h>
#include <pthread.h>

#include <cutils/log.h>
#include <cutils/atomic.h>
#include <hardware/hardware.h>
#include <hardware/gralloc.h>
#include "sec_format.h"
#include "graphics.h"

#include "gralloc_priv.h"
#include "gralloc_helper.h"
#include "framebuffer_device.h"

#include "ump.h"
#include "ump_ref_drv.h"
#include "secion.h"

/*****************************************************************************/
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>

#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include "format.h"

#include "videodev2.h"
#include "s5p_fimc.h"

#ifdef SAMSUNG_EXYNOS4x12
#define PFX_NODE_FIMC0   "/dev/video0"
#define PFX_NODE_FIMC1   "/dev/video3"
#endif
#ifdef SAMSUNG_EXYNOS4210
#define PFX_NODE_FIMC0	"/dev/video0"
#define PFX_NODE_FIMC1   "/dev/video1"
#endif

#ifndef OMX_COLOR_FormatYUV420Planar
#define OMX_COLOR_FormatYUV420Planar 0x13
#endif

#ifndef OMX_COLOR_FormatYUV420SemiPlanar
#define OMX_COLOR_FormatYUV420SemiPlanar 0x15
#endif

bool ion_dev_open = true;
static pthread_mutex_t l_surface= PTHREAD_MUTEX_INITIALIZER;
static int buffer_offset = 0;
static unsigned int gReservedMemAddr = 0;
static int gReservedMemSize = 0;
static int gFimc1Fd = 0;

#ifdef USE_PARTIAL_FLUSH
extern struct private_handle_rect *rect_list;
extern private_handle_rect *find_rect(int secure_id);
extern private_handle_rect *find_last_rect(int secure_id);
extern int release_rect(int secure_id);
extern int count_rect(int secure_id);
extern void dump_rect();
extern void insert_rect_first(private_handle_rect *new_rect);
extern void insert_rect_last(private_handle_rect *new_rect);
#endif

extern int get_bpp(int format);

#define EXYNOS4_ALIGN( value, base ) (((value) + ((base) - 1)) & ~((base) - 1))

int gralloc_alloc_fimc1(size_t size, int usage,
                        buffer_handle_t* pHandle, int w, int h,
                        int format, int bpp, int stride_raw, int stride) {
    char node[20];
    int ret;
    int offset=0;
    struct v4l2_control vc;
    struct v4l2_format fmt;
    int rc;
    int fd;
    int lock_state=0;
    private_handle_t* hnd;
    unsigned int current_address = 0;

    if (gReservedMemAddr == 0) {
        sprintf(node, "%s", PFX_NODE_FIMC0);

        fd = open(node, O_RDWR);
        if (fd < 0) {
            ALOGE("%s:: %s Post processor open error\n", __func__, node);
            return false;
        }

        fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;

        rc = ioctl(fd, VIDIOC_G_FMT, &fmt);
        if (rc < 0) {
            ALOGE("%s:: %s VIDIOC_G_FMT error\n", __func__, node);
            return false;
        }

        vc.id = V4L2_CID_RESERVED_MEM_BASE_ADDR;
        vc.value = 0;
        ret = ioctl(fd, VIDIOC_G_CTRL, &vc);
        if (ret < 0) {
            ALOGE("%s Error in video VIDIOC_G_CTRL - V4L2_CID_RESERVED_MEM_BASE_ADDR (%d)\n", __func__, ret);
            return false;
        }
        gReservedMemAddr = (unsigned int)vc.value;

        vc.id = V4L2_CID_RESERVED_MEM_SIZE;
        vc.value = 0;
        ret = ioctl(fd, VIDIOC_G_CTRL, &vc);
        if (ret < 0) {
            ALOGE("%s Error in video VIDIOC_G_CTRL - V4L2_CID_RESERVED_MEM_SIZE (%d)\n", __func__, ret);
            return false;
        }
        gReservedMemSize = vc.value;

        if (gReservedMemSize == 0) {
            ALOGE("%s Error in video VIDIOC_G_CTRL - V4L2_CID_RESERVED_MEM_SIZE is 0\n", __func__);
            return false;
        }

        if (gReservedMemSize >= 4096) {
            gReservedMemAddr += ((gReservedMemSize - 4096) * 1024);
            gReservedMemSize = 4096;
        }

        close(fd);

        if (gFimc1Fd == 0) {
            sprintf(node, "%s", PFX_NODE_FIMC1);

            gFimc1Fd = open(node, O_RDWR);

            if (gFimc1Fd < 0) {
                ALOGE("%s:: %s Post processor open error\n", __func__, node);
                return false;
            }
        } // fimc3 setup
    } // fimc1 setup

    if ((unsigned int) (buffer_offset + size) >= (unsigned int) (gReservedMemSize * 1024))
        buffer_offset = 0;

    current_address = gReservedMemAddr + buffer_offset;

    if ( (usage < 0 || usage & (GRALLOC_USAGE_HWC_HWOVERLAY | GRALLOC_USAGE_HW_FIMC1)) &&
         (format == (int)HAL_PIXEL_FORMAT_RGBA_8888 || format == (int)HAL_PIXEL_FORMAT_RGB_565) ) {

        if (usage & GRALLOC_USAGE_PRIVATE_NONECACHE) {
            hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_NONE_CACHED | private_handle_t::PRIV_FLAGS_USES_HDMI,
                                       size, 0, private_handle_t::LOCK_STATE_MAPPED, 0, 0);
        } else {
            hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_USES_HDMI,
                                       size, 0, private_handle_t::LOCK_STATE_MAPPED, 0, 0);
        }
    } else {

        if (usage & GRALLOC_USAGE_PRIVATE_NONECACHE) {
            hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_NONE_CACHED | private_handle_t::PRIV_FLAGS_USES_IOCTL,
                                       size, 0, private_handle_t::LOCK_STATE_MAPPED, 0, 0);
        } else {
            hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_USES_IOCTL,
                                       size, 0, private_handle_t::LOCK_STATE_MAPPED, 0, 0);
        }

    }

    *pHandle = hnd;
    hnd->format = format;
    hnd->usage = usage;
    hnd->width = w;
    hnd->height = h;
    hnd->bpp = bpp;
    hnd->paddr = current_address;
    hnd->yaddr = gReservedMemSize;
    hnd->offset = buffer_offset;
    hnd->stride = stride;
    hnd->fd = gFimc1Fd;
    hnd->uoffset = (EXYNOS4_ALIGN((EXYNOS4_ALIGN(hnd->width, 16) * EXYNOS4_ALIGN(hnd->height, 16)), 4096));
    hnd->voffset = (EXYNOS4_ALIGN((EXYNOS4_ALIGN((hnd->width >> 1), 16) * EXYNOS4_ALIGN((hnd->height >> 1), 16)), 4096));

    buffer_offset += size;

    ALOGD_IF(debug_level > 0, "%s hnd=%x paddr=%x yaddr=%x offset=%x", __func__, hnd, current_address, gReservedMemSize, buffer_offset);
    return 0;
}

static int gralloc_alloc_ion(alloc_device_t *dev, size_t size, int usage,
							 int format, ion_buffer *ion_fd, ion_phys_addr_t *ion_paddr,
							 int *priv_alloc_flag, ump_handle *ump_mem_handle) {
	unsigned int ion_flags = 0;
	private_module_t* m;

    if (!ion_dev_open) {
        ALOGE("%s ERROR, failed to open ion", __func__);
        return -1;
    }

    m = reinterpret_cast<private_module_t*>(dev->common.module);

    if (usage < 0 || usage & GRALLOC_USAGE_HWC_HWOVERLAY )
    {
        if ((format == HAL_PIXEL_FORMAT_RGBA_8888) || (format == HAL_PIXEL_FORMAT_RGB_565)) {
            *priv_alloc_flag |= (private_handle_t::PRIV_FLAGS_USES_ION | private_handle_t::PRIV_FLAGS_USES_HDMI);
        } else {
            *priv_alloc_flag |= private_handle_t::PRIV_FLAGS_USES_ION;
        }
    } else {
        *priv_alloc_flag |= private_handle_t::PRIV_FLAGS_USES_ION;
    }

    if (usage & GRALLOC_USAGE_PRIVATE_NONECACHE) {
        *priv_alloc_flag |= private_handle_t::PRIV_FLAGS_NONE_CACHED;
        ion_flags = ION_EXYNOS_NONCACHE_MASK | ION_HEAP_EXYNOS_CONTIG_MASK;
    } else {
        ion_flags = ION_HEAP_EXYNOS_CONTIG_MASK;
    }

    *ion_fd = ion_alloc(m->ion_client, size, 0, ion_flags);
    if (*ion_fd < 0) {
        ALOGE("%s Failed to ion_alloc", __func__);
        return -1;
    }

    *ion_paddr = ion_getphys(m->ion_client, *ion_fd);

/* TODO: #ifdef SAMSUNG_EXYNOS_CACHE_UMP here...*/
    if (usage & GRALLOC_USAGE_PRIVATE_NONECACHE) {
        ALOGD_IF(debug_level > 0, "%s FIMC1 none", __func__);
        *ump_mem_handle = ump_ref_drv_ion_import(*ion_fd, UMP_REF_DRV_CONSTRAINT_NONE);
    } else {
        ALOGD_IF(debug_level > 0, "%s FIMC1 cached", __func__);
        *ump_mem_handle = ump_ref_drv_ion_import(*ion_fd, UMP_REF_DRV_CONSTRAINT_USE_CACHE);
        ump_cpu_msync_now((ump_handle)*ump_mem_handle, UMP_MSYNC_CLEAN_AND_INVALIDATE, NULL, 0);
    }
    return 0;
}

static int gralloc_alloc_buffer(alloc_device_t* dev, size_t size, int usage,
                                buffer_handle_t* pHandle, int w, int h,
                                int format, int bpp, int stride_raw, int stride)
{
    ump_handle ump_mem_handle;
    void *cpu_ptr;
    ump_secure_id ump_id;
    ion_buffer ion_fd = 0;
    ion_phys_addr_t ion_paddr = 0;
    int priv_alloc_flag = private_handle_t::PRIV_FLAGS_USES_UMP;
    int ret = 0;

    ALOGD_IF(debug_level > 0, "%s size=%d usage=0x%x format=0x%x\n", __func__, size, usage, format);

    size = round_up_to_page_size(size);

    if (usage & GRALLOC_USAGE_HW_FIMC1) {
        ALOGD_IF(debug_level > 0, "%s usage = GRALLOC_USAGE_HW_FIMC1", __func__);
        return gralloc_alloc_fimc1(size, usage, pHandle, w, h, format, bpp, stride_raw, stride);
    }

    ret = -1;
#if 0
    if (usage & (GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_ION)) {
        ALOGV("%s: Allocating for HWC via ION...", __func__);
        // the handle is guaranteed to have this usage flag set
        // if it is going to be used as an HWC layer (see hwcomposer.h in hardware/libhardware)
        ret = gralloc_alloc_ion(dev, size, usage, format, &ion_fd, &ion_paddr, &priv_alloc_flag, &ump_mem_handle);
    } else if (usage & (GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE)) {
        ALOGV("%s: Allocating graphicbuffer via ION...", __func__);
        priv_alloc_flag = priv_alloc_flag | private_handle_t::PRIV_FLAGS_GRAPHICBUFFER;
        ret = gralloc_alloc_ion(dev, size, usage, format, &ion_fd, &ion_paddr, &priv_alloc_flag, &ump_mem_handle);
    }
#endif

    if (ret < 0) {
        // may happen if ion carveout is out of memory, or if the
        // handle is not needed for HWC
        ALOGV("Falling back to UMP-only allocation...");
        priv_alloc_flag = private_handle_t::PRIV_FLAGS_USES_UMP;
#ifdef SAMSUNG_EXYNOS_CACHE_UMP
        if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN) {
            ALOGD_IF(debug_level > 0, "%s UMP cached", __func__);
            ump_mem_handle = ump_ref_drv_allocate(size, UMP_REF_DRV_CONSTRAINT_USE_CACHE);
            ump_cpu_msync_now((ump_handle)ump_mem_handle, UMP_MSYNC_CLEAN_AND_INVALIDATE, NULL, 0);
        } else {
            ALOGD_IF(debug_level > 0, "%s UMP none", __func__);
            ump_mem_handle = ump_ref_drv_allocate(size, UMP_REF_DRV_CONSTRAINT_NONE);
        }
#else
        ump_mem_handle = ump_ref_drv_allocate(size, UMP_REF_DRV_CONSTRAINT_NONE);
#endif
    }

    if (UMP_INVALID_MEMORY_HANDLE != ump_mem_handle) {
        cpu_ptr = ump_mapped_pointer_get(ump_mem_handle);
        if (NULL != cpu_ptr) {
            ump_id = ump_secure_id_get(ump_mem_handle);
            if (UMP_INVALID_SECURE_ID != ump_id) {
                private_handle_t* hnd;

                hnd = new private_handle_t(priv_alloc_flag, size, (int)cpu_ptr,
                private_handle_t::LOCK_STATE_MAPPED, ump_id, ump_mem_handle, ion_fd < 0 ? 0 : ion_fd, 0, 0);

                if (NULL != hnd) {
                    *pHandle = hnd;

#ifdef USE_PARTIAL_FLUSH
                    if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP) {
                        ALOGD_IF(debug_partial_flush > 0,
                            "%s: PARTIAL_FLUSH ump_id:%d === BEGIN === ump_mem_handle:%08x flags=%x usage=%x count:%d backingstore:%d",
                            __func__, hnd->ump_id, hnd->ump_mem_handle, hnd->flags, hnd->usage, count_rect(hnd->ump_id), hnd->backing_store);
                        if (debug_partial_flush > 0)
                            dump_rect();

                        private_handle_rect *psRect;
                        psRect = (private_handle_rect *)calloc(1, sizeof(private_handle_rect));
                        psRect->handle = (int)hnd->ump_id;
                        psRect->stride = stride_raw;
                        ALOGD_IF(debug_partial_flush > 0,
                            "%s: PARTIAL_FLUSH ump_id:%d === insert_rect_last === ump_mem_handle:%08x flags=%x usage=%x count:%d backingstore:%d",
                            __func__, hnd->ump_id, hnd->ump_mem_handle, hnd->flags, hnd->usage, count_rect(hnd->ump_id), hnd->backing_store);
                        insert_rect_last(psRect);

                        if (debug_partial_flush > 0)
                            dump_rect();
                        ALOGD_IF(debug_partial_flush > 0,
                            "%s: PARTIAL_FLUSH ump_id:%d === END === ump_mem_handle:%08x flags=%x usage=%x count:%d backingstore:%d",
                            __func__, hnd->ump_id, hnd->ump_mem_handle, hnd->flags, hnd->usage, count_rect(hnd->ump_id), hnd->backing_store);
                    }
#endif

                    hnd->backing_store = ump_id;
                    hnd->format = format;
                    hnd->usage = usage;
                    hnd->width = w;
                    hnd->height = h;
                    hnd->bpp = bpp;
                    hnd->stride = stride;
                    hnd->uoffset = ((EXYNOS4_ALIGN(hnd->width, 16) * EXYNOS4_ALIGN(hnd->height, 16)));
                    hnd->voffset = ((EXYNOS4_ALIGN((hnd->width / 2), 16) * EXYNOS4_ALIGN((hnd->height / 2), 16)));
                    hnd->paddr = ion_paddr;
                    if (ion_fd >= 0)
                        hnd->ion_memory = ion_map(ion_fd, size, 0);

                    ALOGD_IF(debug_level > 0, "%s hnd->format=0x%x hnd->uoffset=%d hnd->voffset=%d hnd->paddr=%x hnd->bpp=%d", __func__, hnd->format, hnd->uoffset, hnd->voffset, hnd->paddr, hnd->bpp);

                    ALOGD_IF(debug_level > 1, "%s: ump_id:%d", __func__, hnd->ump_id);
                    ALOGD_IF(debug_partial_flush > 0, "%s: PARTIAL_FLUSH ump_id:%d ump_mem_handle:%08x flags=%x usage=%x", __func__, hnd->ump_id, hnd->ump_mem_handle, hnd->flags, hnd->usage);


/*
                    if (hnd->flags & private_handle_t::PRIV_FLAGS_GRAPHICBUFFER) {
                        ALOGD_IF(debug_level > 0, "%s: GraphicBuffer (ump_id:%d): ump_reference_add ump_mem_handle:%08x", __func__, ump_id, ump_mem_handle);
                        ump_reference_add(ump_mem_handle);
                    }
*/
                    return 0;
                } else {
                    ALOGE("%s failed to allocate handle", __func__);
                }
            } else {
                ALOGE("%s failed to retrieve valid secure id", __func__);
            }

            ump_mapped_pointer_release(ump_mem_handle);
        } else {
            ALOGE("%s failed to map UMP memory", __func__);
        }

        ump_reference_release(ump_mem_handle);
    } else {
        ALOGE("%s failed to allocate UMP memory", __func__);
    }

    return -1;
}

static int gralloc_alloc_framebuffer_locked(alloc_device_t* dev, size_t size, int usage,
                                            buffer_handle_t* pHandle, int w, int h,
                                            int format, int bpp, int stride)
{
    ALOGV("%s: size:%d usage:%d w:%d h:%d format:%d bpp:%d ",
        __func__, size, usage, w,h, format, bpp);
    private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module);

    ALOGD_IF(debug_level > 0, "%s size=0x%x usage=0x%x w=%d h=%d format=0x%x(%d) bpp=%d stride=%d", __func__, size, usage, w, h, format, format, bpp, stride);

    /* allocate the framebuffer */
    if (m->framebuffer == NULL) {
        /* initialize the framebuffer, the framebuffer is mapped once and forever. */
        int err = init_frame_buffer_locked(m);
        if (err < 0)
        {
            return err;
        }
    }

    const uint32_t bufferMask = m->bufferMask;
    const uint32_t numBuffers = m->numBuffers;
    const size_t bufferSize = m->finfo.line_length * m->info.yres;
    if (numBuffers == 1) {
        /*
         * If we have only one buffer, we never use page-flipping. Instead,
         * we return a regular buffer which will be memcpy'ed to the main
         * screen when post is called.
         */
        int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
        ALOGE("%s fallback to single buffering", __func__);
        return gralloc_alloc_buffer(dev, bufferSize, newUsage, pHandle, w, h, format, bpp, 0, 0);
    }

    if (bufferMask >= ((1LU<<numBuffers)-1))
    {
        // We ran out of buffers.
        ALOGE("%s Ran out of buffers",__func__); //wjj added
        return -ENOMEM;
    }

    int current = m->framebuffer->base;
    int vaddr = m->finfo.smem_start - current;
    int l_paddr = m->finfo.smem_start;

    ALOGD_IF(debug_level > 0, "%s current=0x%x vaddr=0x%x l_paddr=0x%x before", __func__, current, vaddr, l_paddr);

    /* find a free slot */
    for (uint32_t i = 0; i < numBuffers; i++) {
        if ((bufferMask & (1LU<<i)) == 0) {
            m->bufferMask |= (1LU<<i);
            break;
        }
        current += bufferSize;
        l_paddr = vaddr + current;
    }

    ALOGD_IF(debug_level > 0, "%s current=0x%x vaddr=0x%x l_paddr=0x%x after", __func__, current, vaddr, l_paddr);

    /*
     * The entire framebuffer memory is already mapped,
     * now create a buffer object for parts of this memory
     */
    private_handle_t* hnd = new private_handle_t
            (private_handle_t::PRIV_FLAGS_FRAMEBUFFER, size, current,
             0, dup(m->framebuffer->fd), current - m->framebuffer->base);

    hnd->format = format;
    hnd->usage = usage;
    hnd->width = w;
    hnd->height = h;
    hnd->bpp = bpp;
    hnd->backing_store = 0;
    hnd->paddr = l_paddr;
    hnd->stride = stride;

    *pHandle = hnd;

    return 0;
}

static int gralloc_alloc_framebuffer(alloc_device_t* dev, size_t size, int usage,
                                     buffer_handle_t* pHandle, int w, int h,
                                     int format, int bpp, int stride)
{
    ALOGV("%s: size:%d usage:%d w:%d h:%d format:%d bpp:%d",
        __func__, size, usage, w,h, format, bpp);

    private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module);
    pthread_mutex_lock(&m->lock);
    int err = gralloc_alloc_framebuffer_locked(dev, size, usage, pHandle, w, h, format, bpp, stride);
    pthread_mutex_unlock(&m->lock);
    return err;
}

static int alloc_device_alloc(alloc_device_t* dev, int w, int h, int format,
                              int usage, buffer_handle_t* pHandle, int* pStride)
{
    ALOGV("%s: usage:%d w:%d h:%d format:%d ",
        __func__, usage, w,h, format);

    int l_usage = usage;
    int bpp = 0;

    if (!pHandle || !pStride || w < 0 || h < 0)
        return -EINVAL;

    ALOGD_IF(debug_level > 0, "%s w=%d h=%d format=0x%x usage=0x%x", __func__, w, h, format, usage);

    size_t size = 0;
    size_t stride = 0;
    size_t stride_raw = 0;

    if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP ||
        format == HAL_PIXEL_FORMAT_YCrCb_420_SP ||
        format == HAL_PIXEL_FORMAT_YCbCr_422_SP ||
        format == HAL_PIXEL_FORMAT_YCbCr_420_P  ||
        format == HAL_PIXEL_FORMAT_YV12 ||
        format == HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP ||
        format == HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP_TILED ||
        format == GGL_PIXEL_FORMAT_L_8 ||
        format == OMX_COLOR_FormatYUV420Planar ||
        format == OMX_COLOR_FormatYUV420SemiPlanar ||
        /* TODO: find constants for these literals, it's no problem if they are duplicated */
        format == 0x200 ||
        format == 0x201 ||
        format == 0x202 ||
        format == 0x203 ||
        format == 0x204 ||
        format == 0x205 ||
        format == 0x206 ||
        format == 0x207 ||
        format == 0x208 ||
        format == 0x209 ||
        format == 0x210 ||
        format == 0x211 ||
        format == 0x212 ||
        format == 0x213 ||
        format == 0x214 ||
        format == 0x215 ) {

        /* FIXME: there is no way to return the vstride */
        int vstride = 0;

        stride = EXYNOS4_ALIGN(w, 16);
        vstride = EXYNOS4_ALIGN(h, 16);

        /* take borrowed logic which was deactivated in https://github.com/NamelessRom/android_frameworks_native/commit/4860d41c044e0cff732fbfa13d83af76fc37e1b5 */
        if ((format == HAL_PIXEL_FORMAT_YCbCr_420_P) ||
            (format == HAL_PIXEL_FORMAT_YCbCr_420_SP) ||
            (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)) {
            // 0x101 = HAL_PIXEL_FORMAT_YCbCr_420_P (Samsung-specific pixel format)
            // 0x105 = HAL_PIXEL_FORMAT_YCbCr_420_SP (Samsung-specific pixel format)
            // 0x107 = HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED (Samsung-specific pixel format)

            /* We need at least 0x1802930 for playing video */
            if (!(l_usage & GRALLOC_USAGE_HW_ION)) {
                l_usage |= GRALLOC_USAGE_HW_ION; // Exynos HWC wants ION-friendly memory allocation
                ALOGD("%s added usage GRALLOC_USAGE_HW_ION because of format\n", __func__);
            }

            if (!(l_usage & GRALLOC_USAGE_PRIVATE_NONECACHE)) {
                l_usage |= GRALLOC_USAGE_PRIVATE_NONECACHE; // Exynos HWC wants ION-friendly memory allocation
                ALOGD("%s added usage GRALLOC_USAGE_PRIVATE_NONECACHE because of format\n", __func__);
            }

            if (!(l_usage & GRALLOC_USAGE_SW_WRITE_OFTEN)) {
                l_usage |= GRALLOC_USAGE_SW_WRITE_OFTEN; // Exynos HWC wants ION-friendly memory allocation
                ALOGD("%s added usage GRALLOC_USAGE_SW_WRITE_OFTEN because of format\n", __func__);
            }
        }

        switch (format) {
        case HAL_PIXEL_FORMAT_YV12: //0x32315659
            l_usage |= GRALLOC_USAGE_HW_ION;
            ALOGD("%s added GRALLOC_USAGE_HW_ION because of YV12", __func__);

        case 0x200:
        case 0x201:
        case 0x202:
        case 0x203:
        case 0x204:
        case 0x205:
        case 0x206:
        case 0x207:
        case 0x208:
        case 0x209:
        case 0x210:
        case 0x211:
        case 0x212:
        case 0x213:
        case 0x214:
        case 0x215:
        case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP: //0x111
        case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP_TILED: //0x112
        case HAL_PIXEL_FORMAT_YCbCr_420_SP: //0x105
        case HAL_PIXEL_FORMAT_YCrCb_420_SP: //0x11
        case HAL_PIXEL_FORMAT_YCbCr_420_P: //0x101
            //lc_20a4
            size = (stride * vstride) + ((EXYNOS4_ALIGN(w/2,16) * EXYNOS4_ALIGN(h/2,16)) * 2);
            //stride_raw = l_usage & GRALLOC_USAGE_HW_ION; //It is later overwritten to 0

            if (l_usage & GRALLOC_USAGE_HW_ION)
                size += (PAGE_SIZE * 2);

            break;

        case HAL_PIXEL_FORMAT_YCbCr_422_SP: //0x10
            size = (stride * vstride) + EXYNOS4_ALIGN(w * h / 2, 2);
            break;

        case GGL_PIXEL_FORMAT_L_8: //0x9 , defined in system/core/include/pixelflinger/format.h
            size = (stride * vstride);
            break;

        /* These two formats are not in stock XXUUMK6 */
        case OMX_COLOR_FormatYUV420Planar: //0x13
        case OMX_COLOR_FormatYUV420SemiPlanar: //0x15
            size = stride * vstride + EXYNOS4_ALIGN((w / 2), 16) * EXYNOS4_ALIGN((h / 2), 16) * 2;
            if (l_usage & GRALLOC_USAGE_HW_ION)
                size += PAGE_SIZE * 2;

            break;

        default:
            return -EINVAL;
        }

    } else {
        ALOGD_IF(debug_level > 0, "%s about to get bpp", __func__);
        bpp = get_bpp(format);
        if (bpp == 0)
            return -EINVAL;

        stride_raw = EXYNOS4_ALIGN(w*bpp,8);
        size = stride_raw * h;
        stride = stride_raw / bpp;
    }

    int err;

    pthread_mutex_lock(&l_surface);
    if (l_usage & GRALLOC_USAGE_HW_FB)
        err = gralloc_alloc_framebuffer(dev, size, l_usage, pHandle, w, h, format, 32, stride);
    else
        err = gralloc_alloc_buffer(dev, size, l_usage, pHandle, w, h, format, 0, (int)stride_raw, (int)stride);

    pthread_mutex_unlock(&l_surface);

    if (err < 0)
        return err;

    *pStride = stride;
    return 0;
}

static int alloc_device_free(alloc_device_t* dev, buffer_handle_t handle)
{
    if (private_handle_t::validate(handle) < 0)
        return -EINVAL;

    ALOGD_IF(debug_level > 0, "%s",__func__);

    private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
    private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module);
    ALOGD_IF(debug_partial_flush > 0, "%s: PARTIAL_FLUSH ump_id:%d ump_mem_handle:%08x flags=%x usage=%x", __func__, hnd->ump_id, hnd->ump_mem_handle, hnd->flags, hnd->usage);

    ALOGD_IF(debug_level > 1, "%s: ump_id:%d", __func__, hnd->ump_id);
    pthread_mutex_lock(&l_surface);
    if (hnd->flags & private_handle_t::PRIV_FLAGS_GRAPHICBUFFER) {
        ALOGD_IF(debug_level > 0, "%s: GraphicBuffer (ump_id:%d): Freeing ump_mem_handle:%08x", __func__, hnd->ump_id, hnd->ump_mem_handle);
    }

    if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
        /* free this buffer */
        const size_t bufferSize = m->finfo.line_length * m->info.yres;
        int index = (hnd->base - m->framebuffer->base) / bufferSize;
#ifndef USE_GRALLOC_HIDL
        m->bufferMask &= ~(1<<index);
#endif
        close(hnd->fd);

    } else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP) {
        ALOGD_IF(debug_level > 0, "%s hnd->ump_mem_handle:%08x hnd->ump_id=%d", __func__, hnd->ump_mem_handle, hnd->ump_id);

#ifdef USE_PARTIAL_FLUSH
        ALOGD_IF(debug_partial_flush > 0,
            "%s: PARTIAL_FLUSH ump_id:%d === BEGIN === ump_mem_handle:%08x flags=%x usage=%x count:%d backingstore:%d",
            __func__, hnd->ump_id, hnd->ump_mem_handle, hnd->flags, hnd->usage, count_rect(hnd->ump_id), hnd->backing_store);
        if (debug_partial_flush > 0)
            dump_rect();

        ALOGD_IF(debug_partial_flush > 0,
            "%s: PARTIAL_FLUSH ump_id:%d === release_rect === ump_mem_handle:%08x flags=%x usage=%x count:%d backingstore:%d",
            __func__, hnd->ump_id, hnd->ump_mem_handle, hnd->flags, hnd->usage, count_rect(hnd->ump_id), hnd->backing_store);
        if (!release_rect((int)hnd->ump_id))
            ALOGE("%s: PARTIAL_FLUSH secure id: 0x%d, release error", __func__, (int)hnd->ump_id);

        if (debug_partial_flush > 0)
            dump_rect();
        ALOGD_IF(debug_partial_flush > 0,
            "%s: PARTIAL_FLUSH ump_id:%d === END === ump_mem_handle:%08x flags=%x usage=%x count:%d backingstore:%d",
            __func__, hnd->ump_id, hnd->ump_mem_handle, hnd->flags, hnd->usage, count_rect(hnd->ump_id), hnd->backing_store);
#endif

        ump_mapped_pointer_release((ump_handle)hnd->ump_mem_handle);
        ump_reference_release((ump_handle)hnd->ump_mem_handle);
    }

    if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) {
        if (hnd->ion_memory != NULL)
            munmap(hnd->ion_memory, hnd->size);
        ion_free(hnd->fd);
    }

    pthread_mutex_unlock(&l_surface);
    delete hnd;

    return 0;
}

static int alloc_device_close(struct hw_device_t *device)
{
    ALOGD_IF(debug_level > 0, "%s",__func__);

    alloc_device_t* dev = reinterpret_cast<alloc_device_t*>(device);
    if (dev) {
        private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module);
        if (ion_dev_open)
            ion_client_destroy(m->ion_client);
        delete dev;
        ump_close();
    }
    return 0;
}

int alloc_device_open(hw_module_t const* module, const char* name __unused, hw_device_t** device)
{
    alloc_device_t *dev;

    dev = new alloc_device_t;
    if (NULL == dev)
        return -1;

    ALOGD_IF(debug_level > 0, "%s",__func__);

    dev->common.module = const_cast<hw_module_t*>(module);
    private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module);

    m->ion_client = ion_client_create();
    ump_result ump_res = ump_open();

    if (0 > m->ion_client)
        ion_dev_open = false;

    if (UMP_OK != ump_res) {
        ALOGE("%s UMP open failed ump_res %d", __func__, ump_res);
        delete dev;
        return -1;
    }

    /* initialize our state here */
    memset(dev, 0, sizeof(*dev));

    /* initialize the procs */
    dev->common.tag = HARDWARE_DEVICE_TAG;
    dev->common.version = 0;
    dev->common.module = const_cast<hw_module_t*>(module);
    dev->common.close = alloc_device_close;
    dev->alloc = alloc_device_alloc;
    dev->free = alloc_device_free;

    *device = &dev->common;

    return 0;
}