blob: 15a98297d41d314222a2b660e8e1d16f112fb06c [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"
26#include "include/gpu/GrContext.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"
bsalomon993a4212015-05-29 11:37:25 -070032
Ben Wagner7fde8e12019-05-01 17:28:53 -040033class GrRenderTargetContext;
34
bsalomon993a4212015-05-29 11:37:25 -070035namespace skiagm {
Chris Dalton3a778372019-02-07 15:23:36 -070036class ImageFromYUVTextures : public GpuGM {
bsalomon993a4212015-05-29 11:37:25 -070037public:
38 ImageFromYUVTextures() {
39 this->setBGColor(0xFFFFFFFF);
40 }
41
42protected:
43 SkString onShortName() override {
44 return SkString("image_from_yuv_textures");
45 }
46
Brian Salomon5c4c61e2020-03-25 14:26:01 -040047 SkISize onISize() override { return {1420, 610}; }
Brian Salomon9b228382020-03-24 12:55:29 -040048
Brian Salomon5575e3c2020-03-25 15:19:37 +000049 void onOnceBeforeDraw() override {
Brian Salomon5c4c61e2020-03-25 14:26:01 -040050 fRGBABmp = this->createBmpAndPlanes("images/mandrill_32.png", fYUVABmps);
51 }
Brian Salomon36737482020-03-24 16:47:52 -040052
Brian Salomon5c4c61e2020-03-25 14:26:01 -040053 SkBitmap createBmpAndPlanes(const char* name, SkBitmap yuvaBmps[4]) {
54 SkBitmap bmp;
55 if (!GetResourceAsBitmap(name, &bmp)) {
56 return {};
57 }
58 auto ii = SkImageInfo::Make(bmp.dimensions(), kRGBA_8888_SkColorType, kPremul_SkAlphaType);
59
60 SkBitmap rgbaBmp;
61 rgbaBmp.allocPixels(ii);
62 bmp.readPixels(rgbaBmp.pixmap(), 0, 0);
63
64 SkImageInfo yaInfo = SkImageInfo::Make(rgbaBmp.dimensions(), kAlpha_8_SkColorType,
65 kUnpremul_SkAlphaType);
66 yuvaBmps[0].allocPixels(yaInfo);
67 SkISize uvSize = {rgbaBmp.width()/2, rgbaBmp.height()/2};
68 SkImageInfo uvInfo = SkImageInfo::Make(uvSize, kAlpha_8_SkColorType, kUnpremul_SkAlphaType);
69 yuvaBmps[1].allocPixels(uvInfo);
70 yuvaBmps[2].allocPixels(uvInfo);
71 yuvaBmps[3].allocPixels(yaInfo);
72
73 unsigned char* yuvPixels[] = {
74 static_cast<unsigned char*>(yuvaBmps[0].getPixels()),
75 static_cast<unsigned char*>(yuvaBmps[1].getPixels()),
76 static_cast<unsigned char*>(yuvaBmps[2].getPixels()),
77 static_cast<unsigned char*>(yuvaBmps[3].getPixels()),
78 };
bsalomon993a4212015-05-29 11:37:25 -070079
Brian Salomon7c94d182020-03-18 17:24:02 -040080 float m[20];
81 SkColorMatrix_RGB2YUV(kJPEG_SkYUVColorSpace, m);
Robert Phillips0a22ba82019-03-06 12:36:47 -050082 // Here we encode using the kJPEG_SkYUVColorSpace (i.e., full-swing Rec 601) even though
83 // we will draw it with all the supported yuv color spaces when converted back to RGB
Brian Salomon5c4c61e2020-03-25 14:26:01 -040084 for (int j = 0; j < yaInfo.height(); ++j) {
85 for (int i = 0; i < yaInfo.width(); ++i) {
86 auto rgba = *rgbaBmp.getAddr32(i, j);
87 auto r = (rgba & 0x000000ff) >> 0;
88 auto g = (rgba & 0x0000ff00) >> 8;
89 auto b = (rgba & 0x00ff0000) >> 16;
90 auto a = (rgba & 0xff000000) >> 24;
91 yuvPixels[0][j*yaInfo.width() + i] = SkToU8(
92 sk_float_round2int(m[0]*r + m[1]*g + m[2]*b + m[3]*a + 255*m[4]));
93 yuvPixels[3][j*yaInfo.width() + i] = SkToU8(sk_float_round2int(
94 m[15]*r + m[16]*g + m[17]*b + m[18]*a + 255*m[19]));
95 }
bsalomon993a4212015-05-29 11:37:25 -070096 }
Brian Salomon5c4c61e2020-03-25 14:26:01 -040097 for (int j = 0; j < uvInfo.height(); ++j) {
98 for (int i = 0; i < uvInfo.width(); ++i) {
bsalomon993a4212015-05-29 11:37:25 -070099 // Average together 4 pixels of RGB.
Brian Salomon7c94d182020-03-18 17:24:02 -0400100 int rgba[] = {0, 0, 0, 0};
bsalomon993a4212015-05-29 11:37:25 -0700101 for (int y = 0; y < 2; ++y) {
102 for (int x = 0; x < 2; ++x) {
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400103 auto src = *rgbaBmp.getAddr32(2 * i + x, 2 * j + y);
104 rgba[0] += (src & 0x000000ff) >> 0;
105 rgba[1] += (src & 0x0000ff00) >> 8;
106 rgba[2] += (src & 0x00ff0000) >> 16;
107 rgba[3] += (src & 0xff000000) >> 24;
bsalomon993a4212015-05-29 11:37:25 -0700108 }
109 }
Brian Salomon7c94d182020-03-18 17:24:02 -0400110 for (int c = 0; c < 4; ++c) {
111 rgba[c] /= 4;
bsalomon993a4212015-05-29 11:37:25 -0700112 }
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400113 int uvIndex = j*uvInfo.width() + i;
114 yuvPixels[1][uvIndex] = SkToU8(sk_float_round2int(
Brian Salomon7c94d182020-03-18 17:24:02 -0400115 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 -0400116 yuvPixels[2][uvIndex] = SkToU8(sk_float_round2int(
Brian Salomon7c94d182020-03-18 17:24:02 -0400117 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 -0700118 }
119 }
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400120 return rgbaBmp;
bsalomon993a4212015-05-29 11:37:25 -0700121 }
122
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400123 void createYUVTextures(SkBitmap bmps[4], GrContext* context, GrBackendTexture textures[4]) {
124 for (int i = 0; i < 4; ++i) {
Brian Salomonc505a452020-04-06 10:29:02 -0400125 textures[i] = context->createBackendTexture(bmps[i].pixmap(), GrRenderable::kNo,
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400126 GrProtected::kNo);
bsalomon993a4212015-05-29 11:37:25 -0700127 }
bsalomon993a4212015-05-29 11:37:25 -0700128 }
129
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400130 void createResultTexture(GrContext* context, SkISize size, GrBackendTexture* resultTexture) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400131 *resultTexture = context->createBackendTexture(
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400132 size.width(), size.height(), kRGBA_8888_SkColorType, SkColors::kTransparent,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400133 GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
Brian Salomond2fcfb52018-09-17 21:57:11 -0400134 }
135
Brian Salomon279f8732018-09-18 11:19:33 -0400136 void deleteBackendTextures(GrContext* context, GrBackendTexture textures[], int n) {
Khushalc421ca12018-06-26 14:38:34 -0700137 if (context->abandoned()) {
Robert Phillips6d363702018-06-25 08:53:09 -0400138 return;
139 }
jvanverth672bb7f2015-07-13 07:19:57 -0700140
Robert Phillipscb1adb42019-06-10 15:09:34 -0400141 GrFlushInfo flushInfo;
142 flushInfo.fFlags = kSyncCpu_GrFlushFlag;
143 context->flush(flushInfo);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400144 context->submit(true);
bsalomon993a4212015-05-29 11:37:25 -0700145
Brian Salomon279f8732018-09-18 11:19:33 -0400146 for (int i = 0; i < n; ++i) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400147 context->deleteBackendTexture(textures[i]);
bsalomon993a4212015-05-29 11:37:25 -0700148 }
bsalomon993a4212015-05-29 11:37:25 -0700149 }
150
Chris Dalton3a778372019-02-07 15:23:36 -0700151 void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400152 GrBackendTexture yuvaTextures[4];
Brian Salomonc505a452020-04-06 10:29:02 -0400153
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400154 this->createYUVTextures(fYUVABmps, context, yuvaTextures);
Brian Salomonc505a452020-04-06 10:29:02 -0400155 SkYUVAIndex indices[4];
156 for (int i = 0; i < 4; ++i) {
157 if (!yuvaTextures[i].isValid()) {
158 return;
159 }
160 auto chanMask = yuvaTextures[i].getBackendFormat().channelMask();
161 // We expect the single channel bitmaps to produce single channel textures.
162 SkASSERT(chanMask && SkIsPow2(chanMask));
163 if (chanMask & kGray_SkColorChannelFlag) {
164 indices[i].fChannel = SkColorChannel::kR;
165 } else {
166 indices[i].fChannel = static_cast<SkColorChannel>(31 - SkCLZ(chanMask));
167 }
168 indices[i].fIndex = i;
169 }
Brian Salomon8efbbbc2020-04-02 16:59:19 -0400170
171 // We remake this image before each draw because if any draw flattens it to RGBA then
172 // all subsequent draws use the RGBA texture.
173 auto makeImage1 = [&]() {
174 return SkImage::MakeFromYUVATextures(context,
175 kJPEG_SkYUVColorSpace,
176 yuvaTextures,
Brian Salomonc505a452020-04-06 10:29:02 -0400177 indices,
Brian Salomon8efbbbc2020-04-02 16:59:19 -0400178 fRGBABmp.dimensions(),
179 kTopLeft_GrSurfaceOrigin);
180 };
bsalomon993a4212015-05-29 11:37:25 -0700181
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400182 GrBackendTexture resultTexture;
183 this->createResultTexture(context, fRGBABmp.dimensions(), &resultTexture);
184 auto image2 = SkImage::MakeFromYUVTexturesCopyWithExternalBackend(context,
185 kJPEG_SkYUVColorSpace,
186 yuvaTextures,
187 kTopLeft_GrSurfaceOrigin,
188 resultTexture);
bsalomon993a4212015-05-29 11:37:25 -0700189
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400190 auto draw_image = [canvas](SkImage* image, SkFilterQuality fq) -> SkSize {
191 if (!image) {
192 return {0, 0};
Brian Salomon36737482020-03-24 16:47:52 -0400193 }
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400194 SkPaint paint;
195 paint.setFilterQuality(fq);
196 canvas->drawImage(image, 0, 0, &paint);
197 return {SkIntToScalar(image->width()), SkIntToScalar(image->height())};
198 };
Brian Salomon36737482020-03-24 16:47:52 -0400199
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400200 auto draw_image_rect = [canvas](SkImage* image, SkFilterQuality fq) -> SkSize {
201 if (!image) {
202 return {0, 0};
203 }
204 SkPaint paint;
205 paint.setFilterQuality(fq);
206 auto subset = SkRect::Make(image->dimensions());
207 subset.inset(subset.width() * .05f, subset.height() * .1f);
208 auto dst = SkRect::MakeWH(subset.width(), subset.height());
209 canvas->drawImageRect(image, subset, dst, &paint);
210 return {dst.width(), dst.height()};
211 };
212
213 auto draw_image_shader = [canvas](SkImage* image, SkFilterQuality fq) -> SkSize {
214 if (!image) {
215 return {0, 0};
216 }
217 SkMatrix m;
218 m.setRotate(45, image->width()/2.f, image->height()/2.f);
219 auto shader = image->makeShader(SkTileMode::kMirror, SkTileMode::kDecal, m);
220 SkPaint paint;
221 paint.setFilterQuality(fq);
222 paint.setShader(std::move(shader));
223 auto rect = SkRect::MakeWH(image->width() * 1.3f, image->height());
224 canvas->drawRect(rect, paint);
225 return {rect.width(), rect.height()};
226 };
227
228 canvas->translate(kPad, kPad);
229 using DrawSig = SkSize(SkImage* image, SkFilterQuality fq);
230 using DF = std::function<DrawSig>;
231 for (const auto& draw : {DF(draw_image), DF(draw_image_rect), DF(draw_image_shader)}) {
232 for (auto scale : {1.f, 4.f, 0.75f}) {
233 SkScalar h = 0;
234 canvas->save();
235 for (auto fq : {kNone_SkFilterQuality, kLow_SkFilterQuality,
236 kMedium_SkFilterQuality, kHigh_SkFilterQuality}) {
237 canvas->save();
238 canvas->scale(scale, scale);
Brian Salomon8efbbbc2020-04-02 16:59:19 -0400239 auto s1 = draw(makeImage1().get(), fq);
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400240 canvas->restore();
241 canvas->translate(kPad + SkScalarCeilToScalar(scale*s1.width()), 0);
242 canvas->save();
243 canvas->scale(scale, scale);
244 auto s2 = draw(image2.get(), fq);
245 canvas->restore();
246 canvas->translate(kPad + SkScalarCeilToScalar(scale*s2.width()), 0);
247 h = std::max({h, s1.height(), s2.height()});
248 }
249 canvas->restore();
250 canvas->translate(0, kPad + SkScalarCeilToScalar(scale*h));
251 }
bsalomon993a4212015-05-29 11:37:25 -0700252 }
Brian Salomond2fcfb52018-09-17 21:57:11 -0400253
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400254 this->deleteBackendTextures(context, &resultTexture, 1);
255 this->deleteBackendTextures(context, yuvaTextures, 4);
bsalomon993a4212015-05-29 11:37:25 -0700256 }
257
258private:
Brian Salomon5c4c61e2020-03-25 14:26:01 -0400259 SkBitmap fRGBABmp;
260 SkBitmap fYUVABmps[4];
bsalomon993a4212015-05-29 11:37:25 -0700261
Robert Phillips0a22ba82019-03-06 12:36:47 -0500262 static constexpr SkScalar kPad = 10.0f;
bsalomon993a4212015-05-29 11:37:25 -0700263
264 typedef GM INHERITED;
265};
266
halcanary385fe4d2015-08-26 13:07:48 -0700267DEF_GM(return new ImageFromYUVTextures;)
bsalomon993a4212015-05-29 11:37:25 -0700268}