blob: 28808685a1a27c974ef81e175f199e88b87a7eda [file] [log] [blame]
Brian Salomon8f46ecc2020-11-17 13:28:45 -05001/*
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
10#include "gm/gm.h"
11#include "include/core/SkBitmap.h"
12#include "include/core/SkColor.h"
13#include "include/core/SkImageInfo.h"
14#include "include/core/SkMatrix.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkYUVAIndex.h"
20#include "include/private/GrTypesPriv.h"
21#include "src/gpu/GrBitmapTextureMaker.h"
22#include "src/gpu/GrDirectContextPriv.h"
23#include "src/gpu/GrPaint.h"
24#include "src/gpu/GrRenderTargetContext.h"
Brian Salomon8f46ecc2020-11-17 13:28:45 -050025#include "src/gpu/GrSamplerState.h"
26#include "src/gpu/GrTextureProxy.h"
27#include "src/gpu/effects/GrYUVtoRGBEffect.h"
28
29#include <memory>
30#include <utility>
31
32class SkCanvas;
33
34#define YSIZE 8
35#define USIZE 4
36#define VSIZE 4
37
38namespace skiagm {
39
40//////////////////////////////////////////////////////////////////////////////
41
42// This GM tests subsetting YUV multiplanar images where the U and V
43// planes have different resolution from Y. See skbug:8959
44
45class YUVtoRGBSubsetEffect : public GpuGM {
46public:
47 YUVtoRGBSubsetEffect() {
48 this->setBGColor(0xFFFFFFFF);
49 }
50
51protected:
52 SkString onShortName() override {
53 return SkString("yuv_to_rgb_subset_effect");
54 }
55
56 SkISize onISize() override { return {1310, 540}; }
57
58 void onOnceBeforeDraw() override {
59 SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
60 fBitmaps[0].allocPixels(yinfo);
61 SkImageInfo uinfo = SkImageInfo::MakeA8(USIZE, USIZE);
62 fBitmaps[1].allocPixels(uinfo);
63 SkImageInfo vinfo = SkImageInfo::MakeA8(VSIZE, VSIZE);
64 fBitmaps[2].allocPixels(vinfo);
65
66 unsigned char innerY[16] = {149, 160, 130, 105,
67 160, 130, 105, 149,
68 130, 105, 149, 160,
69 105, 149, 160, 130};
70 unsigned char innerU[4] = {43, 75, 145, 200};
71 unsigned char innerV[4] = {88, 180, 200, 43};
72 int outerYUV[] = {128, 128, 128};
73 for (int i = 0; i < 3; ++i) {
74 fBitmaps[i].eraseColor(SkColorSetARGB(outerYUV[i], 0, 0, 0));
75 }
76 SkPixmap innerYPM(SkImageInfo::MakeA8(4, 4), innerY, 4);
77 SkPixmap innerUPM(SkImageInfo::MakeA8(2, 2), innerU, 2);
78 SkPixmap innerVPM(SkImageInfo::MakeA8(2, 2), innerV, 2);
79 fBitmaps[0].writePixels(innerYPM, 2, 2);
80 fBitmaps[1].writePixels(innerUPM, 1, 1);
81 fBitmaps[2].writePixels(innerVPM, 1, 1);
82 for (auto& fBitmap : fBitmaps) {
83 fBitmap.setImmutable();
84 }
85 }
86
87 DrawResult onDraw(GrRecordingContext* context, GrRenderTargetContext* renderTargetContext,
88 SkCanvas* canvas, SkString* errorMsg) override {
89 GrSurfaceProxyView views[3];
90
91 for (int i = 0; i < 3; ++i) {
92 GrBitmapTextureMaker maker(context, fBitmaps[i], GrImageTexGenPolicy::kDraw);
93 views[i] = maker.view(GrMipmapped::kNo);
94 if (!views[i]) {
95 *errorMsg = "Failed to create proxy";
96 return DrawResult::kFail;
97 }
98 }
99
100 static const GrSamplerState::Filter kFilters[] = {GrSamplerState::Filter::kNearest,
101 GrSamplerState::Filter::kLinear};
102 static const SkRect kColorRect = SkRect::MakeLTRB(2.f, 2.f, 6.f, 6.f);
103
104 SkYUVAIndex yuvaIndices[4] = {
105 { SkYUVAIndex::kY_Index, SkColorChannel::kR },
106 { SkYUVAIndex::kU_Index, SkColorChannel::kR },
107 { SkYUVAIndex::kV_Index, SkColorChannel::kR },
108 { -1, SkColorChannel::kA }
109 };
110 // Outset to visualize wrap modes.
111 SkRect rect = SkRect::MakeWH(YSIZE, YSIZE).makeOutset(YSIZE/2, YSIZE/2);
112
113 SkScalar y = kTestPad;
114 // Rows are filter modes.
115 for (uint32_t i = 0; i < SK_ARRAY_COUNT(kFilters); ++i) {
116 SkScalar x = kTestPad;
117 // Columns are non-subsetted followed by subsetted with each WrapMode in a row
118 for (uint32_t j = 0; j < GrSamplerState::kWrapModeCount + 1; ++j) {
119 SkMatrix ctm = SkMatrix::Translate(x, y);
120 ctm.postScale(10.f, 10.f);
121
122 const SkRect* subset = j > 0 ? &kColorRect : nullptr;
123
124 GrSamplerState samplerState;
125 samplerState.setFilterMode(kFilters[i]);
126 if (j > 0) {
127 auto wm = static_cast<GrSamplerState::WrapMode>(j - 1);
128 samplerState.setWrapModeX(wm);
129 samplerState.setWrapModeY(wm);
130 }
131 const auto& caps = *context->priv().caps();
132 std::unique_ptr<GrFragmentProcessor> fp(
133 GrYUVtoRGBEffect::Make(views, yuvaIndices, kJPEG_SkYUVColorSpace,
134 samplerState, caps, SkMatrix::I(), subset));
135 if (fp) {
136 GrPaint grPaint;
137 grPaint.setColorFragmentProcessor(std::move(fp));
138 renderTargetContext->drawRect(
139 nullptr, std::move(grPaint), GrAA::kYes, ctm, rect);
140 }
141 x += rect.width() + kTestPad;
142 }
143
144 y += rect.height() + kTestPad;
145 }
146
147 return DrawResult::kOk;
148 }
149
150private:
151 SkBitmap fBitmaps[3];
152
153 static constexpr SkScalar kTestPad = 10.f;
154
155 using INHERITED = GM;
156};
157
158DEF_GM(return new YUVtoRGBSubsetEffect;)
159} // namespace skiagm