xref: /aosp_15_r20/frameworks/native/libs/gui/GLConsumerUtils.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2010 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "GLConsumerUtils"
18*38e8c45fSAndroid Build Coastguard Worker //#define LOG_NDEBUG 0
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #include <gui/GLConsumer.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <math/mat4.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <system/window.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <utils/Log.h>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker namespace android {
26*38e8c45fSAndroid Build Coastguard Worker 
computeTransformMatrix(float outTransform[16],const sp<GraphicBuffer> & buf,const Rect & cropRect,uint32_t transform,bool filtering)27*38e8c45fSAndroid Build Coastguard Worker void GLConsumer::computeTransformMatrix(float outTransform[16],
28*38e8c45fSAndroid Build Coastguard Worker         const sp<GraphicBuffer>& buf, const Rect& cropRect, uint32_t transform,
29*38e8c45fSAndroid Build Coastguard Worker         bool filtering) {
30*38e8c45fSAndroid Build Coastguard Worker     computeTransformMatrix(outTransform, buf->getWidth(), buf->getHeight(), buf->getPixelFormat(),
31*38e8c45fSAndroid Build Coastguard Worker                            cropRect, transform, filtering);
32*38e8c45fSAndroid Build Coastguard Worker }
33*38e8c45fSAndroid Build Coastguard Worker 
computeTransformMatrix(float outTransform[16],float bufferWidth,float bufferHeight,PixelFormat pixelFormat,const Rect & cropRect,uint32_t transform,bool filtering)34*38e8c45fSAndroid Build Coastguard Worker void GLConsumer::computeTransformMatrix(float outTransform[16], float bufferWidth,
35*38e8c45fSAndroid Build Coastguard Worker                                         float bufferHeight, PixelFormat pixelFormat,
36*38e8c45fSAndroid Build Coastguard Worker                                         const Rect& cropRect, uint32_t transform, bool filtering) {
37*38e8c45fSAndroid Build Coastguard Worker     // Transform matrices
38*38e8c45fSAndroid Build Coastguard Worker     static const mat4 mtxFlipH(
39*38e8c45fSAndroid Build Coastguard Worker         -1, 0, 0, 0,
40*38e8c45fSAndroid Build Coastguard Worker         0, 1, 0, 0,
41*38e8c45fSAndroid Build Coastguard Worker         0, 0, 1, 0,
42*38e8c45fSAndroid Build Coastguard Worker         1, 0, 0, 1
43*38e8c45fSAndroid Build Coastguard Worker     );
44*38e8c45fSAndroid Build Coastguard Worker     static const mat4 mtxFlipV(
45*38e8c45fSAndroid Build Coastguard Worker         1, 0, 0, 0,
46*38e8c45fSAndroid Build Coastguard Worker         0, -1, 0, 0,
47*38e8c45fSAndroid Build Coastguard Worker         0, 0, 1, 0,
48*38e8c45fSAndroid Build Coastguard Worker         0, 1, 0, 1
49*38e8c45fSAndroid Build Coastguard Worker     );
50*38e8c45fSAndroid Build Coastguard Worker     static const mat4 mtxRot90(
51*38e8c45fSAndroid Build Coastguard Worker         0, 1, 0, 0,
52*38e8c45fSAndroid Build Coastguard Worker         -1, 0, 0, 0,
53*38e8c45fSAndroid Build Coastguard Worker         0, 0, 1, 0,
54*38e8c45fSAndroid Build Coastguard Worker         1, 0, 0, 1
55*38e8c45fSAndroid Build Coastguard Worker     );
56*38e8c45fSAndroid Build Coastguard Worker 
57*38e8c45fSAndroid Build Coastguard Worker     mat4 xform;
58*38e8c45fSAndroid Build Coastguard Worker     if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
59*38e8c45fSAndroid Build Coastguard Worker         xform *= mtxFlipH;
60*38e8c45fSAndroid Build Coastguard Worker     }
61*38e8c45fSAndroid Build Coastguard Worker     if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
62*38e8c45fSAndroid Build Coastguard Worker         xform *= mtxFlipV;
63*38e8c45fSAndroid Build Coastguard Worker     }
64*38e8c45fSAndroid Build Coastguard Worker     if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
65*38e8c45fSAndroid Build Coastguard Worker         xform *= mtxRot90;
66*38e8c45fSAndroid Build Coastguard Worker     }
67*38e8c45fSAndroid Build Coastguard Worker 
68*38e8c45fSAndroid Build Coastguard Worker     if (!cropRect.isEmpty()) {
69*38e8c45fSAndroid Build Coastguard Worker         float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
70*38e8c45fSAndroid Build Coastguard Worker         float shrinkAmount = 0.0f;
71*38e8c45fSAndroid Build Coastguard Worker         if (filtering) {
72*38e8c45fSAndroid Build Coastguard Worker             // In order to prevent bilinear sampling beyond the edge of the
73*38e8c45fSAndroid Build Coastguard Worker             // crop rectangle we may need to shrink it by 2 texels in each
74*38e8c45fSAndroid Build Coastguard Worker             // dimension.  Normally this would just need to take 1/2 a texel
75*38e8c45fSAndroid Build Coastguard Worker             // off each end, but because the chroma channels of YUV420 images
76*38e8c45fSAndroid Build Coastguard Worker             // are subsampled we may need to shrink the crop region by a whole
77*38e8c45fSAndroid Build Coastguard Worker             // texel on each side.
78*38e8c45fSAndroid Build Coastguard Worker             switch (pixelFormat) {
79*38e8c45fSAndroid Build Coastguard Worker                 case PIXEL_FORMAT_RGBA_8888:
80*38e8c45fSAndroid Build Coastguard Worker                 case PIXEL_FORMAT_RGBX_8888:
81*38e8c45fSAndroid Build Coastguard Worker                 case PIXEL_FORMAT_RGBA_FP16:
82*38e8c45fSAndroid Build Coastguard Worker                 case PIXEL_FORMAT_RGBA_1010102:
83*38e8c45fSAndroid Build Coastguard Worker                 case PIXEL_FORMAT_RGB_888:
84*38e8c45fSAndroid Build Coastguard Worker                 case PIXEL_FORMAT_RGB_565:
85*38e8c45fSAndroid Build Coastguard Worker                 case PIXEL_FORMAT_BGRA_8888:
86*38e8c45fSAndroid Build Coastguard Worker                     // We know there's no subsampling of any channels, so we
87*38e8c45fSAndroid Build Coastguard Worker                     // only need to shrink by a half a pixel.
88*38e8c45fSAndroid Build Coastguard Worker                     shrinkAmount = 0.5;
89*38e8c45fSAndroid Build Coastguard Worker                     break;
90*38e8c45fSAndroid Build Coastguard Worker 
91*38e8c45fSAndroid Build Coastguard Worker                 default:
92*38e8c45fSAndroid Build Coastguard Worker                     // If we don't recognize the format, we must assume the
93*38e8c45fSAndroid Build Coastguard Worker                     // worst case (that we care about), which is YUV420.
94*38e8c45fSAndroid Build Coastguard Worker                     shrinkAmount = 1.0;
95*38e8c45fSAndroid Build Coastguard Worker                     break;
96*38e8c45fSAndroid Build Coastguard Worker             }
97*38e8c45fSAndroid Build Coastguard Worker         }
98*38e8c45fSAndroid Build Coastguard Worker 
99*38e8c45fSAndroid Build Coastguard Worker         // Only shrink the dimensions that are not the size of the buffer.
100*38e8c45fSAndroid Build Coastguard Worker         if (cropRect.width() < bufferWidth) {
101*38e8c45fSAndroid Build Coastguard Worker             tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
102*38e8c45fSAndroid Build Coastguard Worker             sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
103*38e8c45fSAndroid Build Coastguard Worker                     bufferWidth;
104*38e8c45fSAndroid Build Coastguard Worker         }
105*38e8c45fSAndroid Build Coastguard Worker         if (cropRect.height() < bufferHeight) {
106*38e8c45fSAndroid Build Coastguard Worker             ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
107*38e8c45fSAndroid Build Coastguard Worker                     bufferHeight;
108*38e8c45fSAndroid Build Coastguard Worker             sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
109*38e8c45fSAndroid Build Coastguard Worker                     bufferHeight;
110*38e8c45fSAndroid Build Coastguard Worker         }
111*38e8c45fSAndroid Build Coastguard Worker 
112*38e8c45fSAndroid Build Coastguard Worker         mat4 crop(
113*38e8c45fSAndroid Build Coastguard Worker             sx, 0, 0, 0,
114*38e8c45fSAndroid Build Coastguard Worker             0, sy, 0, 0,
115*38e8c45fSAndroid Build Coastguard Worker             0, 0, 1, 0,
116*38e8c45fSAndroid Build Coastguard Worker             tx, ty, 0, 1
117*38e8c45fSAndroid Build Coastguard Worker         );
118*38e8c45fSAndroid Build Coastguard Worker         xform = crop * xform;
119*38e8c45fSAndroid Build Coastguard Worker     }
120*38e8c45fSAndroid Build Coastguard Worker 
121*38e8c45fSAndroid Build Coastguard Worker     // GLConsumer uses the GL convention where (0, 0) is the bottom-left
122*38e8c45fSAndroid Build Coastguard Worker     // corner and (1, 1) is the top-right corner.  Add an additional vertical
123*38e8c45fSAndroid Build Coastguard Worker     // flip after all other transforms to map from GL convention to buffer
124*38e8c45fSAndroid Build Coastguard Worker     // queue memory layout, where (0, 0) is the top-left corner.
125*38e8c45fSAndroid Build Coastguard Worker     xform = mtxFlipV * xform;
126*38e8c45fSAndroid Build Coastguard Worker 
127*38e8c45fSAndroid Build Coastguard Worker     memcpy(outTransform, xform.asArray(), sizeof(xform));
128*38e8c45fSAndroid Build Coastguard Worker }
129*38e8c45fSAndroid Build Coastguard Worker 
130*38e8c45fSAndroid Build Coastguard Worker }; // namespace android
131