Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2007 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 #include <ui/PixelFormat.h>
     18 #include <hardware/hardware.h>
     19 
     20 // ----------------------------------------------------------------------------
     21 namespace android {
     22 // ----------------------------------------------------------------------------
     23 
     24 static const int COMPONENT_YUV = 0xFF;
     25 
     26 struct Info {
     27     size_t      size;
     28     size_t      bitsPerPixel;
     29     struct {
     30         uint8_t     ah;
     31         uint8_t     al;
     32         uint8_t     rh;
     33         uint8_t     rl;
     34         uint8_t     gh;
     35         uint8_t     gl;
     36         uint8_t     bh;
     37         uint8_t     bl;
     38     };
     39     uint8_t     components;
     40 };
     41 
     42 static Info const sPixelFormatInfos[] = {
     43         { 0,  0, { 0, 0,   0, 0,   0, 0,   0, 0 }, 0 },
     44         { 4, 32, {32,24,   8, 0,  16, 8,  24,16 }, PixelFormatInfo::RGBA },
     45         { 4, 24, { 0, 0,   8, 0,  16, 8,  24,16 }, PixelFormatInfo::RGB  },
     46         { 3, 24, { 0, 0,   8, 0,  16, 8,  24,16 }, PixelFormatInfo::RGB  },
     47         { 2, 16, { 0, 0,  16,11,  11, 5,   5, 0 }, PixelFormatInfo::RGB  },
     48         { 4, 32, {32,24,  24,16,  16, 8,   8, 0 }, PixelFormatInfo::RGBA },
     49         { 2, 16, { 1, 0,  16,11,  11, 6,   6, 1 }, PixelFormatInfo::RGBA },
     50         { 2, 16, { 4, 0,  16,12,  12, 8,   8, 4 }, PixelFormatInfo::RGBA },
     51         { 1,  8, { 8, 0,   0, 0,   0, 0,   0, 0 }, PixelFormatInfo::ALPHA},
     52         { 1,  8, { 0, 0,   8, 0,   8, 0,   8, 0 }, PixelFormatInfo::L    },
     53         { 2, 16, {16, 8,   8, 0,   8, 0,   8, 0 }, PixelFormatInfo::LA   },
     54         { 1,  8, { 0, 0,   8, 5,   5, 2,   2, 0 }, PixelFormatInfo::RGB  },
     55 };
     56 
     57 static const Info* gGetPixelFormatTable(size_t* numEntries) {
     58     if (numEntries) {
     59         *numEntries = sizeof(sPixelFormatInfos)/sizeof(Info);
     60     }
     61     return sPixelFormatInfos;
     62 }
     63 
     64 // ----------------------------------------------------------------------------
     65 
     66 size_t PixelFormatInfo::getScanlineSize(unsigned int width) const
     67 {
     68     size_t size;
     69     if (components == COMPONENT_YUV) {
     70         // YCbCr formats are different.
     71         size = (width * bitsPerPixel)>>3;
     72     } else {
     73         size = width * bytesPerPixel;
     74     }
     75     return size;
     76 }
     77 
     78 ssize_t bytesPerPixel(PixelFormat format)
     79 {
     80     PixelFormatInfo info;
     81     status_t err = getPixelFormatInfo(format, &info);
     82     return (err < 0) ? err : info.bytesPerPixel;
     83 }
     84 
     85 ssize_t bitsPerPixel(PixelFormat format)
     86 {
     87     PixelFormatInfo info;
     88     status_t err = getPixelFormatInfo(format, &info);
     89     return (err < 0) ? err : info.bitsPerPixel;
     90 }
     91 
     92 status_t getPixelFormatInfo(PixelFormat format, PixelFormatInfo* info)
     93 {
     94     if (format <= 0)
     95         return BAD_VALUE;
     96 
     97     if (info->version != sizeof(PixelFormatInfo))
     98         return INVALID_OPERATION;
     99 
    100     // YUV format from the HAL are handled here
    101     switch (format) {
    102     case HAL_PIXEL_FORMAT_YCbCr_422_SP:
    103     case HAL_PIXEL_FORMAT_YCbCr_422_I:
    104         info->bitsPerPixel = 16;
    105         goto done;
    106     case HAL_PIXEL_FORMAT_YCrCb_420_SP:
    107     case HAL_PIXEL_FORMAT_YV12:
    108         info->bitsPerPixel = 12;
    109      done:
    110         info->format = format;
    111         info->components = COMPONENT_YUV;
    112         info->bytesPerPixel = 1;
    113         info->h_alpha = 0;
    114         info->l_alpha = 0;
    115         info->h_red = info->h_green = info->h_blue = 8;
    116         info->l_red = info->l_green = info->l_blue = 0;
    117         return NO_ERROR;
    118     }
    119 
    120     size_t numEntries;
    121     const Info *i = gGetPixelFormatTable(&numEntries) + format;
    122     bool valid = uint32_t(format) < numEntries;
    123     if (!valid) {
    124         return BAD_INDEX;
    125     }
    126 
    127     info->format = format;
    128     info->bytesPerPixel = i->size;
    129     info->bitsPerPixel  = i->bitsPerPixel;
    130     info->h_alpha       = i->ah;
    131     info->l_alpha       = i->al;
    132     info->h_red         = i->rh;
    133     info->l_red         = i->rl;
    134     info->h_green       = i->gh;
    135     info->l_green       = i->gl;
    136     info->h_blue        = i->bh;
    137     info->l_blue        = i->bl;
    138     info->components    = i->components;
    139 
    140     return NO_ERROR;
    141 }
    142 
    143 // ----------------------------------------------------------------------------
    144 }; // namespace android
    145 // ----------------------------------------------------------------------------
    146 
    147