blob: 945a3722611e25d526ead6ba3515cd48cab0ce9e [file] [log] [blame]
Brian Salomonc1a249d2020-10-19 10:55:45 -04001/*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "include/gpu/GrYUVABackendTextures.h"
9
10static int num_channels(const GrBackendFormat& format) {
11 switch (format.channelMask()) {
12 case kRed_SkColorChannelFlag : return 1;
13 case kAlpha_SkColorChannelFlag: return 1;
14 case kGray_SkColorChannelFlag : return 1;
15 case kRG_SkColorChannelFlags : return 2;
16 case kRGB_SkColorChannelFlags : return 3;
17 case kRGBA_SkColorChannelFlags: return 4;
18 default : return 0;
19 }
20}
21
22GrYUVABackendTextures::GrYUVABackendTextures(
23 const SkYUVAInfo& yuvaInfo,
24 const GrBackendTexture textures[SkYUVAInfo::kMaxPlanes],
25 GrSurfaceOrigin textureOrigin)
26 : fYUVAInfo(yuvaInfo), fTextureOrigin(textureOrigin) {
27 if (!fYUVAInfo.isValid()) {
28 return;
29 }
30 SkISize planeDimensions[SkYUVAInfo::kMaxPlanes];
31 int numPlanes = yuvaInfo.planeDimensions(planeDimensions);
32 for (int i = 0; i < numPlanes; ++i) {
33 int numRequiredChannels = fYUVAInfo.numChannelsInPlane(i);
34 if (!textures[i].isValid() ||
35 textures[i].dimensions() != planeDimensions[i] ||
36 textures[i].backend() != textures[0].backend() ||
37 num_channels(textures[i].getBackendFormat()) < numRequiredChannels) {
38 *this = {};
39 return;
40 }
41 fTextures[i] = textures[i];
42 }
43}
44
45bool GrYUVABackendTextures::toYUVAIndices(SkYUVAIndex indices[SkYUVAIndex::kIndexCount]) const {
46 SkASSERT(indices);
47 uint32_t channelFlags[] = {fTextures[0].getBackendFormat().channelMask(),
48 fTextures[1].getBackendFormat().channelMask(),
49 fTextures[2].getBackendFormat().channelMask(),
50 fTextures[3].getBackendFormat().channelMask()};
51 return fYUVAInfo.toYUVAIndices(channelFlags, indices);
52}