Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 |
| 13 | |
| 14 | #include "GrContextFactory.h" |
| 15 | #include "GrContextPriv.h" |
| 16 | #include "GrGpu.h" |
| 17 | #include "GrResourceProvider.h" |
| 18 | #include "GrSurfaceProxy.h" |
| 19 | #include "GrTexture.h" |
| 20 | #include "GrTest.h" |
| 21 | #include "SkGr.h" |
| 22 | #include "SkSurface.h" |
| 23 | #include "Test.h" |
| 24 | |
| 25 | using sk_gpu_test::GrContextFactory; |
| 26 | |
| 27 | void fill_transfer_data(int left, int top, int width, int height, int bufferWidth, |
| 28 | GrColor* data) { |
| 29 | |
| 30 | // build red-green gradient |
| 31 | for (int j = top; j < top + height; ++j) { |
| 32 | for (int i = left; i < left + width; ++i) { |
| 33 | unsigned int red = (unsigned int)(256.f*((i - left) / (float)width)); |
| 34 | unsigned int green = (unsigned int)(256.f*((j - top) / (float)height)); |
| 35 | data[i + j*bufferWidth] = GrColorPackRGBA(red - (red>>8), |
| 36 | green - (green>>8), 0xff, 0xff); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | bool does_full_buffer_contain_correct_values(GrColor* srcBuffer, |
| 42 | GrColor* dstBuffer, |
| 43 | int width, |
| 44 | int height, |
| 45 | int bufferWidth, |
| 46 | int bufferHeight, |
| 47 | GrSurfaceOrigin origin) { |
| 48 | GrColor* srcPtr = srcBuffer; |
| 49 | bool bottomUp = SkToBool(kBottomLeft_GrSurfaceOrigin == origin); |
| 50 | GrColor* dstPtr = bottomUp ? dstBuffer + bufferWidth*(bufferHeight-1) : dstBuffer; |
| 51 | int dstIncrement = bottomUp ? -bufferWidth : +bufferWidth; |
| 52 | |
| 53 | for (int j = 0; j < height; ++j) { |
| 54 | for (int i = 0; i < width; ++i) { |
| 55 | if (srcPtr[i] != dstPtr[i]) { |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | srcPtr += bufferWidth; |
| 60 | dstPtr += dstIncrement; |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | void basic_transfer_test(skiatest::Reporter* reporter, GrContext* context, GrPixelConfig config, |
| 66 | GrSurfaceOrigin origin, bool renderTarget) { |
Brian Salomon | 7f56d3d | 2017-10-09 13:02:49 -0400 | [diff] [blame] | 67 | if (GrCaps::kNone_MapFlags == context->caps()->mapBufferFlags()) { |
| 68 | return; |
| 69 | } |
| 70 | |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 71 | // set up the data |
| 72 | const int kTextureWidth = 16; |
| 73 | const int kTextureHeight = 16; |
| 74 | const int kBufferWidth = 20; |
| 75 | const int kBufferHeight = 16; |
| 76 | size_t rowBytes = kBufferWidth * sizeof(GrColor); |
| 77 | SkAutoTMalloc<GrColor> srcBuffer(kBufferWidth*kBufferHeight); |
| 78 | SkAutoTMalloc<GrColor> dstBuffer(kBufferWidth*kBufferHeight); |
| 79 | |
| 80 | fill_transfer_data(0, 0, kTextureWidth, kTextureHeight, kBufferWidth, srcBuffer.get()); |
| 81 | |
| 82 | // create and fill transfer buffer |
| 83 | size_t size = rowBytes*kBufferHeight; |
| 84 | uint32_t bufferFlags = GrResourceProvider::kNoPendingIO_Flag; |
| 85 | sk_sp<GrBuffer> buffer(context->resourceProvider()->createBuffer(size, |
| 86 | kXferCpuToGpu_GrBufferType, |
| 87 | kDynamic_GrAccessPattern, |
| 88 | bufferFlags)); |
| 89 | if (!buffer) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | void* data = buffer->map(); |
| 94 | memcpy(data, srcBuffer.get(), size); |
| 95 | buffer->unmap(); |
| 96 | |
| 97 | // create texture |
| 98 | GrSurfaceDesc desc; |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 99 | desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
| 100 | desc.fOrigin = origin; |
| 101 | desc.fWidth = kTextureWidth; |
| 102 | desc.fHeight = kTextureHeight; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 103 | desc.fConfig = config; |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 104 | desc.fSampleCnt = 0; |
| 105 | sk_sp<GrTexture> tex = context->resourceProvider()->createTexture(desc, SkBudgeted::kNo); |
| 106 | |
| 107 | ////////////////////////// |
| 108 | // transfer full data |
| 109 | |
| 110 | bool result; |
| 111 | result = context->getGpu()->transferPixels(tex.get(), 0, 0, kTextureWidth, kTextureHeight, |
| 112 | config, buffer.get(), 0, rowBytes); |
| 113 | REPORTER_ASSERT(reporter, result); |
| 114 | |
| 115 | memset(dstBuffer.get(), 0xCDCD, size); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 116 | result = context->getGpu()->readPixels(tex.get(), origin, 0, 0, kTextureWidth, kTextureHeight, |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 117 | config, dstBuffer.get(), rowBytes); |
Jim Van Verth | 52fb02e | 2017-06-27 10:36:56 -0400 | [diff] [blame] | 118 | if (result) { |
| 119 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_values(srcBuffer, |
| 120 | dstBuffer, |
| 121 | kTextureWidth, |
| 122 | kTextureHeight, |
| 123 | kBufferWidth, |
| 124 | kBufferHeight, |
| 125 | origin)); |
| 126 | } |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 127 | |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 128 | ////////////////////////// |
| 129 | // transfer partial data |
| 130 | |
| 131 | const int kLeft = 2; |
| 132 | const int kTop = 10; |
| 133 | const int kWidth = 10; |
| 134 | const int kHeight = 2; |
| 135 | |
| 136 | // change color of subrectangle |
| 137 | fill_transfer_data(kLeft, kTop, kWidth, kHeight, kBufferWidth, srcBuffer.get()); |
| 138 | data = buffer->map(); |
| 139 | memcpy(data, srcBuffer.get(), size); |
| 140 | buffer->unmap(); |
| 141 | |
| 142 | size_t offset = sizeof(GrColor)*(kTop*kBufferWidth + kLeft); |
| 143 | result = context->getGpu()->transferPixels(tex.get(), kLeft, kTop, kWidth, kHeight, config, |
| 144 | buffer.get(), offset, rowBytes); |
| 145 | REPORTER_ASSERT(reporter, result); |
| 146 | |
| 147 | memset(dstBuffer.get(), 0xCDCD, size); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 148 | result = context->getGpu()->readPixels(tex.get(), origin, 0, 0, kTextureWidth, kTextureHeight, |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 149 | config, dstBuffer.get(), rowBytes); |
Jim Van Verth | 52fb02e | 2017-06-27 10:36:56 -0400 | [diff] [blame] | 150 | if (result) { |
| 151 | REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_values(srcBuffer, |
| 152 | dstBuffer, |
| 153 | kTextureWidth, |
| 154 | kTextureHeight, |
| 155 | kBufferWidth, |
| 156 | kBufferHeight, |
| 157 | origin)); |
| 158 | } |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsTest, reporter, ctxInfo) { |
| 162 | // RGBA |
| 163 | basic_transfer_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, |
| 164 | kTopLeft_GrSurfaceOrigin, false); |
| 165 | basic_transfer_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, |
| 166 | kTopLeft_GrSurfaceOrigin, true); |
| 167 | basic_transfer_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, |
| 168 | kBottomLeft_GrSurfaceOrigin, false); |
| 169 | basic_transfer_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig, |
| 170 | kBottomLeft_GrSurfaceOrigin, true); |
| 171 | |
| 172 | // BGRA |
| 173 | basic_transfer_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, |
| 174 | kTopLeft_GrSurfaceOrigin, false); |
| 175 | basic_transfer_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, |
| 176 | kTopLeft_GrSurfaceOrigin, true); |
| 177 | basic_transfer_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, |
| 178 | kBottomLeft_GrSurfaceOrigin, false); |
| 179 | basic_transfer_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig, |
| 180 | kBottomLeft_GrSurfaceOrigin, true); |
| 181 | } |
| 182 | |
| 183 | #endif |