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: |
| 33 | virtual SkString onShortName() SK_OVERRIDE { |
| 34 | return SkString("yuv_to_rgb_effect"); |
| 35 | } |
| 36 | |
| 37 | virtual 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 | |
| 41 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 42 | // This is a GPU-specific GM. |
| 43 | return kGPUOnly_Flag; |
| 44 | } |
| 45 | |
| 46 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 47 | SkImageInfo info = SkImageInfo::MakeA8(24, 24); |
| 48 | fBmp[0].allocPixels(info); |
| 49 | fBmp[1].allocPixels(info); |
| 50 | fBmp[2].allocPixels(info); |
| 51 | unsigned char* pixels[3]; |
| 52 | for (int i = 0; i < 3; ++i) { |
| 53 | pixels[i] = (unsigned char*)fBmp[i].getPixels(); |
| 54 | } |
| 55 | int color[] = {0, 85, 170}; |
| 56 | const int limit[] = {255, 0, 255}; |
| 57 | const int invl[] = {0, 255, 0}; |
| 58 | const int inc[] = {1, -1, 1}; |
| 59 | for (int j = 0; j < 576; ++j) { |
| 60 | for (int i = 0; i < 3; ++i) { |
| 61 | pixels[i][j] = (unsigned char)color[i]; |
| 62 | color[i] = (color[i] == limit[i]) ? invl[i] : color[i] + inc[i]; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 68 | GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget(); |
| 69 | if (NULL == rt) { |
| 70 | return; |
| 71 | } |
| 72 | GrContext* context = rt->getContext(); |
| 73 | if (NULL == context) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | GrTestTarget tt; |
| 78 | context->getTestTarget(&tt); |
| 79 | if (NULL == tt.target()) { |
| 80 | SkDEBUGFAIL("Couldn't get Gr test target."); |
| 81 | return; |
| 82 | } |
| 83 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 84 | SkAutoTUnref<GrTexture> texture[3]; |
| 85 | texture[0].reset(GrRefCachedBitmapTexture(context, fBmp[0], NULL)); |
| 86 | texture[1].reset(GrRefCachedBitmapTexture(context, fBmp[1], NULL)); |
| 87 | texture[2].reset(GrRefCachedBitmapTexture(context, fBmp[2], NULL)); |
| 88 | |
| 89 | if (!texture[0] || !texture[1] || !texture[2]) { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
| 93 | static const SkScalar kDrawPad = 10.f; |
| 94 | static const SkScalar kTestPad = 10.f; |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 95 | static const SkScalar kColorSpaceOffset = 64.f; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 96 | |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 97 | for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; |
| 98 | ++space) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 99 | SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), |
| 100 | SkIntToScalar(fBmp[0].height())); |
| 101 | renderRect.outset(kDrawPad, kDrawPad); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 102 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 103 | SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset; |
| 104 | SkScalar x = kDrawPad + kTestPad; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 105 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 106 | const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, |
| 107 | {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 108 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 109 | for (int i = 0; i < 6; ++i) { |
| 110 | SkAutoTUnref<GrFragmentProcessor> fp( |
| 111 | GrYUVtoRGBEffect::Create(texture[indices[i][0]], |
| 112 | texture[indices[i][1]], |
| 113 | texture[indices[i][2]], |
| 114 | static_cast<SkYUVColorSpace>(space))); |
| 115 | if (fp) { |
| 116 | SkMatrix viewMatrix; |
| 117 | viewMatrix.setTranslate(x, y); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 118 | GrDrawState drawState(viewMatrix); |
| 119 | drawState.setRenderTarget(rt); |
| 120 | drawState.setColor(0xffffffff); |
| 121 | drawState.addColorProcessor(fp); |
| 122 | tt.target()->drawSimpleRect(&drawState, renderRect); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 123 | } |
| 124 | x += renderRect.width() + kTestPad; |
| 125 | } |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 126 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 127 | } |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 128 | |
| 129 | private: |
| 130 | SkBitmap fBmp[3]; |
| 131 | |
| 132 | typedef GM INHERITED; |
| 133 | }; |
| 134 | |
| 135 | DEF_GM( return SkNEW(YUVtoRGBEffect); ) |
| 136 | } |
| 137 | |
| 138 | #endif |