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" |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 15 | #include "GrDrawContextPriv.h" |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 16 | #include "SkBitmap.h" |
| 17 | #include "SkGr.h" |
| 18 | #include "SkGradientShader.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 19 | #include "batches/GrDrawBatch.h" |
| 20 | #include "batches/GrRectBatchFactory.h" |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 21 | #include "effects/GrYUVEffect.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 { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 71 | GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext(); |
| 72 | if (!drawContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 73 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 74 | return; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 75 | } |
| 76 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 77 | GrContext* context = canvas->getGrContext(); |
| 78 | if (!context) { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 82 | SkAutoTUnref<GrTexture> texture[3]; |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 83 | texture[0].reset(GrRefCachedBitmapTexture(context, fBmp[0], |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 84 | GrTextureParams::ClampBilerp(), |
| 85 | SkSourceGammaTreatment::kRespect)); |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 86 | texture[1].reset(GrRefCachedBitmapTexture(context, fBmp[1], |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 87 | GrTextureParams::ClampBilerp(), |
| 88 | SkSourceGammaTreatment::kRespect)); |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 89 | texture[2].reset(GrRefCachedBitmapTexture(context, fBmp[2], |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 90 | GrTextureParams::ClampBilerp(), |
| 91 | SkSourceGammaTreatment::kRespect)); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 92 | |
| 93 | if (!texture[0] || !texture[1] || !texture[2]) { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 97 | constexpr SkScalar kDrawPad = 10.f; |
| 98 | constexpr SkScalar kTestPad = 10.f; |
| 99 | constexpr SkScalar kColorSpaceOffset = 36.f; |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 100 | SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}}; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 101 | |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 102 | for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; |
| 103 | ++space) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 104 | SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), |
| 105 | SkIntToScalar(fBmp[0].height())); |
| 106 | renderRect.outset(kDrawPad, kDrawPad); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 107 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 108 | SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset; |
| 109 | SkScalar x = kDrawPad + kTestPad; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 110 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 111 | const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, |
| 112 | {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 113 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 114 | for (int i = 0; i < 6; ++i) { |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 115 | GrPaint grPaint; |
| 116 | grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode)); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 117 | sk_sp<GrFragmentProcessor> fp(GrYUVEffect::MakeYUVToRGB( |
| 118 | texture[indices[i][0]], texture[indices[i][1]], texture[indices[i][2]], sizes, |
| 119 | static_cast<SkYUVColorSpace>(space), false)); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 120 | if (fp) { |
| 121 | SkMatrix viewMatrix; |
| 122 | viewMatrix.setTranslate(x, y); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 123 | grPaint.addColorFragmentProcessor(std::move(fp)); |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 124 | SkAutoTUnref<GrDrawBatch> batch( |
| 125 | GrRectBatchFactory::CreateNonAAFill(GrColor_WHITE, viewMatrix, |
| 126 | renderRect, nullptr, nullptr)); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 127 | drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 128 | } |
| 129 | x += renderRect.width() + kTestPad; |
| 130 | } |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 131 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 132 | } |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 133 | |
| 134 | private: |
| 135 | SkBitmap fBmp[3]; |
| 136 | |
| 137 | typedef GM INHERITED; |
| 138 | }; |
| 139 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 140 | DEF_GM(return new YUVtoRGBEffect;) |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 141 | |
| 142 | ////////////////////////////////////////////////////////////////////////////// |
| 143 | |
| 144 | class YUVNV12toRGBEffect : public GM { |
| 145 | public: |
| 146 | YUVNV12toRGBEffect() { |
| 147 | this->setBGColor(0xFFFFFFFF); |
| 148 | } |
| 149 | |
| 150 | protected: |
| 151 | SkString onShortName() override { |
| 152 | return SkString("yuv_nv12_to_rgb_effect"); |
| 153 | } |
| 154 | |
| 155 | SkISize onISize() override { |
| 156 | return SkISize::Make(48, 120); |
| 157 | } |
| 158 | |
| 159 | void onOnceBeforeDraw() override { |
| 160 | SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE); |
| 161 | fBmp[0].allocPixels(yinfo); |
| 162 | SkImageInfo uvinfo = SkImageInfo::MakeN32Premul(USIZE, USIZE); |
| 163 | fBmp[1].allocPixels(uvinfo); |
| 164 | int color[] = {0, 85, 170}; |
| 165 | const int limit[] = {255, 0, 255}; |
| 166 | const int invl[] = {0, 255, 0}; |
| 167 | const int inc[] = {1, -1, 1}; |
| 168 | |
| 169 | { |
| 170 | unsigned char* pixels = (unsigned char*)fBmp[0].getPixels(); |
| 171 | const size_t nbBytes = fBmp[0].rowBytes() * fBmp[0].height(); |
| 172 | for (size_t j = 0; j < nbBytes; ++j) { |
| 173 | pixels[j] = (unsigned char)color[0]; |
| 174 | color[0] = (color[0] == limit[0]) ? invl[0] : color[0] + inc[0]; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | { |
| 179 | for (int y = 0; y < fBmp[1].height(); ++y) { |
| 180 | uint32_t* pixels = fBmp[1].getAddr32(0, y); |
| 181 | for (int j = 0; j < fBmp[1].width(); ++j) { |
| 182 | pixels[j] = SkColorSetARGB(0, color[1], color[2], 0); |
| 183 | color[1] = (color[1] == limit[1]) ? invl[1] : color[1] + inc[1]; |
| 184 | color[2] = (color[2] == limit[2]) ? invl[2] : color[2] + inc[2]; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void onDraw(SkCanvas* canvas) override { |
| 191 | GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext(); |
| 192 | if (!drawContext) { |
| 193 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | GrContext* context = canvas->getGrContext(); |
| 198 | if (!context) { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | SkAutoTUnref<GrTexture> texture[3]; |
| 203 | texture[0].reset(GrRefCachedBitmapTexture(context, fBmp[0], GrTextureParams::ClampBilerp(), |
| 204 | SkSourceGammaTreatment::kRespect)); |
| 205 | texture[1].reset(GrRefCachedBitmapTexture(context, fBmp[1], GrTextureParams::ClampBilerp(), |
| 206 | SkSourceGammaTreatment::kRespect)); |
| 207 | texture[2].reset(GrRefCachedBitmapTexture(context, fBmp[1], GrTextureParams::ClampBilerp(), |
| 208 | SkSourceGammaTreatment::kRespect)); |
| 209 | |
| 210 | if (!texture[0] || !texture[1] || !texture[2]) { |
| 211 | return; |
| 212 | } |
| 213 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 214 | constexpr SkScalar kDrawPad = 10.f; |
| 215 | constexpr SkScalar kTestPad = 10.f; |
| 216 | constexpr SkScalar kColorSpaceOffset = 36.f; |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 217 | SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}}; |
| 218 | |
| 219 | for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) { |
| 220 | SkRect renderRect = |
| 221 | SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), SkIntToScalar(fBmp[0].height())); |
| 222 | renderRect.outset(kDrawPad, kDrawPad); |
| 223 | |
| 224 | SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset; |
| 225 | SkScalar x = kDrawPad + kTestPad; |
| 226 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 227 | GrPaint grPaint; |
| 228 | grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode)); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 229 | sk_sp<GrFragmentProcessor> fp( |
| 230 | GrYUVEffect::MakeYUVToRGB(texture[0], texture[1], texture[2], sizes, |
| 231 | static_cast<SkYUVColorSpace>(space), true)); |
| 232 | if (fp) { |
| 233 | SkMatrix viewMatrix; |
| 234 | viewMatrix.setTranslate(x, y); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 235 | grPaint.addColorFragmentProcessor(fp); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 236 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill( |
| 237 | GrColor_WHITE, viewMatrix, renderRect, nullptr, nullptr)); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 238 | drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | private: |
| 244 | SkBitmap fBmp[2]; |
| 245 | |
| 246 | typedef GM INHERITED; |
| 247 | }; |
| 248 | |
| 249 | DEF_GM(return new YUVNV12toRGBEffect;) |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | #endif |