Flutter Engine
The Flutter Engine
Functions
ReadWritePixelsGpuTest.cpp File Reference
#include "include/core/SkAlphaType.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkColorType.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPixmap.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSamplingOptions.h"
#include "include/core/SkScalar.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTileMode.h"
#include "include/core/SkTypes.h"
#include "include/effects/SkGradientShader.h"
#include "include/gpu/GpuTypes.h"
#include "include/gpu/GrBackendSurface.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/GrRecordingContext.h"
#include "include/gpu/GrTypes.h"
#include "include/gpu/ganesh/SkImageGanesh.h"
#include "include/gpu/ganesh/SkSurfaceGanesh.h"
#include "include/private/base/SkTArray.h"
#include "include/private/gpu/ganesh/GrTypesPriv.h"
#include "src/base/SkRectMemcpy.h"
#include "src/core/SkAutoPixmapStorage.h"
#include "src/core/SkImageInfoPriv.h"
#include "src/gpu/SkBackingFit.h"
#include "src/gpu/ganesh/GrCaps.h"
#include "src/gpu/ganesh/GrDataUtils.h"
#include "src/gpu/ganesh/GrDirectContextPriv.h"
#include "src/gpu/ganesh/GrFragmentProcessor.h"
#include "src/gpu/ganesh/GrImageInfo.h"
#include "src/gpu/ganesh/GrPixmap.h"
#include "src/gpu/ganesh/GrSamplerState.h"
#include "src/gpu/ganesh/GrSurfaceProxy.h"
#include "src/gpu/ganesh/SurfaceContext.h"
#include "src/gpu/ganesh/SurfaceFillContext.h"
#include "src/gpu/ganesh/effects/GrTextureEffect.h"
#include "tests/CtsEnforcement.h"
#include "tests/Test.h"
#include "tests/TestUtils.h"
#include "tools/ToolUtils.h"
#include "tools/gpu/BackendSurfaceFactory.h"
#include "tools/gpu/BackendTextureImageFactory.h"
#include "tools/gpu/ContextType.h"
#include <algorithm>
#include <array>
#include <cstring>
#include <functional>
#include <initializer_list>
#include <memory>
#include <utility>
#include <vector>

Go to the source code of this file.

Functions

static constexpr int min_rgb_channel_bits (SkColorType ct)
 
static constexpr int alpha_channel_bits (SkColorType ct)
 
std::vector< SkIRectmake_long_rect_array (int w, int h)
 
std::vector< SkIRectmake_short_rect_array (int w, int h)
 
SkPixmap make_pixmap_have_valid_alpha_type (SkPixmap pm)
 
static SkAutoPixmapStorage make_ref_data (const SkImageInfo &info, bool forceOpaque)
 
