Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | // This is a GPU-backend specific test. It relies on static intializers to work |
| 9 | |
| 10 | #include "SkTypes.h" |
| 11 | |
| 12 | #if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS && defined(SK_VULKAN) |
| 13 | |
| 14 | #include "GrContextFactory.h" |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 15 | #include "GrContextPriv.h" |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame^] | 16 | #include "GrSurfaceProxy.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 17 | #include "GrTest.h" |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame^] | 18 | #include "SkGr.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 19 | #include "Test.h" |
| 20 | #include "vk/GrVkGpu.h" |
| 21 | |
kkinnunen | 2ae4b2e | 2016-03-31 08:08:20 -0700 | [diff] [blame] | 22 | using sk_gpu_test::GrContextFactory; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 23 | |
| 24 | void fill_pixel_data(int width, int height, GrColor* data) { |
| 25 | |
| 26 | // build red-green gradient |
| 27 | for (int j = 0; j < height; ++j) { |
| 28 | for (int i = 0; i < width; ++i) { |
| 29 | unsigned int red = (unsigned int)(256.f*(i / (float)width)); |
| 30 | unsigned int green = (unsigned int)(256.f*(j / (float)height)); |
| 31 | data[i + j*width] = GrColorPackRGBA(red - (red>>8), green - (green>>8), 0xff, 0xff); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | bool does_full_buffer_contain_correct_color(GrColor* srcBuffer, |
| 37 | GrColor* dstBuffer, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 38 | int width, |
| 39 | int height) { |
| 40 | GrColor* srcPtr = srcBuffer; |
| 41 | GrColor* dstPtr = dstBuffer; |
| 42 | for (int j = 0; j < height; ++j) { |
| 43 | for (int i = 0; i < width; ++i) { |
| 44 | if (srcPtr[i] != dstPtr[i]) { |
| 45 | return false; |
| 46 | } |
| 47 | } |
| 48 | srcPtr += width; |
| 49 | dstPtr += width; |
| 50 | } |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | void basic_texture_test(skiatest::Reporter* reporter, GrContext* context, GrPixelConfig config, |
| 55 | bool renderTarget, bool linearTiling) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 56 | const int kWidth = 16; |
| 57 | const int kHeight = 16; |
| 58 | SkAutoTMalloc<GrColor> srcBuffer(kWidth*kHeight); |
| 59 | SkAutoTMalloc<GrColor> dstBuffer(kWidth*kHeight); |
| 60 | |
| 61 | fill_pixel_data(kWidth, kHeight, srcBuffer.get()); |
| 62 | |
| 63 | const GrVkCaps* caps = reinterpret_cast<const GrVkCaps*>(context->caps()); |
| 64 | |
| 65 | bool canCreate = true; |
| 66 | // the expectation is that the given config is texturable/renderable with optimal tiling |
| 67 | // but may not be with linear tiling |
| 68 | if (linearTiling) { |
egdaniel | a95d46b | 2016-08-15 08:06:29 -0700 | [diff] [blame] | 69 | if (!caps->isConfigTexturableLinearly(config) || |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 70 | (renderTarget && !caps->isConfigRenderableLinearly(config, false))) { |
| 71 | canCreate = false; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | GrSurfaceDesc surfDesc; |
| 76 | surfDesc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
| 77 | if (linearTiling) { |
| 78 | surfDesc.fFlags |= kZeroCopy_GrSurfaceFlag; |
| 79 | } |
| 80 | surfDesc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 81 | surfDesc.fWidth = kWidth; |
| 82 | surfDesc.fHeight = kHeight; |
| 83 | surfDesc.fConfig = config; |
| 84 | surfDesc.fSampleCnt = 0; |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame^] | 85 | |
| 86 | SkColorType ct; |
| 87 | SkAssertResult(GrPixelConfigToColorType(config, &ct)); |
| 88 | |
| 89 | sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), |
| 90 | surfDesc, SkBudgeted::kNo, |
| 91 | srcBuffer, 0); |
| 92 | |
| 93 | if (proxy) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 94 | REPORTER_ASSERT(reporter, canCreate); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame^] | 95 | |
| 96 | sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext( |
| 97 | proxy, nullptr); |
| 98 | |
| 99 | SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kOpaque_SkAlphaType); |
| 100 | |
| 101 | bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0); |
| 102 | REPORTER_ASSERT(reporter, result); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 103 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, |
| 104 | dstBuffer, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 105 | kWidth, |
| 106 | kHeight)); |
| 107 | |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame^] | 108 | dstInfo = SkImageInfo::Make(10, 2, ct, kOpaque_SkAlphaType); |
| 109 | result = sContext->writePixels(dstInfo, srcBuffer, 0, 2, 10); |
| 110 | REPORTER_ASSERT(reporter, result); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 111 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 112 | memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor)); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame^] | 113 | |
| 114 | result = sContext->readPixels(dstInfo, dstBuffer, 0, 2, 10); |
| 115 | REPORTER_ASSERT(reporter, result); |
| 116 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 117 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, |
| 118 | dstBuffer, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 119 | 10, |
| 120 | 2)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 121 | } else { |
| 122 | REPORTER_ASSERT(reporter, !canCreate); |
| 123 | } |
| 124 | |
| 125 | surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame^] | 126 | |
| 127 | proxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), |
| 128 | surfDesc, SkBudgeted::kNo, |
| 129 | srcBuffer, 0); |
| 130 | if (proxy) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 131 | REPORTER_ASSERT(reporter, canCreate); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame^] | 132 | |
| 133 | sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext( |
| 134 | proxy, nullptr); |
| 135 | |
| 136 | SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kOpaque_SkAlphaType); |
| 137 | |
| 138 | bool result = sContext->readPixels(dstInfo, dstBuffer, 0, 0, 0); |
| 139 | REPORTER_ASSERT(reporter, result); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 140 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, |
| 141 | dstBuffer, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 142 | kWidth, |
| 143 | kHeight)); |
| 144 | |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame^] | 145 | dstInfo = SkImageInfo::Make(4, 5, ct, kOpaque_SkAlphaType); |
| 146 | result = sContext->writePixels(dstInfo, srcBuffer, 0, 5, 4); |
| 147 | REPORTER_ASSERT(reporter, result); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 148 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 149 | memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor)); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame^] | 150 | |
| 151 | result = sContext->readPixels(dstInfo, dstBuffer, 0, 5, 4); |
| 152 | REPORTER_ASSERT(reporter, result); |
| 153 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 154 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, |
| 155 | dstBuffer, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 156 | 4, |
| 157 | 5)); |
| 158 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 159 | } else { |
| 160 | REPORTER_ASSERT(reporter, !canCreate); |
| 161 | } |
| 162 | } |
| 163 | |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 164 | DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkUploadPixelsTests, reporter, ctxInfo) { |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame^] | 165 | // RGBA |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 166 | basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, false, false); |
| 167 | basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, true, false); |
| 168 | basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, false, true); |
| 169 | basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, true, true); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame^] | 170 | |
| 171 | // BGRA |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 172 | basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, false, false); |
| 173 | basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, true, false); |
| 174 | basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, false, true); |
| 175 | basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, true, true); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | #endif |