blob: dfc5a048b34a2ddea6576092698340cbc61badca [file] [log] [blame]
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001/*
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
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040012#include "GrContextFactory.h"
13#include "GrContextPriv.h"
14#include "GrGpu.h"
15#include "GrResourceProvider.h"
16#include "GrSurfaceProxy.h"
17#include "GrTexture.h"
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040018#include "SkGr.h"
19#include "SkSurface.h"
20#include "Test.h"
21
22using sk_gpu_test::GrContextFactory;
23
24void fill_transfer_data(int left, int top, int width, int height, int bufferWidth,
25 GrColor* data) {
26
27 // build red-green gradient
28 for (int j = top; j < top + height; ++j) {
29 for (int i = left; i < left + width; ++i) {
30 unsigned int red = (unsigned int)(256.f*((i - left) / (float)width));
31 unsigned int green = (unsigned int)(256.f*((j - top) / (float)height));
32 data[i + j*bufferWidth] = GrColorPackRGBA(red - (red>>8),
33 green - (green>>8), 0xff, 0xff);
34 }
35 }
36}
37
38bool does_full_buffer_contain_correct_values(GrColor* srcBuffer,
39 GrColor* dstBuffer,
40 int width,
41 int height,
42 int bufferWidth,
Brian Salomona6948702018-06-01 15:33:20 -040043 int bufferHeight) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040044 GrColor* srcPtr = srcBuffer;
Brian Salomona6948702018-06-01 15:33:20 -040045 GrColor* dstPtr = dstBuffer;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040046
47 for (int j = 0; j < height; ++j) {
48 for (int i = 0; i < width; ++i) {
49 if (srcPtr[i] != dstPtr[i]) {
50 return false;
51 }
52 }
53 srcPtr += bufferWidth;
Brian Salomona6948702018-06-01 15:33:20 -040054 dstPtr += bufferWidth;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040055 }
56 return true;
57}
58
Brian Salomonc320b152018-02-20 14:05:36 -050059void basic_transfer_test(skiatest::Reporter* reporter, GrContext* context, GrColorType colorType,
Brian Salomona6948702018-06-01 15:33:20 -040060 bool renderTarget) {
Robert Phillips9da87e02019-02-04 13:26:26 -050061 if (GrCaps::kNone_MapFlags == context->priv().caps()->mapBufferFlags()) {
Brian Salomon7f56d3d2017-10-09 13:02:49 -040062 return;
63 }
64
Robert Phillips9da87e02019-02-04 13:26:26 -050065 auto resourceProvider = context->priv().resourceProvider();
66 GrGpu* gpu = context->priv().getGpu();
Robert Phillips6be756b2018-01-16 15:07:54 -050067
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040068 // set up the data
69 const int kTextureWidth = 16;
70 const int kTextureHeight = 16;
Jim Van Verth3a749252018-12-07 10:57:26 -050071#ifdef SK_BUILD_FOR_IOS
72 // UNPACK_ROW_LENGTH is broken on iOS so rowBytes needs to match data width
Robert Phillips4217ea72019-01-30 13:08:28 -050073 const int kBufferWidth = GrBackendApi::kOpenGL == context->backend() ? 16 : 20;
Jim Van Verth3a749252018-12-07 10:57:26 -050074#else
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040075 const int kBufferWidth = 20;
Jim Van Verth3a749252018-12-07 10:57:26 -050076#endif
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040077 const int kBufferHeight = 16;
78 size_t rowBytes = kBufferWidth * sizeof(GrColor);
79 SkAutoTMalloc<GrColor> srcBuffer(kBufferWidth*kBufferHeight);
80 SkAutoTMalloc<GrColor> dstBuffer(kBufferWidth*kBufferHeight);
81
82 fill_transfer_data(0, 0, kTextureWidth, kTextureHeight, kBufferWidth, srcBuffer.get());
83
84 // create and fill transfer buffer
85 size_t size = rowBytes*kBufferHeight;
Chris Daltond004e0b2018-09-27 09:28:03 -060086 auto bufferFlags = GrResourceProvider::Flags::kNoPendingIO;
Brian Salomonae64c192019-02-05 09:41:37 -050087 sk_sp<GrBuffer> buffer(resourceProvider->createBuffer(size, GrGpuBufferType::kXferCpuToGpu,
88 kDynamic_GrAccessPattern, bufferFlags));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040089 if (!buffer) {
90 return;
91 }
92
93 void* data = buffer->map();
94 memcpy(data, srcBuffer.get(), size);
95 buffer->unmap();
96
Brian Salomonc320b152018-02-20 14:05:36 -050097 for (auto srgbEncoding : {GrSRGBEncoded::kNo, GrSRGBEncoded::kYes}) {
98 // create texture
99 GrSurfaceDesc desc;
100 desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Brian Salomonc320b152018-02-20 14:05:36 -0500101 desc.fWidth = kTextureWidth;
102 desc.fHeight = kTextureHeight;
103 desc.fConfig = GrColorTypeToPixelConfig(colorType, srgbEncoding);
104 desc.fSampleCnt = 1;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400105
Brian Salomonc320b152018-02-20 14:05:36 -0500106 if (kUnknown_GrPixelConfig == desc.fConfig) {
107 SkASSERT(GrSRGBEncoded::kYes == srgbEncoding);
108 continue;
109 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400110
Robert Phillips9da87e02019-02-04 13:26:26 -0500111 if (!context->priv().caps()->isConfigTexturable(desc.fConfig) ||
112 (renderTarget && !context->priv().caps()->isConfigRenderable(desc.fConfig))) {
Brian Salomonc320b152018-02-20 14:05:36 -0500113 continue;
114 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400115
Brian Salomonc320b152018-02-20 14:05:36 -0500116 sk_sp<GrTexture> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Robert Phillips6fc30922018-07-27 12:21:37 -0400117 if (!tex) {
118 continue;
119 }
Robert Phillips16d8ec62017-07-27 16:16:25 -0400120
Brian Salomonc320b152018-02-20 14:05:36 -0500121 //////////////////////////
122 // transfer full data
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400123
Brian Salomonc320b152018-02-20 14:05:36 -0500124 bool result;
125 result = gpu->transferPixels(tex.get(), 0, 0, kTextureWidth, kTextureHeight, colorType,
126 buffer.get(), 0, rowBytes);
127 REPORTER_ASSERT(reporter, result);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400128
Brian Salomonc320b152018-02-20 14:05:36 -0500129 memset(dstBuffer.get(), 0xCDCD, size);
Brian Salomona6948702018-06-01 15:33:20 -0400130 result = gpu->readPixels(tex.get(), 0, 0, kTextureWidth, kTextureHeight, colorType,
Brian Salomonc320b152018-02-20 14:05:36 -0500131 dstBuffer.get(), rowBytes);
132 if (result) {
133 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_values(srcBuffer,
134 dstBuffer,
135 kTextureWidth,
136 kTextureHeight,
137 kBufferWidth,
Brian Salomona6948702018-06-01 15:33:20 -0400138 kBufferHeight));
Brian Salomonc320b152018-02-20 14:05:36 -0500139 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400140
Brian Salomonc320b152018-02-20 14:05:36 -0500141 //////////////////////////
142 // transfer partial data
Jim Van Verth3a749252018-12-07 10:57:26 -0500143#ifdef SK_BUILD_FOR_IOS
144 // UNPACK_ROW_LENGTH is broken on iOS so we can't do partial transfers
Robert Phillips4217ea72019-01-30 13:08:28 -0500145 if (GrBackendApi::kOpenGL == context->backend()) {
Jim Van Verth3a749252018-12-07 10:57:26 -0500146 continue;
147 }
148#endif
Brian Salomonc320b152018-02-20 14:05:36 -0500149 const int kLeft = 2;
150 const int kTop = 10;
151 const int kWidth = 10;
152 const int kHeight = 2;
153
154 // change color of subrectangle
155 fill_transfer_data(kLeft, kTop, kWidth, kHeight, kBufferWidth, srcBuffer.get());
156 data = buffer->map();
157 memcpy(data, srcBuffer.get(), size);
158 buffer->unmap();
159
160 size_t offset = sizeof(GrColor) * (kTop * kBufferWidth + kLeft);
161 result = gpu->transferPixels(tex.get(), kLeft, kTop, kWidth, kHeight, colorType,
162 buffer.get(), offset, rowBytes);
163 REPORTER_ASSERT(reporter, result);
164
165 memset(dstBuffer.get(), 0xCDCD, size);
Brian Salomona6948702018-06-01 15:33:20 -0400166 result = gpu->readPixels(tex.get(), 0, 0, kTextureWidth, kTextureHeight, colorType,
Brian Salomonc320b152018-02-20 14:05:36 -0500167 dstBuffer.get(), rowBytes);
168 if (result) {
169 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_values(srcBuffer,
170 dstBuffer,
171 kTextureWidth,
172 kTextureHeight,
173 kBufferWidth,
Brian Salomona6948702018-06-01 15:33:20 -0400174 kBufferHeight));
Brian Salomonc320b152018-02-20 14:05:36 -0500175 }
Jim Van Verth52fb02e2017-06-27 10:36:56 -0400176 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400177}
178
179DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsTest, reporter, ctxInfo) {
180 // RGBA
Brian Salomona6948702018-06-01 15:33:20 -0400181 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888, false);
182 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888, true);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400183
184 // BGRA
Brian Salomona6948702018-06-01 15:33:20 -0400185 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888, false);
186 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888, true);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400187}