| 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 |
|
| 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/SkShader.h" |
| 18 |
#include "include/core/SkSize.h" |
| 19 |
#include "include/core/SkString.h" |
| 20 |
#include "include/core/SkYUVAInfo.h" |
| 21 |
#include "include/core/SkYUVAPixmaps.h" |
| 22 |
#include "src/core/SkCanvasPriv.h" |
| 23 |
#include "tools/gpu/YUVUtils.h" |
| 24 |
|
| 25 |
#if defined(SK_GANESH) |
| 26 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 27 |
#include "src/gpu/ganesh/SkGr.h" |
| 28 |
#endif |
| 29 |
|
| 30 |
#include <memory> |
| 31 |
#include <utility> |
| 32 |
|
| 33 |
class SkCanvas; |
| 34 |
|
| 35 |
namespace skiagm { |
| 36 |
|
| 37 |
////////////////////////////////////////////////////////////////////////////// |
| 38 |
|
| 39 |
// This GM tests subsetting YUV multiplanar images where the U and V |
| 40 |
// planes have different resolution from Y. See skbug.com/40040241 |
| 41 |
|
| 42 |
class YUVtoRGBSubsetEffect : public GM { |
| 43 |
public: |
| 44 |
YUVtoRGBSubsetEffect() { |
| 45 |
this->setBGColor(0xFFFFFFFF); |
| 46 |
} |
| 47 |
|
| 48 |
protected: |
| 49 |
SkString getName() const override { return SkString("yuv_to_rgb_subset_effect"); } |
| 50 |
|
| 51 |
SkISize getISize() override { return {1310, 540}; } |
| 52 |
|
| 53 |
void makePixmaps() { |
| 54 |
SkYUVAInfo yuvaInfo = SkYUVAInfo({8, 8}, |
| 55 |
SkYUVAInfo::PlaneConfig::kY_U_V, |
| 56 |
SkYUVAInfo::Subsampling::k420, |
| 57 |
kJPEG_Full_SkYUVColorSpace); |
| 58 |
SkColorType colorTypes[] = {kAlpha_8_SkColorType, |
| 59 |
kAlpha_8_SkColorType, |
| 60 |
kAlpha_8_SkColorType}; |
| 61 |
SkYUVAPixmapInfo pmapInfo(yuvaInfo, colorTypes, nullptr); |
| 62 |
fPixmaps = SkYUVAPixmaps::Allocate(pmapInfo); |
| 63 |
|
| 64 |
unsigned char innerY[16] = {149, 160, 130, 105, |
| 65 |
160, 130, 105, 149, |
| 66 |
130, 105, 149, 160, |
| 67 |
105, 149, 160, 130}; |
| 68 |
unsigned char innerU[4] = {43, 75, 145, 200}; |
| 69 |
unsigned char innerV[4] = {88, 180, 200, 43}; |
| 70 |
int outerYUV[] = {128, 128, 128}; |
| 71 |
SkBitmap bitmaps[3]; |
| 72 |
for (int i = 0; i < 3; ++i) { |
| 73 |
bitmaps[i].installPixels(fPixmaps.plane(i)); |
| 74 |
bitmaps[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 |
bitmaps[0].writePixels(innerYPM, 2, 2); |
| 80 |
bitmaps[1].writePixels(innerUPM, 1, 1); |
| 81 |
bitmaps[2].writePixels(innerVPM, 1, 1); |
| 82 |
} |
| 83 |
|
| 84 |
DrawResult onGpuSetup(SkCanvas* canvas, SkString* errorMsg, GraphiteTestContext*) override { |
| 85 |
skgpu::graphite::Recorder* recorder = nullptr; |
| 86 |
GrDirectContext* context = nullptr; |
| 87 |
|
| 88 |
#if defined(SK_GRAPHITE) |
| 89 |
recorder = canvas->recorder(); |
| 90 |
#endif |
| 91 |
#if defined(SK_GANESH) |
| 92 |
context = GrAsDirectContext(canvas->recordingContext()); |
| 93 |
#endif |
| 94 |
|
| 95 |
if (!context && !recorder) { |
| 96 |
return DrawResult::kSkip; |
| 97 |
} |
| 98 |
|
| 99 |
if (!fPixmaps.isValid()) { |
| 100 |
this->makePixmaps(); |
| 101 |
} |
| 102 |
|
| 103 |
auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(fPixmaps); |
| 104 |
#if defined(SK_GRAPHITE) |
| 105 |
if (recorder) { |
| 106 |
fYUVImage = lazyYUV->refImage(recorder, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps); |
| 107 |
} |
| 108 |
#endif |
| 109 |
#if defined(SK_GANESH) |
| 110 |
if (context) { |
| 111 |
fYUVImage = lazyYUV->refImage(context, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps); |
| 112 |
} |
| 113 |
#endif |
| 114 |
|
| 115 |
return DrawResult::kOk; |
| 116 |
} |
| 117 |
|
| 118 |
void onGpuTeardown() override { fYUVImage.reset(); } |
| 119 |
|
| 120 |
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
| 121 |
SkRecorder* recorder = canvas->baseRecorder(); |
| 122 |
if (!recorder) { |
| 123 |
*errorMsg = kErrorMsg_DrawSkippedGpuOnly; |
| 124 |
return DrawResult::kSkip; |
| 125 |
} |
| 126 |
|
| 127 |
if (!fYUVImage) { |
| 128 |
*errorMsg = "No valid YUV image generated -- skipping"; |
| 129 |
return DrawResult::kSkip; |
| 130 |
} |
| 131 |
|
| 132 |
static const SkFilterMode kFilters[] = {SkFilterMode::kNearest, |
| 133 |
SkFilterMode::kLinear}; |
| 134 |
static const SkIRect kColorRect = SkIRect::MakeLTRB(2, 2, 6, 6); |
| 135 |
|
| 136 |
// Outset to visualize wrap modes. |
| 137 |
SkRect rect = SkRect::Make(fYUVImage->dimensions()); |
| 138 |
rect = rect.makeOutset(fYUVImage->width()/2.f, fYUVImage->height()/2.f); |
| 139 |
|
| 140 |
SkScalar y = kTestPad; |
| 141 |
// Rows are filter modes. |
| 142 |
for (uint32_t i = 0; i < std::size(kFilters); ++i) { |
| 143 |
SkScalar x = kTestPad; |
| 144 |
// Columns are non-subsetted followed by subsetted with each TileMode in a row |
| 145 |
for (uint32_t j = 0; j < kSkTileModeCount + 1; ++j) { |
| 146 |
SkMatrix ctm = SkMatrix::Translate(x, y); |
| 147 |
ctm.postScale(10.f, 10.f); |
| 148 |
|
| 149 |
const SkIRect* subset = j > 0 ? &kColorRect : nullptr; |
| 150 |
|
| 151 |
auto tm = SkTileMode::kClamp; |
| 152 |
if (j > 0) { |
| 153 |
tm = static_cast<SkTileMode>(j - 1); |
| 154 |
} |
| 155 |
|
| 156 |
canvas->save(); |
| 157 |
canvas->concat(ctm); |
| 158 |
SkSamplingOptions sampling(kFilters[i]); |
| 159 |
SkPaint paint; |
| 160 |
// Draw black rectangle in background so rendering with Decal tilemode matches |
| 161 |
// the previously used ClampToBorder wrapmode. |
| 162 |
paint.setColor(SK_ColorBLACK); |
| 163 |
canvas->drawRect(rect, paint); |
| 164 |
if (subset) { |
| 165 |
sk_sp<SkImage> subsetImg = fYUVImage->makeSubset(recorder, *subset, {false}); |
| 166 |
SkASSERT(subsetImg); |
| 167 |
paint.setShader(subsetImg->makeShader(tm, tm, |
| 168 |
sampling, SkMatrix::Translate(2, 2))); |
| 169 |
} else { |
| 170 |
paint.setShader(fYUVImage->makeShader(tm, tm, |
| 171 |
sampling, SkMatrix::I())); |
| 172 |
} |
| 173 |
canvas->drawRect(rect, paint); |
| 174 |
canvas->restore(); |
| 175 |
x += rect.width() + kTestPad; |
| 176 |
} |
| 177 |
|
| 178 |
y += rect.height() + kTestPad; |
| 179 |
} |
| 180 |
|
| 181 |
return DrawResult::kOk; |
| 182 |
} |
| 183 |
|
| 184 |
private: |
| 185 |
SkYUVAPixmaps fPixmaps; |
| 186 |
sk_sp<SkImage> fYUVImage; |
| 187 |
|
| 188 |
inline static constexpr SkScalar kTestPad = 10.f; |
| 189 |
|
| 190 |
using INHERITED = GM; |
| 191 |
}; |
| 192 |
|
| 193 |
DEF_GM(return new YUVtoRGBSubsetEffect;) |
| 194 |
} // namespace skiagm |