• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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 package android.graphics;
18 
19 import android.annotation.IntDef;
20 import android.ravenwood.annotation.RavenwoodKeepWholeClass;
21 
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 
25 @RavenwoodKeepWholeClass
26 public class PixelFormat {
27     /** @hide */
28     @IntDef({UNKNOWN, TRANSLUCENT, TRANSPARENT, OPAQUE})
29     @Retention(RetentionPolicy.SOURCE)
30     public @interface Opacity {}
31 
32     /** @hide */
33     @Retention(RetentionPolicy.SOURCE)
34     @IntDef({RGBA_8888, RGBX_8888, RGBA_F16, RGBA_1010102, RGB_888, RGB_565, R_8})
35     public @interface Format { }
36 
37     // NOTE: these constants must match the values from graphics/common/x.x/types.hal
38 
39     public static final int UNKNOWN      = 0;
40 
41     /** System chooses a format that supports translucency (many alpha bits) */
42     public static final int TRANSLUCENT  = -3;
43 
44     /**
45      * System chooses a format that supports transparency
46      * (at least 1 alpha bit)
47      */
48     public static final int TRANSPARENT  = -2;
49 
50     /** System chooses an opaque format (no alpha bits required) */
51     public static final int OPAQUE       = -1;
52 
53     public static final int RGBA_8888    = 1;
54     public static final int RGBX_8888    = 2;
55     public static final int RGB_888      = 3;
56     public static final int RGB_565      = 4;
57 
58     @Deprecated
59     public static final int RGBA_5551    = 6;
60     @Deprecated
61     public static final int RGBA_4444    = 7;
62     @Deprecated
63     public static final int A_8          = 8;
64     @Deprecated
65     public static final int L_8          = 9;
66     @Deprecated
67     public static final int LA_88        = 0xA;
68     @Deprecated
69     public static final int RGB_332      = 0xB;
70 
71     /**
72      * @deprecated use {@link android.graphics.ImageFormat#NV16
73      * ImageFormat.NV16} instead.
74      */
75     @Deprecated
76     public static final int YCbCr_422_SP = 0x10;
77 
78     /**
79      * @deprecated use {@link android.graphics.ImageFormat#NV21
80      * ImageFormat.NV21} instead.
81      */
82     @Deprecated
83     public static final int YCbCr_420_SP = 0x11;
84 
85     /**
86      * @deprecated use {@link android.graphics.ImageFormat#YUY2
87      * ImageFormat.YUY2} instead.
88      */
89     @Deprecated
90     public static final int YCbCr_422_I  = 0x14;
91 
92     public static final int RGBA_F16     = 0x16;
93     public static final int RGBA_1010102 = 0x2B;
94 
95     /** @hide */
96     public static final int HSV_888 = 0x37;
97 
98     /** @hide */
99     public static final int R_8 = 0x38;
100 
101     /**
102      * @deprecated use {@link android.graphics.ImageFormat#JPEG
103      * ImageFormat.JPEG} instead.
104      */
105     @Deprecated
106     public static final int JPEG        = 0x100;
107 
108     public int bytesPerPixel;
109     public int bitsPerPixel;
110 
getPixelFormatInfo(@ormat int format, PixelFormat info)111     public static void getPixelFormatInfo(@Format int format, PixelFormat info) {
112         switch (format) {
113             case RGBA_8888:
114             case RGBX_8888:
115             case RGBA_1010102:
116                 info.bitsPerPixel = 32;
117                 info.bytesPerPixel = 4;
118                 break;
119             case RGB_888:
120             case HSV_888:
121                 info.bitsPerPixel = 24;
122                 info.bytesPerPixel = 3;
123                 break;
124             case RGB_565:
125             case RGBA_5551:
126             case RGBA_4444:
127             case LA_88:
128                 info.bitsPerPixel = 16;
129                 info.bytesPerPixel = 2;
130                 break;
131             case A_8:
132             case L_8:
133             case RGB_332:
134                 info.bitsPerPixel = 8;
135                 info.bytesPerPixel = 1;
136                 break;
137             case YCbCr_422_SP:
138             case YCbCr_422_I:
139                 info.bitsPerPixel = 16;
140                 info.bytesPerPixel = 1;
141                 break;
142             case YCbCr_420_SP:
143                 info.bitsPerPixel = 12;
144                 info.bytesPerPixel = 1;
145                 break;
146             case RGBA_F16:
147                 info.bitsPerPixel = 64;
148                 info.bytesPerPixel = 8;
149                 break;
150             case R_8:
151                 info.bitsPerPixel = 8;
152                 info.bytesPerPixel = 1;
153                 break;
154             default:
155                 throw new IllegalArgumentException("unknown pixel format " + format);
156         }
157     }
158 
formatHasAlpha(@ormat int format)159     public static boolean formatHasAlpha(@Format int format) {
160         switch (format) {
161             case PixelFormat.A_8:
162             case PixelFormat.LA_88:
163             case PixelFormat.RGBA_4444:
164             case PixelFormat.RGBA_5551:
165             case PixelFormat.RGBA_8888:
166             case PixelFormat.RGBA_F16:
167             case PixelFormat.RGBA_1010102:
168             case PixelFormat.TRANSLUCENT:
169             case PixelFormat.TRANSPARENT:
170                 return true;
171         }
172         return false;
173     }
174 
175     /**
176      * Determine whether or not this is a public-visible and non-deprecated {@code format}.
177      *
178      * <p>In particular, {@code @hide} formats will return {@code false}.</p>
179      *
180      * <p>Any other indirect formats (such as {@code TRANSPARENT} or {@code TRANSLUCENT})
181      * will return {@code false}.</p>
182      *
183      * @param format an integer format
184      * @return a boolean
185      *
186      * @hide
187      */
isPublicFormat(@ormat int format)188     public static boolean isPublicFormat(@Format int format) {
189         switch (format) {
190             case RGBA_8888:
191             case RGBX_8888:
192             case RGB_888:
193             case RGB_565:
194             case RGBA_F16:
195             case RGBA_1010102:
196                 return true;
197         }
198 
199         return false;
200     }
201 
202     /**
203      * @hide
204      */
formatToString(@ormat int format)205     public static String formatToString(@Format int format) {
206         switch (format) {
207             case UNKNOWN:
208                 return "UNKNOWN";
209             case TRANSLUCENT:
210                 return "TRANSLUCENT";
211             case TRANSPARENT:
212                 return "TRANSPARENT";
213             case RGBA_8888:
214                 return "RGBA_8888";
215             case RGBX_8888:
216                 return "RGBX_8888";
217             case RGB_888:
218                 return "RGB_888";
219             case RGB_565:
220                 return "RGB_565";
221             case RGBA_5551:
222                 return "RGBA_5551";
223             case RGBA_4444:
224                 return "RGBA_4444";
225             case A_8:
226                 return "A_8";
227             case L_8:
228                 return "L_8";
229             case LA_88:
230                 return "LA_88";
231             case RGB_332:
232                 return "RGB_332";
233             case YCbCr_422_SP:
234                 return "YCbCr_422_SP";
235             case YCbCr_420_SP:
236                 return "YCbCr_420_SP";
237             case YCbCr_422_I:
238                 return "YCbCr_422_I";
239             case RGBA_F16:
240                 return "RGBA_F16";
241             case RGBA_1010102:
242                 return "RGBA_1010102";
243             case HSV_888:
244                 return "HSV_888";
245             case JPEG:
246                 return "JPEG";
247             case R_8:
248                 return "R_8";
249             default:
250                 return Integer.toString(format);
251         }
252     }
253 }
254