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