blob: 324e1901207cd5878ab5281a0621652861f1af6d [file] [log] [blame]
bsalomon993a4212015-05-29 11:37:25 -07001/*
2 * Copyright 2015 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
8// This test only works with the GPU backend.
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkBitmap.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkCanvas.h"
13#include "include/core/SkColor.h"
14#include "include/core/SkColorFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#include "include/core/SkImageInfo.h"
17#include "include/core/SkPaint.h"
18#include "include/core/SkPixmap.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040019#include "include/core/SkRefCnt.h"
20#include "include/core/SkScalar.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/core/SkSize.h"
22#include "include/core/SkString.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "include/core/SkTypes.h"
Brian Salomon5c4c61e2020-03-25 14:26:01 -040024#include "include/core/SkYUVAIndex.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/gpu/GrBackendSurface.h"
Robert Phillipsb87b39b2020-07-01 14:45:24 -040026#include "include/gpu/GrDirectContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040027#include "include/gpu/GrTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "include/private/SkTo.h"
Brian Salomonc505a452020-04-06 10:29:02 -040029#include "src/core/SkMathPriv.h"
Brian Salomon7c94d182020-03-18 17:24:02 -040030#include "src/core/SkYUVMath.h"
Brian Salomon5c4c61e2020-03-25 14:26:01 -040031#include "tools/Resources.h"
Robert Phillipsf105d382020-06-19 14:27:14 -040032#include "tools/gpu/YUVUtils.h"
33
34using sk_gpu_test::YUVABackendReleaseContext;
bsalomon993a4212015-05-29 11:37:25 -070035
Ben Wagner7fde8e12019-05-01 17:28:53 -040036class GrRenderTargetContext;
37
bsalomon993a4212015-05-29 11:37:25 -070038namespace skiagm {
Chris Dalton3a778372019-02-07 15:23:36 -070039class ImageFromYUVTextures : public GpuGM {
bsalomon993a4212015-05-29 11:37:25 -070040public:
41 ImageFromYUVTextures() {
42 this->setBGColor(0xFFFFFFFF);
43 }
44
45protected:
46 SkString onShortName() override {
47 return SkString("image_from_yuv_textures");
48 }
49
Brian Salomon5c4c61e2020-03-25 14:26:01 -040050 SkISize onISize() override { return {1420, 610}; }
Brian Salomon9b228382020-03-24 12:55:29 -040051
Robert Phillipsf105d382020-06-19 14:27:14 -040052 static SkBitmap CreateBmpAndPlanes(const char* name, SkBitmap yuvaBmps[4]) {
Brian Salomon5c4c61e2020-03-25 14:26:01 -040053 SkBitmap bmp;
54 if (!GetResourceAsBitmap(name, &bmp)) {
55 return {};
56 }
57 auto ii = SkImageInfo::Make(bmp.dimensions(), kRGBA_8888_SkColorType, kPremul_SkAlphaType);
58
59 SkBitmap rgbaBmp;
60 rgbaBmp.allocPixels(ii);
61 bmp.readPixels(rgbaBmp.pixmap(), 0, 0);
62
63 SkImageInfo yaInfo = SkImageInfo::Make(rgbaBmp.dimensions(), kAlpha_8_SkColorType,
64 kUnpremul_SkAlphaType);
65 yuvaBmps[0].allocPixels(yaInfo);
66 SkISize uvSize = {rgbaBmp.width()/2, rgbaBmp.height()/2};
67 SkImageInfo uvInfo = SkImageInfo::Make(uvSize, kAlpha_8_SkColorType, kUnpremul_SkAlphaType);
68 yuvaBmps[1].allocPixels(uvInfo);
69 yuvaBmps[2].allocPixels(uvInfo);
70 yuvaBmps[3].allocPixels(yaInfo);
71
72 unsigned char* yuvPixels[] = {
73 static_cast<unsigned char*>(yuvaBmps[0].getPixels()),
74 static_cast<unsigned char*>(yuvaBmps[1].getPixels()),
75 static_cast<unsigned char*>(yuvaBmps[2].getPixels()),
76 static_cast<unsigned char*>(yuvaBmps[3].getPixels()),
77 };
bsalomon993a4212015-05-29 11:37:25 -070078
Brian Salomon7c94d182020-03-18 17:24:02 -040079 float m[20];
80 SkColorMatrix_RGB2YUV(kJPEG_SkYUVColorSpace, m);
Robert Phillips0a22ba82019-03-06 12:36:47 -050081 // Here we encode using the kJPEG_SkYUVColorSpace (i.e., full-swing Rec 601) even though
82 // we will draw it with all the supported yuv color spaces when converted back to RGB
Brian Salomon5c4c61e2020-03-25 14:26:01 -040083 for (int j = 0; j < yaInfo.height(); ++j) {
84 for (int i = 0; i < yaInfo.width(); ++i) {
85 auto rgba = *rgbaBmp.getAddr32(i, j);
86 auto r = (rgba & 0x000000ff) >> 0;
87 auto g = (rgba & 0x0000ff00) >> 8;
88 auto b = (rgba & 0x00ff0000) >> 16;
89 auto a = (rgba & 0xff000000) >> 24;
90 yuvPixels[0][j*yaInfo.width() + i] = SkToU8(
91 sk_float_round2int(m[0]*r + m[1]*g + m[2]*b + m[3]*a + 255*m[4]));
92 yuvPixels[3][j*yaInfo.width() + i] = SkToU8(sk_float_round2int(
93 m[15]*r + m[16]*g + m[17]*b + m[18]*a + 255*m[19]));
94 }
bsalomon993a4212015-05-29 11:37:25 -070095 }
Brian Salomon5c4c61e2020-03-25 14:26:01 -040096 for (int j = 0; j < uvInfo.height(); ++j) {
97 for (int i = 0; i < uvInfo.width(); ++i) {
bsalomon993a4212015-05-29 11:37:25 -070098 // Average together 4 pixels of RGB.
Brian Salomon7c94d182020-03-18 17:24:02 -040099 int rgba[] = {0, 0, 0, 0};
bsalomon993a4212015-05-29 11:37:25 -0700100 for (int y = 0; y < 2; ++y) {
101 for (int x = 0; x < 2; ++x) {
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400102 auto src = *rgbaBmp.getAddr32(2 * i + x, 2 * j + y);
103 rgba[0] += (src & 0x000000ff) >> 0;
104 rgba[1] += (src & 0x0000ff00) >> 8;
105 rgba[2] += (src & 0x00ff0000) >> 16;
106 rgba[3] += (src & 0xff000000) >> 24;
bsalomon993a4212015-05-29 11:37:25 -0700107 }
108 }
Brian Salomon7c94d182020-03-18 17:24:02 -0400109 for (int c = 0; c < 4; ++c) {
110 rgba[c] /= 4;
bsalomon993a4212015-05-29 11:37:25 -0700111 }
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400112 int uvIndex = j*uvInfo.width() + i;
113 yuvPixels[1][uvIndex] = SkToU8(sk_float_round2int(
Brian Salomon7c94d182020-03-18 17:24:02 -0400114 m[5]*rgba[0] + m[6]*rgba[1] + m[7]*rgba[2] + m[8]*rgba[3] + 255*m[9]));
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400115 yuvPixels[2][uvIndex] = SkToU8(sk_float_round2int(
Brian Salomon7c94d182020-03-18 17:24:02 -0400116 m[10]*rgba[0] + m[11]*rgba[1] + m[12]*rgba[2] + m[13]*rgba[3] + 255*m[14]));
bsalomon993a4212015-05-29 11:37:25 -0700117 }
118 }
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400119 return rgbaBmp;
bsalomon993a4212015-05-29 11:37:25 -0700120 }
121
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400122 static bool CreateYUVBackendTextures(GrDirectContext* context, SkBitmap bmps[4],
Robert Phillipsf105d382020-06-19 14:27:14 -0400123 SkYUVAIndex indices[4],
124 YUVABackendReleaseContext* beContext) {
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400125 for (int i = 0; i < 4; ++i) {
Robert Phillips98c39ba2020-06-26 10:01:19 -0400126 GrBackendTexture tmp = context->createBackendTexture(
127 bmps[i].pixmap(), GrRenderable::kNo, GrProtected::kNo,
128 YUVABackendReleaseContext::CreationCompleteProc(i),
129 beContext);
Robert Phillipsf105d382020-06-19 14:27:14 -0400130 if (!tmp.isValid()) {
131 return false;
Brian Salomonc505a452020-04-06 10:29:02 -0400132 }
Robert Phillipsf105d382020-06-19 14:27:14 -0400133
134 beContext->set(i, tmp);
135 }
136
137 for (int i = 0; i < 4; ++i) {
138 auto chanMask = beContext->beTexture(i).getBackendFormat().channelMask();
Brian Salomonc505a452020-04-06 10:29:02 -0400139 // We expect the single channel bitmaps to produce single channel textures.
140 SkASSERT(chanMask && SkIsPow2(chanMask));
141 if (chanMask & kGray_SkColorChannelFlag) {
142 indices[i].fChannel = SkColorChannel::kR;
143 } else {
144 indices[i].fChannel = static_cast<SkColorChannel>(31 - SkCLZ(chanMask));
145 }
146 indices[i].fIndex = i;
147 }
Brian Salomon8efbbbc2020-04-02 16:59:19 -0400148
Robert Phillipsf105d382020-06-19 14:27:14 -0400149 return true;
150 }
bsalomon993a4212015-05-29 11:37:25 -0700151
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400152 sk_sp<SkImage> makeYUVAImage(GrDirectContext* context) {
Robert Phillipsf105d382020-06-19 14:27:14 -0400153 auto releaseContext = new YUVABackendReleaseContext(context);
154 SkYUVAIndex indices[4];
bsalomon993a4212015-05-29 11:37:25 -0700155
Robert Phillipsf105d382020-06-19 14:27:14 -0400156 if (!CreateYUVBackendTextures(context, fYUVABmps, indices, releaseContext)) {
Robert Phillips98c39ba2020-06-26 10:01:19 -0400157 YUVABackendReleaseContext::Unwind(context, releaseContext, false);
Robert Phillipsf105d382020-06-19 14:27:14 -0400158 return nullptr;
159 }
160
161 return SkImage::MakeFromYUVATextures(context,
162 kJPEG_SkYUVColorSpace,
163 releaseContext->beTextures(),
164 indices,
165 fRGBABmp.dimensions(),
166 kTopLeft_GrSurfaceOrigin,
167 nullptr,
168 YUVABackendReleaseContext::Release,
169 releaseContext);
170 }
171
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400172 sk_sp<SkImage> createReferenceImage(GrDirectContext* context) {
Robert Phillipsf105d382020-06-19 14:27:14 -0400173 auto planeReleaseContext = new YUVABackendReleaseContext(context);
174 SkYUVAIndex indices[4];
175
176 if (!CreateYUVBackendTextures(context, fYUVABmps, indices, planeReleaseContext)) {
Robert Phillips98c39ba2020-06-26 10:01:19 -0400177 YUVABackendReleaseContext::Unwind(context, planeReleaseContext, false);
Robert Phillipsf105d382020-06-19 14:27:14 -0400178 return nullptr;
179 }
180
181 auto rgbaReleaseContext = new YUVABackendReleaseContext(context);
Robert Phillips98c39ba2020-06-26 10:01:19 -0400182
183 GrBackendTexture resultTexture = context->createBackendTexture(
184 fRGBABmp.dimensions().width(), fRGBABmp.dimensions().height(),
185 kRGBA_8888_SkColorType, SkColors::kTransparent,
186 GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo,
187 YUVABackendReleaseContext::CreationCompleteProc(0),
188 rgbaReleaseContext);
189 if (!resultTexture.isValid()) {
190 YUVABackendReleaseContext::Unwind(context, planeReleaseContext, false);
191 YUVABackendReleaseContext::Unwind(context, rgbaReleaseContext, false);
192 return nullptr;
193 }
194
Robert Phillipsf105d382020-06-19 14:27:14 -0400195 rgbaReleaseContext->set(0, resultTexture);
196
197 auto tmp = SkImage::MakeFromYUVATexturesCopyWithExternalBackend(
198 context,
199 kJPEG_SkYUVColorSpace,
200 planeReleaseContext->beTextures(),
201 indices,
202 fRGBABmp.dimensions(),
203 kTopLeft_GrSurfaceOrigin,
204 resultTexture,
205 nullptr,
206 YUVABackendReleaseContext::Release,
207 rgbaReleaseContext);
Robert Phillips98c39ba2020-06-26 10:01:19 -0400208 YUVABackendReleaseContext::Unwind(context, planeReleaseContext, true);
Robert Phillipsf105d382020-06-19 14:27:14 -0400209 return tmp;
210 }
211
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400212 DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
Robert Phillipsf105d382020-06-19 14:27:14 -0400213 if (!context || context->abandoned()) {
214 return DrawResult::kSkip;
215 }
216
Robert Phillipsf105d382020-06-19 14:27:14 -0400217 fRGBABmp = CreateBmpAndPlanes("images/mandrill_32.png", fYUVABmps);
218
219 // We make a version of this image for each draw because, if any draw flattens it to
220 // RGBA, then all subsequent draws would use the RGBA texture.
221 for (int i = 0; i < kNumImages; ++i) {
222 fYUVAImages[i] = this->makeYUVAImage(context);
223 if (!fYUVAImages[i]) {
224 *errorMsg = "Couldn't create src YUVA image.";
225 return DrawResult::kFail;
226 }
227 }
228
229 fReferenceImage = this->createReferenceImage(context);
230 if (!fReferenceImage) {
231 *errorMsg = "Couldn't create reference YUVA image.";
232 return DrawResult::kFail;
233 }
234
235 // Some backends (e.g., Vulkan) require all work be completed for backend textures
236 // before they are deleted. Since we don't know when we'll next have access to a
237 // direct context, flush all the work now.
Greg Danielce9f0162020-06-30 13:42:46 -0400238 context->flush();
Robert Phillipsf105d382020-06-19 14:27:14 -0400239 context->submit(true);
240
241 return DrawResult::kOk;
242 }
243
Robert Phillipsb795bea2020-06-25 12:38:53 -0400244 void onGpuTeardown() override {
245 for (sk_sp<SkImage>& image : fYUVAImages) {
246 image.reset();
247 }
248 fReferenceImage.reset();
249 }
250
Robert Phillipsf105d382020-06-19 14:27:14 -0400251 SkImage* getYUVAImage(int index) {
252 SkASSERT(index >= 0 && index < kNumImages);
253 return fYUVAImages[index].get();
254 }
255
Robert Phillips95c250c2020-06-29 15:36:12 -0400256 void onDraw(GrRecordingContext*, GrRenderTargetContext*, SkCanvas* canvas) override {
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400257 auto draw_image = [canvas](SkImage* image, SkFilterQuality fq) -> SkSize {
258 if (!image) {
259 return {0, 0};
Brian Salomon36737482020-03-24 16:47:52 -0400260 }
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400261 SkPaint paint;
262 paint.setFilterQuality(fq);
263 canvas->drawImage(image, 0, 0, &paint);
264 return {SkIntToScalar(image->width()), SkIntToScalar(image->height())};
265 };
Brian Salomon36737482020-03-24 16:47:52 -0400266
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400267 auto draw_image_rect = [canvas](SkImage* image, SkFilterQuality fq) -> SkSize {
268 if (!image) {
269 return {0, 0};
270 }
271 SkPaint paint;
272 paint.setFilterQuality(fq);
273 auto subset = SkRect::Make(image->dimensions());
274 subset.inset(subset.width() * .05f, subset.height() * .1f);
275 auto dst = SkRect::MakeWH(subset.width(), subset.height());
276 canvas->drawImageRect(image, subset, dst, &paint);
277 return {dst.width(), dst.height()};
278 };
279
280 auto draw_image_shader = [canvas](SkImage* image, SkFilterQuality fq) -> SkSize {
281 if (!image) {
282 return {0, 0};
283 }
284 SkMatrix m;
285 m.setRotate(45, image->width()/2.f, image->height()/2.f);
286 auto shader = image->makeShader(SkTileMode::kMirror, SkTileMode::kDecal, m);
287 SkPaint paint;
288 paint.setFilterQuality(fq);
289 paint.setShader(std::move(shader));
290 auto rect = SkRect::MakeWH(image->width() * 1.3f, image->height());
291 canvas->drawRect(rect, paint);
292 return {rect.width(), rect.height()};
293 };
294
295 canvas->translate(kPad, kPad);
Robert Phillipsf105d382020-06-19 14:27:14 -0400296 int imageIndex = 0;
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400297 using DrawSig = SkSize(SkImage* image, SkFilterQuality fq);
298 using DF = std::function<DrawSig>;
299 for (const auto& draw : {DF(draw_image), DF(draw_image_rect), DF(draw_image_shader)}) {
300 for (auto scale : {1.f, 4.f, 0.75f}) {
301 SkScalar h = 0;
302 canvas->save();
303 for (auto fq : {kNone_SkFilterQuality, kLow_SkFilterQuality,
304 kMedium_SkFilterQuality, kHigh_SkFilterQuality}) {
305 canvas->save();
306 canvas->scale(scale, scale);
Robert Phillipsf105d382020-06-19 14:27:14 -0400307 auto s1 = draw(this->getYUVAImage(imageIndex++), fq);
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400308 canvas->restore();
309 canvas->translate(kPad + SkScalarCeilToScalar(scale*s1.width()), 0);
310 canvas->save();
311 canvas->scale(scale, scale);
Robert Phillipsf105d382020-06-19 14:27:14 -0400312 auto s2 = draw(fReferenceImage.get(), fq);
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400313 canvas->restore();
314 canvas->translate(kPad + SkScalarCeilToScalar(scale*s2.width()), 0);
315 h = std::max({h, s1.height(), s2.height()});
316 }
317 canvas->restore();
318 canvas->translate(0, kPad + SkScalarCeilToScalar(scale*h));
319 }
bsalomon993a4212015-05-29 11:37:25 -0700320 }
321 }
322
323private:
Robert Phillipsf105d382020-06-19 14:27:14 -0400324 SkBitmap fRGBABmp; // TODO: oddly, it looks like this could just be an SkISize
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400325 SkBitmap fYUVABmps[4];
bsalomon993a4212015-05-29 11:37:25 -0700326
Robert Phillipsf105d382020-06-19 14:27:14 -0400327 // 3 draws x 3 scales x 4 filter qualities
328 static constexpr int kNumImages = 3 * 3 * 4;
329 sk_sp<SkImage> fYUVAImages[kNumImages];
330 sk_sp<SkImage> fReferenceImage;
331
Robert Phillips0a22ba82019-03-06 12:36:47 -0500332 static constexpr SkScalar kPad = 10.0f;
bsalomon993a4212015-05-29 11:37:25 -0700333
334 typedef GM INHERITED;
335};
336
halcanary385fe4d2015-08-26 13:07:48 -0700337DEF_GM(return new ImageFromYUVTextures;)
bsalomon993a4212015-05-29 11:37:25 -0700338}