• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef USE_M133_SKIA
29         GrBackendTexture backendTexture;
30 #else
31         GrBackendTexture backendTexture(0, 0, imageInfo);
32 #endif
33         return backendTexture;
34     }
35 
36     auto vkInfo = info.GetVKTextureInfo();
37     if (!vkInfo) {
38 #ifdef USE_M133_SKIA
39         GrBackendTexture backendTexture = GrBackendTextures::MakeVk(info.GetWidth(), info.GetHeight(), imageInfo);
40 #else
41         GrBackendTexture backendTexture(info.GetWidth(), info.GetHeight(), imageInfo);
42 #endif
43         return backendTexture;
44     }
45     imageInfo.fImage = vkInfo->vkImage;
46 #ifdef USE_M133_SKIA
47     skgpu::VulkanAlloc alloc;
48 #else
49     GrVkAlloc alloc;
50 #endif
51     alloc.fMemory = vkInfo->vkAlloc.memory;
52     alloc.fOffset = vkInfo->vkAlloc.offset;
53     alloc.fSize = vkInfo->vkAlloc.size;
54     alloc.fFlags = vkInfo->vkAlloc.flags;
55     imageInfo.fAlloc = alloc;
56 
57     imageInfo.fImageTiling = vkInfo->imageTiling;
58     imageInfo.fImageLayout = vkInfo->imageLayout;
59     imageInfo.fFormat = vkInfo->format;
60     imageInfo.fImageUsageFlags = vkInfo->imageUsageFlags;
61     imageInfo.fSampleCount = vkInfo->sampleCount;
62     imageInfo.fLevelCount = vkInfo->levelCount;
63     imageInfo.fCurrentQueueFamily = vkInfo->currentQueueFamily;
64     imageInfo.fProtected = vkInfo->vkProtected ? GrProtected::kYes : GrProtected::kNo;
65 
66 #ifdef USE_M133_SKIA
67     skgpu::VulkanYcbcrConversionInfo ycbcrInfo = {
68 #else
69     GrVkYcbcrConversionInfo ycbcrInfo = {
70 #endif
71         .fFormat = vkInfo->ycbcrConversionInfo.format,
72         .fExternalFormat = vkInfo->ycbcrConversionInfo.externalFormat,
73         .fYcbcrModel = vkInfo->ycbcrConversionInfo.ycbcrModel,
74         .fYcbcrRange = vkInfo->ycbcrConversionInfo.ycbcrRange,
75         .fXChromaOffset = vkInfo->ycbcrConversionInfo.xChromaOffset,
76         .fYChromaOffset = vkInfo->ycbcrConversionInfo.yChromaOffset,
77         .fChromaFilter = vkInfo->ycbcrConversionInfo.chromaFilter,
78         .fForceExplicitReconstruction = vkInfo->ycbcrConversionInfo.forceExplicitReconstruction,
79         .fFormatFeatures = vkInfo->ycbcrConversionInfo.formatFeatures
80     };
81     imageInfo.fYcbcrConversionInfo = ycbcrInfo;
82 
83     imageInfo.fSharingMode = vkInfo->sharingMode;
84 
85 #ifdef USE_M133_SKIA
86     GrBackendTexture backendTexture = GrBackendTextures::MakeVk(info.GetWidth(), info.GetHeight(), imageInfo);
87 #else
88     GrBackendTexture backendTexture(info.GetWidth(), info.GetHeight(), imageInfo);
89 #endif
90     return backendTexture;
91 }
92 
93 void SkiaTextureInfo::ConvertToVKTexture(const GrBackendTexture& backendTexture, TextureInfo& info)
94 {
95     if (!SystemProperties::IsUseVulkan()) {
96         return;
97     }
98     std::shared_ptr<VKTextureInfo> vkInfo = std::make_shared<VKTextureInfo>();
99     info.SetWidth(backendTexture.width());
100     info.SetHeight(backendTexture.height());
101 
102     GrVkImageInfo vkImageInfo;
103 #ifdef USE_M133_SKIA
104     GrBackendTextures::GetVkImageInfo(backendTexture, &vkImageInfo);
105 #else
106     backendTexture.getVkImageInfo(&vkImageInfo);
107 #endif
108 
109     vkInfo->vkImage = vkImageInfo.fImage;
110 
111     vkInfo->vkAlloc.memory = vkImageInfo.fAlloc.fMemory;
112     vkInfo->vkAlloc.offset = vkImageInfo.fAlloc.fOffset;
113     vkInfo->vkAlloc.size = vkImageInfo.fAlloc.fSize;
114     vkInfo->vkAlloc.flags = vkImageInfo.fAlloc.fFlags;
115 
116     vkInfo->imageTiling = vkImageInfo.fImageTiling;
117     vkInfo->imageLayout = vkImageInfo.fImageLayout;
118     vkInfo->format = vkImageInfo.fFormat;
119     vkInfo->imageUsageFlags = vkImageInfo.fImageUsageFlags;
120     vkInfo->sampleCount = vkImageInfo.fSampleCount;
121     vkInfo->levelCount = vkImageInfo.fLevelCount;
122     vkInfo->currentQueueFamily = vkImageInfo.fCurrentQueueFamily;
123     vkInfo->vkProtected = (vkImageInfo.fProtected == GrProtected::kYes) ? true : false;
124 
125     vkInfo->ycbcrConversionInfo.format = vkImageInfo.fYcbcrConversionInfo.fFormat;
126     vkInfo->ycbcrConversionInfo.externalFormat = vkImageInfo.fYcbcrConversionInfo.fExternalFormat;
127     vkInfo->ycbcrConversionInfo.ycbcrModel = vkImageInfo.fYcbcrConversionInfo.fYcbcrModel;
128     vkInfo->ycbcrConversionInfo.ycbcrRange = vkImageInfo.fYcbcrConversionInfo.fYcbcrRange;
129     vkInfo->ycbcrConversionInfo.xChromaOffset = vkImageInfo.fYcbcrConversionInfo.fXChromaOffset;
130     vkInfo->ycbcrConversionInfo.yChromaOffset = vkImageInfo.fYcbcrConversionInfo.fYChromaOffset;
131     vkInfo->ycbcrConversionInfo.chromaFilter = vkImageInfo.fYcbcrConversionInfo.fChromaFilter;
132     vkInfo->ycbcrConversionInfo.forceExplicitReconstruction =
133         vkImageInfo.fYcbcrConversionInfo.fForceExplicitReconstruction;
134     vkInfo->ycbcrConversionInfo.formatFeatures = vkImageInfo.fYcbcrConversionInfo.fFormatFeatures;
135 
136     vkInfo->sharingMode = vkImageInfo.fSharingMode;
137 
138     info.SetVKTextureInfo(vkInfo);
139 }
140 #endif
141 #ifdef RS_ENABLE_GPU
142 GrBackendTexture SkiaTextureInfo::ConvertToGrBackendTexture(const TextureInfo& info)
143 {
144 #ifdef RS_ENABLE_VK
145     if (SystemProperties::IsUseVulkan()) {
146         return ConvertToGrBackendVKTexture(info);
147     } else {
148         GrGLTextureInfo grGLTextureInfo = { info.GetTarget(), info.GetID(), info.GetFormat() };
149 #ifdef USE_M133_SKIA
150         GrBackendTexture backendTexture = GrBackendTextures::MakeGL(info.GetWidth(), info.GetHeight(),
151             static_cast<skgpu::Mipmapped>(info.GetIsMipMapped()? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo),
152             grGLTextureInfo);
153 #else
154         GrBackendTexture backendTexture(info.GetWidth(), info.GetHeight(),
155             static_cast<GrMipMapped>(info.GetIsMipMapped()), grGLTextureInfo);
156 #endif
157         return backendTexture;
158     }
159 #else
160     GrGLTextureInfo grGLTextureInfo = { info.GetTarget(), info.GetID(), info.GetFormat() };
161 #ifdef USE_M133_SKIA
162     GrBackendTexture backendTexture = GrBackendTextures::MakeGL(info.GetWidth(), info.GetHeight(),
163         static_cast<skgpu::Mipmapped>(info.GetIsMipMapped()? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo),
164         grGLTextureInfo);
165 #else
166     GrBackendTexture backendTexture(info.GetWidth(), info.GetHeight(),
167         static_cast<GrMipMapped>(info.GetIsMipMapped()), grGLTextureInfo);
168 #endif
169     return backendTexture;
170 #endif
171 }
172 
173 TextureInfo SkiaTextureInfo::ConvertToTextureInfo(const GrBackendTexture& grBackendTexture)
174 {
175     GrGLTextureInfo* grGLTextureInfo = new GrGLTextureInfo();
176 #ifdef USE_M133_SKIA
177     GrBackendTextures::GetGLTextureInfo(grBackendTexture, grGLTextureInfo);
178 #else
179     grBackendTexture.getGLTextureInfo(grGLTextureInfo);
180 #endif
181     TextureInfo textureInfo;
182     textureInfo.SetWidth(grBackendTexture.width());
183     textureInfo.SetHeight(grBackendTexture.height());
184     textureInfo.SetIsMipMapped(static_cast<bool>(grBackendTexture.mipmapped()));
185     textureInfo.SetTarget(grGLTextureInfo->fTarget);
186     textureInfo.SetID(grGLTextureInfo->fID);
187     textureInfo.SetFormat(grGLTextureInfo->fFormat);
188     delete grGLTextureInfo;
189     return textureInfo;
190 }
191 #endif
192 } // namespace Drawing
193 } // namespace Rosen
194 } // namespace OHOS
195