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