| 1 |
/* |
| 2 |
* Copyright 2019 Google LLC |
| 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 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkPaint.h" |
| 12 |
#include "include/core/SkRect.h" |
| 13 |
#include "include/core/SkSurface.h" |
| 14 |
#include "include/core/SkYUVAInfo.h" |
| 15 |
#include "include/core/SkYUVAPixmaps.h" |
| 16 |
#include "include/effects/SkGradient.h" |
| 17 |
#include "src/base/SkScopeExit.h" |
| 18 |
#include "src/core/SkAutoPixmapStorage.h" |
| 19 |
#include "tools/DecodeUtils.h" |
| 20 |
#include "tools/Resources.h" |
| 21 |
#include "tools/ToolUtils.h" |
| 22 |
#include "tools/gpu/YUVUtils.h" |
| 23 |
|
| 24 |
#if defined(SK_GANESH) |
| 25 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 26 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 27 |
#include "include/gpu/ganesh/SkImageGanesh.h" |
| 28 |
#endif |
| 29 |
|
| 30 |
#if defined(SK_GRAPHITE) |
| 31 |
#include "include/gpu/graphite/Context.h" |
| 32 |
#include "include/gpu/graphite/Image.h" |
| 33 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 34 |
#include "tools/graphite/GraphiteTestContext.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
#include <variant> |
| 38 |
|
| 39 |
namespace { |
| 40 |
/// We test reading from images and surfaces |
| 41 |
enum class ReadSource { |
| 42 |
kImage, |
| 43 |
kSurface, |
| 44 |
}; |
| 45 |
|
| 46 |
// We test reading to RGBA, YUV, and YUVA |
| 47 |
enum class Type { |
| 48 |
kRGBA, |
| 49 |
kYUV, |
| 50 |
kYUVA |
| 51 |
}; |
| 52 |
|
| 53 |
template <ReadSource> struct SourceS; |
| 54 |
template <> struct SourceS<ReadSource::kImage> { using Type = SkImage; }; |
| 55 |
template <> struct SourceS<ReadSource::kSurface> { using Type = SkSurface; }; |
| 56 |
|
| 57 |
template <ReadSource RS> using Source = typename SourceS<RS>::Type; |
| 58 |
|
| 59 |
// Converts a source image to either an SkImage or SkSurface, backed by GPU if canvas is. Returns |
| 60 |
// kSkip or kFail if the image cannot be converted. |
| 61 |
template <ReadSource RS> |
| 62 |
std::variant<sk_sp<Source<RS>>, skiagm::DrawResult> convert_image_to_source(SkCanvas* canvas, |
| 63 |
sk_sp<SkImage> image, |
| 64 |
SkString* errorMsg); |
| 65 |
|
| 66 |
template <> |
| 67 |
std::variant<sk_sp<SkImage>, skiagm::DrawResult> convert_image_to_source<ReadSource::kImage>( |
| 68 |
SkCanvas* canvas, |
| 69 |
sk_sp<SkImage> image, |
| 70 |
SkString* errorMsg) { |
| 71 |
#if defined(SK_GRAPHITE) |
| 72 |
if (auto recorder = canvas->recorder()) { |
| 73 |
image = SkImages::TextureFromImage(recorder, image); |
| 74 |
if (image) { |
| 75 |
return image; |
| 76 |
} |
| 77 |
*errorMsg = "Could not create Graphite image"; |
| 78 |
return skiagm::DrawResult::kFail; |
| 79 |
} |
| 80 |
#endif |
| 81 |
#if defined(SK_GANESH) |
| 82 |
auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 83 |
if (!dContext && canvas->recordingContext()) { |
| 84 |
*errorMsg = "Not supported in DDL mode"; |
| 85 |
return skiagm::DrawResult::kSkip; |
| 86 |
} |
| 87 |
if (dContext) { |
| 88 |
image = SkImages::TextureFromImage(dContext, image); |
| 89 |
if (image) { |
| 90 |
return image; |
| 91 |
} |
| 92 |
// When testing abandoned GrContext we expect surface creation to fail. |
| 93 |
if (dContext && dContext->abandoned()) { |
| 94 |
return skiagm::DrawResult::kSkip; |
| 95 |
} |
| 96 |
*errorMsg = "Could not create Ganesh image"; |
| 97 |
return skiagm::DrawResult::kFail; |
| 98 |
} |
| 99 |
#endif |
| 100 |
return image; |
| 101 |
} |
| 102 |
|
| 103 |
template <> |
| 104 |
std::variant<sk_sp<SkSurface>, skiagm::DrawResult> convert_image_to_source<ReadSource::kSurface>( |
| 105 |
SkCanvas* canvas, |
| 106 |
sk_sp<SkImage> image, |
| 107 |
SkString* errorMsg) { |
| 108 |
// Turn the image into a surface in order to call the read and rescale API |
| 109 |
auto surfInfo = image->imageInfo().makeDimensions(image->dimensions()); |
| 110 |
auto surface = canvas->makeSurface(surfInfo); |
| 111 |
if (!surface && surfInfo.colorType() == kBGRA_8888_SkColorType) { |
| 112 |
surfInfo = surfInfo.makeColorType(kRGBA_8888_SkColorType); |
| 113 |
surface = canvas->makeSurface(surfInfo); |
| 114 |
} |
| 115 |
if (!surface) { |
| 116 |
*errorMsg = "Could not create surface for image."; |
| 117 |
#if defined(SK_GANESH) |
| 118 |
// When testing abandoned GrContext we expect surface creation to fail. |
| 119 |
if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) { |
| 120 |
return skiagm::DrawResult::kSkip; |
| 121 |
} |
| 122 |
#endif |
| 123 |
return skiagm::DrawResult::kFail; |
| 124 |
} |
| 125 |
SkPaint paint; |
| 126 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 127 |
surface->getCanvas()->drawImage(image, 0, 0, SkSamplingOptions(), &paint); |
| 128 |
return surface; |
| 129 |
} |
| 130 |
|
| 131 |
class AsyncReadGMBase : public skiagm::GM { |
| 132 |
public: |
| 133 |
AsyncReadGMBase(const char* name) : fName(name) {} |
| 134 |
|
| 135 |
SkString getName() const override { return fName; } |
| 136 |
|
| 137 |
protected: |
| 138 |
// Does a rescale and read using Graphite, Ganesh, or CPU and returns the result as a pixmap |
| 139 |
// image. |
| 140 |
template <ReadSource ReadSource> |
| 141 |
sk_sp<SkImage> readAndScaleRGBA(Source<ReadSource>* src, |
| 142 |
SkIRect srcRect, |
| 143 |
GrDirectContext* direct, |
| 144 |
skgpu::graphite::Recorder* recorder, |
| 145 |
const SkImageInfo& ii, |
| 146 |
SkImage::RescaleGamma rescaleGamma, |
| 147 |
SkImage::RescaleMode rescaleMode) { |
| 148 |
auto* asyncContext = new AsyncContext(); |
| 149 |
if (recorder) { |
| 150 |
#if defined(SK_GRAPHITE) |
| 151 |
skgpu::graphite::Context* graphiteContext = recorder->priv().context(); |
| 152 |
if (!graphiteContext) { |
| 153 |
return nullptr; |
| 154 |
} |
| 155 |
// We need to flush the existing drawing commands before we try to read |
| 156 |
std::unique_ptr<skgpu::graphite::Recording> recording = recorder->snap(); |
| 157 |
if (!recording) { |
| 158 |
return nullptr; |
| 159 |
} |
| 160 |
skgpu::graphite::InsertRecordingInfo recordingInfo; |
| 161 |
recordingInfo.fRecording = recording.get(); |
| 162 |
if (!graphiteContext->insertRecording(recordingInfo)) { |
| 163 |
return nullptr; |
| 164 |
} |
| 165 |
|
| 166 |
graphiteContext->asyncRescaleAndReadPixels(src, |
| 167 |
ii, |
| 168 |
srcRect, |
| 169 |
rescaleGamma, |
| 170 |
rescaleMode, |
| 171 |
AsyncCallback, |
| 172 |
asyncContext); |
| 173 |
graphiteContext->submit(); |
| 174 |
while (!asyncContext->fCalled) { |
| 175 |
graphiteContext->checkAsyncWorkCompletion(); |
| 176 |
if (this->graphiteTestContext()) { |
| 177 |
this->graphiteTestContext()->tick(); |
| 178 |
} |
| 179 |
} |
| 180 |
#endif |
| 181 |
} else { |
| 182 |
src->asyncRescaleAndReadPixels(ii, |
| 183 |
srcRect, |
| 184 |
rescaleGamma, |
| 185 |
rescaleMode, |
| 186 |
AsyncCallback, |
| 187 |
asyncContext); |
| 188 |
#if defined(SK_GANESH) |
| 189 |
if (direct) { |
| 190 |
direct->submit(); |
| 191 |
} |
| 192 |
while (!asyncContext->fCalled) { |
| 193 |
// Only GPU should actually be asynchronous. |
| 194 |
SkASSERT(direct); |
| 195 |
direct->checkAsyncWorkCompletion(); |
| 196 |
} |
| 197 |
#endif |
| 198 |
} |
| 199 |
SkASSERT(asyncContext->fCalled); |
| 200 |
if (!asyncContext->fResult) { |
| 201 |
return nullptr; |
| 202 |
} |
| 203 |
SkPixmap pixmap(ii, asyncContext->fResult->data(0), asyncContext->fResult->rowBytes(0)); |
| 204 |
auto releasePixels = [](const void*, void* c) { delete static_cast<AsyncContext*>(c); }; |
| 205 |
return SkImages::RasterFromPixmap(pixmap, releasePixels, asyncContext); |
| 206 |
} |
| 207 |
|
| 208 |
// Does a YUV[A] rescale and read using Graphite or Ganesh (no CPU support) and returns the |
| 209 |
// result as a YUVA planar texture image. |
| 210 |
template <ReadSource ReadSource> |
| 211 |
sk_sp<SkImage> readAndScaleYUVA(Source<ReadSource>* src, |
| 212 |
SkIRect srcRect, |
| 213 |
SkISize resultSize, |
| 214 |
bool readAlpha, |
| 215 |
GrDirectContext* direct, |
| 216 |
skgpu::graphite::Recorder* recorder, |
| 217 |
SkYUVColorSpace yuvCS, |
| 218 |
SkImage::RescaleGamma rescaleGamma, |
| 219 |
SkImage::RescaleMode rescaleMode, |
| 220 |
SkScopeExit* cleanup) { |
| 221 |
SkASSERT(!(resultSize.width() & 0b1) && !(resultSize.height() & 0b1)); |
| 222 |
|
| 223 |
SkISize uvSize = {resultSize.width() / 2, resultSize.height() / 2}; |
| 224 |
SkImageInfo yaII = SkImageInfo::Make(resultSize, kGray_8_SkColorType, kPremul_SkAlphaType); |
| 225 |
SkImageInfo uvII = SkImageInfo::Make(uvSize, kGray_8_SkColorType, kPremul_SkAlphaType); |
| 226 |
|
| 227 |
AsyncContext asyncContext; |
| 228 |
if (recorder) { |
| 229 |
#if defined(SK_GRAPHITE) |
| 230 |
skgpu::graphite::Context* graphiteContext = recorder->priv().context(); |
| 231 |
if (!graphiteContext) { |
| 232 |
return nullptr; |
| 233 |
} |
| 234 |
// We need to flush the existing drawing commands before we try to read |
| 235 |
std::unique_ptr<skgpu::graphite::Recording> recording = recorder->snap(); |
| 236 |
if (!recording) { |
| 237 |
return nullptr; |
| 238 |
} |
| 239 |
skgpu::graphite::InsertRecordingInfo recordingInfo; |
| 240 |
recordingInfo.fRecording = recording.get(); |
| 241 |
if (!graphiteContext->insertRecording(recordingInfo)) { |
| 242 |
return nullptr; |
| 243 |
} |
| 244 |
|
| 245 |
if (readAlpha) { |
| 246 |
graphiteContext->asyncRescaleAndReadPixelsYUVA420(src, |
| 247 |
yuvCS, |
| 248 |
SkColorSpace::MakeSRGB(), |
| 249 |
srcRect, |
| 250 |
resultSize, |
| 251 |
rescaleGamma, |
| 252 |
rescaleMode, |
| 253 |
AsyncCallback, |
| 254 |
&asyncContext); |
| 255 |
} else { |
| 256 |
graphiteContext->asyncRescaleAndReadPixelsYUV420(src, |
| 257 |
yuvCS, |
| 258 |
SkColorSpace::MakeSRGB(), |
| 259 |
srcRect, |
| 260 |
resultSize, |
| 261 |
rescaleGamma, |
| 262 |
rescaleMode, |
| 263 |
AsyncCallback, |
| 264 |
&asyncContext); |
| 265 |
} |
| 266 |
graphiteContext->submit(); |
| 267 |
while (!asyncContext.fCalled) { |
| 268 |
graphiteContext->checkAsyncWorkCompletion(); |
| 269 |
if (this->graphiteTestContext()) { |
| 270 |
this->graphiteTestContext()->tick(); |
| 271 |
} |
| 272 |
} |
| 273 |
#endif |
| 274 |
} else { |
| 275 |
if (readAlpha) { |
| 276 |
src->asyncRescaleAndReadPixelsYUVA420(yuvCS, |
| 277 |
SkColorSpace::MakeSRGB(), |
| 278 |
srcRect, |
| 279 |
resultSize, |
| 280 |
rescaleGamma, |
| 281 |
rescaleMode, |
| 282 |
AsyncCallback, |
| 283 |
&asyncContext); |
| 284 |
} else { |
| 285 |
src->asyncRescaleAndReadPixelsYUV420(yuvCS, |
| 286 |
SkColorSpace::MakeSRGB(), |
| 287 |
srcRect, |
| 288 |
resultSize, |
| 289 |
rescaleGamma, |
| 290 |
rescaleMode, |
| 291 |
AsyncCallback, |
| 292 |
&asyncContext); |
| 293 |
} |
| 294 |
#if defined(SK_GANESH) |
| 295 |
if (direct) { |
| 296 |
direct->submit(); |
| 297 |
} |
| 298 |
while (!asyncContext.fCalled) { |
| 299 |
// Only GPU should actually be asynchronous. |
| 300 |
SkASSERT(direct); |
| 301 |
direct->checkAsyncWorkCompletion(); |
| 302 |
} |
| 303 |
#endif |
| 304 |
} |
| 305 |
SkASSERT(asyncContext.fCalled); |
| 306 |
if (!asyncContext.fResult) { |
| 307 |
return nullptr; |
| 308 |
} |
| 309 |
auto planeConfig = readAlpha ? SkYUVAInfo::PlaneConfig::kY_U_V_A : |
| 310 |
SkYUVAInfo::PlaneConfig::kY_U_V; |
| 311 |
SkYUVAInfo yuvaInfo(resultSize, |
| 312 |
planeConfig, |
| 313 |
SkYUVAInfo::Subsampling::k420, |
| 314 |
yuvCS); |
| 315 |
SkPixmap yuvPMs[4] = { |
| 316 |
{yaII, asyncContext.fResult->data(0), asyncContext.fResult->rowBytes(0)}, |
| 317 |
{uvII, asyncContext.fResult->data(1), asyncContext.fResult->rowBytes(1)}, |
| 318 |
{uvII, asyncContext.fResult->data(2), asyncContext.fResult->rowBytes(2)}, |
| 319 |
{}, |
| 320 |
}; |
| 321 |
if (readAlpha) { |
| 322 |
yuvPMs[3] = {yaII, asyncContext.fResult->data(3), asyncContext.fResult->rowBytes(3)}; |
| 323 |
} |
| 324 |
auto pixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, yuvPMs); |
| 325 |
SkASSERT(pixmaps.isValid()); |
| 326 |
auto lazyYUVImage = sk_gpu_test::LazyYUVImage::Make(pixmaps); |
| 327 |
SkASSERT(lazyYUVImage); |
| 328 |
#if defined(SK_GRAPHITE) |
| 329 |
if (recorder) { |
| 330 |
return lazyYUVImage->refImage(recorder, sk_gpu_test::LazyYUVImage::Type::kFromTextures); |
| 331 |
} else |
| 332 |
#endif |
| 333 |
{ |
| 334 |
return lazyYUVImage->refImage(direct, sk_gpu_test::LazyYUVImage::Type::kFromTextures); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
// Draws a 3x2 grid of rescales. The columns are none, low, and high filter quality. The rows |
| 339 |
// are rescale in src gamma and rescale in linear gamma. |
| 340 |
template <ReadSource ReadSource> |
| 341 |
skiagm::DrawResult drawRescaleGrid(SkCanvas* canvas, |
| 342 |
Source<ReadSource>* src, |
| 343 |
SkIRect srcRect, |
| 344 |
SkISize readSize, |
| 345 |
Type type, |
| 346 |
SkString* errorMsg, |
| 347 |
int pad = 0) { |
| 348 |
SkASSERT(canvas->imageInfo().colorType() != kUnknown_SkColorType); |
| 349 |
|
| 350 |
GrDirectContext* direct = nullptr; |
| 351 |
#if defined(SK_GANESH) |
| 352 |
direct = GrAsDirectContext(canvas->recordingContext()); |
| 353 |
SkASSERT(direct || !canvas->recordingContext()); |
| 354 |
#endif |
| 355 |
|
| 356 |
auto recorder = canvas->recorder(); |
| 357 |
|
| 358 |
SkYUVColorSpace yuvColorSpace = kRec601_SkYUVColorSpace; |
| 359 |
canvas->save(); |
| 360 |
for (auto gamma : {SkImage::RescaleGamma::kSrc, SkImage::RescaleGamma::kLinear}) { |
| 361 |
canvas->save(); |
| 362 |
for (auto mode : {SkImage::RescaleMode::kNearest, |
| 363 |
SkImage::RescaleMode::kRepeatedLinear, |
| 364 |
SkImage::RescaleMode::kRepeatedCubic}) { |
| 365 |
SkScopeExit cleanup; |
| 366 |
sk_sp<SkImage> result; |
| 367 |
switch (type) { |
| 368 |
case Type::kRGBA: { |
| 369 |
const auto ii = canvas->imageInfo().makeDimensions(readSize); |
| 370 |
result = readAndScaleRGBA<ReadSource>(src, |
| 371 |
srcRect, |
| 372 |
direct, |
| 373 |
recorder, |
| 374 |
ii, |
| 375 |
gamma, |
| 376 |
mode); |
| 377 |
if (!result) { |
| 378 |
errorMsg->printf("async read call failed."); |
| 379 |
return skiagm::DrawResult::kFail; |
| 380 |
} |
| 381 |
break; |
| 382 |
} |
| 383 |
case Type::kYUV: |
| 384 |
case Type::kYUVA: |
| 385 |
result = readAndScaleYUVA<ReadSource>(src, |
| 386 |
srcRect, |
| 387 |
readSize, |
| 388 |
/*readAlpha=*/type == Type::kYUVA, |
| 389 |
direct, |
| 390 |
recorder, |
| 391 |
yuvColorSpace, |
| 392 |
gamma, |
| 393 |
mode, |
| 394 |
&cleanup); |
| 395 |
if (!result) { |
| 396 |
errorMsg->printf("YUV[A]420 async call failed. Allowed for now."); |
| 397 |
return skiagm::DrawResult::kSkip; |
| 398 |
} |
| 399 |
int nextCS = static_cast<int>(yuvColorSpace + 1) % |
| 400 |
(kLastEnum_SkYUVColorSpace + 1); |
| 401 |
yuvColorSpace = static_cast<SkYUVColorSpace>(nextCS); |
| 402 |
break; |
| 403 |
} |
| 404 |
canvas->drawImage(result, 0, 0); |
| 405 |
canvas->translate(readSize.width() + pad, 0); |
| 406 |
} |
| 407 |
canvas->restore(); |
| 408 |
canvas->translate(0, readSize.height() + pad); |
| 409 |
} |
| 410 |
canvas->restore(); |
| 411 |
return skiagm::DrawResult::kOk; |
| 412 |
} |
| 413 |
|
| 414 |
private: |
| 415 |
struct AsyncContext { |
| 416 |
bool fCalled = false; |
| 417 |
std::unique_ptr<const SkImage::AsyncReadResult> fResult; |
| 418 |
}; |
| 419 |
|
| 420 |
// Making this a lambda in the test functions caused: |
| 421 |
// "error: cannot compile this forwarded non-trivially copyable parameter yet" |
| 422 |
// on x86/Win/Clang bot, referring to 'result'. |
| 423 |
static void AsyncCallback(void* c, std::unique_ptr<const SkImage::AsyncReadResult> result) { |
| 424 |
auto context = static_cast<AsyncContext*>(c); |
| 425 |
context->fResult = std::move(result); |
| 426 |
context->fCalled = true; |
| 427 |
} |
| 428 |
|
| 429 |
SkString fName; |
| 430 |
}; |
| 431 |
|
| 432 |
template <ReadSource ReadSource, Type Type> |
| 433 |
class AsyncRescaleAndReadGridGM : public AsyncReadGMBase { |
| 434 |
public: |
| 435 |
AsyncRescaleAndReadGridGM(const char* name, |
| 436 |
const char* imageFile, |
| 437 |
SkIRect srcRect, |
| 438 |
SkISize readSize) |
| 439 |
: AsyncReadGMBase(name) |
| 440 |
, fImageFile(imageFile) |
| 441 |
, fSrcRect(srcRect) |
| 442 |
, fReadSize(readSize) {} |
| 443 |
|
| 444 |
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
| 445 |
ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25); |
| 446 |
auto image = ToolUtils::GetResourceAsImage(fImageFile.c_str()); |
| 447 |
if (!image) { |
| 448 |
errorMsg->printf("Could not load image file %s.", fImageFile.c_str()); |
| 449 |
return skiagm::DrawResult::kFail; |
| 450 |
} |
| 451 |
if (canvas->imageInfo().colorType() == kUnknown_SkColorType) { |
| 452 |
*errorMsg = "Not supported on recording/vector backends."; |
| 453 |
return skiagm::DrawResult::kSkip; |
| 454 |
} |
| 455 |
|
| 456 |
auto sourceOrResult = convert_image_to_source<ReadSource>(canvas, image, errorMsg); |
| 457 |
if (auto dr = std::get_if<skiagm::DrawResult>(&sourceOrResult)) { |
| 458 |
return *dr; |
| 459 |
} |
| 460 |
|
| 461 |
using Src = sk_sp<Source<ReadSource>>; |
| 462 |
return drawRescaleGrid<ReadSource>(canvas, |
| 463 |
std::get<Src>(sourceOrResult).get(), |
| 464 |
fSrcRect, |
| 465 |
fReadSize, |
| 466 |
Type, |
| 467 |
errorMsg); |
| 468 |
} |
| 469 |
|
| 470 |
SkISize getISize() override { return {3 * fReadSize.width(), 2 * fReadSize.height()}; } |
| 471 |
|
| 472 |
private: |
| 473 |
SkString fImageFile; |
| 474 |
SkIRect fSrcRect; |
| 475 |
SkISize fReadSize; |
| 476 |
}; |
| 477 |
} // anonymous namespace |
| 478 |
|
| 479 |
#define DEF_RESCALE_AND_READ_GRID_GM(IMAGE_FILE, TAG, SRC_RECT, W, H, SOURCE, TYPE) \ |
| 480 |
DEF_GM(return new (AsyncRescaleAndReadGridGM<SOURCE, TYPE>)( \ |
| 481 |
"async_rescale_and_read_" #TAG, #IMAGE_FILE, SRC_RECT, SkISize{W, H});) |
| 482 |
|
| 483 |
DEF_RESCALE_AND_READ_GRID_GM(images/yellow_rose.webp, |
| 484 |
yuv420_rose, |
| 485 |
SkIRect::MakeXYWH(50, 5, 200, 150), |
| 486 |
410, |
| 487 |
376, |
| 488 |
ReadSource::kSurface, |
| 489 |
Type::kYUVA) |
| 490 |
|
| 491 |
DEF_RESCALE_AND_READ_GRID_GM(images/yellow_rose.webp, |
| 492 |
yuv420_rose_down, |
| 493 |
SkIRect::MakeXYWH(50, 5, 200, 150), |
| 494 |
106, |
| 495 |
60, |
| 496 |
ReadSource::kImage, |
| 497 |
Type::kYUV) |
| 498 |
|
| 499 |
DEF_RESCALE_AND_READ_GRID_GM(images/yellow_rose.webp, |
| 500 |
rose, |
| 501 |
SkIRect::MakeXYWH(100, 20, 100, 100), |
| 502 |
410, |
| 503 |
410, |
| 504 |
ReadSource::kSurface, |
| 505 |
Type::kRGBA) |
| 506 |
|
| 507 |
DEF_RESCALE_AND_READ_GRID_GM(images/dog.jpg, |
| 508 |
dog_down, |
| 509 |
SkIRect::MakeXYWH(0, 10, 180, 150), |
| 510 |
45, |
| 511 |
45, |
| 512 |
ReadSource::kSurface, |
| 513 |
Type::kRGBA) |
| 514 |
|
| 515 |
DEF_RESCALE_AND_READ_GRID_GM(images/dog.jpg, |
| 516 |
dog_up, |
| 517 |
SkIRect::MakeWH(180, 180), |
| 518 |
800, |
| 519 |
400, |
| 520 |
ReadSource::kImage, |
| 521 |
Type::kRGBA) |
| 522 |
|
| 523 |
DEF_RESCALE_AND_READ_GRID_GM(images/text.png, |
| 524 |
text_down, |
| 525 |
SkIRect::MakeWH(637, 105), |
| 526 |
(int)(0.7 * 637), |
| 527 |
(int)(0.7 * 105), |
| 528 |
ReadSource::kImage, |
| 529 |
Type::kRGBA) |
| 530 |
|
| 531 |
DEF_RESCALE_AND_READ_GRID_GM(images/text.png, |
| 532 |
text_up, |
| 533 |
SkIRect::MakeWH(637, 105), |
| 534 |
(int)(1.2 * 637), |
| 535 |
(int)(1.2 * 105), |
| 536 |
ReadSource::kSurface, |
| 537 |
Type::kRGBA) |
| 538 |
|
| 539 |
DEF_RESCALE_AND_READ_GRID_GM(images/text.png, |
| 540 |
text_up_large, |
| 541 |
SkIRect::MakeXYWH(300, 0, 300, 105), |
| 542 |
(int)(2.4 * 300), |
| 543 |
(int)(2.4 * 105), |
| 544 |
ReadSource::kImage, |
| 545 |
Type::kRGBA) |
| 546 |
|
| 547 |
namespace { |
| 548 |
class AyncYUVNoScaleGM : public AsyncReadGMBase { |
| 549 |
public: |
| 550 |
AyncYUVNoScaleGM() : AsyncReadGMBase("async_yuv_no_scale") {} |
| 551 |
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
| 552 |
auto surface = canvas->getSurface(); |
| 553 |
if (!surface) { |
| 554 |
*errorMsg = "Not supported on recording/vector backends."; |
| 555 |
return skiagm::DrawResult::kSkip; |
| 556 |
} |
| 557 |
|
| 558 |
GrDirectContext* dContext = nullptr; |
| 559 |
#if defined(SK_GANESH) |
| 560 |
dContext = GrAsDirectContext(surface->recordingContext()); |
| 561 |
if (!dContext && surface->recordingContext()) { |
| 562 |
*errorMsg = "Not supported in DDL mode"; |
| 563 |
return skiagm::DrawResult::kSkip; |
| 564 |
} |
| 565 |
#endif |
| 566 |
|
| 567 |
auto image = ToolUtils::GetResourceAsImage("images/yellow_rose.webp"); |
| 568 |
if (!image) { |
| 569 |
return skiagm::DrawResult::kFail; |
| 570 |
} |
| 571 |
|
| 572 |
static constexpr SkIPoint kOffset = {15, 12}; |
| 573 |
SkISize evenSz = {image->width() & ~0b1, image->height() & ~0b1}; |
| 574 |
canvas->drawImage(image.get(), kOffset.fX, kOffset.fY); |
| 575 |
|
| 576 |
skgpu::graphite::Recorder* recorder = canvas->recorder(); |
| 577 |
SkScopeExit scopeExit; |
| 578 |
auto yuvImage = readAndScaleYUVA<ReadSource::kSurface>(surface, |
| 579 |
SkIRect::MakePtSize(kOffset, evenSz), |
| 580 |
evenSz, |
| 581 |
/*readAlpha=*/false, |
| 582 |
dContext, |
| 583 |
recorder, |
| 584 |
kRec601_SkYUVColorSpace, |
| 585 |
SkImage::RescaleGamma::kSrc, |
| 586 |
SkImage::RescaleMode::kNearest, |
| 587 |
&scopeExit); |
| 588 |
|
| 589 |
canvas->clear(SK_ColorWHITE); |
| 590 |
canvas->drawImage(yuvImage.get(), 0, 0); |
| 591 |
|
| 592 |
return skiagm::DrawResult::kOk; |
| 593 |
} |
| 594 |
SkISize getISize() override { return {400, 300}; } |
| 595 |
}; |
| 596 |
} // namespace |
| 597 |
|
| 598 |
DEF_GM(return new AyncYUVNoScaleGM();) |
| 599 |
|
| 600 |
namespace { |
| 601 |
class AsyncRescaleAndReadNoBleedGM : public AsyncReadGMBase { |
| 602 |
public: |
| 603 |
AsyncRescaleAndReadNoBleedGM() : AsyncReadGMBase("async_rescale_and_read_no_bleed") {} |
| 604 |
|
| 605 |
SkISize getISize() override { return {60, 60}; } |
| 606 |
|
| 607 |
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
| 608 |
if (canvas->imageInfo().colorType() == kUnknown_SkColorType) { |
| 609 |
*errorMsg = "Not supported on recording/vector backends."; |
| 610 |
return skiagm::DrawResult::kSkip; |
| 611 |
} |
| 612 |
|
| 613 |
#if defined(SK_GANESH) |
| 614 |
auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 615 |
if (!dContext && canvas->recordingContext()) { |
| 616 |
*errorMsg = "Not supported in DDL mode"; |
| 617 |
return skiagm::DrawResult::kSkip; |
| 618 |
} |
| 619 |
#endif |
| 620 |
|
| 621 |
static constexpr int kBorder = 5; |
| 622 |
static constexpr int kInner = 5; |
| 623 |
const auto srcRect = SkIRect::MakeXYWH(kBorder, kBorder, kInner, kInner); |
| 624 |
auto surfaceII = SkImageInfo::Make(kInner + 2 * kBorder, |
| 625 |
kInner + 2 * kBorder, |
| 626 |
kRGBA_8888_SkColorType, |
| 627 |
kPremul_SkAlphaType, |
| 628 |
SkColorSpace::MakeSRGB()); |
| 629 |
auto surface = canvas->makeSurface(surfaceII); |
| 630 |
if (!surface) { |
| 631 |
*errorMsg = "Could not create surface for image."; |
| 632 |
#if defined(SK_GANESH) |
| 633 |
// When testing abandoned GrContext we expect surface creation to fail. |
| 634 |
if (canvas->recordingContext() && canvas->recordingContext()->abandoned()) { |
| 635 |
return skiagm::DrawResult::kSkip; |
| 636 |
} |
| 637 |
#endif |
| 638 |
return skiagm::DrawResult::kFail; |
| 639 |
} |
| 640 |
surface->getCanvas()->clear(SK_ColorRED); |
| 641 |
surface->getCanvas()->save(); |
| 642 |
surface->getCanvas()->clipRect(SkRect::Make(srcRect), SkClipOp::kIntersect, false); |
| 643 |
surface->getCanvas()->clear(SK_ColorBLUE); |
| 644 |
surface->getCanvas()->restore(); |
| 645 |
static constexpr int kPad = 2; |
| 646 |
canvas->translate(kPad, kPad); |
| 647 |
skiagm::DrawResult result; |
| 648 |
SkISize downSize = {static_cast<int>(kInner / 2), static_cast<int>(kInner / 2)}; |
| 649 |
result = drawRescaleGrid<ReadSource::kSurface>(canvas, |
| 650 |
surface.get(), |
| 651 |
srcRect, |
| 652 |
downSize, |
| 653 |
Type::kRGBA, |
| 654 |
errorMsg, |
| 655 |
kPad); |
| 656 |
if (result != skiagm::DrawResult::kOk) { |
| 657 |
return result; |
| 658 |
} |
| 659 |
canvas->translate(0, 4 * downSize.height()); |
| 660 |
SkISize upSize = {static_cast<int>(kInner * 3.5), static_cast<int>(kInner * 4.6)}; |
| 661 |
result = drawRescaleGrid<ReadSource::kSurface>(canvas, |
| 662 |
surface.get(), |
| 663 |
srcRect, |
| 664 |
upSize, |
| 665 |
Type::kRGBA, |
| 666 |
errorMsg, |
| 667 |
kPad); |
| 668 |
if (result != skiagm::DrawResult::kOk) { |
| 669 |
return result; |
| 670 |
} |
| 671 |
return skiagm::DrawResult::kOk; |
| 672 |
} |
| 673 |
}; |
| 674 |
} // namespace |
| 675 |
|
| 676 |
DEF_GM(return new AsyncRescaleAndReadNoBleedGM();) |
| 677 |
|
| 678 |
namespace { |
| 679 |
class AsyncRescaleAndReadAlphaTypeGM : public AsyncReadGMBase { |
| 680 |
public: |
| 681 |
AsyncRescaleAndReadAlphaTypeGM() : AsyncReadGMBase("async_rescale_and_read_alpha_type") {} |
| 682 |
|
| 683 |
SkISize getISize() override { return {512, 512}; } |
| 684 |
|
| 685 |
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
| 686 |
GrDirectContext* dContext = nullptr; |
| 687 |
#if defined(SK_GANESH) |
| 688 |
dContext = GrAsDirectContext(canvas->recordingContext()); |
| 689 |
if (!dContext && canvas->recordingContext()) { |
| 690 |
*errorMsg = "Not supported in DDL mode"; |
| 691 |
return skiagm::DrawResult::kSkip; |
| 692 |
} |
| 693 |
#endif |
| 694 |
|
| 695 |
if (canvas->recorder()) { |
| 696 |
*errorMsg = "Reading to unpremul not supported in Graphite."; |
| 697 |
return skiagm::DrawResult::kSkip; |
| 698 |
} |
| 699 |
|
| 700 |
auto upmII = SkImageInfo::Make(200, 200, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType); |
| 701 |
|
| 702 |
auto pmII = upmII.makeAlphaType(kPremul_SkAlphaType); |
| 703 |
|
| 704 |
auto upmSurf = SkSurfaces::Raster(upmII); |
| 705 |
auto pmSurf = SkSurfaces::Raster(pmII); |
| 706 |
|
| 707 |
SkColor4f colors[] = { |
| 708 |
{.3f, .3f, .3f, .3f}, |
| 709 |
{1.f, .2f, .6f, .9f}, |
| 710 |
{0.f, .1f, 1.f, .1f}, |
| 711 |
{.7f, .8f, .2f, .7f}, |
| 712 |
}; |
| 713 |
auto shader = SkShaders::RadialGradient({100, 100}, 230, |
| 714 |
{{colors, {}, SkTileMode::kRepeat}, {}}); |
| 715 |
SkPaint paint; |
| 716 |
paint.setShader(std::move(shader)); |
| 717 |
|
| 718 |
upmSurf->getCanvas()->drawPaint(paint); |
| 719 |
pmSurf ->getCanvas()->drawPaint(paint); |
| 720 |
|
| 721 |
auto pmImg = pmSurf->makeImageSnapshot(); |
| 722 |
auto upmImg = upmSurf->makeImageSnapshot(); |
| 723 |
|
| 724 |
auto imageOrResult = convert_image_to_source<ReadSource::kImage>(canvas, |
| 725 |
std::move(pmImg), |
| 726 |
errorMsg); |
| 727 |
if (const auto* dr = std::get_if<skiagm::DrawResult>(&imageOrResult)) { |
| 728 |
return *dr; |
| 729 |
} |
| 730 |
pmImg = std::move(std::get<sk_sp<SkImage>>(imageOrResult)); |
| 731 |
|
| 732 |
imageOrResult = convert_image_to_source<ReadSource::kImage>(canvas, |
| 733 |
std::move(upmImg), |
| 734 |
errorMsg); |
| 735 |
if (const auto* dr = std::get_if<skiagm::DrawResult>(&imageOrResult)) { |
| 736 |
return *dr; |
| 737 |
} |
| 738 |
upmImg = std::move(std::get<sk_sp<SkImage>>(imageOrResult)); |
| 739 |
|
| 740 |
int size = 256; |
| 741 |
|
| 742 |
ToolUtils::draw_checkerboard(canvas, SK_ColorWHITE, SK_ColorBLACK, 32); |
| 743 |
|
| 744 |
for (const auto& img : {pmImg, upmImg}) { |
| 745 |
canvas->save(); |
| 746 |
for (auto readAT : {kPremul_SkAlphaType, kUnpremul_SkAlphaType}) { |
| 747 |
auto readInfo = img->imageInfo().makeAlphaType(readAT).makeWH(size, size); |
| 748 |
auto result = |
| 749 |
readAndScaleRGBA<ReadSource::kImage>(img.get(), |
| 750 |
SkIRect::MakeSize(img->dimensions()), |
| 751 |
dContext, |
| 752 |
canvas->recorder(), |
| 753 |
readInfo, |
| 754 |
SkImage::RescaleGamma::kSrc, |
| 755 |
SkImage::RescaleMode::kRepeatedCubic); |
| 756 |
if (!result) { |
| 757 |
*errorMsg = "async readback failed"; |
| 758 |
return skiagm::DrawResult::kFail; |
| 759 |
} |
| 760 |
canvas->drawImage(result, 0, 0); |
| 761 |
canvas->translate(size, 0); |
| 762 |
} |
| 763 |
canvas->restore(); |
| 764 |
canvas->translate(0, size); |
| 765 |
} |
| 766 |
return skiagm::DrawResult::kOk; |
| 767 |
} |
| 768 |
}; |
| 769 |
} // namespace |
| 770 |
|
| 771 |
DEF_GM(return new AsyncRescaleAndReadAlphaTypeGM();) |