1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <surface_utils.h>
17 #include <cinttypes>
18 #include "securec.h"
19 #include "buffer_log.h"
20
21 namespace OHOS {
22 using namespace HiviewDFX;
23 constexpr int64_t TRANSFORM_MATRIX_ELE_COUNT = 16;
24
GetInstance()25 SurfaceUtils* SurfaceUtils::GetInstance()
26 {
27 static SurfaceUtils instance;
28 return &instance;
29 }
30
~SurfaceUtils()31 SurfaceUtils::~SurfaceUtils()
32 {
33 surfaceCache_.clear();
34 }
35
GetSurface(uint64_t uniqueId)36 sptr<Surface> SurfaceUtils::GetSurface(uint64_t uniqueId)
37 {
38 std::lock_guard<std::mutex> lockGuard(mutex_);
39 if (surfaceCache_.count(uniqueId) == 0) {
40 BLOGE("Cannot find surface by uniqueId %" PRIu64 ".", uniqueId);
41 return nullptr;
42 }
43 return surfaceCache_[uniqueId];
44 }
45
Add(uint64_t uniqueId,const sptr<Surface> & surface)46 SurfaceError SurfaceUtils::Add(uint64_t uniqueId, const sptr<Surface> &surface)
47 {
48 std::lock_guard<std::mutex> lockGuard(mutex_);
49 if (surface == nullptr) {
50 BLOGE(" surface is nullptr.");
51 return GSERROR_INVALID_ARGUMENTS;
52 }
53 if (surfaceCache_.count(uniqueId) == 0) {
54 surfaceCache_[uniqueId] = surface;
55 return GSERROR_OK;
56 }
57 BLOGW("the surface by uniqueId %" PRIu64 " already existed", uniqueId);
58 return GSERROR_OK;
59 }
60
Remove(uint64_t uniqueId)61 SurfaceError SurfaceUtils::Remove(uint64_t uniqueId)
62 {
63 std::lock_guard<std::mutex> lockGuard(mutex_);
64 if (surfaceCache_.count(uniqueId) == 0) {
65 BLOGE("Delete failed without surface by uniqueId %" PRIu64, uniqueId);
66 return GSERROR_INVALID_OPERATING;
67 }
68 surfaceCache_.erase(uniqueId);
69 return GSERROR_OK;
70 }
71
MatrixProduct(const std::array<float,16> & lMat,const std::array<float,16> & rMat)72 std::array<float, 16> SurfaceUtils::MatrixProduct(const std::array<float, 16>& lMat, const std::array<float, 16>& rMat)
73 {
74 // Product matrix 4 * 4 = 16
75 return std::array<float, 16> {lMat[0] * rMat[0] + lMat[4] * rMat[1] + lMat[8] * rMat[2] + lMat[12] * rMat[3],
76 lMat[1] * rMat[0] + lMat[5] * rMat[1] + lMat[9] * rMat[2] + lMat[13] * rMat[3],
77 lMat[2] * rMat[0] + lMat[6] * rMat[1] + lMat[10] * rMat[2] + lMat[14] * rMat[3],
78 lMat[3] * rMat[0] + lMat[7] * rMat[1] + lMat[11] * rMat[2] + lMat[15] * rMat[3],
79
80 lMat[0] * rMat[4] + lMat[4] * rMat[5] + lMat[8] * rMat[6] + lMat[12] * rMat[7],
81 lMat[1] * rMat[4] + lMat[5] * rMat[5] + lMat[9] * rMat[6] + lMat[13] * rMat[7],
82 lMat[2] * rMat[4] + lMat[6] * rMat[5] + lMat[10] * rMat[6] + lMat[14] * rMat[7],
83 lMat[3] * rMat[4] + lMat[7] * rMat[5] + lMat[11] * rMat[6] + lMat[15] * rMat[7],
84
85 lMat[0] * rMat[8] + lMat[4] * rMat[9] + lMat[8] * rMat[10] + lMat[12] * rMat[11],
86 lMat[1] * rMat[8] + lMat[5] * rMat[9] + lMat[9] * rMat[10] + lMat[13] * rMat[11],
87 lMat[2] * rMat[8] + lMat[6] * rMat[9] + lMat[10] * rMat[10] + lMat[14] * rMat[11],
88 lMat[3] * rMat[8] + lMat[7] * rMat[9] + lMat[11] * rMat[10] + lMat[15] * rMat[11],
89
90 lMat[0] * rMat[12] + lMat[4] * rMat[13] + lMat[8] * rMat[14] + lMat[12] * rMat[15],
91 lMat[1] * rMat[12] + lMat[5] * rMat[13] + lMat[9] * rMat[14] + lMat[13] * rMat[15],
92 lMat[2] * rMat[12] + lMat[6] * rMat[13] + lMat[10] * rMat[14] + lMat[14] * rMat[15],
93 lMat[3] * rMat[12] + lMat[7] * rMat[13] + lMat[11] * rMat[14] + lMat[15] * rMat[15]};
94 }
95
ComputeTransformMatrix(float matrix[16],sptr<SurfaceBuffer> & buffer,GraphicTransformType & transform,Rect & crop)96 void SurfaceUtils::ComputeTransformMatrix(float matrix[16],
97 sptr<SurfaceBuffer>& buffer, GraphicTransformType& transform, Rect& crop)
98 {
99 const std::array<float, TRANSFORM_MATRIX_ELE_COUNT> rotate90 = {0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1};
100 std::array<float, TRANSFORM_MATRIX_ELE_COUNT> transformMatrix = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
101 float tx = 0.f;
102 float ty = 0.f;
103 float sx = 1.f;
104 float sy = 1.f;
105 if (transform == GraphicTransformType::GRAPHIC_ROTATE_90) {
106 transformMatrix = MatrixProduct(transformMatrix, rotate90);
107 }
108 float bufferWidth = buffer->GetWidth();
109 float bufferHeight = buffer->GetHeight();
110 if (crop.w < bufferWidth && bufferWidth != 0) {
111 tx = (float(crop.x) / bufferWidth);
112 sx = (float(crop.w) / bufferWidth);
113 }
114 if (crop.h < bufferHeight && bufferHeight != 0) {
115 ty = (float(bufferHeight - crop.y) / bufferHeight);
116 sy = (float(crop.h) / bufferHeight);
117 }
118 static const std::array<float, 16> cropMatrix = {sx, 0, 0, 0, 0, sy, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1};
119 transformMatrix = MatrixProduct(cropMatrix, transformMatrix);
120
121 auto ret = memcpy_s(matrix, sizeof(transformMatrix),
122 transformMatrix.data(), sizeof(transformMatrix));
123 if (ret != EOK) {
124 BLOGE("ComputeTransformMatrix: transformMatrix memcpy_s failed");
125 }
126 }
127 } // namespace OHOS
128