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
36#define ARGB_8888_MAX   0xFFFFFFFF
37#define ARGB_8888_MIN   0x00000000
38#define RGB_565_MAX 0xFFFF
39#define RGB_565_MIN 0x0000
40
41struct test_t
42{
43    char name[256];
44    uint32_t src_color;
45    uint16_t dst_color;
46    size_t count;
47};
48
49struct test_t tests[] =
50{
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
63void scanline_col32cb16blend_mips(uint16_t *dst, uint32_t src, size_t count);
64void scanline_col32cb16blend_c(uint16_t * dst, uint32_t src, size_t count)
65{
66    uint32_t srcAlpha = (src>>24);
67    uint32_t f = 0x100 - (srcAlpha + (srcAlpha>>7));
68
69    while (count--)
70    {
71        uint16_t d = *dst;
72        int dstR = (d>>11)&0x1f;
73        int dstG = (d>>5)&0x3f;
74        int dstB = (d)&0x1f;
75        int srcR = (src >> (   3))&0x1F;
76        int srcG = (src >> ( 8+2))&0x3F;
77        int srcB = (src >> (16+3))&0x1F;
78        srcR += (f*dstR)>>8;
79        srcG += (f*dstG)>>8;
80        srcB += (f*dstB)>>8;
81        *dst++ = (uint16_t)((srcR<<11)|(srcG<<5)|srcB);
82    }
83}
84
85void scanline_col32cb16blend_test()
86{
87    uint16_t dst_c[16], dst_asm[16];
88    uint32_t i, j;
89
90    for(i = 0; i < sizeof(tests)/sizeof(struct test_t); ++i)
91    {
92        struct test_t test = tests[i];
93
94        printf("Testing - %s:",test.name);
95
96        memset(dst_c, 0, sizeof(dst_c));
97        memset(dst_asm, 0, sizeof(dst_asm));
98
99        for(j = 0; j < test.count; ++j)
100        {
101            dst_c[j]   = test.dst_color;
102            dst_asm[j] = test.dst_color;
103        }
104
105
106        scanline_col32cb16blend_c(dst_c, test.src_color, test.count);
107        scanline_col32cb16blend_mips(dst_asm, test.src_color, test.count);
108
109        if(memcmp(dst_c, dst_asm, sizeof(dst_c)) == 0)
110            printf("Passed\n");
111        else
112            printf("Failed\n");
113
114        for(j = 0; j < test.count; ++j)
115        {
116            printf("dst_c[%d] = %x, dst_asm[%d] = %x \n", j, dst_c[j], j, dst_asm[j]);
117        }
118    }
119}
120
121int main()
122{
123    scanline_col32cb16blend_test();
124    return 0;
125}
126