sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
| 10 | #include "gm.h" |
| 11 | |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 12 | #include "GrContext.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 13 | #include "GrContextPriv.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 14 | #include "GrProxyProvider.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 15 | #include "GrRenderTargetContextPriv.h" |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 16 | #include "GrTextureProxy.h" |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 17 | #include "SkBitmap.h" |
| 18 | #include "SkGr.h" |
| 19 | #include "SkGradientShader.h" |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 20 | #include "effects/GrYUVtoRGBEffect.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 21 | #include "ops/GrDrawOp.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 22 | #include "ops/GrRectOpFactory.h" |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 23 | |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 24 | #define YSIZE 8 |
| 25 | #define USIZE 4 |
| 26 | #define VSIZE 4 |
| 27 | |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 28 | namespace skiagm { |
| 29 | /** |
| 30 | * This GM directly exercises GrYUVtoRGBEffect. |
| 31 | */ |
| 32 | class YUVtoRGBEffect : public GM { |
| 33 | public: |
| 34 | YUVtoRGBEffect() { |
| 35 | this->setBGColor(0xFFFFFFFF); |
| 36 | } |
| 37 | |
| 38 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | SkString onShortName() override { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 40 | return SkString("yuv_to_rgb_effect"); |
| 41 | } |
| 42 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | SkISize onISize() override { |
rileya | 1340039 | 2015-07-20 15:00:03 -0700 | [diff] [blame] | 44 | return SkISize::Make(238, 120); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 45 | } |
| 46 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | void onOnceBeforeDraw() override { |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 48 | SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE); |
| 49 | fBmp[0].allocPixels(yinfo); |
| 50 | SkImageInfo uinfo = SkImageInfo::MakeA8(USIZE, USIZE); |
| 51 | fBmp[1].allocPixels(uinfo); |
| 52 | SkImageInfo vinfo = SkImageInfo::MakeA8(VSIZE, VSIZE); |
| 53 | fBmp[2].allocPixels(vinfo); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 54 | unsigned char* pixels[3]; |
| 55 | for (int i = 0; i < 3; ++i) { |
| 56 | pixels[i] = (unsigned char*)fBmp[i].getPixels(); |
| 57 | } |
| 58 | int color[] = {0, 85, 170}; |
| 59 | const int limit[] = {255, 0, 255}; |
| 60 | const int invl[] = {0, 255, 0}; |
| 61 | const int inc[] = {1, -1, 1}; |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 62 | for (int i = 0; i < 3; ++i) { |
| 63 | const size_t nbBytes = fBmp[i].rowBytes() * fBmp[i].height(); |
| 64 | for (size_t j = 0; j < nbBytes; ++j) { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 65 | pixels[i][j] = (unsigned char)color[i]; |
| 66 | color[i] = (color[i] == limit[i]) ? invl[i] : color[i] + inc[i]; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 71 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 72 | GrRenderTargetContext* renderTargetContext = |
| 73 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 74 | if (!renderTargetContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 75 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 76 | return; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 77 | } |
| 78 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 79 | GrContext* context = canvas->getGrContext(); |
| 80 | if (!context) { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 84 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 85 | sk_sp<GrTextureProxy> proxy[3]; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 86 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 87 | for (int i = 0; i < 3; ++i) { |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 88 | GrSurfaceDesc desc; |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 89 | desc.fWidth = fBmp[i].width(); |
| 90 | desc.fHeight = fBmp[i].height(); |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 91 | desc.fConfig = SkColorType2GrPixelConfig(fBmp[i].colorType()); |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 92 | SkASSERT(kUnknown_GrPixelConfig != desc.fConfig); |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 93 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 94 | proxy[i] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, |
| 95 | fBmp[i].getPixels(), fBmp[i].rowBytes()); |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 96 | if (!proxy[i]) { |
| 97 | return; |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 98 | } |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 99 | } |
| 100 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 101 | constexpr SkScalar kDrawPad = 10.f; |
| 102 | constexpr SkScalar kTestPad = 10.f; |
| 103 | constexpr SkScalar kColorSpaceOffset = 36.f; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 104 | |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 105 | for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 106 | SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), |
| 107 | SkIntToScalar(fBmp[0].height())); |
| 108 | renderRect.outset(kDrawPad, kDrawPad); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 109 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 110 | SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset; |
| 111 | SkScalar x = kDrawPad + kTestPad; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 112 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 113 | const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, |
| 114 | {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 115 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 116 | for (int i = 0; i < 6; ++i) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 117 | std::unique_ptr<GrFragmentProcessor> fp( |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 118 | GrYUVtoRGBEffect::Make(proxy[indices[i][0]], |
| 119 | proxy[indices[i][1]], |
| 120 | proxy[indices[i][2]], |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 121 | static_cast<SkYUVColorSpace>(space), |
| 122 | false)); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 123 | if (fp) { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 124 | GrPaint grPaint; |
| 125 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
| 126 | grPaint.addColorFragmentProcessor(std::move(fp)); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 127 | SkMatrix viewMatrix; |
| 128 | viewMatrix.setTranslate(x, y); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 129 | renderTargetContext->priv().testingOnly_addDrawOp( |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 130 | GrRectOpFactory::MakeNonAAFill(context, std::move(grPaint), viewMatrix, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 131 | renderRect, GrAAType::kNone)); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 132 | } |
| 133 | x += renderRect.width() + kTestPad; |
| 134 | } |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 135 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 136 | } |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 137 | |
| 138 | private: |
| 139 | SkBitmap fBmp[3]; |
| 140 | |
| 141 | typedef GM INHERITED; |
| 142 | }; |
| 143 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 144 | DEF_GM(return new YUVtoRGBEffect;) |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 145 | |
| 146 | ////////////////////////////////////////////////////////////////////////////// |
| 147 | |
| 148 | class YUVNV12toRGBEffect : public GM { |
| 149 | public: |
| 150 | YUVNV12toRGBEffect() { |
| 151 | this->setBGColor(0xFFFFFFFF); |
| 152 | } |
| 153 | |
| 154 | protected: |
| 155 | SkString onShortName() override { |
| 156 | return SkString("yuv_nv12_to_rgb_effect"); |
| 157 | } |
| 158 | |
| 159 | SkISize onISize() override { |
| 160 | return SkISize::Make(48, 120); |
| 161 | } |
| 162 | |
| 163 | void onOnceBeforeDraw() override { |
| 164 | SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE); |
| 165 | fBmp[0].allocPixels(yinfo); |
| 166 | SkImageInfo uvinfo = SkImageInfo::MakeN32Premul(USIZE, USIZE); |
| 167 | fBmp[1].allocPixels(uvinfo); |
| 168 | int color[] = {0, 85, 170}; |
| 169 | const int limit[] = {255, 0, 255}; |
| 170 | const int invl[] = {0, 255, 0}; |
| 171 | const int inc[] = {1, -1, 1}; |
| 172 | |
| 173 | { |
| 174 | unsigned char* pixels = (unsigned char*)fBmp[0].getPixels(); |
| 175 | const size_t nbBytes = fBmp[0].rowBytes() * fBmp[0].height(); |
| 176 | for (size_t j = 0; j < nbBytes; ++j) { |
| 177 | pixels[j] = (unsigned char)color[0]; |
| 178 | color[0] = (color[0] == limit[0]) ? invl[0] : color[0] + inc[0]; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | { |
| 183 | for (int y = 0; y < fBmp[1].height(); ++y) { |
| 184 | uint32_t* pixels = fBmp[1].getAddr32(0, y); |
| 185 | for (int j = 0; j < fBmp[1].width(); ++j) { |
| 186 | pixels[j] = SkColorSetARGB(0, color[1], color[2], 0); |
| 187 | color[1] = (color[1] == limit[1]) ? invl[1] : color[1] + inc[1]; |
| 188 | color[2] = (color[2] == limit[2]) ? invl[2] : color[2] + inc[2]; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 195 | GrRenderTargetContext* renderTargetContext = |
| 196 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 197 | if (!renderTargetContext) { |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 198 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | GrContext* context = canvas->getGrContext(); |
| 203 | if (!context) { |
| 204 | return; |
| 205 | } |
| 206 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 207 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 208 | sk_sp<GrTextureProxy> proxy[3]; |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 209 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 210 | for (int i = 0; i < 3; ++i) { |
| 211 | int index = (0 == i) ? 0 : 1; |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 212 | GrSurfaceDesc desc; |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 213 | desc.fWidth = fBmp[index].width(); |
| 214 | desc.fHeight = fBmp[index].height(); |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 215 | desc.fConfig = SkColorType2GrPixelConfig(fBmp[index].colorType()); |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 216 | SkASSERT(kUnknown_GrPixelConfig != desc.fConfig); |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 217 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 218 | proxy[i] = proxyProvider->createTextureProxy( |
| 219 | desc, SkBudgeted::kYes, fBmp[index].getPixels(), fBmp[index].rowBytes()); |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 220 | if (!proxy[i]) { |
| 221 | return; |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 222 | } |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 223 | } |
| 224 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 225 | constexpr SkScalar kDrawPad = 10.f; |
| 226 | constexpr SkScalar kTestPad = 10.f; |
| 227 | constexpr SkScalar kColorSpaceOffset = 36.f; |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 228 | |
| 229 | for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) { |
| 230 | SkRect renderRect = |
| 231 | SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), SkIntToScalar(fBmp[0].height())); |
| 232 | renderRect.outset(kDrawPad, kDrawPad); |
| 233 | |
| 234 | SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset; |
| 235 | SkScalar x = kDrawPad + kTestPad; |
| 236 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 237 | GrPaint grPaint; |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 238 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
Weiliang Chen | 3e95e57 | 2018-05-30 15:15:23 -0400 | [diff] [blame] | 239 | auto fp = GrYUVtoRGBEffect::Make(proxy[0], proxy[1], proxy[2], |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 240 | static_cast<SkYUVColorSpace>(space), true); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 241 | if (fp) { |
| 242 | SkMatrix viewMatrix; |
| 243 | viewMatrix.setTranslate(x, y); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 244 | grPaint.addColorFragmentProcessor(std::move(fp)); |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 245 | std::unique_ptr<GrDrawOp> op(GrRectOpFactory::MakeNonAAFill( |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 246 | context, std::move(grPaint), viewMatrix, renderRect, GrAAType::kNone)); |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 247 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | private: |
| 253 | SkBitmap fBmp[2]; |
| 254 | |
| 255 | typedef GM INHERITED; |
| 256 | }; |
| 257 | |
| 258 | DEF_GM(return new YUVNV12toRGBEffect;) |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 259 | } |