1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <assert.h>
30#include <stdlib.h>
31#include <stdio.h>
32#include <string.h>
33#include <inttypes.h>
34
35#define ARGB_8888_MAX   0xFFFFFFFF
36#define ARGB_8888_MIN   0x00000000
37#define RGB_565_MAX     0xFFFF
38#define RGB_565_MIN     0x0000
39
40struct test_t
41{
42    char name[256];
43    uint32_t src_color;
44    uint16_t dst_color;
45    size_t count;
46};
47
48struct test_t tests[] =
49{
50    {"Count 0", 0, 0, 0},
51    {"Count 1, Src=Max, Dst=Min", ARGB_8888_MAX, RGB_565_MIN, 1},
52    {"Count 2, Src=Min, Dst=Max", ARGB_8888_MIN, RGB_565_MAX, 2},
53    {"Count 3, Src=Max, Dst=Max", ARGB_8888_MAX, RGB_565_MAX, 3},
54    {"Count 4, Src=Min, Dst=Min", ARGB_8888_MAX, RGB_565_MAX, 4},
55    {"Count 1, Src=Rand, Dst=Rand", 0x12345678, 0x9ABC, 1},
56    {"Count 2, Src=Rand, Dst=Rand", 0xABCDEF12, 0x2345, 2},
57    {"Count 3, Src=Rand, Dst=Rand", 0x11111111, 0xEDFE, 3},
58    {"Count 4, Src=Rand, Dst=Rand", 0x12345678, 0x9ABC, 4},
59    {"Count 5, Src=Rand, Dst=Rand", 0xEFEFFEFE, 0xFACC, 5},
60    {"Count 10, Src=Rand, Dst=Rand", 0x12345678, 0x9ABC, 10}
61
62};
63
64void scanline_t32cb16blend_mips(uint16_t*, uint32_t*, size_t);
65void scanline_t32cb16blend_c(uint16_t * dst, uint32_t* src, size_t count)
66{
67    while (count--)
68    {
69        uint16_t d = *dst;
70        uint32_t s = *src++;
71        int dstR = (d>>11)&0x1f;
72        int dstG = (d>>5)&0x3f;
73        int dstB = (d)&0x1f;
74        int srcR = (s >> (   3))&0x1F;
75        int srcG = (s >> ( 8+2))&0x3F;
76        int srcB = (s >> (16+3))&0x1F;
77        int srcAlpha = (s>>24) & 0xFF;
78
79
80        int f = 0x100 - (srcAlpha + ((srcAlpha>>7) & 0x1));
81        srcR += (f*dstR)>>8;
82        srcG += (f*dstG)>>8;
83        srcB += (f*dstB)>>8;
84        // srcR = srcR > 0x1F? 0x1F: srcR;
85        // srcG = srcG > 0x3F? 0x3F: srcG;
86        // srcB = srcB > 0x1F? 0x1F: srcB;
87        *dst++ = (uint16_t)((srcR<<11)|(srcG<<5)|srcB);
88    }
89}
90
91void scanline_t32cb16blend_test()
92{
93    uint16_t dst_c[16], dst_asm[16];
94    uint32_t src[16];
95    uint32_t i;
96    uint32_t  j;
97
98    for(i = 0; i < sizeof(tests)/sizeof(struct test_t); ++i)
99    {
100        struct test_t test = tests[i];
101
102        printf("Testing - %s:",test.name);
103
104        memset(dst_c, 0, sizeof(dst_c));
105        memset(dst_asm, 0, sizeof(dst_asm));
106
107        for(j = 0; j < test.count; ++j)
108        {
109            dst_c[j]   = test.dst_color;
110            dst_asm[j] = test.dst_color;
111            src[j] = test.src_color;
112        }
113
114        scanline_t32cb16blend_c(dst_c,src,test.count);
115        scanline_t32cb16blend_mips(dst_asm,src,test.count);
116
117
118        if(memcmp(dst_c, dst_asm, sizeof(dst_c)) == 0)
119            printf("Passed\n");
120        else
121            printf("Failed\n");
122
123        for(j = 0; j < test.count; ++j)
124        {
125            printf("dst_c[%d] = %x, dst_asm[%d] = %x \n", j, dst_c[j], j, dst_asm[j]);
126        }
127    }
128}
129
130int main()
131{
132    scanline_t32cb16blend_test();
133    return 0;
134}
135