blob: a1c69e7d6d5680fcb501e8852563969703ae0494 [file] [log] [blame]
Chong Zhang60fa4fd2019-04-19 10:00:27 -07001/*
2 * Copyright (C) 2010 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#define LOG_TAG "GLConsumerUtils"
18//#define LOG_NDEBUG 0
19
20#include <gui/GLConsumer.h>
21#include <math/mat4.h>
22#include <system/window.h>
23#include <utils/Log.h>
24
25namespace android {
26
27void GLConsumer::computeTransformMatrix(float outTransform[16],
28 const sp<GraphicBuffer>& buf, const Rect& cropRect, uint32_t transform,
29 bool filtering) {
Vishnu Nairf8a873a2022-06-15 17:34:34 +000030 computeTransformMatrix(outTransform, buf->getWidth(), buf->getHeight(), buf->getPixelFormat(),
31 cropRect, transform, filtering);
32}
33
34void GLConsumer::computeTransformMatrix(float outTransform[16], float bufferWidth,
35 float bufferHeight, PixelFormat pixelFormat,
36 const Rect& cropRect, uint32_t transform, bool filtering) {
Chong Zhang60fa4fd2019-04-19 10:00:27 -070037 // Transform matrices
38 static const mat4 mtxFlipH(
39 -1, 0, 0, 0,
40 0, 1, 0, 0,
41 0, 0, 1, 0,
42 1, 0, 0, 1
43 );
44 static const mat4 mtxFlipV(
45 1, 0, 0, 0,
46 0, -1, 0, 0,
47 0, 0, 1, 0,
48 0, 1, 0, 1
49 );
50 static const mat4 mtxRot90(
51 0, 1, 0, 0,
52 -1, 0, 0, 0,
53 0, 0, 1, 0,
54 1, 0, 0, 1
55 );
56
57 mat4 xform;
58 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
59 xform *= mtxFlipH;
60 }
61 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
62 xform *= mtxFlipV;
63 }
64 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
65 xform *= mtxRot90;
66 }
67
68 if (!cropRect.isEmpty()) {
69 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
Chong Zhang60fa4fd2019-04-19 10:00:27 -070070 float shrinkAmount = 0.0f;
71 if (filtering) {
72 // In order to prevent bilinear sampling beyond the edge of the
73 // crop rectangle we may need to shrink it by 2 texels in each
74 // dimension. Normally this would just need to take 1/2 a texel
75 // off each end, but because the chroma channels of YUV420 images
76 // are subsampled we may need to shrink the crop region by a whole
77 // texel on each side.
Vishnu Nairf8a873a2022-06-15 17:34:34 +000078 switch (pixelFormat) {
Chong Zhang60fa4fd2019-04-19 10:00:27 -070079 case PIXEL_FORMAT_RGBA_8888:
80 case PIXEL_FORMAT_RGBX_8888:
81 case PIXEL_FORMAT_RGBA_FP16:
82 case PIXEL_FORMAT_RGBA_1010102:
83 case PIXEL_FORMAT_RGB_888:
84 case PIXEL_FORMAT_RGB_565:
85 case PIXEL_FORMAT_BGRA_8888:
86 // We know there's no subsampling of any channels, so we
87 // only need to shrink by a half a pixel.
88 shrinkAmount = 0.5;
89 break;
90
91 default:
92 // If we don't recognize the format, we must assume the
93 // worst case (that we care about), which is YUV420.
94 shrinkAmount = 1.0;
95 break;
96 }
97 }
98
99 // Only shrink the dimensions that are not the size of the buffer.
100 if (cropRect.width() < bufferWidth) {
101 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
102 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
103 bufferWidth;
104 }
105 if (cropRect.height() < bufferHeight) {
106 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
107 bufferHeight;
108 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
109 bufferHeight;
110 }
111
112 mat4 crop(
113 sx, 0, 0, 0,
114 0, sy, 0, 0,
115 0, 0, 1, 0,
116 tx, ty, 0, 1
117 );
118 xform = crop * xform;
119 }
120
121 // GLConsumer uses the GL convention where (0, 0) is the bottom-left
122 // corner and (1, 1) is the top-right corner. Add an additional vertical
123 // flip after all other transforms to map from GL convention to buffer
124 // queue memory layout, where (0, 0) is the top-left corner.
125 xform = mtxFlipV * xform;
126
127 memcpy(outTransform, xform.asArray(), sizeof(xform));
128}
129
130}; // namespace android