template<typename T >
static void gpu_read_pixels_test_driver (skiatest::Reporter *reporter, const GpuReadPixelTestRules &rules, const std::function< GpuSrcFactory< T > > &srcFactory, const std::function< GpuReadSrcFn< T > > &read, SkString label)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (SurfaceContextReadPixels, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 
 DEF_GANESH_TEST_FOR_ALL_CONTEXTS (ReadPixels_InvalidRowBytes_Gpu, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 
 DEF_GANESH_TEST_FOR_ALL_CONTEXTS (WritePixels_InvalidRowBytes_Gpu, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 
static void async_callback (void *c, std::unique_ptr< const SkImage::AsyncReadResult > result)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (SurfaceAsyncReadPixels, reporter, ctxInfo, CtsEnforcement::kApiLevel_V)
 
static void image_async_read_pixels (GrRenderable renderable, GrSurfaceOrigin origin, skiatest::Reporter *reporter, const sk_gpu_test::ContextInfo &ctxInfo)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (ImageAsyncReadPixels_NonRenderable_TopLeft, reporter, ctxInfo, CtsEnforcement::kApiLevel_V)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (ImageAsyncReadPixels_NonRenderable_BottomLeft, reporter, ctxInfo, CtsEnforcement::kApiLevel_V)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (ImageAsyncReadPixels_Renderable_TopLeft, reporter, ctxInfo, CtsEnforcement::kApiLevel_V)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (ImageAsyncReadPixels_Renderable_BottomLeft, reporter, ctxInfo, CtsEnforcement::kApiLevel_V)
 
 DEF_GANESH_TEST (AsyncReadPixelsContextShutdown, reporter, options, CtsEnforcement::kApiLevel_T)
 
template<typename T >
static void gpu_write_pixels_test_driver (skiatest::Reporter *reporter, const std::function< GpuDstFactory< T > > &dstFactory, const std::function< GpuWriteDstFn< T > > &write, const std::function< GpuReadDstFn< T > > &read)
 
static void surface_context_write_pixels (GrRenderable renderable, GrSurfaceOrigin origin, skiatest::Reporter *reporter, const sk_gpu_test::ContextInfo &ctxInfo)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (SurfaceContextWritePixels_NonRenderable_TopLeft, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (SurfaceContextWritePixels_NonRenderable_BottomLeft, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (SurfaceContextWritePixels_Renderable_TopLeft, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (SurfaceContextWritePixels_Renderable_BottomLeft, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (SurfaceContextWritePixelsMipped, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 
 DEF_GANESH_TEST_FOR_GL_CONTEXT (GLReadPixelsUnbindPBO, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 

Function Documentation

◆ alpha_channel_bits()

static constexpr int alpha_channel_bits ( SkColorType  ct)
staticconstexpr

Definition at line 107 of file ReadWritePixelsGpuTest.cpp.

107 {
108 switch (ct) {
109 case kUnknown_SkColorType: return 0;
110 case kAlpha_8_SkColorType: return 8;
111 case kA16_unorm_SkColorType: return 16;
112 case kA16_float_SkColorType: return 16;
113 case kRGB_565_SkColorType: return 0;
114 case kARGB_4444_SkColorType: return 4;
115 case kR8G8_unorm_SkColorType: return 0;
116 case kR16G16_unorm_SkColorType: return 0;
117 case kR16G16_float_SkColorType: return 0;
118 case kRGBA_8888_SkColorType: return 8;
119 case kSRGBA_8888_SkColorType: return 8;
120 case kRGB_888x_SkColorType: return 0;
121 case kBGRA_8888_SkColorType: return 8;
122 case kRGBA_1010102_SkColorType: return 2;
123 case kRGB_101010x_SkColorType: return 0;
124 case kBGRA_1010102_SkColorType: return 2;
125 case kBGR_101010x_SkColorType: return 0;
126 case kBGR_101010x_XR_SkColorType: return 0;
127 case kRGBA_10x6_SkColorType: return 10;
128 case kBGRA_10101010_XR_SkColorType: return 10;
129 case kGray_8_SkColorType: return 0;
130 case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa
131 case kRGBA_F16_SkColorType: return 10; // just counting the mantissa
132 case kRGBA_F32_SkColorType: return 23; // just counting the mantissa
133 case kR16G16B16A16_unorm_SkColorType: return 16;
134 case kR8_unorm_SkColorType: return 0;
135 }
137}
#define SkUNREACHABLE
Definition: SkAssert.h:135
@ kR16G16B16A16_unorm_SkColorType
pixel with a little endian uint16_t for red, green, blue
Definition: SkColorType.h:50
@ kRGBA_10x6_SkColorType
pixel with 10 used bits (most significant) followed by 6 unused
Definition: SkColorType.h:33
@ kR8_unorm_SkColorType
Definition: SkColorType.h:54
@ kBGR_101010x_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word
Definition: SkColorType.h:30
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition: SkColorType.h:23
@ kR8G8_unorm_SkColorType
pixel with a uint8_t for red and green
Definition: SkColorType.h:43
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kA16_unorm_SkColorType
pixel with a little endian uint16_t for alpha
Definition: SkColorType.h:48
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:38
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition: SkColorType.h:21
@ kRGB_101010x_SkColorType
pixel with 10 bits each for red, green, blue; in 32-bit word
Definition: SkColorType.h:29
@ kSRGBA_8888_SkColorType
Definition: SkColorType.h:53
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition: SkColorType.h:35
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition: SkColorType.h:22
@ kBGRA_10101010_XR_SkColorType
pixel with 10 bits each for blue, green, red, alpha; in 64-bit word, extended range
Definition: SkColorType.h:32
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
@ kRGB_888x_SkColorType
pixel with 8 bits each for red, green, blue; in 32-bit word
Definition: SkColorType.h:25
@ kBGRA_1010102_SkColorType
10 bits for blue, green, red; 2 bits for alpha; in 32-bit word
Definition: SkColorType.h:28
@ kA16_float_SkColorType
pixel with a half float for alpha
Definition: SkColorType.h:45
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
Definition: SkColorType.h:40
@ kRGBA_1010102_SkColorType
10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
Definition: SkColorType.h:27
@ kBGR_101010x_XR_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word, extended range
Definition: SkColorType.h:31
@ kR16G16_unorm_SkColorType
pixel with a little endian uint16_t for red and green
Definition: SkColorType.h:49
@ kRGBA_F16Norm_SkColorType
pixel with half floats in [0,1] for red, green, blue, alpha;
Definition: SkColorType.h:36
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20
@ kR16G16_float_SkColorType
pixel with a half float for red and green
Definition: SkColorType.h:46

◆ async_callback()

static void async_callback ( void *  c,
std::unique_ptr< const SkImage::AsyncReadResult result 
)
static

Definition at line 662 of file ReadWritePixelsGpuTest.cpp.

662 {
663 auto context = static_cast<AsyncContext*>(c);
664 context->fResult = std::move(result);
665 context->fCalled = true;
666}
GAsyncResult * result

◆ DEF_GANESH_TEST()

DEF_GANESH_TEST ( AsyncReadPixelsContextShutdown  ,
reporter  ,
options  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 829 of file ReadWritePixelsGpuTest.cpp.

829 {
832 enum class ShutdownSequence {
833 kFreeResult_DestroyContext,
834 kDestroyContext_FreeResult,
835 kFreeResult_ReleaseAndAbandon_DestroyContext,
836 kFreeResult_Abandon_DestroyContext,
837 kReleaseAndAbandon_FreeResult_DestroyContext,
838 kAbandon_FreeResult_DestroyContext,
839 kReleaseAndAbandon_DestroyContext_FreeResult,
840 kAbandon_DestroyContext_FreeResult,
841 };
842 for (int t = 0; t < skgpu::kContextTypeCount; ++t) {
843 auto type = static_cast<skgpu::ContextType>(t);
844 for (auto sequence : {ShutdownSequence::kFreeResult_DestroyContext,
845 ShutdownSequence::kDestroyContext_FreeResult,
846 ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext,
847 ShutdownSequence::kFreeResult_Abandon_DestroyContext,
848 ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext,
849 ShutdownSequence::kAbandon_FreeResult_DestroyContext,
850 ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult,
851 ShutdownSequence::kAbandon_DestroyContext_FreeResult}) {
852 // Vulkan and D3D context abandoning without resource release has issues outside of the
853 // scope of this test.
855 (sequence == ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext ||
856 sequence == ShutdownSequence::kFreeResult_Abandon_DestroyContext ||
857 sequence == ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext ||
858 sequence == ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult ||
859 sequence == ShutdownSequence::kAbandon_FreeResult_DestroyContext ||
860 sequence == ShutdownSequence::kAbandon_DestroyContext_FreeResult)) {
861 continue;
862 }
863 enum class ReadType {
864 kRGBA,
865 kYUV,
866 kYUVA
867 };
868 for (ReadType readType : {ReadType::kRGBA, ReadType::kYUV, ReadType::kYUVA}) {
870 auto direct = factory.get(type);
871 if (!direct) {
872 continue;
873 }
874 // This test is only meaningful for contexts that support transfer buffers for
875 // reads.
876 if (!direct->priv().caps()->transferFromSurfaceToBufferSupport()) {
877 continue;
878 }
879 auto surf = SkSurfaces::RenderTarget(direct, skgpu::Budgeted::kYes, ii, 1, nullptr);
880 if (!surf) {
881 continue;
882 }
883 AsyncContext cbContext;
884 switch (readType) {
885 case ReadType::kRGBA:
886 surf->asyncRescaleAndReadPixels(ii, ii.bounds(),
889 &async_callback, &cbContext);
890 break;
891 case ReadType::kYUV:
892 surf->asyncRescaleAndReadPixelsYUV420(
894 ii.dimensions(), SkImage::RescaleGamma::kSrc,
896 break;
897 case ReadType::kYUVA:
898 surf->asyncRescaleAndReadPixelsYUVA420(
900 ii.dimensions(), SkImage::RescaleGamma::kSrc,
902 break;
903 }
904
905 direct->submit();
906 while (!cbContext.fCalled) {
907 direct->checkAsyncWorkCompletion();
908 }
909 if (!cbContext.fResult) {
910 const char* readTypeStr;
911 switch (readType) {
912 case ReadType::kRGBA: readTypeStr = "rgba"; break;
913 case ReadType::kYUV: readTypeStr = "yuv"; break;
914 case ReadType::kYUVA: readTypeStr = "yuva"; break;
915 }
916 ERRORF(reporter, "Callback failed on %s. read type is: %s",
917 skgpu::ContextTypeName(type), readTypeStr);
918 continue;
919 }
920 // For vulkan we need to release all refs to the GrDirectContext before trying to
921 // destroy the test context. The surface here is holding a ref.
922 surf.reset();
923
924 // The real test is that we don't crash, get Vulkan validation errors, etc, during
925 // this shutdown sequence.
926 switch (sequence) {
927 case ShutdownSequence::kFreeResult_DestroyContext:
928 case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext:
929 case ShutdownSequence::kFreeResult_Abandon_DestroyContext:
930 break;
931 case ShutdownSequence::kDestroyContext_FreeResult:
932 factory.destroyContexts();
933 break;
934 case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext:
935 factory.releaseResourcesAndAbandonContexts();
936 break;
937 case ShutdownSequence::kAbandon_FreeResult_DestroyContext:
938 factory.abandonContexts();
939 break;
940 case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult:
941 factory.releaseResourcesAndAbandonContexts();
942 factory.destroyContexts();
943 break;
944 case ShutdownSequence::kAbandon_DestroyContext_FreeResult:
945 factory.abandonContexts();
946 factory.destroyContexts();
947 break;
948 }
949 cbContext.fResult.reset();
950 switch (sequence) {
951 case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext:
952 factory.releaseResourcesAndAbandonContexts();
953 break;
954 case ShutdownSequence::kFreeResult_Abandon_DestroyContext:
955 factory.abandonContexts();
956 break;
957 case ShutdownSequence::kFreeResult_DestroyContext:
958 case ShutdownSequence::kDestroyContext_FreeResult:
959 case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext:
960 case ShutdownSequence::kAbandon_FreeResult_DestroyContext:
961 case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult:
962 case ShutdownSequence::kAbandon_DestroyContext_FreeResult:
963 break;
964 }
965 }
966 }
967 }
968}
const char * options
reporter
Definition: FontMgrTest.cpp:39
static void async_callback(void *c, std::unique_ptr< const SkImage::AsyncReadResult > result)
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kIdentity_SkYUVColorSpace
maps Y->R, U->G, V->B
Definition: SkImageInfo.h:93
#define ERRORF(r,...)
Definition: Test.h:293
GLenum type
static sk_sp< SkColorSpace > MakeSRGB()
@ kRGBA
Definition: embedder.h:607
@ kYUVA
Definition: embedder.h:606
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
static const int kContextTypeCount
Definition: ContextType.h:42
ContextType
Definition: ContextType.h:19
@ kVulkan
ANGLE on Metal ES 3 context.
const char * ContextTypeName(skgpu::ContextType type)
Definition: ContextType.cpp:13
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)

◆ DEF_GANESH_TEST_FOR_ALL_CONTEXTS() [1/2]

DEF_GANESH_TEST_FOR_ALL_CONTEXTS ( ReadPixels_InvalidRowBytes_Gpu  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 611 of file ReadWritePixelsGpuTest.cpp.

614 {
616 auto surf = SkSurfaces::RenderTarget(ctxInfo.directContext(), skgpu::Budgeted::kYes, srcII);
617 for (int ct = 0; ct < kLastEnum_SkColorType + 1; ++ct) {
618 auto colorType = static_cast<SkColorType>(ct);
620 if (bpp <= 1) {
621 continue;
622 }
623 auto dstII = srcII.makeColorType(colorType);
624 size_t badRowBytes = (surf->width() + 1)*bpp - 1;
625 auto storage = std::make_unique<char[]>(badRowBytes*surf->height());
626 REPORTER_ASSERT(reporter, !surf->readPixels(dstII, storage.get(), badRowBytes, 0, 0));
627 }
628}
SkColorType
Definition: SkColorType.h:19
@ kLastEnum_SkColorType
last valid value
Definition: SkColorType.h:56
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
SK_API int SkColorTypeBytesPerPixel(SkColorType ct)
Definition: SkImageInfo.cpp:16
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286

◆ DEF_GANESH_TEST_FOR_ALL_CONTEXTS() [2/2]

DEF_GANESH_TEST_FOR_ALL_CONTEXTS ( WritePixels_InvalidRowBytes_Gpu  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 630 of file ReadWritePixelsGpuTest.cpp.

633 {
635 auto surf = SkSurfaces::RenderTarget(ctxInfo.directContext(), skgpu::Budgeted::kYes, dstII);
636 for (int ct = 0; ct < kLastEnum_SkColorType + 1; ++ct) {
637 auto colorType = static_cast<SkColorType>(ct);
639 if (bpp <= 1) {
640 continue;
641 }
642 auto srcII = dstII.makeColorType(colorType);
643 size_t badRowBytes = (surf->width() + 1)*bpp - 1;
644 auto storage = std::make_unique<char[]>(badRowBytes*surf->height());
645 memset(storage.get(), 0, badRowBytes * surf->height());
646 // SkSurface::writePixels doesn't report bool, SkCanvas's does.
648 !surf->getCanvas()->writePixels(srcII, storage.get(), badRowBytes, 0, 0));
649 }
650}

◆ DEF_GANESH_TEST_FOR_GL_CONTEXT()

DEF_GANESH_TEST_FOR_GL_CONTEXT ( GLReadPixelsUnbindPBO  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 1444 of file ReadWritePixelsGpuTest.cpp.

1447 {
1448 // Start with a async read so that we bind to GL_PIXEL_PACK_BUFFER.
1450 SkAutoPixmapStorage pmap = make_ref_data(info, /*forceOpaque=*/false);
1451 auto image = SkImages::RasterFromPixmap(pmap, nullptr, nullptr);
1452 image = SkImages::TextureFromImage(ctxInfo.directContext(), image);
1453 if (!image) {
1454 ERRORF(reporter, "Couldn't make texture image.");
1455 return;
1456 }
1457
1458 AsyncContext asyncContext;
1460 SkIRect::MakeSize(info.dimensions()),
1464 &asyncContext);
1465
1466 // This will force the async readback to finish.
1467 ctxInfo.directContext()->flushAndSubmit(GrSyncCpu::kYes);
1468 if (!asyncContext.fCalled) {
1469 ERRORF(reporter, "async_callback not called.");
1470 }
1471 if (!asyncContext.fResult) {
1472 ERRORF(reporter, "async read failed.");
1473 }
1474
1475 SkPixmap asyncResult(info, asyncContext.fResult->data(0), asyncContext.fResult->rowBytes(0));
1476
1477 // Bug was that this would cause GrGLGpu to think no buffer was left bound to
1478 // GL_PIXEL_PACK_BUFFER even though async transfer did leave one bound. So the sync read
1479 // wouldn't bind buffer 0.
1480 ctxInfo.directContext()->resetContext();
1481
1482 SkBitmap syncResult;
1483 syncResult.allocPixels(info);
1484 syncResult.eraseARGB(0xFF, 0xFF, 0xFF, 0xFF);
1485
1486 image->readPixels(ctxInfo.directContext(), syncResult.pixmap(), 0, 0);
1487
1488 float tol[4] = {}; // expect exactly same pixels, no conversions.
1489 auto error = std::function<ComparePixmapsErrorReporter>([&](int x, int y,
1490 const float diffs[4]) {
1491 SkASSERT(x >= 0 && y >= 0);
1492 ERRORF(reporter, "Expect sync and async read to be the same. "
1493 "Error at %d, %d. Diff in floats: (%f, %f, %f, %f)",
1494 x, y, diffs[0], diffs[1], diffs[2], diffs[3]);
1495 });
1496
1497 ComparePixels(syncResult.pixmap(), asyncResult, tol, error);
1498}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
static SkAutoPixmapStorage make_ref_data(const SkImageInfo &info, bool forceOpaque)
#define SkASSERT(cond)
Definition: SkAssert.h:116
bool ComparePixels(const GrCPixmap &a, const GrCPixmap &b, const float tolRGBA[4], std::function< ComparePixmapsErrorReporter > &error)
Definition: TestUtils.cpp:142
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const
Definition: SkBitmap.h:817
const SkPixmap & pixmap() const
Definition: SkBitmap.h:133
bool readPixels(GrDirectContext *context, const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY, CachingHint cachingHint=kAllow_CachingHint) const
Definition: SkImage.cpp:42
void asyncRescaleAndReadPixels(const SkImageInfo &info, const SkIRect &srcRect, RescaleGamma rescaleGamma, RescaleMode rescaleMode, ReadPixelsCallback callback, ReadPixelsContext context) const
Definition: SkImage.cpp:64
const uint8_t uint32_t uint32_t GError ** error
double y
double x
SK_API sk_sp< SkImage > RasterFromPixmap(const SkPixmap &pixmap, RasterReleaseProc rasterReleaseProc, ReleaseContext releaseContext)
SK_API sk_sp< SkImage > TextureFromImage(GrDirectContext *, const SkImage *, skgpu::Mipmapped=skgpu::Mipmapped::kNo, skgpu::Budgeted=skgpu::Budgeted::kYes)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
static constexpr SkIRect MakeSize(const SkISize &size)
Definition: SkRect.h:66

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [1/11]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( ImageAsyncReadPixels_NonRenderable_BottomLeft  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_V   
)

Definition at line 805 of file ReadWritePixelsGpuTest.cpp.

808 {
810 reporter, ctxInfo);
811}
@ kBottomLeft_GrSurfaceOrigin
Definition: GrTypes.h:149
static void image_async_read_pixels(GrRenderable renderable, GrSurfaceOrigin origin, skiatest::Reporter *reporter, const sk_gpu_test::ContextInfo &ctxInfo)

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [2/11]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( ImageAsyncReadPixels_NonRenderable_TopLeft  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_V   
)

Definition at line 797 of file ReadWritePixelsGpuTest.cpp.

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [3/11]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( ImageAsyncReadPixels_Renderable_BottomLeft  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_V   
)

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [4/11]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( ImageAsyncReadPixels_Renderable_TopLeft  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_V   
)

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [5/11]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( SurfaceAsyncReadPixels  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_V   
)

Definition at line 668 of file ReadWritePixelsGpuTest.cpp.

671 {
672 using Surface = sk_sp<SkSurface>;
673 auto reader = std::function<GpuReadSrcFn<Surface>>(
674 [](const Surface& surface, const SkIPoint& offset, const SkPixmap& pixels) {
675 auto direct = surface->recordingContext()->asDirectContext();
676 SkASSERT(direct);
677
678 AsyncContext context;
679 auto rect = SkIRect::MakeSize(pixels.dimensions()).makeOffset(offset);
680
681 // Rescale quality and linearity don't matter since we're doing a non-scaling
682 // readback.
683 surface->asyncRescaleAndReadPixels(pixels.info(), rect,
686 async_callback, &context);
687 direct->submit();
688 while (!context.fCalled) {
689 direct->checkAsyncWorkCompletion();
690 }
691 if (!context.fResult) {
692 return Result::kFail;
693 }
694 SkRectMemcpy(pixels.writable_addr(), pixels.rowBytes(), context.fResult->data(0),
695 context.fResult->rowBytes(0), pixels.info().minRowBytes(),
696 pixels.height());
697 return Result::kSuccess;
698 });
699 GpuReadPixelTestRules rules;
700 rules.fAllowUnpremulSrc = false;
701 rules.fUncontainedRectSucceeds = false;
702 // TODO: some mobile GPUs have issues reading back sRGB src data with GLES -- skip for now
703 // b/296440036
704 if (ctxInfo.type() == skgpu::ContextType::kGLES) {
705 rules.fSkipSRGBCT = true;
706 }
707
709 auto factory = std::function<GpuSrcFactory<Surface>>(
710 [context = ctxInfo.directContext(), origin](const SkPixmap& src) {
711 auto surf = SkSurfaces::RenderTarget(
712 context, skgpu::Budgeted::kYes, src.info(), 1, origin, nullptr);
713 if (surf) {
714 surf->writePixels(src, 0, 0);
715 }
716 return surf;
717 });
718 auto label = SkStringPrintf("Origin: %d", origin);
719 gpu_read_pixels_test_driver(reporter, rules, factory, reader, label);
720 auto backendRTFactory = std::function<GpuSrcFactory<Surface>>(
721 [context = ctxInfo.directContext(), origin](const SkPixmap& src) {
723 src.info(),
724 origin,
725 1);
726 if (surf) {
727 surf->writePixels(src, 0, 0);
728 }
729 return surf;
730 });
731 label = SkStringPrintf("BERT Origin: %d", origin);
732 gpu_read_pixels_test_driver(reporter, rules, backendRTFactory, reader, label);
733 }
734}
GrSurfaceOrigin
Definition: GrTypes.h:147
static void gpu_read_pixels_test_driver(skiatest::Reporter *reporter, const GpuReadPixelTestRules &rules, const std::function< GpuSrcFactory< T > > &srcFactory, const std::function< GpuReadSrcFn< T > > &read, SkString label)
static void SkRectMemcpy(void *dst, size_t dstRB, const void *src, size_t srcRB, size_t trimRowBytes, int rowCount)
Definition: SkRectMemcpy.h:16
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
@ kSuccess
Definition: embedder.h:73
VkSurfaceKHR surface
Definition: main.cc:49
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
sk_sp< SkSurface > MakeBackendRenderTargetSurface(GrDirectContext *dContext, const SkImageInfo &ii, GrSurfaceOrigin origin, int sampleCnt, GrProtected isProtected, const SkSurfaceProps *props)
@ kGLES
OpenGL context.
SeparatedVector2 offset
constexpr SkIRect makeOffset(int32_t dx, int32_t dy) const
Definition: SkRect.h:300

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [6/11]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( SurfaceContextReadPixels  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 569 of file ReadWritePixelsGpuTest.cpp.

572 {
573 using Surface = std::unique_ptr<skgpu::ganesh::SurfaceContext>;
574 GrDirectContext* direct = ctxInfo.directContext();
575 auto reader = std::function<GpuReadSrcFn<Surface>>(
576 [direct](const Surface& surface, const SkIPoint& offset, const SkPixmap& pixels) {
577 if (surface->readPixels(direct, pixels, offset)) {
578 return Result::kSuccess;
579 } else {
580 // Reading from a non-renderable format is not guaranteed to work on GL.
581 // We'd have to be able to force a copy or draw to a renderable format.
582 const auto& caps = *direct->priv().caps();
583 if (direct->backend() == GrBackendApi::kOpenGL &&
584 !caps.isFormatRenderable(surface->asSurfaceProxy()->backendFormat(), 1)) {
585 return Result::kExcusedFailure;
586 }
587 return Result::kFail;
588 }
589 });
590 GpuReadPixelTestRules rules;
591 rules.fAllowUnpremulSrc = true;
592 rules.fUncontainedRectSucceeds = true;
593
594 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
596 auto factory = std::function<GpuSrcFactory<Surface>>(
597 [direct, origin, renderable](const SkPixmap& src) {
598 auto sc = CreateSurfaceContext(
599 direct, src.info(), SkBackingFit::kExact, origin, renderable);
600 if (sc) {
601 sc->writePixels(direct, src, {0, 0});
602 }
603 return sc;
604 });
605 auto label = SkStringPrintf("Renderable: %d, Origin: %d", (int)renderable, origin);
606 gpu_read_pixels_test_driver(reporter, rules, factory, reader, label);
607 }
608 }
609}
std::unique_ptr< skgpu::ganesh::SurfaceContext > CreateSurfaceContext(GrRecordingContext *rContext, const GrImageInfo &info, SkBackingFit fit, GrSurfaceOrigin origin, GrRenderable renderable, int sampleCount, skgpu::Mipmapped mipmapped, GrProtected isProtected, skgpu::Budgeted budgeted)
Definition: TestUtils.cpp:242
const GrCaps * caps() const
SK_API GrBackendApi backend() const
GrDirectContextPriv priv()

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [7/11]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( SurfaceContextWritePixels_NonRenderable_BottomLeft  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 1270 of file ReadWritePixelsGpuTest.cpp.

1273 {
1275 reporter, ctxInfo);
1276}
static void surface_context_write_pixels(GrRenderable renderable, GrSurfaceOrigin origin, skiatest::Reporter *reporter, const sk_gpu_test::ContextInfo &ctxInfo)

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [8/11]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( SurfaceContextWritePixels_NonRenderable_TopLeft  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [9/11]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( SurfaceContextWritePixels_Renderable_BottomLeft  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [10/11]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( SurfaceContextWritePixels_Renderable_TopLeft  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [11/11]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( SurfaceContextWritePixelsMipped  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 1294 of file ReadWritePixelsGpuTest.cpp.

1297 {
1298 auto direct = ctxInfo.directContext();
1299 if (!direct->priv().caps()->mipmapSupport()) {
1300 return;
1301 }
1302 static constexpr int kW = 25,
1303 kH = 37;
1307 nullptr),
1308 false);
1312 nullptr),
1313 true);
1314
1315 for (int c = 0; c < kGrColorTypeCnt; ++c) {
1316 auto ct = static_cast<GrColorType>(c);
1317 // Below we use rendering to read the level pixels back.
1318 auto format = direct->priv().caps()->getDefaultBackendFormat(ct, GrRenderable::kYes);
1319 if (!format.isValid()) {
1320 continue;
1321 }
1323 GrImageInfo info(ct, at, nullptr, kW, kH);
1324 TArray<GrCPixmap> levels;
1325 const auto& ref = at == kPremul_SkAlphaType ? refP : refO;
1326 for (int w = kW, h = kH; w || h; w/=2, h/=2) {
1327 auto level = GrPixmap::Allocate(info.makeWH(std::max(w, 1), std::max(h, 1)));
1328 SkPixmap src;
1329 SkAssertResult(ref.extractSubset(&src, SkIRect::MakeSize(level.dimensions())));
1331 levels.push_back(level);
1332 }
1333
1334 for (bool unowned : {false, true}) { // test a GrCPixmap that doesn't own its storage.
1335 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
1338 auto sc = CreateSurfaceContext(direct,
1339 info,
1341 origin,
1342 renderable,
1343 /*sample count*/ 1,
1345 if (!sc) {
1346 continue;
1347 }
1348 // Keeps pixels in unowned case alive until after writePixels is called but no
1349 // longer.
1350 GrPixmap keepAlive;
1351 GrCPixmap savedLevel = levels[1];
1352 if (unowned) {
1353 // Also test non-tight row bytes with the unowned pixmap, bump width by 1.
1354 int w = levels[1].width() + 1;
1355 int h = levels[1].height();
1356 keepAlive = GrPixmap::Allocate(levels[1].info().makeWH(w, h));
1357 SkPixmap src;
1358 // These pixel values will be the same as the original level 1.
1359 SkAssertResult(ref.extractSubset(&src, SkIRect::MakeWH(w, h)));
1360 SkAssertResult(GrConvertPixels(keepAlive, src));
1361 levels[1] = GrCPixmap(levels[1].info(),
1362 keepAlive.addr(),
1363 keepAlive.rowBytes());
1364 }
1365 // Going through intermediate textures is not supported for MIP levels (because
1366 // we don't support rendering to non-base levels). So it's hard to have any hard
1367 // rules about when we expect success.
1368 if (!sc->writePixels(direct, levels.begin(), levels.size())) {
1369 continue;
1370 }
1371 // Make sure the pixels from the unowned pixmap are released and then put the
1372 // original level back in for the comparison after the read below.
1373 keepAlive = {};
1374 levels[1] = savedLevel;
1375
1376 // TODO: Update this when read pixels supports reading back levels to read
1377 // directly rather than using minimizing draws.
1378 auto dstSC = CreateSurfaceContext(direct,
1379 info,
1383 SkASSERT(dstSC);
1385 for (int i = 1; i <= 1; ++i) {
1386 auto te = GrTextureEffect::Make(sc->readSurfaceView(),
1387 info.alphaType(),
1388 SkMatrix::I(),
1389 sampler,
1390 *direct->priv().caps());
1391 dstSC->asFillContext()->fillRectToRectWithFP(
1392 SkIRect::MakeSize(sc->dimensions()),
1393 SkIRect::MakeSize(levels[i].dimensions()),
1394 std::move(te));
1395 GrImageInfo readInfo =
1396 dstSC->imageInfo().makeDimensions(levels[i].dimensions());
1397 GrPixmap read = GrPixmap::Allocate(readInfo);
1398 if (!dstSC->readPixels(direct, read, {0, 0})) {
1399 continue;
1400 }
1401
1402 auto skCT = GrColorTypeToSkColorType(info.colorType());
1403 int rgbBits = std::min(min_rgb_channel_bits(skCT), 8);
1404 float rgbTol = (rgbBits == 0) ? 1.f : 2.f / ((1 << rgbBits) - 1);
1405 int alphaBits = std::min(alpha_channel_bits(skCT), 8);
1406 float alphaTol = (alphaBits == 0) ? 1.f : 2.f / ((1 << alphaBits) - 1);
1407 float tol[] = {rgbTol, rgbTol, rgbTol, alphaTol};
1408
1409 GrCPixmap a = levels[i];
1410 GrCPixmap b = read;
1411 // The compare code will linearize when reading the srgb data. This will
1412 // magnify differences at the high end. Rather than adjusting the tolerance
1413 // to compensate we do the comparison without going through srgb->linear.
1414 if (ct == GrColorType::kRGBA_8888_SRGB) {
1415 a = GrCPixmap(a.info().makeColorType(GrColorType::kRGBA_8888),
1416 a.addr(),
1417 a.rowBytes());
1418 b = GrCPixmap(b.info().makeColorType(GrColorType::kRGBA_8888),
1419 b.addr(),
1420 b.rowBytes());
1421 }
1422
1423 auto error = std::function<ComparePixmapsErrorReporter>(
1424 [&](int x, int y, const float diffs[4]) {
1425 SkASSERT(x >= 0 && y >= 0);
1427 "CT: %s, Level %d, Unowned: %d. "
1428 "Error at %d, %d. Diff in floats:"
1429 "(%f, %f, %f, %f)",
1430 GrColorTypeToStr(info.colorType()), i, unowned, x, y,
1431 diffs[0], diffs[1], diffs[2], diffs[3]);
1432 });
1433 ComparePixels(a, b, tol, error);
1434 }
1435 }
1436 }
1437 }
1438 }
1439}
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
bool GrConvertPixels(const GrPixmap &dst, const GrCPixmap &src, bool flipY)
static const int kGrColorTypeCnt
Definition: GrTypesPriv.h:587
static constexpr SkColorType GrColorTypeToSkColorType(GrColorType ct)
Definition: GrTypesPriv.h:589
GrColorType
Definition: GrTypesPriv.h:540
static constexpr bool GrColorTypeHasAlpha(GrColorType ct)
Definition: GrTypesPriv.h:892
static constexpr int alpha_channel_bits(SkColorType ct)
static constexpr int min_rgb_channel_bits(SkColorType ct)
SkAlphaType
Definition: SkAlphaType.h:26
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
static bool read(SkStream *stream, void *buffer, size_t amount)
constexpr int kH
constexpr int kW
GrImageInfo makeDimensions(SkISize dimensions) const
Definition: GrImageInfo.cpp:50
size_t rowBytes() const
Definition: GrPixmap.h:21
T * addr() const
Definition: GrPixmap.h:20
static GrPixmap Allocate(const GrImageInfo &info)
Definition: GrPixmap.h:101
static std::unique_ptr< GrFragmentProcessor > Make(GrSurfaceProxyView, SkAlphaType, const SkMatrix &=SkMatrix::I(), GrSamplerState::Filter=GrSamplerState::Filter::kNearest, GrSamplerState::MipmapMode mipmapMode=GrSamplerState::MipmapMode::kNone)
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
int size() const
Definition: SkTArray.h:421
static bool b
struct MyStruct a[10]
uint32_t uint32_t * format
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
SkScalar w
SkScalar h
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56

◆ gpu_read_pixels_test_driver()

template<typename T >
static void gpu_read_pixels_test_driver ( skiatest::Reporter reporter,
const GpuReadPixelTestRules &  rules,
const std::function< GpuSrcFactory< T > > &  srcFactory,
const std::function< GpuReadSrcFn< T > > &  read,
SkString  label 
)
static

Definition at line 290 of file ReadWritePixelsGpuTest.cpp.

294 {
295 if (!label.isEmpty()) {
296 // Add space for printing.
297 label.append(" ");
298 }
299 // Separate this out just to give it some line width to breathe. Note 'srcPixels' should have
300 // the same image info as src. We will do a converting readPixels() on it to get the data
301 // to compare with the results of 'read'.
302 auto runTest = [&](const T& src,
303 const SkPixmap& srcPixels,
304 const SkImageInfo& readInfo,
306 const bool csConversion =
307 !SkColorSpace::Equals(readInfo.colorSpace(), srcPixels.info().colorSpace());
308 const auto readCT = readInfo.colorType();
309 const auto readAT = readInfo.alphaType();
310 const auto srcCT = srcPixels.info().colorType();
311 const auto srcAT = srcPixels.info().alphaType();
312 const auto rect = SkIRect::MakeWH(readInfo.width(), readInfo.height()).makeOffset(offset);
313 const auto surfBounds = SkIRect::MakeWH(srcPixels.width(), srcPixels.height());
314 const size_t readBpp = SkColorTypeBytesPerPixel(readCT);
315
316 // Make the row bytes in the dst be loose for extra stress.
317 const size_t dstRB = readBpp * readInfo.width() + 10 * readBpp;
318 // This will make the last row tight.
319 const size_t dstSize = readInfo.computeByteSize(dstRB);
320 std::unique_ptr<char[]> dstData(new char[dstSize]);
321 SkPixmap dstPixels(readInfo, dstData.get(), dstRB);
322 // Initialize with an arbitrary value for each byte. Later we will check that only the
323 // correct part of the destination gets overwritten by 'read'.
324 static constexpr auto kInitialByte = static_cast<char>(0x1B);
325 std::fill_n(static_cast<char*>(dstPixels.writable_addr()),
326 dstPixels.computeByteSize(),
327 kInitialByte);
328
329 const Result result = read(src, offset, dstPixels);
330
331 if (!SkIRect::Intersects(rect, surfBounds)) {
333 } else if (readCT == kUnknown_SkColorType) {
335 } else if ((readAT == kUnknown_SkAlphaType) != (srcAT == kUnknown_SkAlphaType)) {
337 } else if (!rules.fUncontainedRectSucceeds && !surfBounds.contains(rect)) {
339 } else if (result == Result::kFail) {
340 // TODO: Support RGB/BGR 101010x, BGRA 1010102 on the GPU.
343 "Read failed. %sSrc CT: %s, Src AT: %s Read CT: %s, Read AT: %s, "
344 "Rect [%d, %d, %d, %d], CS conversion: %d\n",
345 label.c_str(),
348 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, csConversion);
349 }
350 return result;
351 }
352
353 bool guardOk = true;
354 auto guardCheck = [](char x) { return x == kInitialByte; };
355
356 // Considering the rect we tried to read and the surface bounds figure out which pixels in
357 // both src and dst space should actually have been read and written.
358 SkIRect srcReadRect;
359 if (result == Result::kSuccess && srcReadRect.intersect(surfBounds, rect)) {
360 SkIRect dstWriteRect = srcReadRect.makeOffset(-rect.fLeft, -rect.fTop);
361
362 const bool lumConversion =
365 // A CS or luminance conversion allows a 3 value difference and otherwise a 2 value
366 // difference. Note that sometimes read back on GPU can be lossy even when there no
367 // conversion at all because GPU->CPU read may go to a lower bit depth format and then
368 // be promoted back to the original type. For example, GL ES cannot read to 1010102, so
369 // we go through 8888.
370 float numer = (lumConversion || csConversion) ? 3.f : 2.f;
371 // Allow some extra tolerance if unpremuling.
372 if (srcAT == kPremul_SkAlphaType && readAT == kUnpremul_SkAlphaType) {
373 numer += 1;
374 }
375 int rgbBits = std::min({min_rgb_channel_bits(readCT), min_rgb_channel_bits(srcCT), 8});
376 float tol = (rgbBits == 0) ? 1.f : numer / ((1 << rgbBits) - 1);
377 // Swiftshader is producing alpha errors with 16-bit UNORM. We choose to always allow
378 // a small tolerance:
379 float alphaTol = 1.f / ((1 << 10) - 1);
380 if (readAT != kOpaque_SkAlphaType && srcAT != kOpaque_SkAlphaType) {
381 // Alpha can also get squashed down to 8 bits going through an intermediate
382 // color format.
383 const int alphaBits = std::min({alpha_channel_bits(readCT),
384 alpha_channel_bits(srcCT),
385 8});
386 alphaTol = (alphaBits == 0) ? 1.f : 2.f / ((1 << alphaBits) - 1);
387 }
388
389 const float tols[4] = {tol, tol, tol, alphaTol};
390 auto error = std::function<ComparePixmapsErrorReporter>([&](int x, int y,
391 const float diffs[4]) {
392 SkASSERT(x >= 0 && y >= 0);
394 "%sSrc CT: %s, Src AT: %s, Read CT: %s, Read AT: %s, Rect [%d, %d, %d, %d]"
395 ", CS conversion: %d\n"
396 "Error at %d, %d. Diff in floats: (%f, %f, %f, %f)",
397 label.c_str(),
400 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, csConversion, x, y,
401 diffs[0], diffs[1], diffs[2], diffs[3]);
402 });
404 SkImageInfo refInfo = readInfo.makeDimensions(dstWriteRect.size());
405 ref.alloc(refInfo);
406 if (readAT == kUnknown_SkAlphaType) {
407 // Do a spoofed read where src and dst alpha type are both kUnpremul. This will
408 // allow SkPixmap readPixels to succeed and won't do any alpha type conversion.
409 SkPixmap unpremulRef(refInfo.makeAlphaType(kUnpremul_SkAlphaType),
410 ref.addr(),
411 ref.rowBytes());
412 SkPixmap unpremulSRc(srcPixels.info().makeAlphaType(kUnpremul_SkAlphaType),
413 srcPixels.addr(),
414 srcPixels.rowBytes());
415
416 unpremulSRc.readPixels(unpremulRef, srcReadRect.x(), srcReadRect.y());
417 } else {
418 srcPixels.readPixels(ref, srcReadRect.x(), srcReadRect.y());
419 }
420 // This is the part of dstPixels that should have been updated.
421 SkPixmap actual;
422 SkAssertResult(dstPixels.extractSubset(&actual, dstWriteRect));
423 ComparePixels(ref, actual, tols, error);
424
425 const auto* v = dstData.get();
426 const auto* end = dstData.get() + dstSize;
427 guardOk = std::all_of(v, v + dstWriteRect.top() * dstPixels.rowBytes(), guardCheck);
428 v += dstWriteRect.top() * dstPixels.rowBytes();
429 for (int y = dstWriteRect.top(); y < dstWriteRect.bottom(); ++y) {
430 guardOk |= std::all_of(v, v + dstWriteRect.left() * readBpp, guardCheck);
431 auto pad = v + dstWriteRect.right() * readBpp;
432 auto rowEnd = std::min(end, v + dstPixels.rowBytes());
433 // min protects against reading past the end of the tight last row.
434 guardOk |= std::all_of(pad, rowEnd, guardCheck);
435 v = rowEnd;
436 }
437 guardOk |= std::all_of(v, end, guardCheck);
438 } else {
439 guardOk = std::all_of(dstData.get(), dstData.get() + dstSize, guardCheck);
440 }
441 if (!guardOk) {
443 "Result pixels modified result outside read rect [%d, %d, %d, %d]. "
444 "%sSrc CT: %s, Read CT: %s, CS conversion: %d",
445 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, label.c_str(),
447 csConversion);
448 }
449 return result;
450 };
451
452 static constexpr int kW = 16;
453 static constexpr int kH = 16;
454
455 const std::vector<SkIRect> longRectArray = make_long_rect_array(kW, kH);
456 const std::vector<SkIRect> shortRectArray = make_short_rect_array(kW, kH);
457
458 // We ensure we use the long array once per src and read color type and otherwise use the
459 // short array to improve test run time.
460 // Also, some color types have no alpha values and thus Opaque Premul and Unpremul are
461 // equivalent. Just ensure each redundant AT is tested once with each CT (src and read).
462 // Similarly, alpha-only color types behave the same for all alpha types so just test premul
463 // after one iter.
464 // We consider a src or read CT thoroughly tested once it has run through the long rect array
465 // and full complement of alpha types with one successful read in the loop.
466 std::array<bool, kLastEnum_SkColorType + 1> srcCTTestedThoroughly = {},
467 readCTTestedThoroughly = {};
468 for (int sat = 0; sat <= kLastEnum_SkAlphaType; ++sat) {
469 const auto srcAT = static_cast<SkAlphaType>(sat);
470 if (srcAT == kUnpremul_SkAlphaType && !rules.fAllowUnpremulSrc) {
471 continue;
472 }
473 for (int sct = 0; sct <= kLastEnum_SkColorType; ++sct) {
474 const auto srcCT = static_cast<SkColorType>(sct);
475 if (rules.fSkipSRGBCT && srcCT == kSRGBA_8888_SkColorType) {
476 continue;
477 }
478 if (rules.fSkip16BitCT &&
479 (srcCT == kR16G16_unorm_SkColorType ||
481 continue;
482 }
483
484 // We always make our ref data as F32
485 auto refInfo = SkImageInfo::Make(kW, kH,
487 srcAT,
489 // 1010102 formats have an issue where it's easy to make a resulting
490 // color where r, g, or b is greater than a. CPU/GPU differ in whether the stored color
491 // channels are clipped to the alpha value. CPU clips but GPU does not.
492 // Note that we only currently use srcCT for the 1010102 workaround. If we remove this
493 // we can also put the ref data setup above the srcCT loop.
494 bool forceOpaque = srcAT == kPremul_SkAlphaType &&
496
497 SkAutoPixmapStorage refPixels = make_ref_data(refInfo, forceOpaque);
498 // Convert the ref data to our desired src color type.
499 const auto srcInfo = SkImageInfo::Make(kW, kH, srcCT, srcAT, SkColorSpace::MakeSRGB());
500 SkAutoPixmapStorage srcPixels;
501 srcPixels.alloc(srcInfo);
502 {
503 SkPixmap readPixmap = srcPixels;
504 // Spoof the alpha type to kUnpremul so the read will succeed without doing any
505 // conversion (because we made our surface also use kUnpremul).
506 if (srcAT == kUnknown_SkAlphaType) {
507 readPixmap.reset(srcPixels.info().makeAlphaType(kUnpremul_SkAlphaType),
508 srcPixels.addr(),
509 srcPixels.rowBytes());
510 }
511 refPixels.readPixels(readPixmap, 0, 0);
512 }
513
514 auto src = srcFactory(srcPixels);
515 if (!src) {
516 continue;
517 }
518 if (SkColorTypeIsAlwaysOpaque(srcCT) && srcCTTestedThoroughly[srcCT] &&
519 (kPremul_SkAlphaType == srcAT || kUnpremul_SkAlphaType == srcAT)) {
520 continue;
521 }
522 if (SkColorTypeIsAlphaOnly(srcCT) && srcCTTestedThoroughly[srcCT] &&
523 (kUnpremul_SkAlphaType == srcAT ||
524 kOpaque_SkAlphaType == srcAT ||
525 kUnknown_SkAlphaType == srcAT)) {
526 continue;
527 }
528 for (int rct = 0; rct <= kLastEnum_SkColorType; ++rct) {
529 const auto readCT = static_cast<SkColorType>(rct);
530 for (const sk_sp<SkColorSpace>& readCS :
532 for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) {
533 const auto readAT = static_cast<SkAlphaType>(at);
534 if (srcAT != kOpaque_SkAlphaType && readAT == kOpaque_SkAlphaType) {
535 // This doesn't make sense.
536 continue;
537 }
538 if (SkColorTypeIsAlwaysOpaque(readCT) && readCTTestedThoroughly[readCT] &&
539 (kPremul_SkAlphaType == readAT || kUnpremul_SkAlphaType == readAT)) {
540 continue;
541 }
542 if (SkColorTypeIsAlphaOnly(readCT) && readCTTestedThoroughly[readCT] &&
543 (kUnpremul_SkAlphaType == readAT ||
544 kOpaque_SkAlphaType == readAT ||
545 kUnknown_SkAlphaType == readAT)) {
546 continue;
547 }
548 const auto& rects =
549 srcCTTestedThoroughly[sct] && readCTTestedThoroughly[rct]
550 ? shortRectArray
551 : longRectArray;
552 for (const auto& rect : rects) {
553 const auto readInfo = SkImageInfo::Make(rect.width(), rect.height(),
554 readCT, readAT, readCS);
555 const SkIPoint offset = rect.topLeft();
556 Result r = runTest(src, srcPixels, readInfo, offset);
557 if (r == Result::kSuccess) {
558 srcCTTestedThoroughly[sct] = true;
559 readCTTestedThoroughly[rct] = true;
560 }
561 }
562 }
563 }
564 }
565 }
566 }
567}
static constexpr GrColorType SkColorTypeToGrColorType(SkColorType ct)
Definition: GrTypesPriv.h:629
kUnpremul_SkAlphaType
std::vector< SkIRect > make_short_rect_array(int w, int h)
std::vector< SkIRect > make_long_rect_array(int w, int h)
@ kUnknown_SkAlphaType
uninitialized
Definition: SkAlphaType.h:27
@ kLastEnum_SkAlphaType
last valid value
Definition: SkAlphaType.h:31
@ kGray_SkColorChannelFlag
Definition: SkColor.h:243
static uint32_t SkColorTypeChannelFlags(SkColorType ct)
static bool SkColorTypeIsAlphaOnly(SkColorType ct)
SK_API bool SkColorTypeIsAlwaysOpaque(SkColorType ct)
Definition: SkImageInfo.cpp:48
void alloc(const SkImageInfo &)
static bool Equals(const SkColorSpace *, const SkColorSpace *)
static sk_sp< SkColorSpace > MakeSRGBLinear()
size_t rowBytes() const
Definition: SkPixmap.h:145
const SkImageInfo & info() const
Definition: SkPixmap.h:135
const void * addr() const
Definition: SkPixmap.h:153
void reset()
Definition: SkPixmap.cpp:32
bool isEmpty() const
Definition: SkString.h:130
void append(const char text[])
Definition: SkString.h:203
const char * c_str() const
Definition: SkString.h:133
glong glong end
static float sat(float r, float g, float b)
Definition: hsl.cpp:51
const char * colortype_name(SkColorType ct)
Definition: ToolUtils.cpp:65
const char * alphatype_name(SkAlphaType at)
Definition: ToolUtils.cpp:55
#define T
Definition: precompiler.cc:65
Definition: SkRect.h:32
constexpr int32_t x() const
Definition: SkRect.h:141
constexpr int32_t y() const
Definition: SkRect.h:148
bool intersect(const SkIRect &r)
Definition: SkRect.h:513
static bool Intersects(const SkIRect &a, const SkIRect &b)
Definition: SkRect.h:535
constexpr int32_t top() const
Definition: SkRect.h:120
constexpr SkISize size() const
Definition: SkRect.h:172
constexpr int32_t bottom() const
Definition: SkRect.h:134
constexpr int32_t right() const
Definition: SkRect.h:127
constexpr int32_t left() const
Definition: SkRect.h:113
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const
Definition: SkImageInfo.h:466
SkImageInfo makeDimensions(SkISize newSize) const
Definition: SkImageInfo.h:454

◆ gpu_write_pixels_test_driver()

template<typename T >
static void gpu_write_pixels_test_driver ( skiatest::Reporter reporter,
const std::function< GpuDstFactory< T > > &  dstFactory,
const std::function< GpuWriteDstFn< T > > &  write,
const std::function< GpuReadDstFn< T > > &  read 
)
static

Definition at line 971 of file ReadWritePixelsGpuTest.cpp.

974 {
975 // Separate this out just to give it some line width to breathe.
976 auto runTest = [&](const T& dst,
977 const SkImageInfo& dstInfo,
978 const SkPixmap& srcPixels,
980 const bool csConversion =
981 !SkColorSpace::Equals(dstInfo.colorSpace(), srcPixels.info().colorSpace());
982 const auto writeCT = srcPixels.colorType();
983 const auto writeAT = srcPixels.alphaType();
984 const auto dstCT = dstInfo.colorType();
985 const auto dstAT = dstInfo.alphaType();
986 const auto rect = SkIRect::MakePtSize(offset, srcPixels.dimensions());
987 const auto surfBounds = SkIRect::MakeSize(dstInfo.dimensions());
988
989 // Do an initial read before the write.
990 SkAutoPixmapStorage firstReadPM = read(dst);
991 if (!firstReadPM.addr()) {
992 // Particularly with GLES 2 we can have formats that are unreadable with our current
993 // implementation of read pixels. If the format can't be attached to a FBO we don't have
994 // a code path that draws it to another readable color type/format combo and reads from
995 // that.
996 return Result::kExcusedFailure;
997 }
998
999 const Result result = write(dst, offset, srcPixels);
1000
1001 if (!SkIRect::Intersects(rect, surfBounds)) {
1003 } else if (writeCT == kUnknown_SkColorType) {
1005 } else if ((writeAT == kUnknown_SkAlphaType) != (dstAT == kUnknown_SkAlphaType)) {
1007 } else if (result == Result::kExcusedFailure) {
1008 return result;
1009 } else if (result == Result::kFail) {
1010 // TODO: Support RGB/BGR 101010x, BGRA 1010102 on the GPU.
1013 "Write failed. Write CT: %s, Write AT: %s Dst CT: %s, Dst AT: %s, "
1014 "Rect [%d, %d, %d, %d], CS conversion: %d\n",
1017 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, csConversion);
1018 }
1019 return result;
1020 }
1021
1022 SkIRect checkRect;
1023 if (result != Result::kSuccess || !checkRect.intersect(surfBounds, rect)) {
1024 return result;
1025 }
1026
1027 // Do an initial read before the write. We'll use this to verify that areas outside the
1028 // write are unaffected.
1029 SkAutoPixmapStorage secondReadPM = read(dst);
1030 if (!secondReadPM.addr()) {
1031 // The first read succeeded so this one should, too.
1033 "could not read from dst (CT: %s, AT: %s)\n",
1036 return Result::kFail;
1037 }
1038
1039 // Sometimes wider types go through 8bit unorm intermediates because of API
1040 // restrictions.
1041 int rgbBits = std::min({min_rgb_channel_bits(writeCT), min_rgb_channel_bits(dstCT), 8});
1042 float tol = (rgbBits == 0) ? 1.f : 2.f / ((1 << rgbBits) - 1);
1043 float alphaTol = 0;
1044 if (writeAT != kOpaque_SkAlphaType && dstAT != kOpaque_SkAlphaType) {
1045 // Alpha can also get squashed down to 8 bits going through an intermediate
1046 // color format.
1047 const int alphaBits = std::min({alpha_channel_bits(writeCT),
1048 alpha_channel_bits(dstCT),
1049 8});
1050 alphaTol = (alphaBits == 0) ? 1.f : 2.f / ((1 << alphaBits) - 1);
1051 }
1052
1053 const float tols[4] = {tol, tol, tol, alphaTol};
1054 auto error = std::function<ComparePixmapsErrorReporter>([&](int x,
1055 int y,
1056 const float diffs[4]) {
1057 SkASSERT(x >= 0 && y >= 0);
1059 "Write CT: %s, Write AT: %s, Dst CT: %s, Dst AT: %s, Rect [%d, %d, %d, %d]"
1060 ", CS conversion: %d\n"
1061 "Error at %d, %d. Diff in floats: (%f, %f, %f, %f)",
1066 rect.fLeft,
1067 rect.fTop,
1068 rect.fRight,
1069 rect.fBottom,
1070 csConversion,
1071 x,
1072 y,
1073 diffs[0],
1074 diffs[1],
1075 diffs[2],
1076 diffs[3]);
1077 });
1078
1080 ref.alloc(secondReadPM.info().makeDimensions(checkRect.size()));
1081 // Here we use the CPU backend to do the equivalent conversion as the write we're
1082 // testing, using kUnpremul instead of kUnknown since CPU requires a valid alpha type.
1085 std::max(0, -offset.fX),
1086 std::max(0, -offset.fY)));
1087 // This is the part of secondReadPixels that should have been updated by the write.
1088 SkPixmap actual;
1089 SkAssertResult(secondReadPM.extractSubset(&actual, checkRect));
1090 ComparePixels(ref, actual, tols, error);
1091 // The area around written rect should be the same in the first and second read.
1092 SkIRect borders[]{
1093 { 0, 0, secondReadPM.width(), secondReadPM.height()},
1094 {checkRect.fRight, 0, checkRect.fLeft, secondReadPM.height()},
1095 { checkRect.fLeft, 0, checkRect.fRight, checkRect.fTop},
1096 { checkRect.fLeft, checkRect.fBottom, checkRect.fRight, secondReadPM.height()}
1097 };
1098 for (const auto r : borders) {
1099 if (!r.isEmpty()) {
1100 // Make a copy because MSVC for some reason doesn't correctly capture 'r'.
1101 SkIPoint tl = r.topLeft();
1102 auto guardError = std::function<ComparePixmapsErrorReporter>(
1103 [&](int x, int y, const float diffs[4]) {
1104 x += tl.x();
1105 y += tl.y();
1107 "Write CT: %s, Write AT: %s, Dst CT: %s, Dst AT: %s,"
1108 "Rect [%d, %d, %d, %d], CS conversion: %d\n"
1109 "Error in guard region %d, %d. Diff in floats: (%f, %f, %f, %f)",
1114 rect.fLeft,
1115 rect.fTop,
1116 rect.fRight,
1117 rect.fBottom,
1118 csConversion,
1119 x,
1120 y,
1121 diffs[0],
1122 diffs[1],
1123 diffs[2],
1124 diffs[3]);
1125 });
1126 SkPixmap a, b;
1127 SkAssertResult(firstReadPM.extractSubset(&a, r));
1128 SkAssertResult(firstReadPM.extractSubset(&b, r));
1129 float zeroTols[4] = {};
1130 ComparePixels(a, b, zeroTols, guardError);
1131 }
1132 }
1133 return result;
1134 };
1135
1136 static constexpr int kW = 16;
1137 static constexpr int kH = 16;
1138
1139 const std::vector<SkIRect> longRectArray = make_long_rect_array(kW, kH);
1140 const std::vector<SkIRect> shortRectArray = make_short_rect_array(kW, kH);
1141
1142 // We ensure we use the long array once per src and read color type and otherwise use the
1143 // short array to improve test run time.
1144 // Also, some color types have no alpha values and thus Opaque Premul and Unpremul are
1145 // equivalent. Just ensure each redundant AT is tested once with each CT (dst and write).
1146 // Similarly, alpha-only color types behave the same for all alpha types so just test premul
1147 // after one iter.
1148 // We consider a dst or write CT thoroughly tested once it has run through the long rect array
1149 // and full complement of alpha types with one successful read in the loop.
1150 std::array<bool, kLastEnum_SkColorType + 1> dstCTTestedThoroughly = {},
1151 writeCTTestedThoroughly = {};
1152 for (int dat = 0; dat < kLastEnum_SkAlphaType; ++dat) {
1153 const auto dstAT = static_cast<SkAlphaType>(dat);
1154 for (int dct = 0; dct <= kLastEnum_SkColorType; ++dct) {
1155 const auto dstCT = static_cast<SkColorType>(dct);
1156 const auto dstInfo = SkImageInfo::Make(kW, kH, dstCT, dstAT, SkColorSpace::MakeSRGB());
1157 auto dst = dstFactory(dstInfo);
1158 if (!dst) {
1159 continue;
1160 }
1161 if (SkColorTypeIsAlwaysOpaque(dstCT) && dstCTTestedThoroughly[dstCT] &&
1162 (kPremul_SkAlphaType == dstAT || kUnpremul_SkAlphaType == dstAT)) {
1163 continue;
1164 }
1165 if (SkColorTypeIsAlphaOnly(dstCT) && dstCTTestedThoroughly[dstCT] &&
1166 (kUnpremul_SkAlphaType == dstAT ||
1167 kOpaque_SkAlphaType == dstAT ||
1168 kUnknown_SkAlphaType == dstAT)) {
1169 continue;
1170 }
1171 for (int wct = 0; wct <= kLastEnum_SkColorType; ++wct) {
1172 const auto writeCT = static_cast<SkColorType>(wct);
1173 for (const sk_sp<SkColorSpace>& writeCS : {SkColorSpace::MakeSRGB(),
1175 for (int wat = 0; wat <= kLastEnum_SkAlphaType; ++wat) {
1176 const auto writeAT = static_cast<SkAlphaType>(wat);
1177 if (writeAT != kOpaque_SkAlphaType && dstAT == kOpaque_SkAlphaType) {
1178 // This doesn't make sense.
1179 continue;
1180 }
1181 if (SkColorTypeIsAlwaysOpaque(writeCT) &&
1182 writeCTTestedThoroughly[writeCT] &&
1183 (kPremul_SkAlphaType == writeAT || kUnpremul_SkAlphaType == writeAT)) {
1184 continue;
1185 }
1186 if (SkColorTypeIsAlphaOnly(writeCT) && writeCTTestedThoroughly[writeCT] &&
1187 (kUnpremul_SkAlphaType == writeAT ||
1188 kOpaque_SkAlphaType == writeAT ||
1189 kUnknown_SkAlphaType == writeAT)) {
1190 continue;
1191 }
1192 const auto& rects =
1193 dstCTTestedThoroughly[dct] && writeCTTestedThoroughly[wct]
1194 ? shortRectArray
1195 : longRectArray;
1196 for (const auto& rect : rects) {
1197 auto writeInfo = SkImageInfo::Make(rect.size(),
1198 writeCT,
1199 writeAT,
1200 writeCS);
1201 // CPU and GPU handle 1010102 differently. CPU clamps RGB to A, GPU
1202 // doesn't.
1203 bool forceOpaque = writeCT == kRGBA_1010102_SkColorType ||
1204 writeCT == kBGRA_1010102_SkColorType;
1205 SkAutoPixmapStorage writePixels = make_ref_data(writeInfo, forceOpaque);
1206 const SkIPoint offset = rect.topLeft();
1207 Result r = runTest(dst, dstInfo, writePixels, offset);
1208 if (r == Result::kSuccess) {
1209 dstCTTestedThoroughly[dct] = true;
1210 writeCTTestedThoroughly[wct] = true;
1211 }
1212 }
1213 }
1214 }
1215 }
1216 }
1217 }
1218}
SkPixmap make_pixmap_have_valid_alpha_type(SkPixmap pm)
int width() const
Definition: SkPixmap.h:160
int height() const
Definition: SkPixmap.h:166
bool extractSubset(SkPixmap *subset, const SkIRect &area) const
Definition: SkPixmap.cpp:65
dst
Definition: cp.py:12
void write(SkWStream *wStream, const T &text)
Definition: skqp.cpp:188
constexpr int32_t y() const
Definition: SkPoint_impl.h:52
constexpr int32_t x() const
Definition: SkPoint_impl.h:46
int32_t fBottom
larger y-axis bounds
Definition: SkRect.h:36
int32_t fTop
smaller y-axis bounds
Definition: SkRect.h:34
int32_t fLeft
smaller x-axis bounds
Definition: SkRect.h:33
static constexpr SkIRect MakePtSize(SkIPoint pt, SkISize size)
Definition: SkRect.h:78
int32_t fRight
larger x-axis bounds
Definition: SkRect.h:35

