blob: 433aa1e0bacaed24e6e0e3c52578be2e12960f83 [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"
Brian Salomon0857bef2021-01-13 15:54:04 -050019#include "include/core/SkYUVAInfo.h"
20#include "include/core/SkYUVAPixmaps.h"
Brian Salomon8f46ecc2020-11-17 13:28:45 -050021#include "src/gpu/GrPaint.h"
Brian Salomon8f46ecc2020-11-17 13:28:45 -050022#include "src/gpu/GrSamplerState.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050023#include "src/gpu/GrSurfaceDrawContext.h"
Brian Salomon8f46ecc2020-11-17 13:28:45 -050024#include "src/gpu/GrTextureProxy.h"
Brian Salomon0857bef2021-01-13 15:54:04 -050025#include "src/gpu/GrYUVATextureProxies.h"
Brian Salomon27c42022021-04-28 12:39:21 -040026#include "src/gpu/SkGr.h"
Brian Salomon8f46ecc2020-11-17 13:28:45 -050027#include "src/gpu/effects/GrYUVtoRGBEffect.h"
28
29#include <memory>
30#include <utility>
31
32class SkCanvas;
33
Brian Salomon8f46ecc2020-11-17 13:28:45 -050034namespace skiagm {
35
36//////////////////////////////////////////////////////////////////////////////
37
38// This GM tests subsetting YUV multiplanar images where the U and V
39// planes have different resolution from Y. See skbug:8959
40
41class YUVtoRGBSubsetEffect : public GpuGM {
42public:
43 YUVtoRGBSubsetEffect() {
44 this->setBGColor(0xFFFFFFFF);
45 }
46
47protected:
48 SkString onShortName() override {
49 return SkString("yuv_to_rgb_subset_effect");
50 }
51
52 SkISize onISize() override { return {1310, 540}; }
53
Brian Salomon0857bef2021-01-13 15:54:04 -050054 void makePixmaps() {
55 SkYUVAInfo yuvaInfo = SkYUVAInfo({8, 8},
56 SkYUVAInfo::PlaneConfig::kY_U_V,
57 SkYUVAInfo::Subsampling::k420,
58 kJPEG_Full_SkYUVColorSpace);
59 SkColorType colorTypes[] = {kAlpha_8_SkColorType,
60 kAlpha_8_SkColorType,
61 kAlpha_8_SkColorType};
62 SkYUVAPixmapInfo pmapInfo(yuvaInfo, colorTypes, nullptr);
63 fPixmaps = SkYUVAPixmaps::Allocate(pmapInfo);
Brian Salomon8f46ecc2020-11-17 13:28:45 -050064
65 unsigned char innerY[16] = {149, 160, 130, 105,
66 160, 130, 105, 149,
67 130, 105, 149, 160,
68 105, 149, 160, 130};
69 unsigned char innerU[4] = {43, 75, 145, 200};
70 unsigned char innerV[4] = {88, 180, 200, 43};
71 int outerYUV[] = {128, 128, 128};
Brian Salomon0857bef2021-01-13 15:54:04 -050072 SkBitmap bitmaps[3];
Brian Salomon8f46ecc2020-11-17 13:28:45 -050073 for (int i = 0; i < 3; ++i) {
Brian Salomon0857bef2021-01-13 15:54:04 -050074 bitmaps[i].installPixels(fPixmaps.plane(i));
75 bitmaps[i].eraseColor(SkColorSetARGB(outerYUV[i], 0, 0, 0));
Brian Salomon8f46ecc2020-11-17 13:28:45 -050076 }
77 SkPixmap innerYPM(SkImageInfo::MakeA8(4, 4), innerY, 4);
78 SkPixmap innerUPM(SkImageInfo::MakeA8(2, 2), innerU, 2);
79 SkPixmap innerVPM(SkImageInfo::MakeA8(2, 2), innerV, 2);
Brian Salomon0857bef2021-01-13 15:54:04 -050080 bitmaps[0].writePixels(innerYPM, 2, 2);
81 bitmaps[1].writePixels(innerUPM, 1, 1);
82 bitmaps[2].writePixels(innerVPM, 1, 1);
Brian Salomon8f46ecc2020-11-17 13:28:45 -050083 }
84
Brian Salomon0857bef2021-01-13 15:54:04 -050085 DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
86 if (!context) {
87 return DrawResult::kSkip;
88 }
89 if (!fPixmaps.isValid()) {
90 this->makePixmaps();
91 }
92 GrSurfaceProxyView views[SkYUVAInfo::kMaxPlanes];
93 GrColorType colorTypes[SkYUVAInfo::kMaxPlanes];
94 for (int i = 0; i < fPixmaps.numPlanes(); ++i) {
95 SkBitmap bitmap;
96 bitmap.installPixels(fPixmaps.plane(i));
97 bitmap.setImmutable();
Brian Salomon27c42022021-04-28 12:39:21 -040098 views[i] = std::get<0>(GrMakeCachedBitmapProxyView(context, bitmap, GrMipmapped::kNo));
Brian Salomon8f46ecc2020-11-17 13:28:45 -050099 if (!views[i]) {
100 *errorMsg = "Failed to create proxy";
Brian Salomon0857bef2021-01-13 15:54:04 -0500101 return context->abandoned() ? DrawResult::kSkip : DrawResult::kFail;
Brian Salomon8f46ecc2020-11-17 13:28:45 -0500102 }
Brian Salomon0857bef2021-01-13 15:54:04 -0500103 colorTypes[i] = SkColorTypeToGrColorType(bitmap.colorType());
Brian Salomon8f46ecc2020-11-17 13:28:45 -0500104 }
Brian Salomon0857bef2021-01-13 15:54:04 -0500105 fProxies = GrYUVATextureProxies(fPixmaps.yuvaInfo(), views, colorTypes);
106 if (!fProxies.isValid()) {
107 *errorMsg = "Failed to create GrYUVATextureProxies";
108 return DrawResult::kFail;
109 }
110 return DrawResult::kOk;
111 }
Brian Salomon8f46ecc2020-11-17 13:28:45 -0500112
Brian Salomon0857bef2021-01-13 15:54:04 -0500113 void onGpuTeardown() override { fProxies = {}; }
114
115 DrawResult onDraw(GrRecordingContext* context,
116 GrSurfaceDrawContext* surfaceDrawContext,
117 SkCanvas* canvas,
118 SkString* errorMsg) override {
Brian Salomon8f46ecc2020-11-17 13:28:45 -0500119 static const GrSamplerState::Filter kFilters[] = {GrSamplerState::Filter::kNearest,
120 GrSamplerState::Filter::kLinear};
121 static const SkRect kColorRect = SkRect::MakeLTRB(2.f, 2.f, 6.f, 6.f);
122
Brian Salomon8f46ecc2020-11-17 13:28:45 -0500123 // Outset to visualize wrap modes.
Brian Salomon0857bef2021-01-13 15:54:04 -0500124 SkRect rect = SkRect::Make(fProxies.yuvaInfo().dimensions());
125 rect = rect.makeOutset(fProxies.yuvaInfo().width()/2.f, fProxies.yuvaInfo().height()/2.f);
Brian Salomon8f46ecc2020-11-17 13:28:45 -0500126
127 SkScalar y = kTestPad;
128 // Rows are filter modes.
129 for (uint32_t i = 0; i < SK_ARRAY_COUNT(kFilters); ++i) {
130 SkScalar x = kTestPad;
131 // Columns are non-subsetted followed by subsetted with each WrapMode in a row
132 for (uint32_t j = 0; j < GrSamplerState::kWrapModeCount + 1; ++j) {
133 SkMatrix ctm = SkMatrix::Translate(x, y);
134 ctm.postScale(10.f, 10.f);
135
136 const SkRect* subset = j > 0 ? &kColorRect : nullptr;
137
138 GrSamplerState samplerState;
139 samplerState.setFilterMode(kFilters[i]);
140 if (j > 0) {
141 auto wm = static_cast<GrSamplerState::WrapMode>(j - 1);
142 samplerState.setWrapModeX(wm);
143 samplerState.setWrapModeY(wm);
144 }
145 const auto& caps = *context->priv().caps();
Brian Salomon0857bef2021-01-13 15:54:04 -0500146 std::unique_ptr<GrFragmentProcessor> fp =
147 GrYUVtoRGBEffect::Make(fProxies, samplerState, caps, SkMatrix::I(), subset);
Brian Salomon8f46ecc2020-11-17 13:28:45 -0500148 if (fp) {
149 GrPaint grPaint;
150 grPaint.setColorFragmentProcessor(std::move(fp));
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500151 surfaceDrawContext->drawRect(
Brian Salomon8f46ecc2020-11-17 13:28:45 -0500152 nullptr, std::move(grPaint), GrAA::kYes, ctm, rect);
153 }
154 x += rect.width() + kTestPad;
155 }
156
157 y += rect.height() + kTestPad;
158 }
159
160 return DrawResult::kOk;
Brian Salomon0857bef2021-01-13 15:54:04 -0500161 }
Brian Salomon8f46ecc2020-11-17 13:28:45 -0500162
163private:
Brian Salomon0857bef2021-01-13 15:54:04 -0500164 SkYUVAPixmaps fPixmaps;
165 GrYUVATextureProxies fProxies;
Brian Salomon8f46ecc2020-11-17 13:28:45 -0500166
167 static constexpr SkScalar kTestPad = 10.f;
168
169 using INHERITED = GM;
170};
171
172DEF_GM(return new YUVtoRGBSubsetEffect;)
173} // namespace skiagm