OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include <functional> | 8 #include <functional> |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkColorSpace_Base.h" |
10 #include "SkData.h" | 11 #include "SkData.h" |
11 #include "SkDevice.h" | 12 #include "SkDevice.h" |
12 #include "SkImage_Base.h" | 13 #include "SkImage_Base.h" |
13 #include "SkPath.h" | 14 #include "SkPath.h" |
14 #include "SkRRect.h" | 15 #include "SkRRect.h" |
15 #include "SkSurface.h" | 16 #include "SkSurface.h" |
16 #include "SkUtils.h" | 17 #include "SkUtils.h" |
17 #include "Test.h" | 18 #include "Test.h" |
18 | 19 |
19 #if SK_SUPPORT_GPU | 20 #if SK_SUPPORT_GPU |
20 #include "GrContext.h" | 21 #include "GrContext.h" |
21 #include "GrDrawContext.h" | 22 #include "GrDrawContext.h" |
22 #include "GrGpu.h" | 23 #include "GrGpu.h" |
23 #include "GrResourceProvider.h" | 24 #include "GrResourceProvider.h" |
| 25 #include <vector> |
24 #endif | 26 #endif |
25 | 27 |
26 #include <initializer_list> | 28 #include <initializer_list> |
27 | 29 |
28 static void release_direct_surface_storage(void* pixels, void* context) { | 30 static void release_direct_surface_storage(void* pixels, void* context) { |
29 SkASSERT(pixels == context); | 31 SkASSERT(pixels == context); |
30 sk_free(pixels); | 32 sk_free(pixels); |
31 } | 33 } |
32 static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType, | 34 static sk_sp<SkSurface> create_surface(SkAlphaType at = kPremul_SkAlphaType, |
33 SkImageInfo* requestedInfo = nullptr) { | 35 SkImageInfo* requestedInfo = nullptr) { |
(...skipping 869 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
903 // our surface functions. | 905 // our surface functions. |
904 GrRenderTarget* rt = surface->getCanvas()->internal_private_accessTo
pLayerDrawContext() | 906 GrRenderTarget* rt = surface->getCanvas()->internal_private_accessTo
pLayerDrawContext() |
905 ->accessRenderTarget(); | 907 ->accessRenderTarget(); |
906 REPORTER_ASSERT(reporter, | 908 REPORTER_ASSERT(reporter, |
907 ctxInfo.grContext()->resourceProvider()->attachStenc
ilAttachment(rt)); | 909 ctxInfo.grContext()->resourceProvider()->attachStenc
ilAttachment(rt)); |
908 gpu->deleteTestingOnlyBackendTexture(textureObject); | 910 gpu->deleteTestingOnlyBackendTexture(textureObject); |
909 } | 911 } |
910 } | 912 } |
911 } | 913 } |
912 #endif | 914 #endif |
| 915 |
| 916 static void test_surface_creation_and_snapshot_with_color_space( |
| 917 skiatest::Reporter* reporter, |
| 918 const char* prefix, |
| 919 bool colorSpaceSupport, |
| 920 std::function<sk_sp<SkSurface>(const SkImageInfo&)> surfaceMaker) { |
| 921 |
| 922 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); |
| 923 auto adobeColorSpace = SkColorSpace::NewNamed(SkColorSpace::kAdobeRGB_Named)
; |
| 924 SkMatrix44 srgbMatrix = srgbColorSpace->xyz(); |
| 925 const float oddGamma[] = { 2.4f, 2.4f, 2.4f }; |
| 926 auto oddColorSpace = SkColorSpace_Base::NewRGB(oddGamma, srgbMatrix); |
| 927 auto linearColorSpace = srgbColorSpace->makeLinearGamma(); |
| 928 |
| 929 const struct { |
| 930 SkColorType fColorType; |
| 931 sk_sp<SkColorSpace> fColorSpace; |
| 932 bool fShouldWork; |
| 933 const char* fDescription; |
| 934 } testConfigs[] = { |
| 935 { kN32_SkColorType, nullptr, true, "N32-nullptr" }, |
| 936 { kN32_SkColorType, linearColorSpace, false, "N32-linear" }, |
| 937 { kN32_SkColorType, srgbColorSpace, true, "N32-srgb" }, |
| 938 { kN32_SkColorType, adobeColorSpace, true, "N32-adobe" }, |
| 939 { kN32_SkColorType, oddColorSpace, false, "N32-odd" }, |
| 940 { kRGBA_F16_SkColorType, nullptr, false, "F16-nullptr" }, |
| 941 { kRGBA_F16_SkColorType, linearColorSpace, true, "F16-linear" }, |
| 942 { kRGBA_F16_SkColorType, srgbColorSpace, false, "F16-srgb" }, |
| 943 { kRGBA_F16_SkColorType, adobeColorSpace, false, "F16-adobe" }, |
| 944 { kRGBA_F16_SkColorType, oddColorSpace, false, "F16-odd" }, |
| 945 { kRGB_565_SkColorType, srgbColorSpace, false, "565-srgb" }, |
| 946 { kAlpha_8_SkColorType, srgbColorSpace, false, "A8-srgb" }, |
| 947 }; |
| 948 |
| 949 for (auto& testConfig : testConfigs) { |
| 950 SkString fullTestName = SkStringPrintf("%s-%s", prefix, testConfig.fDesc
ription); |
| 951 SkImageInfo info = SkImageInfo::Make(10, 10, testConfig.fColorType, kPre
mul_SkAlphaType, |
| 952 testConfig.fColorSpace); |
| 953 |
| 954 // For some GPU contexts (eg ANGLE), we don't have sRGB support, so we s
hould fail to create |
| 955 // any surface with a color space attached (because we can't handle gamm
a-correct sources). |
| 956 bool shouldWork = testConfig.fShouldWork && (colorSpaceSupport || !testC
onfig.fColorSpace); |
| 957 |
| 958 auto surface(surfaceMaker(info)); |
| 959 REPORTER_ASSERT_MESSAGE(reporter, SkToBool(surface) == shouldWork, fullT
estName.c_str()); |
| 960 |
| 961 if (shouldWork && surface) { |
| 962 sk_sp<SkImage> image(surface->makeImageSnapshot()); |
| 963 REPORTER_ASSERT_MESSAGE(reporter, image, testConfig.fDescription); |
| 964 SkColorSpace* imageColorSpace = as_IB(image)->onImageInfo().colorSpa
ce(); |
| 965 REPORTER_ASSERT_MESSAGE(reporter, imageColorSpace == testConfig.fCol
orSpace.get(), |
| 966 fullTestName.c_str()); |
| 967 } |
| 968 } |
| 969 } |
| 970 |
| 971 DEF_TEST(SurfaceCreationWithColorSpace, reporter) { |
| 972 auto surfaceMaker = [](const SkImageInfo& info) { |
| 973 return SkSurface::MakeRaster(info); |
| 974 }; |
| 975 |
| 976 test_surface_creation_and_snapshot_with_color_space(reporter, "raster", true
, surfaceMaker); |
| 977 } |
| 978 |
| 979 #if SK_SUPPORT_GPU |
| 980 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter,
ctxInfo) { |
| 981 GrContext* context = ctxInfo.grContext(); |
| 982 bool srgbSupport = context->caps()->srgbSupport(); |
| 983 auto surfaceMaker = [context](const SkImageInfo& info) { |
| 984 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info); |
| 985 }; |
| 986 |
| 987 test_surface_creation_and_snapshot_with_color_space(reporter, "gpu", srgbSup
port, surfaceMaker); |
| 988 |
| 989 std::vector<GrBackendObject> textureHandles; |
| 990 auto wrappedSurfaceMaker = [context,&textureHandles](const SkImageInfo& info
) { |
| 991 GrBackendTextureDesc desc; |
| 992 desc.fConfig = SkImageInfo2GrPixelConfig(info, *context->caps()); |
| 993 desc.fWidth = 10; |
| 994 desc.fHeight = 10; |
| 995 desc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 996 desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture
( |
| 997 nullptr, desc.fWidth, desc.fHeight, desc.fConfig, true); |
| 998 |
| 999 if (!desc.fTextureHandle) { |
| 1000 return sk_sp<SkSurface>(nullptr); |
| 1001 } |
| 1002 textureHandles.push_back(desc.fTextureHandle); |
| 1003 |
| 1004 return SkSurface::MakeFromBackendTexture(context, desc, sk_ref_sp(info.c
olorSpace()), |
| 1005 nullptr); |
| 1006 }; |
| 1007 |
| 1008 test_surface_creation_and_snapshot_with_color_space(reporter, "wrapped", srg
bSupport, |
| 1009 wrappedSurfaceMaker); |
| 1010 |
| 1011 for (auto textureHandle : textureHandles) { |
| 1012 context->getGpu()->deleteTestingOnlyBackendTexture(textureHandle); |
| 1013 } |
| 1014 } |
| 1015 #endif |
OLD | NEW |