◆ image_async_read_pixels()

static void image_async_read_pixels ( GrRenderable  renderable,
GrSurfaceOrigin  origin,
skiatest::Reporter reporter,
const sk_gpu_test::ContextInfo ctxInfo 
)
static

Definition at line 737 of file ReadWritePixelsGpuTest.cpp.

740 {
741 using Image = sk_sp<SkImage>;
742 auto context = ctxInfo.directContext();
743 auto reader = std::function<GpuReadSrcFn<Image>>([context](const Image& image,
744 const SkIPoint& offset,
745 const SkPixmap& pixels) {
746 AsyncContext asyncContext;
747 auto rect = SkIRect::MakeSize(pixels.dimensions()).makeOffset(offset);
748 // The GPU implementation is based on rendering and will fail for non-renderable color
749 // types.
751 auto format = context->priv().caps()->getDefaultBackendFormat(ct, GrRenderable::kYes);
752 if (!context->priv().caps()->isFormatAsColorTypeRenderable(ct, format)) {
753 return Result::kExcusedFailure;
754 }
755
756 // Rescale quality and linearity don't matter since we're doing a non-scaling readback.
757 image->asyncRescaleAndReadPixels(pixels.info(), rect,
760 async_callback, &asyncContext);
761 context->submit();
762 while (!asyncContext.fCalled) {
763 context->checkAsyncWorkCompletion();
764 }
765 if (!asyncContext.fResult) {
766 return Result::kFail;
767 }
768 SkRectMemcpy(pixels.writable_addr(), pixels.rowBytes(), asyncContext.fResult->data(0),
769 asyncContext.fResult->rowBytes(0), pixels.info().minRowBytes(),
770 pixels.height());
771 return Result::kSuccess;
772 });
773
774 GpuReadPixelTestRules rules;
775 rules.fAllowUnpremulSrc = true;
776 rules.fUncontainedRectSucceeds = false;
777 // TODO: some mobile GPUs have issues reading back sRGB src data with GLES -- skip for now
778 // b/296440036
779 if (ctxInfo.type() == skgpu::ContextType::kGLES) {
780 rules.fSkipSRGBCT = true;
781 }
782 // TODO: D3D on Intel has issues reading back 16-bit src data -- skip for now
783 // b/296440036
784 if (ctxInfo.type() == skgpu::ContextType::kDirect3D) {
785 rules.fSkip16BitCT = true;
786 }
787
788 auto factory = std::function<GpuSrcFactory<Image>>([&](const SkPixmap& src) {
789 return sk_gpu_test::MakeBackendTextureImage(ctxInfo.directContext(), src,
790 renderable, origin,
792 });
793 auto label = SkStringPrintf("Renderable: %d, Origin: %d", (int)renderable, origin);
794 gpu_read_pixels_test_driver(reporter, rules, factory, reader, label);
795}
SkColorType colorType() const
Definition: SkImage.cpp:152
GrDirectContext * directContext() const
skgpu::ContextType type() const
CanvasImage Image
Definition: dart_ui.cc:55

◆ make_long_rect_array()

std::vector< SkIRect > make_long_rect_array ( int  w,
int  h 
)

Definition at line 139 of file ReadWritePixelsGpuTest.cpp.

139 {
140 return {
141 // entire thing
143 // larger on all sides
144 SkIRect::MakeLTRB(-10, -10, w + 10, h + 10),
145 // fully contained
146 SkIRect::MakeLTRB(w/4, h/4, 3*w/4, 3*h/4),
147 // outside top left
148 SkIRect::MakeLTRB(-10, -10, -1, -1),
149 // touching top left corner
150 SkIRect::MakeLTRB(-10, -10, 0, 0),
151 // overlapping top left corner
152 SkIRect::MakeLTRB(-10, -10, w/4, h/4),
153 // overlapping top left and top right corners
154 SkIRect::MakeLTRB(-10, -10, w + 10, h/4),
155 // touching entire top edge
156 SkIRect::MakeLTRB(-10, -10, w + 10, 0),
157 // overlapping top right corner
158 SkIRect::MakeLTRB(3*w/4, -10, w + 10, h/4),
159 // contained in x, overlapping top edge
160 SkIRect::MakeLTRB(w/4, -10, 3*w/4, h/4),
161 // outside top right corner
162 SkIRect::MakeLTRB(w + 1, -10, w + 10, -1),
163 // touching top right corner
164 SkIRect::MakeLTRB(w, -10, w + 10, 0),
165 // overlapping top left and bottom left corners
166 SkIRect::MakeLTRB(-10, -10, w/4, h + 10),
167 // touching entire left edge
168 SkIRect::MakeLTRB(-10, -10, 0, h + 10),
169 // overlapping bottom left corner
170 SkIRect::MakeLTRB(-10, 3*h/4, w/4, h + 10),
171 // contained in y, overlapping left edge
172 SkIRect::MakeLTRB(-10, h/4, w/4, 3*h/4),
173 // outside bottom left corner
174 SkIRect::MakeLTRB(-10, h + 1, -1, h + 10),
175 // touching bottom left corner
176 SkIRect::MakeLTRB(-10, h, 0, h + 10),
177 // overlapping bottom left and bottom right corners
178 SkIRect::MakeLTRB(-10, 3*h/4, w + 10, h + 10),
179 // touching entire left edge
180 SkIRect::MakeLTRB(0, h, w, h + 10),
181 // overlapping bottom right corner
182 SkIRect::MakeLTRB(3*w/4, 3*h/4, w + 10, h + 10),
183 // overlapping top right and bottom right corners
184 SkIRect::MakeLTRB(3*w/4, -10, w + 10, h + 10),
185 };
186}
static constexpr SkIRect MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b)
Definition: SkRect.h:91

