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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkBitmap.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkBlendMode.h" |
| 13 | #include "include/core/SkColor.h" |
| 14 | #include "include/core/SkImage.h" |
| 15 | #include "include/core/SkImageInfo.h" |
| 16 | #include "include/core/SkMatrix.h" |
| 17 | #include "include/core/SkRect.h" |
| 18 | #include "include/core/SkRefCnt.h" |
| 19 | #include "include/core/SkScalar.h" |
| 20 | #include "include/core/SkSize.h" |
| 21 | #include "include/core/SkString.h" |
| 22 | #include "include/core/SkTypes.h" |
| 23 | #include "include/core/SkYUVAIndex.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "include/gpu/GrContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 25 | #include "include/gpu/GrSamplerState.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "include/private/GrTextureProxy.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 27 | #include "include/private/GrTypesPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/gpu/GrClip.h" |
| 29 | #include "src/gpu/GrContextPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 30 | #include "src/gpu/GrFragmentProcessor.h" |
| 31 | #include "src/gpu/GrPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 32 | #include "src/gpu/GrProxyProvider.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 33 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 34 | #include "src/gpu/GrRenderTargetContextPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 35 | #include "src/gpu/effects/GrPorterDuffXferProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 36 | #include "src/gpu/effects/GrYUVtoRGBEffect.h" |
| 37 | #include "src/gpu/ops/GrDrawOp.h" |
| 38 | #include "src/gpu/ops/GrFillRectOp.h" |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 39 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 40 | #include <memory> |
| 41 | #include <utility> |
| 42 | |
| 43 | class SkCanvas; |
| 44 | |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 45 | #define YSIZE 8 |
| 46 | #define USIZE 4 |
| 47 | #define VSIZE 4 |
| 48 | |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 49 | namespace skiagm { |
| 50 | /** |
| 51 | * This GM directly exercises GrYUVtoRGBEffect. |
| 52 | */ |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 53 | class YUVtoRGBEffect : public GpuGM { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 54 | public: |
| 55 | YUVtoRGBEffect() { |
| 56 | this->setBGColor(0xFFFFFFFF); |
| 57 | } |
| 58 | |
| 59 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 60 | SkString onShortName() override { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 61 | return SkString("yuv_to_rgb_effect"); |
| 62 | } |
| 63 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 64 | SkISize onISize() override { |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 65 | int numRows = kLastEnum_SkYUVColorSpace + 1; |
| 66 | return SkISize::Make(238, kDrawPad + numRows * kColorSpaceOffset); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 67 | } |
| 68 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 69 | void onOnceBeforeDraw() override { |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 70 | SkBitmap bmp[3]; |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 71 | SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE); |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 72 | bmp[0].allocPixels(yinfo); |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 73 | SkImageInfo uinfo = SkImageInfo::MakeA8(USIZE, USIZE); |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 74 | bmp[1].allocPixels(uinfo); |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 75 | SkImageInfo vinfo = SkImageInfo::MakeA8(VSIZE, VSIZE); |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 76 | bmp[2].allocPixels(vinfo); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 77 | unsigned char* pixels[3]; |
| 78 | for (int i = 0; i < 3; ++i) { |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 79 | pixels[i] = (unsigned char*)bmp[i].getPixels(); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 80 | } |
| 81 | int color[] = {0, 85, 170}; |
| 82 | const int limit[] = {255, 0, 255}; |
| 83 | const int invl[] = {0, 255, 0}; |
| 84 | const int inc[] = {1, -1, 1}; |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 85 | for (int i = 0; i < 3; ++i) { |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 86 | const size_t nbBytes = bmp[i].rowBytes() * bmp[i].height(); |
sugoi | 4ccce7e | 2015-02-13 13:57:09 -0800 | [diff] [blame] | 87 | for (size_t j = 0; j < nbBytes; ++j) { |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 88 | pixels[i][j] = (unsigned char)color[i]; |
| 89 | color[i] = (color[i] == limit[i]) ? invl[i] : color[i] + inc[i]; |
| 90 | } |
| 91 | } |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 92 | for (int i = 0; i < 3; ++i) { |
| 93 | fImage[i] = SkImage::MakeFromBitmap(bmp[i]); |
| 94 | } |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 97 | DrawResult onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext, |
| 98 | SkCanvas* canvas, SkString* errorMsg) override { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 99 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 100 | sk_sp<GrTextureProxy> proxies[3]; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 101 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 102 | for (int i = 0; i < 3; ++i) { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 103 | proxies[i] = proxyProvider->createTextureProxy(fImage[i], kNone_GrSurfaceFlags, 1, |
| 104 | SkBudgeted::kYes, SkBackingFit::kExact); |
| 105 | if (!proxies[i]) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 106 | *errorMsg = "Failed to create proxy"; |
| 107 | return DrawResult::kFail; |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 108 | } |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 111 | for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) { |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 112 | SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fImage[0]->width()), |
| 113 | SkIntToScalar(fImage[0]->height())); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 114 | renderRect.outset(kDrawPad, kDrawPad); |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 115 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 116 | SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset; |
| 117 | SkScalar x = kDrawPad + kTestPad; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 118 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 119 | const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, |
| 120 | {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 121 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 122 | for (int i = 0; i < 6; ++i) { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 123 | SkYUVAIndex yuvaIndices[4] = { |
Robert Phillips | 6ba8c83 | 2018-10-10 11:43:01 -0400 | [diff] [blame] | 124 | { indices[i][0], SkColorChannel::kR }, |
| 125 | { indices[i][1], SkColorChannel::kR }, |
| 126 | { indices[i][2], SkColorChannel::kR }, |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 127 | { -1, SkColorChannel::kA } |
| 128 | }; |
| 129 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 130 | std::unique_ptr<GrFragmentProcessor> fp( |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 131 | GrYUVtoRGBEffect::Make(proxies, yuvaIndices, |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 132 | static_cast<SkYUVColorSpace>(space), |
| 133 | GrSamplerState::Filter::kNearest)); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 134 | if (fp) { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 135 | GrPaint grPaint; |
| 136 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
| 137 | grPaint.addColorFragmentProcessor(std::move(fp)); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 138 | SkMatrix viewMatrix; |
| 139 | viewMatrix.setTranslate(x, y); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 140 | renderTargetContext->priv().testingOnly_addDrawOp( |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 141 | GrFillRectOp::Make(context, std::move(grPaint), GrAAType::kNone, |
| 142 | viewMatrix, renderRect)); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 143 | } |
| 144 | x += renderRect.width() + kTestPad; |
| 145 | } |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 146 | } |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 147 | return DrawResult::kOk; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 148 | } |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 149 | |
| 150 | private: |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 151 | sk_sp<SkImage> fImage[3]; |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 152 | |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 153 | static constexpr SkScalar kDrawPad = 10.f; |
| 154 | static constexpr SkScalar kTestPad = 10.f; |
| 155 | static constexpr SkScalar kColorSpaceOffset = 36.f; |
| 156 | |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 157 | typedef GM INHERITED; |
| 158 | }; |
| 159 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 160 | DEF_GM(return new YUVtoRGBEffect;) |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 161 | |
| 162 | ////////////////////////////////////////////////////////////////////////////// |
| 163 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 164 | class YUVNV12toRGBEffect : public GpuGM { |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 165 | public: |
| 166 | YUVNV12toRGBEffect() { |
| 167 | this->setBGColor(0xFFFFFFFF); |
| 168 | } |
| 169 | |
| 170 | protected: |
| 171 | SkString onShortName() override { |
| 172 | return SkString("yuv_nv12_to_rgb_effect"); |
| 173 | } |
| 174 | |
| 175 | SkISize onISize() override { |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 176 | int numRows = kLastEnum_SkYUVColorSpace + 1; |
| 177 | return SkISize::Make(48, kDrawPad + numRows * kColorSpaceOffset); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void onOnceBeforeDraw() override { |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 181 | SkBitmap bmp[2]; |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 182 | SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE); |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 183 | bmp[0].allocPixels(yinfo); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 184 | SkImageInfo uvinfo = SkImageInfo::MakeN32Premul(USIZE, USIZE); |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 185 | bmp[1].allocPixels(uvinfo); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 186 | int color[] = {0, 85, 170}; |
| 187 | const int limit[] = {255, 0, 255}; |
| 188 | const int invl[] = {0, 255, 0}; |
| 189 | const int inc[] = {1, -1, 1}; |
| 190 | |
| 191 | { |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 192 | unsigned char* pixels = (unsigned char*)bmp[0].getPixels(); |
| 193 | const size_t nbBytes = bmp[0].rowBytes() * bmp[0].height(); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 194 | for (size_t j = 0; j < nbBytes; ++j) { |
| 195 | pixels[j] = (unsigned char)color[0]; |
| 196 | color[0] = (color[0] == limit[0]) ? invl[0] : color[0] + inc[0]; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | { |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 201 | for (int y = 0; y < bmp[1].height(); ++y) { |
| 202 | uint32_t* pixels = bmp[1].getAddr32(0, y); |
| 203 | for (int j = 0; j < bmp[1].width(); ++j) { |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 204 | pixels[j] = SkColorSetARGB(0, color[1], color[2], 0); |
| 205 | color[1] = (color[1] == limit[1]) ? invl[1] : color[1] + inc[1]; |
| 206 | color[2] = (color[2] == limit[2]) ? invl[2] : color[2] + inc[2]; |
| 207 | } |
| 208 | } |
| 209 | } |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 210 | |
| 211 | for (int i = 0; i < 2; ++i) { |
| 212 | fImage[i] = SkImage::MakeFromBitmap(bmp[i]); |
| 213 | } |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 216 | DrawResult onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext, |
| 217 | SkCanvas* canvas, SkString* errorMsg) override { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 218 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 219 | sk_sp<GrTextureProxy> proxies[2]; |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 220 | |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 221 | for (int i = 0; i < 2; ++i) { |
| 222 | proxies[i] = proxyProvider->createTextureProxy(fImage[i], kNone_GrSurfaceFlags, 1, |
| 223 | SkBudgeted::kYes, SkBackingFit::kExact); |
| 224 | if (!proxies[i]) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 225 | *errorMsg = "Failed to create proxy"; |
| 226 | return DrawResult::kFail; |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 227 | } |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 230 | SkYUVAIndex yuvaIndices[4] = { |
Robert Phillips | 6ba8c83 | 2018-10-10 11:43:01 -0400 | [diff] [blame] | 231 | { 0, SkColorChannel::kR }, |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 232 | { 1, SkColorChannel::kR }, |
| 233 | { 1, SkColorChannel::kG }, |
| 234 | { -1, SkColorChannel::kA } |
| 235 | }; |
| 236 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 237 | for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) { |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 238 | SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fImage[0]->width()), |
| 239 | SkIntToScalar(fImage[0]->height())); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 240 | renderRect.outset(kDrawPad, kDrawPad); |
| 241 | |
| 242 | SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset; |
| 243 | SkScalar x = kDrawPad + kTestPad; |
| 244 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 245 | GrPaint grPaint; |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 246 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 247 | auto fp = GrYUVtoRGBEffect::Make(proxies, yuvaIndices, |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 248 | static_cast<SkYUVColorSpace>(space), |
| 249 | GrSamplerState::Filter::kNearest); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 250 | if (fp) { |
| 251 | SkMatrix viewMatrix; |
| 252 | viewMatrix.setTranslate(x, y); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 253 | grPaint.addColorFragmentProcessor(std::move(fp)); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 254 | std::unique_ptr<GrDrawOp> op(GrFillRectOp::Make(context, std::move(grPaint), |
| 255 | GrAAType::kNone, viewMatrix, renderRect)); |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 256 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 259 | return DrawResult::kOk; |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | private: |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 263 | sk_sp<SkImage> fImage[2]; |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 264 | |
Robert Phillips | 0a22ba8 | 2019-03-06 12:36:47 -0500 | [diff] [blame] | 265 | static constexpr SkScalar kDrawPad = 10.f; |
| 266 | static constexpr SkScalar kTestPad = 10.f; |
| 267 | static constexpr SkScalar kColorSpaceOffset = 36.f; |
| 268 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 269 | typedef GM INHERITED; |
| 270 | }; |
| 271 | |
| 272 | DEF_GM(return new YUVNV12toRGBEffect;) |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame] | 273 | |
| 274 | ////////////////////////////////////////////////////////////////////////////// |
| 275 | |
| 276 | // This GM tests domain clamping on YUV multiplanar images where the U and V |
| 277 | // planes have different resolution from Y. See skbug:8959 |
| 278 | |
| 279 | class YUVtoRGBDomainEffect : public GpuGM { |
| 280 | public: |
| 281 | YUVtoRGBDomainEffect() { |
| 282 | this->setBGColor(0xFFFFFFFF); |
| 283 | } |
| 284 | |
| 285 | protected: |
| 286 | SkString onShortName() override { |
| 287 | return SkString("yuv_to_rgb_domain_effect"); |
| 288 | } |
| 289 | |
| 290 | SkISize onISize() override { |
| 291 | return SkISize::Make((YSIZE + kTestPad) * 3 + kDrawPad, (YSIZE + kTestPad) * 2 + kDrawPad); |
| 292 | } |
| 293 | |
| 294 | void onOnceBeforeDraw() override { |
| 295 | SkBitmap bmp[3]; |
| 296 | SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE); |
| 297 | bmp[0].allocPixels(yinfo); |
| 298 | SkImageInfo uinfo = SkImageInfo::MakeA8(USIZE, USIZE); |
| 299 | bmp[1].allocPixels(uinfo); |
| 300 | SkImageInfo vinfo = SkImageInfo::MakeA8(VSIZE, VSIZE); |
| 301 | bmp[2].allocPixels(vinfo); |
| 302 | |
| 303 | int innerColor[] = {149, 43, 21}; |
| 304 | int outerColor[] = {128, 128, 128}; |
| 305 | for (int i = 0; i < 3; ++i) { |
| 306 | bmp[i].eraseColor(SkColorSetARGB(outerColor[i], 0, 0, 0)); |
| 307 | SkIRect innerRect = i == 0 ? SkIRect::MakeLTRB(2, 2, 6, 6) : SkIRect::MakeLTRB(1, 1, 3, 3); |
| 308 | bmp[i].erase(SkColorSetARGB(innerColor[i], 0, 0, 0), innerRect); |
| 309 | fImage[i] = SkImage::MakeFromBitmap(bmp[i]); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | DrawResult onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext, |
| 314 | SkCanvas* canvas, SkString* errorMsg) override { |
| 315 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 316 | sk_sp<GrTextureProxy> proxies[3]; |
| 317 | |
| 318 | for (int i = 0; i < 3; ++i) { |
| 319 | proxies[i] = proxyProvider->createTextureProxy(fImage[i], kNone_GrSurfaceFlags, 1, |
| 320 | SkBudgeted::kYes, SkBackingFit::kExact); |
| 321 | if (!proxies[i]) { |
| 322 | *errorMsg = "Failed to create proxy"; |
| 323 | return DrawResult::kFail; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | // Draw a 2x2 grid of the YUV images. |
| 328 | // Rows = kNearest, kBilerp, Cols = No clamp, clamp |
| 329 | static const GrSamplerState::Filter kFilters[] = { |
| 330 | GrSamplerState::Filter::kNearest, GrSamplerState::Filter::kBilerp }; |
| 331 | static const SkRect kGreenRect = SkRect::MakeLTRB(2.f, 2.f, 6.f, 6.f); |
| 332 | |
| 333 | SkYUVAIndex yuvaIndices[4] = { |
| 334 | { SkYUVAIndex::kY_Index, SkColorChannel::kR }, |
| 335 | { SkYUVAIndex::kU_Index, SkColorChannel::kR }, |
| 336 | { SkYUVAIndex::kV_Index, SkColorChannel::kR }, |
| 337 | { -1, SkColorChannel::kA } |
| 338 | }; |
| 339 | SkRect rect = SkRect::MakeWH(YSIZE, YSIZE); |
| 340 | |
| 341 | SkScalar y = kDrawPad + kTestPad; |
| 342 | for (uint32_t i = 0; i < SK_ARRAY_COUNT(kFilters); ++i) { |
| 343 | SkScalar x = kDrawPad + kTestPad; |
| 344 | |
| 345 | for (uint32_t j = 0; j < 2; ++j) { |
| 346 | SkMatrix ctm = SkMatrix::MakeTrans(x, y); |
| 347 | ctm.postScale(10.f, 10.f); |
| 348 | |
| 349 | SkRect domain = kGreenRect; |
| 350 | if (kFilters[i] == GrSamplerState::Filter::kNearest) { |
| 351 | // Make a very small inset for nearest-neighbor filtering so that 0.5px |
| 352 | // centers don't round out beyond the green pixels. |
| 353 | domain.inset(0.01f, 0.01f); |
| 354 | } |
| 355 | |
| 356 | const SkRect* domainPtr = j > 0 ? &domain : nullptr; |
| 357 | std::unique_ptr<GrFragmentProcessor> fp(GrYUVtoRGBEffect::Make(proxies, yuvaIndices, |
| 358 | kJPEG_SkYUVColorSpace, kFilters[i], SkMatrix::I(), domainPtr)); |
| 359 | if (fp) { |
| 360 | GrPaint grPaint; |
| 361 | grPaint.addColorFragmentProcessor(std::move(fp)); |
| 362 | renderTargetContext->drawRect( |
| 363 | GrNoClip(), std::move(grPaint), GrAA::kYes, ctm, rect); |
| 364 | } |
| 365 | x += rect.width() + kTestPad; |
| 366 | } |
| 367 | |
| 368 | y += rect.height() + kTestPad; |
| 369 | } |
| 370 | |
| 371 | return DrawResult::kOk; |
| 372 | } |
| 373 | |
| 374 | private: |
| 375 | sk_sp<SkImage> fImage[3]; |
| 376 | |
| 377 | static constexpr SkScalar kDrawPad = 10.f; |
| 378 | static constexpr SkScalar kTestPad = 10.f; |
| 379 | |
| 380 | typedef GM INHERITED; |
| 381 | }; |
| 382 | |
| 383 | DEF_GM(return new YUVtoRGBDomainEffect;) |
sugoi | 24dcac2 | 2014-07-07 15:09:48 -0700 | [diff] [blame] | 384 | } |