| 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" |
| 16 | #include "GrTest.h" |
| 17 | #include "effects/GrYUVtoRGBEffect.h" |
| 18 | #include "SkBitmap.h" |
| 19 | #include "SkGr.h" |
| 20 | #include "SkGradientShader.h" |
| 21 | |
| 22 | namespace skiagm { |
| 23 | /** |
| 24 | * This GM directly exercises GrYUVtoRGBEffect. |
| 25 | */ |
| 26 | class YUVtoRGBEffect : public GM { |
| 27 | public: |
| 28 | YUVtoRGBEffect() { |
| 29 | this->setBGColor(0xFFFFFFFF); |
| 30 | } |
| 31 | |
| 32 | protected: |
| mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 33 | SkString onShortName() SK_OVERRIDE { |
| sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 34 | return SkString("yuv_to_rgb_effect"); |
| 35 | } |
| 36 | |
| mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 37 | SkISize onISize() SK_OVERRIDE { |
| rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 38 | return SkISize::Make(334, 128); |
| sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 41 | void onOnceBeforeDraw() SK_OVERRIDE { |
| sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 42 | SkImageInfo info = SkImageInfo::MakeA8(24, 24); |
| 43 | fBmp[0].allocPixels(info); |
| 44 | fBmp[1].allocPixels(info); |
| 45 | fBmp[2].allocPixels(info); |
| 46 | unsigned char* pixels[3]; |
| 47 | for (int i = 0; i < 3; ++i) { |
| 48 | pixels[i] = (unsigned char*)fBmp[i].getPixels(); |
| 49 | } |
| 50 | int color[] = {0, 85, 170}; |
| 51 | const int limit[] = {255, 0, 255}; |
| 52 | const int invl[] = {0, 255, 0}; |
| 53 | const int inc[] = {1, -1, 1}; |
| 54 | for (int j = 0; j < 576; ++j) { |
| 55 | for (int i = 0; i < 3; ++i) { |
| 56 | pixels[i][j] = (unsigned char)color[i]; |
| 57 | color[i] = (color[i] == limit[i]) ? invl[i] : color[i] + inc[i]; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 62 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 63 | GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget(); |
| 64 | if (NULL == rt) { |
| 65 | return; |
| 66 | } |
| 67 | GrContext* context = rt->getContext(); |
| 68 | if (NULL == context) { |
| bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame^] | 69 | this->drawGpuOnlyMessage(canvas); |
| sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | |
| 73 | GrTestTarget tt; |
| 74 | context->getTestTarget(&tt); |
| 75 | if (NULL == tt.target()) { |
| 76 | SkDEBUGFAIL("Couldn't get Gr test target."); |
| 77 | return; |
| 78 | } |
| 79 | |
| bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 80 | SkAutoTUnref<GrTexture> texture[3]; |
| 81 | texture[0].reset(GrRefCachedBitmapTexture(context, fBmp[0], NULL)); |
| 82 | texture[1].reset(GrRefCachedBitmapTexture(context, fBmp[1], NULL)); |
| 83 | texture[2].reset(GrRefCachedBitmapTexture(context, fBmp[2], NULL)); |
| 84 | |
| 85 | if (!texture[0] || !texture[1] || !texture[2]) { |
| sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | |
| 89 | static const SkScalar kDrawPad = 10.f; |
| 90 | static const SkScalar kTestPad = 10.f; |
| rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 91 | static const SkScalar kColorSpaceOffset = 64.f; |
| sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 92 | |
| rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 93 | for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; |
| 94 | ++space) { |
| bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 95 | SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), |
| 96 | SkIntToScalar(fBmp[0].height())); |
| 97 | renderRect.outset(kDrawPad, kDrawPad); |
| sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 98 | |
| bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 99 | SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset; |
| 100 | SkScalar x = kDrawPad + kTestPad; |
| sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 101 | |
| bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 102 | const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, |
| 103 | {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}; |
| sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 104 | |
| bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 105 | for (int i = 0; i < 6; ++i) { |
| 106 | SkAutoTUnref<GrFragmentProcessor> fp( |
| 107 | GrYUVtoRGBEffect::Create(texture[indices[i][0]], |
| 108 | texture[indices[i][1]], |
| 109 | texture[indices[i][2]], |
| 110 | static_cast<SkYUVColorSpace>(space))); |
| 111 | if (fp) { |
| 112 | SkMatrix viewMatrix; |
| 113 | viewMatrix.setTranslate(x, y); |
| egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 114 | GrPipelineBuilder pipelineBuilder; |
| 115 | pipelineBuilder.setRenderTarget(rt); |
| 116 | pipelineBuilder.addColorProcessor(fp); |
| 117 | tt.target()->drawSimpleRect(&pipelineBuilder, GrColor_WHITE, viewMatrix, |
| 118 | renderRect); |
| bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 119 | } |
| 120 | x += renderRect.width() + kTestPad; |
| 121 | } |
| sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 122 | } |
| bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 123 | } |
| sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 124 | |
| 125 | private: |
| 126 | SkBitmap fBmp[3]; |
| 127 | |
| 128 | typedef GM INHERITED; |
| 129 | }; |
| 130 | |
| 131 | DEF_GM( return SkNEW(YUVtoRGBEffect); ) |
| 132 | } |
| 133 | |
| 134 | #endif |