blob: dbbbd2bd61f9cacb609c7b689f42c96f0f93e7ae [file] [log] [blame]
Michael Ludwigd9958f82019-03-21 13:08:36 -04001/*
2 * Copyright 2019 Google Inc.
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tools/gpu/YUVUtils.h"
Michael Ludwigd9958f82019-03-21 13:08:36 -04009
Brian Salomon7db71392020-10-16 10:05:21 -040010#include "include/core/SkColorPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkData.h"
Brian Salomonefb5f072020-07-28 21:06:43 -040012#include "include/gpu/GrRecordingContext.h"
Brian Salomonc1a249d2020-10-19 10:55:45 -040013#include "include/gpu/GrYUVABackendTextures.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/codec/SkCodecImageGenerator.h"
Brian Salomon0c0b5a62021-01-11 14:40:44 -050015#include "src/core/SkYUVAInfoLocation.h"
Brian Salomon7db71392020-10-16 10:05:21 -040016#include "src/core/SkYUVMath.h"
Adlai Hollera0693042020-10-14 11:23:11 -040017#include "src/gpu/GrDirectContextPriv.h"
Brian Salomonefb5f072020-07-28 21:06:43 -040018#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon7db71392020-10-16 10:05:21 -040019#include "tools/gpu/ManagedBackendTexture.h"
20
21namespace {
22
23static SkPMColor convert_yuva_to_rgba(const float mtx[20], uint8_t yuva[4]) {
24 uint8_t y = yuva[0];
25 uint8_t u = yuva[1];
26 uint8_t v = yuva[2];
27 uint8_t a = yuva[3];
28
29 uint8_t r = SkTPin(SkScalarRoundToInt(mtx[ 0]*y + mtx[ 1]*u + mtx[ 2]*v + mtx[ 4]*255), 0, 255);
30 uint8_t g = SkTPin(SkScalarRoundToInt(mtx[ 5]*y + mtx[ 6]*u + mtx[ 7]*v + mtx[ 9]*255), 0, 255);
31 uint8_t b = SkTPin(SkScalarRoundToInt(mtx[10]*y + mtx[11]*u + mtx[12]*v + mtx[14]*255), 0, 255);
32
33 return SkPremultiplyARGBInline(a, r, g, b);
34}
35
36static uint8_t look_up(float x1, float y1, const SkPixmap& pmap, SkColorChannel channel) {
37 SkASSERT(x1 > 0 && x1 < 1.0f);
38 SkASSERT(y1 > 0 && y1 < 1.0f);
39 int x = SkScalarFloorToInt(x1 * pmap.width());
40 int y = SkScalarFloorToInt(y1 * pmap.height());
41
42 auto ii = pmap.info().makeColorType(kRGBA_8888_SkColorType).makeWH(1, 1);
43 uint32_t pixel;
44 SkAssertResult(pmap.readPixels(ii, &pixel, sizeof(pixel), x, y));
45 int shift = static_cast<int>(channel) * 8;
46 return static_cast<uint8_t>((pixel >> shift) & 0xff);
47}
48
49class Generator : public SkImageGenerator {
50public:
51 Generator(SkYUVAPixmaps pixmaps, sk_sp<SkColorSpace> cs)
52 : SkImageGenerator(SkImageInfo::Make(pixmaps.yuvaInfo().dimensions(),
53 kN32_SkColorType,
54 kPremul_SkAlphaType,
55 std::move(cs)))
56 , fPixmaps(std::move(pixmaps)) {}
57
58protected:
59 bool onGetPixels(const SkImageInfo& info,
60 void* pixels,
61 size_t rowBytes,
62 const Options&) override {
63 if (kUnknown_SkColorType == fFlattened.colorType()) {
64 fFlattened.allocPixels(info);
65 SkASSERT(info == this->getInfo());
66
67 float mtx[20];
68 SkColorMatrix_YUV2RGB(fPixmaps.yuvaInfo().yuvColorSpace(), mtx);
Brian Salomon0c0b5a62021-01-11 14:40:44 -050069 SkYUVAInfo::YUVALocations yuvaLocations = fPixmaps.toYUVALocations();
70 SkASSERT(SkYUVAInfo::YUVALocation::AreValidLocations(yuvaLocations));
Brian Salomon7db71392020-10-16 10:05:21 -040071
72 for (int y = 0; y < info.height(); ++y) {
73 for (int x = 0; x < info.width(); ++x) {
74 float x1 = (x + 0.5f) / info.width();
75 float y1 = (y + 0.5f) / info.height();
76
77 uint8_t yuva[4] = {0, 0, 0, 255};
78
Brian Salomon0c0b5a62021-01-11 14:40:44 -050079 for (auto c : {SkYUVAInfo::YUVAChannels::kY,
80 SkYUVAInfo::YUVAChannels::kU,
81 SkYUVAInfo::YUVAChannels::kV}) {
82 const auto& pmap = fPixmaps.plane(yuvaLocations[c].fPlane);
83 yuva[c] = look_up(x1, y1, pmap, yuvaLocations[c].fChannel);
Brian Salomon7db71392020-10-16 10:05:21 -040084 }
Brian Salomon0c0b5a62021-01-11 14:40:44 -050085 if (yuvaLocations[SkYUVAInfo::YUVAChannels::kA].fPlane >= 0) {
Brian Salomon7db71392020-10-16 10:05:21 -040086 const auto& pmap =
Brian Salomon0c0b5a62021-01-11 14:40:44 -050087 fPixmaps.plane(yuvaLocations[SkYUVAInfo::YUVAChannels::kA].fPlane);
88 yuva[3] = look_up(
89 x1, y1, pmap, yuvaLocations[SkYUVAInfo::YUVAChannels::kA].fChannel);
Brian Salomon7db71392020-10-16 10:05:21 -040090 }
91
92 // Making premul here.
93 *fFlattened.getAddr32(x, y) = convert_yuva_to_rgba(mtx, yuva);
94 }
95 }
96 }
97
98 return fFlattened.readPixels(info, pixels, rowBytes, 0, 0);
99 }
100
101 bool onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& types,
102 SkYUVAPixmapInfo* info) const override {
103 *info = fPixmaps.pixmapsInfo();
104 return info->isValid();
105 }
106
107 bool onGetYUVAPlanes(const SkYUVAPixmaps& pixmaps) override {
108 SkASSERT(pixmaps.yuvaInfo() == fPixmaps.yuvaInfo());
109 for (int i = 0; i < pixmaps.numPlanes(); ++i) {
110 SkASSERT(fPixmaps.plane(i).colorType() == pixmaps.plane(i).colorType());
111 SkASSERT(fPixmaps.plane(i).dimensions() == pixmaps.plane(i).dimensions());
112 SkASSERT(fPixmaps.plane(i).rowBytes() == pixmaps.plane(i).rowBytes());
113 fPixmaps.plane(i).readPixels(pixmaps.plane(i));
114 }
115 return true;
116 }
117
118private:
119 SkYUVAPixmaps fPixmaps;
120 SkBitmap fFlattened;
121};
122
123} // anonymous namespace
Michael Ludwigd9958f82019-03-21 13:08:36 -0400124
125namespace sk_gpu_test {
126
Brian Salomon7db71392020-10-16 10:05:21 -0400127std::unique_ptr<LazyYUVImage> LazyYUVImage::Make(sk_sp<SkData> data,
128 GrMipmapped mipmapped,
129 sk_sp<SkColorSpace> cs) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400130 std::unique_ptr<LazyYUVImage> image(new LazyYUVImage());
Brian Salomon7db71392020-10-16 10:05:21 -0400131 if (image->reset(std::move(data), mipmapped, std::move(cs))) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400132 return image;
133 } else {
134 return nullptr;
135 }
136}
137
Brian Salomon7db71392020-10-16 10:05:21 -0400138std::unique_ptr<LazyYUVImage> LazyYUVImage::Make(SkYUVAPixmaps pixmaps,
139 GrMipmapped mipmapped,
140 sk_sp<SkColorSpace> cs) {
141 std::unique_ptr<LazyYUVImage> image(new LazyYUVImage());
142 if (image->reset(std::move(pixmaps), mipmapped, std::move(cs))) {
143 return image;
Michael Ludwigd9958f82019-03-21 13:08:36 -0400144 } else {
145 return nullptr;
146 }
147}
148
Brian Salomon7db71392020-10-16 10:05:21 -0400149sk_sp<SkImage> LazyYUVImage::refImage(GrRecordingContext* rContext, Type type) {
150 if (this->ensureYUVImage(rContext, type)) {
151 size_t idx = static_cast<size_t>(type);
Brian Salomon6c11ba22020-10-16 11:11:40 -0400152 SkASSERT(idx < SK_ARRAY_COUNT(fYUVImage));
Brian Salomon7db71392020-10-16 10:05:21 -0400153 return fYUVImage[idx];
Michael Ludwigd9958f82019-03-21 13:08:36 -0400154 } else {
155 return nullptr;
156 }
157}
158
Brian Salomon7db71392020-10-16 10:05:21 -0400159bool LazyYUVImage::reset(sk_sp<SkData> data, GrMipmapped mipmapped, sk_sp<SkColorSpace> cs) {
Brian Salomon6db78b82020-07-31 08:57:48 -0400160 fMipmapped = mipmapped;
Michael Ludwigd9958f82019-03-21 13:08:36 -0400161 auto codec = SkCodecImageGenerator::MakeFromEncodedCodec(data);
162 if (!codec) {
163 return false;
164 }
165
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400166 SkYUVAPixmapInfo yuvaPixmapInfo;
Brian Salomon59c60b02020-09-01 15:01:15 -0400167 if (!codec->queryYUVAInfo(SkYUVAPixmapInfo::SupportedDataTypes::All(), &yuvaPixmapInfo)) {
Brian Salomon87d42e52020-08-24 09:18:16 -0400168 return false;
169 }
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400170 fPixmaps = SkYUVAPixmaps::Allocate(yuvaPixmapInfo);
Brian Salomon87d42e52020-08-24 09:18:16 -0400171 if (!fPixmaps.isValid()) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400172 return false;
173 }
174
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400175 if (!codec->getYUVAPlanes(fPixmaps)) {
Michael Ludwigd9958f82019-03-21 13:08:36 -0400176 return false;
177 }
178
Brian Salomon7db71392020-10-16 10:05:21 -0400179 fColorSpace = std::move(cs);
180
Brian Salomondb0288d2020-10-15 10:31:43 -0400181 // The SkPixmap data is fully configured now for MakeFromYUVAPixmaps once we get a GrContext
182 return true;
183}
184
Brian Salomon7db71392020-10-16 10:05:21 -0400185bool LazyYUVImage::reset(SkYUVAPixmaps pixmaps, GrMipmapped mipmapped, sk_sp<SkColorSpace> cs) {
186 if (!pixmaps.isValid()) {
187 return false;
Brian Salomon839fb222020-10-16 13:30:54 +0000188 }
Brian Salomon7db71392020-10-16 10:05:21 -0400189 fMipmapped = mipmapped;
190 if (pixmaps.ownsStorage()) {
191 fPixmaps = std::move(pixmaps);
192 } else {
193 fPixmaps = SkYUVAPixmaps::MakeCopy(std::move(pixmaps));
194 }
195 fColorSpace = std::move(cs);
196 // The SkPixmap data is fully configured now for MakeFromYUVAPixmaps once we get a GrContext
197 return true;
198}
199
200bool LazyYUVImage::ensureYUVImage(GrRecordingContext* rContext, Type type) {
201 size_t idx = static_cast<size_t>(type);
Brian Salomon6c11ba22020-10-16 11:11:40 -0400202 SkASSERT(idx < SK_ARRAY_COUNT(fYUVImage));
Brian Salomon7db71392020-10-16 10:05:21 -0400203 if (fYUVImage[idx] && fYUVImage[idx]->isValid(rContext)) {
204 return true; // Have already made a YUV image valid for this context.
Michael Ludwigd9958f82019-03-21 13:08:36 -0400205 }
Brian Salomonefb5f072020-07-28 21:06:43 -0400206 // Try to make a new YUV image for this context.
Brian Salomon7db71392020-10-16 10:05:21 -0400207 switch (type) {
208 case Type::kFromPixmaps:
209 if (!rContext || rContext->abandoned()) {
210 return false;
211 }
212 fYUVImage[idx] = SkImage::MakeFromYUVAPixmaps(rContext,
213 fPixmaps,
214 fMipmapped,
215 /*limit to max tex size*/ false,
216 fColorSpace);
217 break;
218 case Type::kFromGenerator: {
219 // Make sure the generator has ownership of its backing planes.
220 auto generator = std::make_unique<Generator>(fPixmaps, fColorSpace);
221 fYUVImage[idx] = SkImage::MakeFromGenerator(std::move(generator));
222 break;
Brian Salomon839fb222020-10-16 13:30:54 +0000223 }
Brian Salomon7db71392020-10-16 10:05:21 -0400224 case Type::kFromTextures:
225 if (!rContext || rContext->abandoned()) {
226 return false;
227 }
Brian Salomon694ff172020-11-04 16:54:28 -0500228 if (fMipmapped == GrMipmapped::kYes) {
229 // If this becomes necessary we should invoke SkMipmapBuilder here to make mip
230 // maps from our src data (and then pass a pixmaps array to initialize the planar
231 // textures.
232 return false;
233 }
Brian Salomon7db71392020-10-16 10:05:21 -0400234 if (auto direct = rContext->asDirectContext()) {
235 sk_sp<sk_gpu_test::ManagedBackendTexture> mbets[SkYUVAInfo::kMaxPlanes];
236 GrBackendTexture textures[SkYUVAInfo::kMaxPlanes];
Brian Salomon7db71392020-10-16 10:05:21 -0400237 for (int i = 0; i < fPixmaps.numPlanes(); ++i) {
238 mbets[i] = sk_gpu_test::ManagedBackendTexture::MakeWithData(
Brian Salomonb5f880a2020-12-07 11:30:16 -0500239 direct,
240 fPixmaps.plane(i),
241 kTopLeft_GrSurfaceOrigin,
242 GrRenderable::kNo,
243 GrProtected::kNo);
Brian Salomon7db71392020-10-16 10:05:21 -0400244 if (mbets[i]) {
245 textures[i] = mbets[i]->texture();
Brian Salomon7db71392020-10-16 10:05:21 -0400246 } else {
247 return false;
248 }
249 }
Brian Salomonc1a249d2020-10-19 10:55:45 -0400250 GrYUVABackendTextures yuvaTextures(fPixmaps.yuvaInfo(),
251 textures,
252 kTopLeft_GrSurfaceOrigin);
253 if (!yuvaTextures.isValid()) {
Brian Salomonbe8004d2020-10-16 22:32:35 +0000254 return false;
255 }
Brian Salomon8670c982020-12-08 16:39:32 -0500256 void* planeRelContext =
257 sk_gpu_test::ManagedBackendTexture::MakeYUVAReleaseContext(mbets);
258 fYUVImage[idx] = SkImage::MakeFromYUVATextures(
259 direct,
260 yuvaTextures,
261 fColorSpace,
262 sk_gpu_test::ManagedBackendTexture::ReleaseProc,
263 planeRelContext);
Brian Salomon7db71392020-10-16 10:05:21 -0400264 }
Brian Salomon839fb222020-10-16 13:30:54 +0000265 }
Brian Salomon7db71392020-10-16 10:05:21 -0400266 return fYUVImage[idx] != nullptr;
Brian Salomon839fb222020-10-16 13:30:54 +0000267}
Michael Ludwigd9958f82019-03-21 13:08:36 -0400268} // namespace sk_gpu_test