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