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" |
| 15 | #include "GrTest.h" |
| 16 | #include "Test.h" |
| 17 | #include "vk/GrVkGpu.h" |
| 18 | |
kkinnunen | 2ae4b2e | 2016-03-31 08:08:20 -0700 | [diff] [blame] | 19 | using sk_gpu_test::GrContextFactory; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 20 | |
| 21 | void fill_pixel_data(int width, int height, GrColor* data) { |
| 22 | |
| 23 | // build red-green gradient |
| 24 | for (int j = 0; j < height; ++j) { |
| 25 | for (int i = 0; i < width; ++i) { |
| 26 | unsigned int red = (unsigned int)(256.f*(i / (float)width)); |
| 27 | unsigned int green = (unsigned int)(256.f*(j / (float)height)); |
| 28 | data[i + j*width] = GrColorPackRGBA(red - (red>>8), green - (green>>8), 0xff, 0xff); |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | bool does_full_buffer_contain_correct_color(GrColor* srcBuffer, |
| 34 | GrColor* dstBuffer, |
| 35 | GrPixelConfig config, |
| 36 | int width, |
| 37 | int height) { |
| 38 | GrColor* srcPtr = srcBuffer; |
| 39 | GrColor* dstPtr = dstBuffer; |
| 40 | for (int j = 0; j < height; ++j) { |
| 41 | for (int i = 0; i < width; ++i) { |
| 42 | if (srcPtr[i] != dstPtr[i]) { |
| 43 | return false; |
| 44 | } |
| 45 | } |
| 46 | srcPtr += width; |
| 47 | dstPtr += width; |
| 48 | } |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | void basic_texture_test(skiatest::Reporter* reporter, GrContext* context, GrPixelConfig config, |
| 53 | bool renderTarget, bool linearTiling) { |
| 54 | GrVkGpu* gpu = static_cast<GrVkGpu*>(context->getGpu()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 55 | |
| 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 | 1b35256 | 2017-04-05 18:56:21 +0000 | [diff] [blame] | 85 | GrTexture* tex0 = gpu->createTexture(surfDesc, SkBudgeted::kNo, srcBuffer, 0); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 86 | if (tex0) { |
| 87 | REPORTER_ASSERT(reporter, canCreate); |
Robert Phillips | 1b35256 | 2017-04-05 18:56:21 +0000 | [diff] [blame] | 88 | gpu->readPixels(tex0, 0, 0, kWidth, kHeight, config, dstBuffer, 0); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 89 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, |
| 90 | dstBuffer, |
| 91 | config, |
| 92 | kWidth, |
| 93 | kHeight)); |
| 94 | |
Robert Phillips | 1b35256 | 2017-04-05 18:56:21 +0000 | [diff] [blame] | 95 | tex0->writePixels(2, 10, 10, 2, config, srcBuffer); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 96 | memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor)); |
Robert Phillips | 1b35256 | 2017-04-05 18:56:21 +0000 | [diff] [blame] | 97 | gpu->readPixels(tex0, 2, 10, 10, 2, config, dstBuffer, 0); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 98 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, |
| 99 | dstBuffer, |
| 100 | config, |
| 101 | 10, |
| 102 | 2)); |
Robert Phillips | 1b35256 | 2017-04-05 18:56:21 +0000 | [diff] [blame] | 103 | |
| 104 | tex0->unref(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 105 | } else { |
| 106 | REPORTER_ASSERT(reporter, !canCreate); |
| 107 | } |
| 108 | |
| 109 | surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
Robert Phillips | 1b35256 | 2017-04-05 18:56:21 +0000 | [diff] [blame] | 110 | GrTexture* tex1 = gpu->createTexture(surfDesc, SkBudgeted::kNo, srcBuffer, 0); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 111 | if (tex1) { |
| 112 | REPORTER_ASSERT(reporter, canCreate); |
Robert Phillips | 1b35256 | 2017-04-05 18:56:21 +0000 | [diff] [blame] | 113 | gpu->readPixels(tex1, 0, 0, kWidth, kHeight, config, dstBuffer, 0); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 114 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, |
| 115 | dstBuffer, |
| 116 | config, |
| 117 | kWidth, |
| 118 | kHeight)); |
| 119 | |
Robert Phillips | 1b35256 | 2017-04-05 18:56:21 +0000 | [diff] [blame] | 120 | tex1->writePixels(5, 4, 4, 5, config, srcBuffer); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 121 | memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor)); |
Robert Phillips | 1b35256 | 2017-04-05 18:56:21 +0000 | [diff] [blame] | 122 | gpu->readPixels(tex1, 5, 4, 4, 5, config, dstBuffer, 0); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 123 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, |
| 124 | dstBuffer, |
| 125 | config, |
| 126 | 4, |
| 127 | 5)); |
| 128 | |
Robert Phillips | 1b35256 | 2017-04-05 18:56:21 +0000 | [diff] [blame] | 129 | tex1->unref(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 130 | } else { |
| 131 | REPORTER_ASSERT(reporter, !canCreate); |
| 132 | } |
| 133 | } |
| 134 | |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 135 | DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkUploadPixelsTests, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 136 | basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, false, false); |
| 137 | basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, true, false); |
| 138 | basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, false, true); |
| 139 | basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, true, true); |
| 140 | basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, false, false); |
| 141 | basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, true, false); |
| 142 | basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, false, true); |
| 143 | basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, true, true); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | #endif |