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