1 /*
2  * Copyright (c) 2023 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 "skia_texture_info.h"
17 #include "utils/system_properties.h"
18 
19 namespace OHOS {
20 namespace Rosen {
21 namespace Drawing {
22 
23 #ifdef RS_ENABLE_VK
ConvertToGrBackendVKTexture(const TextureInfo & info)24 GrBackendTexture SkiaTextureInfo::ConvertToGrBackendVKTexture(const TextureInfo& info)
25 {
26     GrVkImageInfo imageInfo;
27     if (!SystemProperties::IsUseVulkan()) {
28         GrBackendTexture backendTexture(0, 0, imageInfo);
29         return backendTexture;
30     }
31 
32     auto vkInfo = info.GetVKTextureInfo();
33     if (!vkInfo) {
34         GrBackendTexture backendTexture(info.GetWidth(), info.GetHeight(), imageInfo);
35         return backendTexture;
36     }
37     imageInfo.fImage = vkInfo->vkImage;
38     GrVkAlloc alloc;
39     alloc.fMemory = vkInfo->vkAlloc.memory;
40     alloc.fOffset = vkInfo->vkAlloc.offset;
41     alloc.fSize = vkInfo->vkAlloc.size;
42     alloc.fFlags = vkInfo->vkAlloc.flags;
43     imageInfo.fAlloc = alloc;
44 
45     imageInfo.fImageTiling = vkInfo->imageTiling;
46     imageInfo.fImageLayout = vkInfo->imageLayout;
47     imageInfo.fFormat = vkInfo->format;
48     imageInfo.fImageUsageFlags = vkInfo->imageUsageFlags;
49     imageInfo.fSampleCount = vkInfo->sampleCount;
50     imageInfo.fLevelCount = vkInfo->levelCount;
51     imageInfo.fCurrentQueueFamily = vkInfo->currentQueueFamily;
52     imageInfo.fProtected = vkInfo->vkProtected ? GrProtected::kYes : GrProtected::kNo;
53 
54     GrVkYcbcrConversionInfo ycbcrInfo = {
55         .fFormat = vkInfo->ycbcrConversionInfo.format,
56         .fExternalFormat = vkInfo->ycbcrConversionInfo.externalFormat,
57         .fYcbcrModel = vkInfo->ycbcrConversionInfo.ycbcrModel,
58         .fYcbcrRange = vkInfo->ycbcrConversionInfo.ycbcrRange,
59         .fXChromaOffset = vkInfo->ycbcrConversionInfo.xChromaOffset,
60         .fYChromaOffset = vkInfo->ycbcrConversionInfo.yChromaOffset,
61         .fChromaFilter = vkInfo->ycbcrConversionInfo.chromaFilter,
62         .fForceExplicitReconstruction = vkInfo->ycbcrConversionInfo.forceExplicitReconstruction,
63         .fFormatFeatures = vkInfo->ycbcrConversionInfo.formatFeatures
64     };
65     imageInfo.fYcbcrConversionInfo = ycbcrInfo;
66 
67     imageInfo.fSharingMode = vkInfo->sharingMode;
68 
69     GrBackendTexture backendTexture(info.GetWidth(), info.GetHeight(), imageInfo);
70     return backendTexture;
71 }
72 
ConvertToVKTexture(const GrBackendTexture & backendTexture,TextureInfo & info)73 void SkiaTextureInfo::ConvertToVKTexture(const GrBackendTexture& backendTexture, TextureInfo& info)
74 {
75     if (!SystemProperties::IsUseVulkan()) {
76         return;
77     }
78     std::shared_ptr<VKTextureInfo> vkInfo = std::make_shared<VKTextureInfo>();
79     info.SetWidth(backendTexture.width());
80     info.SetHeight(backendTexture.height());
81 
82     GrVkImageInfo vkImageInfo;
83     backendTexture.getVkImageInfo(&vkImageInfo);
84 
85     vkInfo->vkImage = vkImageInfo.fImage;
86 
87     vkInfo->vkAlloc.memory = vkImageInfo.fAlloc.fMemory;
88     vkInfo->vkAlloc.offset = vkImageInfo.fAlloc.fOffset;
89     vkInfo->vkAlloc.size = vkImageInfo.fAlloc.fSize;
90     vkInfo->vkAlloc.flags = vkImageInfo.fAlloc.fFlags;
91 
92     vkInfo->imageTiling = vkImageInfo.fImageTiling;
93     vkInfo->imageLayout = vkImageInfo.fImageLayout;
94     vkInfo->format = vkImageInfo.fFormat;
95     vkInfo->imageUsageFlags = vkImageInfo.fImageUsageFlags;
96     vkInfo->sampleCount = vkImageInfo.fSampleCount;
97     vkInfo->levelCount = vkImageInfo.fLevelCount;
98     vkInfo->currentQueueFamily = vkImageInfo.fCurrentQueueFamily;
99     vkInfo->vkProtected = (vkImageInfo.fProtected == GrProtected::kYes) ? true : false;
100 
101     vkInfo->ycbcrConversionInfo.format = vkImageInfo.fYcbcrConversionInfo.fFormat;
102     vkInfo->ycbcrConversionInfo.externalFormat = vkImageInfo.fYcbcrConversionInfo.fExternalFormat;
103     vkInfo->ycbcrConversionInfo.ycbcrModel = vkImageInfo.fYcbcrConversionInfo.fYcbcrModel;
104     vkInfo->ycbcrConversionInfo.ycbcrRange = vkImageInfo.fYcbcrConversionInfo.fYcbcrRange;
105     vkInfo->ycbcrConversionInfo.xChromaOffset = vkImageInfo.fYcbcrConversionInfo.fXChromaOffset;
106     vkInfo->ycbcrConversionInfo.yChromaOffset = vkImageInfo.fYcbcrConversionInfo.fYChromaOffset;
107     vkInfo->ycbcrConversionInfo.chromaFilter = vkImageInfo.fYcbcrConversionInfo.fChromaFilter;
108     vkInfo->ycbcrConversionInfo.forceExplicitReconstruction =
109         vkImageInfo.fYcbcrConversionInfo.fForceExplicitReconstruction;
110     vkInfo->ycbcrConversionInfo.formatFeatures = vkImageInfo.fYcbcrConversionInfo.fFormatFeatures;
111 
112     vkInfo->sharingMode = vkImageInfo.fSharingMode;
113 
114     info.SetVKTextureInfo(vkInfo);
115 }
116 #endif
117 
ConvertToGrBackendTexture(const TextureInfo & info)118 GrBackendTexture SkiaTextureInfo::ConvertToGrBackendTexture(const TextureInfo& info)
119 {
120 #ifdef RS_ENABLE_VK
121     if (SystemProperties::IsUseVulkan()) {
122         return ConvertToGrBackendVKTexture(info);
123     } else {
124         GrGLTextureInfo grGLTextureInfo = { info.GetTarget(), info.GetID(), info.GetFormat() };
125         GrBackendTexture backendTexture(info.GetWidth(), info.GetHeight(),
126             static_cast<GrMipMapped>(info.GetIsMipMapped()), grGLTextureInfo);
127         return backendTexture;
128     }
129 #else
130     GrGLTextureInfo grGLTextureInfo = { info.GetTarget(), info.GetID(), info.GetFormat() };
131     GrBackendTexture backendTexture(info.GetWidth(), info.GetHeight(),
132         static_cast<GrMipMapped>(info.GetIsMipMapped()), grGLTextureInfo);
133     return backendTexture;
134 #endif
135 }
136 
ConvertToTextureInfo(const GrBackendTexture & grBackendTexture)137 TextureInfo SkiaTextureInfo::ConvertToTextureInfo(const GrBackendTexture& grBackendTexture)
138 {
139     GrGLTextureInfo* grGLTextureInfo = new GrGLTextureInfo();
140     grBackendTexture.getGLTextureInfo(grGLTextureInfo);
141     TextureInfo textureInfo;
142     textureInfo.SetWidth(grBackendTexture.width());
143     textureInfo.SetHeight(grBackendTexture.height());
144     textureInfo.SetIsMipMapped(static_cast<bool>(grBackendTexture.mipmapped()));
145     textureInfo.SetTarget(grGLTextureInfo->fTarget);
146     textureInfo.SetID(grGLTextureInfo->fID);
147     textureInfo.SetFormat(grGLTextureInfo->fFormat);
148     delete grGLTextureInfo;
149     return textureInfo;
150 }
151 
152 } // namespace Drawing
153 } // namespace Rosen
154 } // namespace OHOS
155