sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | // This test only works with the GPU backend. |
| 10 | |
| 11 | #include "gm.h" |
| 12 | |
| 13 | #if SK_SUPPORT_GPU |
| 14 | |
| 15 | #include "GrContext.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 16 | #include "GrDrawContext.h" |
| 17 | #include "GrPipelineBuilder.h" |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 18 | #include "SkBitmap.h" |
| 19 | #include "SkGr.h" |
| 20 | #include "SkGradientShader.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 21 | #include "batches/GrDrawBatch.h" |
| 22 | #include "batches/GrRectBatchFactory.h" |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 23 | #include "effects/GrYUVEffect.h" |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 24 | |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 25 | #define YSIZE 8 |
| 26 | #define USIZE 4 |
| 27 | #define VSIZE 4 |
| 28 | |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 29 | namespace skiagm { |
| 30 | /** |
| 31 | * This GM directly exercises GrYUVtoRGBEffect. |
| 32 | */ |
| 33 | class YUVtoRGBEffect : public GM { |
| 34 | public: |
| 35 | YUVtoRGBEffect() { |
| 36 | this->setBGColor(0xFFFFFFFF); |
| 37 | } |
| 38 | |
| 39 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 40 | SkString onShortName() override { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 41 | return SkString("yuv_to_rgb_effect"); |
| 42 | } |
| 43 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | SkISize onISize() override { |
rileya | 1340039 | 2015-07-20 15:00:03 -0700 | [diff] [blame] | 45 | return SkISize::Make(238, 120); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 46 | } |
| 47 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 48 | void onOnceBeforeDraw() override { |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 49 | SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE); |
| 50 | fBmp[0].allocPixels(yinfo); |
| 51 | SkImageInfo uinfo = SkImageInfo::MakeA8(USIZE, USIZE); |
| 52 | fBmp[1].allocPixels(uinfo); |
| 53 | SkImageInfo vinfo = SkImageInfo::MakeA8(VSIZE, VSIZE); |
| 54 | fBmp[2].allocPixels(vinfo); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 55 | unsigned char* pixels[3]; |
| 56 | for (int i = 0; i < 3; ++i) { |
| 57 | pixels[i] = (unsigned char*)fBmp[i].getPixels(); |
| 58 | } |
| 59 | int color[] = {0, 85, 170}; |
| 60 | const int limit[] = {255, 0, 255}; |
| 61 | const int invl[] = {0, 255, 0}; |
| 62 | const int inc[] = {1, -1, 1}; |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 63 | for (int i = 0; i < 3; ++i) { |
| 64 | const size_t nbBytes = fBmp[i].rowBytes() * fBmp[i].height(); |
| 65 | for (size_t j = 0; j < nbBytes; ++j) { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 66 | pixels[i][j] = (unsigned char)color[i]; |
| 67 | color[i] = (color[i] == limit[i]) ? invl[i] : color[i] + inc[i]; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | void onDraw(SkCanvas* canvas) override { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 73 | GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | if (nullptr == rt) { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | GrContext* context = rt->getContext(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | if (nullptr == context) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 79 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 83 | SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt)); |
| 84 | if (!drawContext) { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 85 | return; |
| 86 | } |
| 87 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 88 | SkAutoTUnref<GrTexture> texture[3]; |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 89 | texture[0].reset(GrRefCachedBitmapTexture(context, fBmp[0], |
| 90 | GrTextureParams::ClampBilerp())); |
| 91 | texture[1].reset(GrRefCachedBitmapTexture(context, fBmp[1], |
| 92 | GrTextureParams::ClampBilerp())); |
| 93 | texture[2].reset(GrRefCachedBitmapTexture(context, fBmp[2], |
| 94 | GrTextureParams::ClampBilerp())); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 95 | |
| 96 | if (!texture[0] || !texture[1] || !texture[2]) { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
| 100 | static const SkScalar kDrawPad = 10.f; |
| 101 | static const SkScalar kTestPad = 10.f; |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 102 | static const SkScalar kColorSpaceOffset = 36.f; |
| 103 | SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}}; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 104 | |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 105 | for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; |
| 106 | ++space) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 107 | SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), |
| 108 | SkIntToScalar(fBmp[0].height())); |
| 109 | renderRect.outset(kDrawPad, kDrawPad); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 110 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 111 | SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset; |
| 112 | SkScalar x = kDrawPad + kTestPad; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 113 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 114 | const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, |
| 115 | {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 116 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 117 | for (int i = 0; i < 6; ++i) { |
joshualitt | 2cdec31 | 2015-07-09 07:31:31 -0700 | [diff] [blame] | 118 | GrPipelineBuilder pipelineBuilder; |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 119 | pipelineBuilder.setXPFactory( |
| 120 | GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref(); |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 121 | SkAutoTUnref<const GrFragmentProcessor> fp( |
| 122 | GrYUVEffect::CreateYUVToRGB(texture[indices[i][0]], |
| 123 | texture[indices[i][1]], |
| 124 | texture[indices[i][2]], |
| 125 | sizes, |
| 126 | static_cast<SkYUVColorSpace>(space))); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 127 | if (fp) { |
| 128 | SkMatrix viewMatrix; |
| 129 | viewMatrix.setTranslate(x, y); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 130 | pipelineBuilder.setRenderTarget(rt); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 131 | pipelineBuilder.addColorFragmentProcessor(fp); |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 132 | SkAutoTUnref<GrDrawBatch> batch( |
| 133 | GrRectBatchFactory::CreateNonAAFill(GrColor_WHITE, viewMatrix, |
| 134 | renderRect, nullptr, nullptr)); |
| 135 | drawContext->internal_drawBatch(pipelineBuilder, batch); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 136 | } |
| 137 | x += renderRect.width() + kTestPad; |
| 138 | } |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 139 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 140 | } |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 141 | |
| 142 | private: |
| 143 | SkBitmap fBmp[3]; |
| 144 | |
| 145 | typedef GM INHERITED; |
| 146 | }; |
| 147 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 148 | DEF_GM(return new YUVtoRGBEffect;) |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | #endif |