| 1 |
/* |
| 2 |
* Copyright 2015 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/SkCanvas.h" |
| 13 |
#include "include/core/SkColor.h" |
| 14 |
#include "include/core/SkColorFilter.h" |
| 15 |
#include "include/core/SkImage.h" |
| 16 |
#include "include/core/SkImageInfo.h" |
| 17 |
#include "include/core/SkPaint.h" |
| 18 |
#include "include/core/SkPixmap.h" |
| 19 |
#include "include/core/SkRefCnt.h" |
| 20 |
#include "include/core/SkScalar.h" |
| 21 |
#include "include/core/SkShader.h" |
| 22 |
#include "include/core/SkSize.h" |
| 23 |
#include "include/core/SkString.h" |
| 24 |
#include "include/core/SkSurface.h" |
| 25 |
#include "include/core/SkTileMode.h" |
| 26 |
#include "include/core/SkTypes.h" |
| 27 |
#include "include/private/base/SkTo.h" |
| 28 |
#include "src/base/SkMathPriv.h" |
| 29 |
#include "src/core/SkYUVMath.h" |
| 30 |
#include "tools/DecodeUtils.h" |
| 31 |
#include "tools/Resources.h" |
| 32 |
#include "tools/gpu/YUVUtils.h" |
| 33 |
|
| 34 |
#if defined(SK_GANESH) |
| 35 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 36 |
#include "include/gpu/ganesh/GrTypes.h" |
| 37 |
#include "include/gpu/ganesh/SkSurfaceGanesh.h" |
| 38 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 39 |
#endif |
| 40 |
|
| 41 |
#if defined(SK_GRAPHITE) |
| 42 |
#include "include/gpu/graphite/Surface.h" |
| 43 |
#endif |
| 44 |
|
| 45 |
namespace skiagm { |
| 46 |
class ImageFromYUV : public GM { |
| 47 |
public: |
| 48 |
enum class Source { |
| 49 |
kTextures, |
| 50 |
kImages, |
| 51 |
}; |
| 52 |
|
| 53 |
ImageFromYUV(Source source) : fSource(source) { |
| 54 |
this->setBGColor(0xFFFFFFFF); |
| 55 |
} |
| 56 |
|
| 57 |
protected: |
| 58 |
SkString getName() const override { |
| 59 |
switch (fSource) { |
| 60 |
case Source::kTextures: return SkString("image_from_yuv_textures"); |
| 61 |
case Source::kImages: return SkString("image_from_yuv_images"); |
| 62 |
} |
| 63 |
SkUNREACHABLE; |
| 64 |
} |
| 65 |
|
| 66 |
SkISize getISize() override { return {1950, 800}; } |
| 67 |
|
| 68 |
static std::unique_ptr<sk_gpu_test::LazyYUVImage> CreatePlanes(const char* name) { |
| 69 |
SkBitmap bmp; |
| 70 |
if (!ToolUtils::GetResourceAsBitmap(name, &bmp)) { |
| 71 |
return {}; |
| 72 |
} |
| 73 |
if (bmp.colorType() != kRGBA_8888_SkColorType) { |
| 74 |
auto info = bmp.info().makeColorType(kRGBA_8888_SkColorType); |
| 75 |
SkBitmap copy; |
| 76 |
copy.allocPixels(info); |
| 77 |
SkAssertResult(bmp.readPixels(copy.pixmap())); |
| 78 |
bmp = copy; |
| 79 |
} |
| 80 |
SkYUVAPixmapInfo pixmapInfo({bmp.dimensions(), |
| 81 |
SkYUVAInfo::PlaneConfig::kY_U_V_A, |
| 82 |
SkYUVAInfo::Subsampling::k420, |
| 83 |
kJPEG_Full_SkYUVColorSpace}, |
| 84 |
SkYUVAPixmapInfo::DataType::kUnorm8, |
| 85 |
nullptr); |
| 86 |
auto pixmaps = SkYUVAPixmaps::Allocate(pixmapInfo); |
| 87 |
|
| 88 |
unsigned char* yuvPixels[] = { |
| 89 |
static_cast<unsigned char*>(pixmaps.planes()[0].writable_addr()), |
| 90 |
static_cast<unsigned char*>(pixmaps.planes()[1].writable_addr()), |
| 91 |
static_cast<unsigned char*>(pixmaps.planes()[2].writable_addr()), |
| 92 |
static_cast<unsigned char*>(pixmaps.planes()[3].writable_addr()), |
| 93 |
}; |
| 94 |
|
| 95 |
float m[20]; |
| 96 |
SkColorMatrix_RGB2YUV(pixmaps.yuvaInfo().yuvColorSpace(), m); |
| 97 |
// Here we encode using the kJPEG_SkYUVColorSpace (i.e., full-swing Rec 601) even though |
| 98 |
// we will draw it with all the supported yuv color spaces when converted back to RGB |
| 99 |
for (int j = 0; j < pixmaps.planes()[0].height(); ++j) { |
| 100 |
for (int i = 0; i < pixmaps.planes()[0].width(); ++i) { |
| 101 |
auto rgba = *bmp.getAddr32(i, j); |
| 102 |
auto r = (rgba & 0x000000ff) >> 0; |
| 103 |
auto g = (rgba & 0x0000ff00) >> 8; |
| 104 |
auto b = (rgba & 0x00ff0000) >> 16; |
| 105 |
auto a = (rgba & 0xff000000) >> 24; |
| 106 |
yuvPixels[0][j*pixmaps.planes()[0].width() + i] = SkToU8( |
| 107 |
sk_float_round2int(m[0]*r + m[1]*g + m[2]*b + m[3]*a + 255*m[4])); |
| 108 |
yuvPixels[3][j*pixmaps.planes()[0].width() + i] = SkToU8(sk_float_round2int( |
| 109 |
m[15]*r + m[16]*g + m[17]*b + m[18]*a + 255*m[19])); |
| 110 |
} |
| 111 |
} |
| 112 |
for (int j = 0; j < pixmaps.planes()[1].height(); ++j) { |
| 113 |
for (int i = 0; i < pixmaps.planes()[1].width(); ++i) { |
| 114 |
// Average together 4 pixels of RGB. |
| 115 |
int rgba[] = {0, 0, 0, 0}; |
| 116 |
int denom = 0; |
| 117 |
int ylimit = std::min(2*j + 2, pixmaps.planes()[0].height()); |
| 118 |
int xlimit = std::min(2*i + 2, pixmaps.planes()[0].width()); |
| 119 |
for (int y = 2*j; y < ylimit; ++y) { |
| 120 |
for (int x = 2*i; x < xlimit; ++x) { |
| 121 |
auto src = *bmp.getAddr32(x, y); |
| 122 |
rgba[0] += (src & 0x000000ff) >> 0; |
| 123 |
rgba[1] += (src & 0x0000ff00) >> 8; |
| 124 |
rgba[2] += (src & 0x00ff0000) >> 16; |
| 125 |
rgba[3] += (src & 0xff000000) >> 24; |
| 126 |
++denom; |
| 127 |
} |
| 128 |
} |
| 129 |
for (int c = 0; c < 4; ++c) { |
| 130 |
rgba[c] /= denom; |
| 131 |
} |
| 132 |
int uvIndex = j*pixmaps.planes()[1].width() + i; |
| 133 |
yuvPixels[1][uvIndex] = SkToU8(sk_float_round2int( |
| 134 |
m[5]*rgba[0] + m[6]*rgba[1] + m[7]*rgba[2] + m[8]*rgba[3] + 255*m[9])); |
| 135 |
yuvPixels[2][uvIndex] = SkToU8(sk_float_round2int( |
| 136 |
m[10]*rgba[0] + m[11]*rgba[1] + m[12]*rgba[2] + m[13]*rgba[3] + 255*m[14])); |
| 137 |
} |
| 138 |
} |
| 139 |
return sk_gpu_test::LazyYUVImage::Make(std::move(pixmaps), skgpu::Mipmapped::kYes); |
| 140 |
} |
| 141 |
|
| 142 |
sk_sp<SkImage> makeYUVAImage(GrDirectContext* context, skgpu::graphite::Recorder* recorder) { |
| 143 |
SkASSERT(!(SkToBool(context) && SkToBool(recorder))); |
| 144 |
sk_gpu_test::LazyYUVImage::Type type; |
| 145 |
switch (fSource) { |
| 146 |
case Source::kTextures: type = sk_gpu_test::LazyYUVImage::Type::kFromTextures; break; |
| 147 |
case Source::kImages: type = sk_gpu_test::LazyYUVImage::Type::kFromImages; break; |
| 148 |
} |
| 149 |
#if defined(SK_GANESH) |
| 150 |
if (context) { |
| 151 |
return fLazyYUVImage->refImage(context, type); |
| 152 |
} |
| 153 |
#endif |
| 154 |
#if defined(SK_GRAPHITE) |
| 155 |
if (recorder) { |
| 156 |
return fLazyYUVImage->refImage(recorder, type); |
| 157 |
} |
| 158 |
#endif |
| 159 |
return nullptr; |
| 160 |
} |
| 161 |
|
| 162 |
sk_sp<SkImage> createReferenceImage(GrDirectContext* dContext, |
| 163 |
skgpu::graphite::Recorder* recorder) { |
| 164 |
auto planarImage = this->makeYUVAImage(dContext, recorder); |
| 165 |
if (!planarImage) { |
| 166 |
return nullptr; |
| 167 |
} |
| 168 |
|
| 169 |
auto resultInfo = SkImageInfo::Make(fLazyYUVImage->dimensions(), |
| 170 |
kRGBA_8888_SkColorType, |
| 171 |
kPremul_SkAlphaType); |
| 172 |
sk_sp<SkSurface> resultSurface; |
| 173 |
#if defined(SK_GANESH) |
| 174 |
if (dContext) { |
| 175 |
resultSurface = SkSurfaces::RenderTarget(dContext, |
| 176 |
skgpu::Budgeted::kYes, |
| 177 |
resultInfo, |
| 178 |
1, |
| 179 |
kTopLeft_GrSurfaceOrigin, |
| 180 |
nullptr, |
| 181 |
/*shouldCreateWithMips=*/true); |
| 182 |
} |
| 183 |
#endif |
| 184 |
#if defined(SK_GRAPHITE) |
| 185 |
if (recorder) { |
| 186 |
resultSurface = SkSurfaces::RenderTarget(recorder, resultInfo, skgpu::Mipmapped::kYes); |
| 187 |
} |
| 188 |
#endif |
| 189 |
if (!resultSurface) { |
| 190 |
return nullptr; |
| 191 |
} |
| 192 |
|
| 193 |
resultSurface->getCanvas()->drawImage(std::move(planarImage), 0, 0); |
| 194 |
return resultSurface->makeImageSnapshot(); |
| 195 |
} |
| 196 |
|
| 197 |
DrawResult onGpuSetup(SkCanvas* canvas, SkString* errorMsg, GraphiteTestContext*) override { |
| 198 |
auto* recorder = canvas->recorder(); |
| 199 |
|
| 200 |
#if defined(SK_GANESH) |
| 201 |
auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 202 |
if (!recorder && (!dContext || dContext->abandoned())) { |
| 203 |
*errorMsg = "DirectContext or graphite::Recorder required to create YUV images"; |
| 204 |
return DrawResult::kSkip; |
| 205 |
} |
| 206 |
|
| 207 |
if (dContext && !dContext->priv().caps()->mipmapSupport()) { |
| 208 |
return DrawResult::kSkip; |
| 209 |
} |
| 210 |
|
| 211 |
if (fSource == Source::kImages && dContext) { |
| 212 |
*errorMsg = "YUV Image from SkImage planes not supported with Ganesh."; |
| 213 |
return DrawResult::kSkip; |
| 214 |
} |
| 215 |
#else |
| 216 |
constexpr GrDirectContext* dContext = nullptr; |
| 217 |
#endif |
| 218 |
|
| 219 |
if (!fLazyYUVImage) { |
| 220 |
fLazyYUVImage = CreatePlanes("images/mandrill_128.png"); |
| 221 |
} |
| 222 |
|
| 223 |
// We make a version of this image for each draw because, if any draw flattens it to |
| 224 |
// RGBA, then all subsequent draws would use the RGBA texture. |
| 225 |
for (int i = 0; i < kNumImages; ++i) { |
| 226 |
fYUVAImages[i] = this->makeYUVAImage(dContext, recorder); |
| 227 |
if (!fYUVAImages[i]) { |
| 228 |
*errorMsg = "Couldn't create src YUVA image."; |
| 229 |
return DrawResult::kFail; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
fReferenceImage = this->createReferenceImage(dContext, recorder); |
| 234 |
if (!fReferenceImage) { |
| 235 |
*errorMsg = "Couldn't create reference YUVA image."; |
| 236 |
return DrawResult::kFail; |
| 237 |
} |
| 238 |
|
| 239 |
#if defined(SK_GANESH) |
| 240 |
if (dContext) { |
| 241 |
// Some backends (e.g., Vulkan) require all work be completed for backend textures |
| 242 |
// before they are deleted. Since we don't know when we'll next have access to a |
| 243 |
// direct context, flush all the work now. |
| 244 |
dContext->flush(); |
| 245 |
dContext->submit(GrSyncCpu::kYes); |
| 246 |
} |
| 247 |
#endif |
| 248 |
|
| 249 |
return DrawResult::kOk; |
| 250 |
} |
| 251 |
|
| 252 |
void onGpuTeardown() override { |
| 253 |
fLazyYUVImage.reset(); |
| 254 |
for (sk_sp<SkImage>& image : fYUVAImages) { |
| 255 |
image.reset(); |
| 256 |
} |
| 257 |
fReferenceImage.reset(); |
| 258 |
} |
| 259 |
|
| 260 |
SkImage* getYUVAImage(int index) { |
| 261 |
SkASSERT(index >= 0 && index < kNumImages); |
| 262 |
return fYUVAImages[index].get(); |
| 263 |
} |
| 264 |
|
| 265 |
void onDraw(SkCanvas* canvas) override { |
| 266 |
auto draw_image = [canvas](SkImage* image, const SkSamplingOptions& sampling) -> SkSize { |
| 267 |
if (!image) { |
| 268 |
return {0, 0}; |
| 269 |
} |
| 270 |
canvas->drawImage(image, 0, 0, sampling, nullptr); |
| 271 |
return {SkIntToScalar(image->width()), SkIntToScalar(image->height())}; |
| 272 |
}; |
| 273 |
|
| 274 |
auto draw_image_rect = [canvas](SkImage* image, |
| 275 |
const SkSamplingOptions& sampling) -> SkSize { |
| 276 |
if (!image) { |
| 277 |
return {0, 0}; |
| 278 |
} |
| 279 |
auto subset = SkRect::Make(image->dimensions()); |
| 280 |
subset.inset(subset.width() * .05f, subset.height() * .1f); |
| 281 |
auto dst = SkRect::MakeWH(subset.width(), subset.height()); |
| 282 |
canvas->drawImageRect(image, subset, dst, sampling, nullptr, |
| 283 |
SkCanvas::kStrict_SrcRectConstraint); |
| 284 |
return {dst.width(), dst.height()}; |
| 285 |
}; |
| 286 |
|
| 287 |
auto draw_image_shader = [canvas](SkImage* image, |
| 288 |
const SkSamplingOptions& sampling) -> SkSize { |
| 289 |
if (!image) { |
| 290 |
return {0, 0}; |
| 291 |
} |
| 292 |
SkMatrix m; |
| 293 |
m.setRotate(45, image->width()/2.f, image->height()/2.f); |
| 294 |
SkPaint paint; |
| 295 |
paint.setShader(image->makeShader(SkTileMode::kMirror, SkTileMode::kDecal, |
| 296 |
sampling, m)); |
| 297 |
auto rect = SkRect::MakeWH(image->width() * 1.3f, image->height()); |
| 298 |
canvas->drawRect(rect, paint); |
| 299 |
return {rect.width(), rect.height()}; |
| 300 |
}; |
| 301 |
|
| 302 |
canvas->translate(kPad, kPad); |
| 303 |
int imageIndex = 0; |
| 304 |
using DrawSig = SkSize(SkImage* image, const SkSamplingOptions&); |
| 305 |
using DF = std::function<DrawSig>; |
| 306 |
for (const auto& draw : {DF(draw_image), DF(draw_image_rect), DF(draw_image_shader)}) { |
| 307 |
float wForDrawFunc = 0; |
| 308 |
canvas->save(); |
| 309 |
for (auto scale : {1.f, 1.5f, 0.3f}) { |
| 310 |
float hForScale = 0; |
| 311 |
float wForScale = 0; |
| 312 |
canvas->save(); |
| 313 |
// We exercise either bicubic or mipmaps depending on the scale. |
| 314 |
SkSamplingOptions samplings[] = { |
| 315 |
{SkFilterMode::kNearest}, |
| 316 |
{SkFilterMode::kLinear}, |
| 317 |
scale > 1.f |
| 318 |
? SkSamplingOptions{SkCubicResampler::CatmullRom()} |
| 319 |
: SkSamplingOptions{SkFilterMode::kLinear, SkMipmapMode::kLinear}}; |
| 320 |
|
| 321 |
for (const auto& sampling : samplings) { |
| 322 |
float yuvAndRefH; |
| 323 |
canvas->save(); |
| 324 |
canvas->scale(scale, scale); |
| 325 |
auto s1 = draw(this->getYUVAImage(imageIndex++), sampling); |
| 326 |
yuvAndRefH = kPad + std::ceil(scale * s1.height()); |
| 327 |
canvas->restore(); |
| 328 |
canvas->save(); |
| 329 |
canvas->translate(0, yuvAndRefH); |
| 330 |
canvas->scale(scale, scale); |
| 331 |
auto s2 = draw(fReferenceImage.get(), sampling); |
| 332 |
yuvAndRefH += std::ceil(scale * s2.height()); |
| 333 |
canvas->restore(); |
| 334 |
|
| 335 |
float thisW = std::ceil(scale * std::max(s1.width(), s2.width())); |
| 336 |
|
| 337 |
SkPaint outline; |
| 338 |
outline.setColor(SK_ColorBLACK); |
| 339 |
outline.setStroke(true); |
| 340 |
outline.setAntiAlias(false); |
| 341 |
canvas->drawRect(SkRect::MakeXYWH(-1, -1, thisW + 1, yuvAndRefH + 1), outline); |
| 342 |
|
| 343 |
thisW += kPad; |
| 344 |
yuvAndRefH += kPad; |
| 345 |
|
| 346 |
canvas->translate(thisW, 0); |
| 347 |
|
| 348 |
wForScale += thisW; |
| 349 |
hForScale = std::max(hForScale, yuvAndRefH); |
| 350 |
} |
| 351 |
canvas->restore(); |
| 352 |
canvas->translate(0, hForScale); |
| 353 |
wForDrawFunc = std::max(wForScale, wForDrawFunc); |
| 354 |
} |
| 355 |
canvas->restore(); |
| 356 |
canvas->translate(wForDrawFunc, 0); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
private: |
| 361 |
Source fSource; |
| 362 |
|
| 363 |
std::unique_ptr<sk_gpu_test::LazyYUVImage> fLazyYUVImage; |
| 364 |
|
| 365 |
// 3 draws x 3 scales x 4 filter qualities |
| 366 |
inline static constexpr int kNumImages = 3 * 3 * 4; |
| 367 |
sk_sp<SkImage> fYUVAImages[kNumImages]; |
| 368 |
sk_sp<SkImage> fReferenceImage; |
| 369 |
|
| 370 |
inline static constexpr SkScalar kPad = 10.0f; |
| 371 |
|
| 372 |
using INHERITED = GM; |
| 373 |
}; |
| 374 |
|
| 375 |
DEF_GM(return new ImageFromYUV(ImageFromYUV::Source::kTextures);) |
| 376 |
DEF_GM(return new ImageFromYUV(ImageFromYUV::Source::kImages);) |
| 377 |
} // namespace skiagm |