blob: 7a06c3d801699dc051b6e0a7a2c4c3267762dda3 [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) {
30 // Transform matrices
31 static const mat4 mtxFlipH(
32 -1, 0, 0, 0,
33 0, 1, 0, 0,
34 0, 0, 1, 0,
35 1, 0, 0, 1
36 );
37 static const mat4 mtxFlipV(
38 1, 0, 0, 0,
39 0, -1, 0, 0,
40 0, 0, 1, 0,
41 0, 1, 0, 1
42 );
43 static const mat4 mtxRot90(
44 0, 1, 0, 0,
45 -1, 0, 0, 0,
46 0, 0, 1, 0,
47 1, 0, 0, 1
48 );
49
50 mat4 xform;
51 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
52 xform *= mtxFlipH;
53 }
54 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
55 xform *= mtxFlipV;
56 }
57 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
58 xform *= mtxRot90;
59 }
60
61 if (!cropRect.isEmpty()) {
62 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
63 float bufferWidth = buf->getWidth();
64 float bufferHeight = buf->getHeight();
65 float shrinkAmount = 0.0f;
66 if (filtering) {
67 // In order to prevent bilinear sampling beyond the edge of the
68 // crop rectangle we may need to shrink it by 2 texels in each
69 // dimension. Normally this would just need to take 1/2 a texel
70 // off each end, but because the chroma channels of YUV420 images
71 // are subsampled we may need to shrink the crop region by a whole
72 // texel on each side.
73 switch (buf->getPixelFormat()) {
74 case PIXEL_FORMAT_RGBA_8888:
75 case PIXEL_FORMAT_RGBX_8888:
76 case PIXEL_FORMAT_RGBA_FP16:
77 case PIXEL_FORMAT_RGBA_1010102:
78 case PIXEL_FORMAT_RGB_888:
79 case PIXEL_FORMAT_RGB_565:
80 case PIXEL_FORMAT_BGRA_8888:
81 // We know there's no subsampling of any channels, so we
82 // only need to shrink by a half a pixel.
83 shrinkAmount = 0.5;
84 break;
85
86 default:
87 // If we don't recognize the format, we must assume the
88 // worst case (that we care about), which is YUV420.
89 shrinkAmount = 1.0;
90 break;
91 }
92 }
93
94 // Only shrink the dimensions that are not the size of the buffer.
95 if (cropRect.width() < bufferWidth) {
96 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
97 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
98 bufferWidth;
99 }
100 if (cropRect.height() < bufferHeight) {
101 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
102 bufferHeight;
103 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
104 bufferHeight;
105 }
106
107 mat4 crop(
108 sx, 0, 0, 0,
109 0, sy, 0, 0,
110 0, 0, 1, 0,
111 tx, ty, 0, 1
112 );
113 xform = crop * xform;
114 }
115
116 // GLConsumer uses the GL convention where (0, 0) is the bottom-left
117 // corner and (1, 1) is the top-right corner. Add an additional vertical
118 // flip after all other transforms to map from GL convention to buffer
119 // queue memory layout, where (0, 0) is the top-left corner.
120 xform = mtxFlipV * xform;
121
122 memcpy(outTransform, xform.asArray(), sizeof(xform));
123}
124
125}; // namespace android