◆ make_pixmap_have_valid_alpha_type()

SkPixmap make_pixmap_have_valid_alpha_type ( SkPixmap  pm)

Definition at line 241 of file ReadWritePixelsGpuTest.cpp.

241 {
242 if (pm.alphaType() == kUnknown_SkAlphaType) {
243 return {pm.info().makeAlphaType(kUnpremul_SkAlphaType), pm.addr(), pm.rowBytes()};
244 }
245 return pm;
246}
SkAlphaType alphaType() const
Definition: SkPixmap.h:175

◆ make_ref_data()

static SkAutoPixmapStorage make_ref_data ( const SkImageInfo info,
bool  forceOpaque 
)
static

Definition at line 248 of file ReadWritePixelsGpuTest.cpp.

248 {
250 if (info.alphaType() == kUnknown_SkAlphaType) {
251 result.alloc(info.makeAlphaType(kUnpremul_SkAlphaType));
252 } else {
253 result.alloc(info);
254 }
256 if (!surface) {
257 return result;
258 }
259
260 SkPoint pts1[] = {{0, 0}, {float(info.width()), float(info.height())}};
261 static constexpr SkColor kColors1[] = {SK_ColorGREEN, SK_ColorRED};
263 paint.setShader(SkGradientShader::MakeLinear(pts1, kColors1, nullptr, 2, SkTileMode::kClamp));
264 surface->getCanvas()->drawPaint(paint);
265
266 SkPoint pts2[] = {{float(info.width()), 0}, {0, float(info.height())}};
267 static constexpr SkColor kColors2[] = {SK_ColorBLUE, SK_ColorBLACK};
268 paint.setShader(SkGradientShader::MakeLinear(pts2, kColors2, nullptr, 2, SkTileMode::kClamp));
269 paint.setBlendMode(SkBlendMode::kPlus);
270 surface->getCanvas()->drawPaint(paint);
271
272 // If not opaque add some fractional alpha.
273 if (info.alphaType() != kOpaque_SkAlphaType && !forceOpaque) {
274 static constexpr SkColor kColors3[] = {SK_ColorWHITE,
276 0x60FFFFFF,
279 static constexpr SkScalar kPos3[] = {0.f, 0.15f, 0.5f, 0.85f, 1.f};
280 paint.setShader(SkGradientShader::MakeRadial({info.width()/2.f, info.height()/2.f},
281 (info.width() + info.height())/10.f,
282 kColors3, kPos3, 5, SkTileMode::kMirror));
283 paint.setBlendMode(SkBlendMode::kDstIn);
284 surface->getCanvas()->drawPaint(paint);
285 }
286 return result;
287}
@ kPlus
r = min(s + d, 1)
@ kDstIn
r = d * sa
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
SK_API sk_sp< SkSurface > WrapPixels(const SkImageInfo &imageInfo, void *pixels, size_t rowBytes, const SkSurfaceProps *surfaceProps=nullptr)

◆ make_short_rect_array()

std::vector< SkIRect > make_short_rect_array ( int  w,
int  h 
)

Definition at line 188 of file ReadWritePixelsGpuTest.cpp.

188 {
189 return {
190 // entire thing
192 // fully contained
193 SkIRect::MakeLTRB(w/4, h/4, 3*w/4, 3*h/4),
194 // overlapping top right corner
195 SkIRect::MakeLTRB(3*w/4, -10, w + 10, h/4),
196 };
197}

◆ min_rgb_channel_bits()

static constexpr int min_rgb_channel_bits ( SkColorType  ct)
staticconstexpr

Definition at line 75 of file ReadWritePixelsGpuTest.cpp.

75 {
76 switch (ct) {
77 case kUnknown_SkColorType: return 0;
78 case kAlpha_8_SkColorType: return 0;
79 case kA16_unorm_SkColorType: return 0;
80 case kA16_float_SkColorType: return 0;
81 case kRGB_565_SkColorType: return 5;
82 case kARGB_4444_SkColorType: return 4;
83 case kR8G8_unorm_SkColorType: return 8;
84 case kR16G16_unorm_SkColorType: return 16;
85 case kR16G16_float_SkColorType: return 16;
86 case kRGBA_8888_SkColorType: return 8;
87 case kSRGBA_8888_SkColorType: return 8;
88 case kRGB_888x_SkColorType: return 8;
89 case kBGRA_8888_SkColorType: return 8;
90 case kRGBA_1010102_SkColorType: return 10;
91 case kRGB_101010x_SkColorType: return 10;
92 case kBGRA_1010102_SkColorType: return 10;
93 case kBGR_101010x_SkColorType: return 10;
94 case kBGR_101010x_XR_SkColorType: return 10;
95 case kRGBA_10x6_SkColorType: return 10;
96 case kBGRA_10101010_XR_SkColorType: return 10;
97 case kGray_8_SkColorType: return 8; // counting gray as "rgb"
98 case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa
99 case kRGBA_F16_SkColorType: return 10; // just counting the mantissa
100 case kRGBA_F32_SkColorType: return 23; // just counting the mantissa
101 case kR16G16B16A16_unorm_SkColorType: return 16;
102 case kR8_unorm_SkColorType: return 8;
103 }
105}

◆ surface_context_write_pixels()

static void surface_context_write_pixels ( GrRenderable  renderable,
GrSurfaceOrigin  origin,
skiatest::Reporter reporter,
const sk_gpu_test::ContextInfo ctxInfo 
)
static

Definition at line 1221 of file ReadWritePixelsGpuTest.cpp.

1224 {
1225 using Surface = std::unique_ptr<skgpu::ganesh::SurfaceContext>;
1226 GrDirectContext* direct = ctxInfo.directContext();
1227 auto writer = std::function<GpuWriteDstFn<Surface>>(
1228 [direct](const Surface& surface, const SkIPoint& offset, const SkPixmap& pixels) {
1229 if (surface->writePixels(direct, pixels, offset)) {
1230 return Result::kSuccess;
1231 } else {
1232 return Result::kFail;
1233 }
1234 });
1235 auto reader = std::function<GpuReadDstFn<Surface>>([direct](const Surface& s) {
1237 auto grInfo = s->imageInfo();
1238 SkColorType ct = GrColorTypeToSkColorType(grInfo.colorType());
1240 auto skInfo = SkImageInfo::Make(grInfo.dimensions(), ct, grInfo.alphaType(),
1241 grInfo.refColorSpace());
1242 result.alloc(skInfo);
1243 if (!s->readPixels(direct, result, {0, 0})) {
1244 SkAutoPixmapStorage badResult;
1245 return badResult;
1246 }
1247 return result;
1248 });
1249
1250 auto factory = std::function<GpuDstFactory<Surface>>(
1251 [direct, origin, renderable](const SkImageInfo& info) {
1252 return CreateSurfaceContext(direct,
1253 info,
1255 origin,
1256 renderable);
1257 });
1258
1259 gpu_write_pixels_test_driver(reporter, factory, writer, reader);
1260}
static void gpu_write_pixels_test_driver(skiatest::Reporter *reporter, const std::function< GpuDstFactory< T > > &dstFactory, const std::function< GpuWriteDstFn< T > > &write, const std::function< GpuReadDstFn< T > > &read)
struct MyStruct s