blob: 1c82ede78ab2ea96d24918502bc7fdae9c19c001 [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
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
25using sk_gpu_test::GrContextFactory;
26
27void 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
41bool 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
Brian Salomonc320b152018-02-20 14:05:36 -050065void basic_transfer_test(skiatest::Reporter* reporter, GrContext* context, GrColorType colorType,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040066 GrSurfaceOrigin origin, bool renderTarget) {
Brian Salomon7f56d3d2017-10-09 13:02:49 -040067 if (GrCaps::kNone_MapFlags == context->caps()->mapBufferFlags()) {
68 return;
69 }
70
Robert Phillips6be756b2018-01-16 15:07:54 -050071 auto resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050072 GrGpu* gpu = context->contextPriv().getGpu();
Robert Phillips6be756b2018-01-16 15:07:54 -050073
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040074 // set up the data
75 const int kTextureWidth = 16;
76 const int kTextureHeight = 16;
77 const int kBufferWidth = 20;
78 const int kBufferHeight = 16;
79 size_t rowBytes = kBufferWidth * sizeof(GrColor);
80 SkAutoTMalloc<GrColor> srcBuffer(kBufferWidth*kBufferHeight);
81 SkAutoTMalloc<GrColor> dstBuffer(kBufferWidth*kBufferHeight);
82
83 fill_transfer_data(0, 0, kTextureWidth, kTextureHeight, kBufferWidth, srcBuffer.get());
84
85 // create and fill transfer buffer
86 size_t size = rowBytes*kBufferHeight;
87 uint32_t bufferFlags = GrResourceProvider::kNoPendingIO_Flag;
Robert Phillips6be756b2018-01-16 15:07:54 -050088 sk_sp<GrBuffer> buffer(resourceProvider->createBuffer(size,
89 kXferCpuToGpu_GrBufferType,
90 kDynamic_GrAccessPattern,
91 bufferFlags));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040092 if (!buffer) {
93 return;
94 }
95
96 void* data = buffer->map();
97 memcpy(data, srcBuffer.get(), size);
98 buffer->unmap();
99
Brian Salomonc320b152018-02-20 14:05:36 -0500100 for (auto srgbEncoding : {GrSRGBEncoded::kNo, GrSRGBEncoded::kYes}) {
101 // create texture
102 GrSurfaceDesc desc;
103 desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
104 desc.fOrigin = origin;
105 desc.fWidth = kTextureWidth;
106 desc.fHeight = kTextureHeight;
107 desc.fConfig = GrColorTypeToPixelConfig(colorType, srgbEncoding);
108 desc.fSampleCnt = 1;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400109
Brian Salomonc320b152018-02-20 14:05:36 -0500110 if (kUnknown_GrPixelConfig == desc.fConfig) {
111 SkASSERT(GrSRGBEncoded::kYes == srgbEncoding);
112 continue;
113 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400114
Brian Salomonc320b152018-02-20 14:05:36 -0500115 if (!context->caps()->isConfigTexturable(desc.fConfig) ||
116 (renderTarget && !context->caps()->isConfigRenderable(desc.fConfig))) {
117 continue;
118 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400119
Brian Salomonc320b152018-02-20 14:05:36 -0500120 sk_sp<GrTexture> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400121
Brian Salomonc320b152018-02-20 14:05:36 -0500122 //////////////////////////
123 // transfer full data
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400124
Brian Salomonc320b152018-02-20 14:05:36 -0500125 bool result;
126 result = gpu->transferPixels(tex.get(), 0, 0, kTextureWidth, kTextureHeight, colorType,
127 buffer.get(), 0, rowBytes);
128 REPORTER_ASSERT(reporter, result);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400129
Brian Salomonc320b152018-02-20 14:05:36 -0500130 memset(dstBuffer.get(), 0xCDCD, size);
131 result = gpu->readPixels(tex.get(), origin, 0, 0, kTextureWidth, kTextureHeight, colorType,
132 dstBuffer.get(), rowBytes);
133 if (result) {
134 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_values(srcBuffer,
135 dstBuffer,
136 kTextureWidth,
137 kTextureHeight,
138 kBufferWidth,
139 kBufferHeight,
140 origin));
141 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400142
Brian Salomonc320b152018-02-20 14:05:36 -0500143 //////////////////////////
144 // transfer partial data
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400145
Brian Salomonc320b152018-02-20 14:05:36 -0500146 const int kLeft = 2;
147 const int kTop = 10;
148 const int kWidth = 10;
149 const int kHeight = 2;
150
151 // change color of subrectangle
152 fill_transfer_data(kLeft, kTop, kWidth, kHeight, kBufferWidth, srcBuffer.get());
153 data = buffer->map();
154 memcpy(data, srcBuffer.get(), size);
155 buffer->unmap();
156
157 size_t offset = sizeof(GrColor) * (kTop * kBufferWidth + kLeft);
158 result = gpu->transferPixels(tex.get(), kLeft, kTop, kWidth, kHeight, colorType,
159 buffer.get(), offset, rowBytes);
160 REPORTER_ASSERT(reporter, result);
161
162 memset(dstBuffer.get(), 0xCDCD, size);
163 result = gpu->readPixels(tex.get(), origin, 0, 0, kTextureWidth, kTextureHeight, colorType,
164 dstBuffer.get(), rowBytes);
165 if (result) {
166 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_values(srcBuffer,
167 dstBuffer,
168 kTextureWidth,
169 kTextureHeight,
170 kBufferWidth,
171 kBufferHeight,
172 origin));
173 }
Jim Van Verth52fb02e2017-06-27 10:36:56 -0400174 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400175}
176
177DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsTest, reporter, ctxInfo) {
178 // RGBA
Brian Salomonc320b152018-02-20 14:05:36 -0500179 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400180 kTopLeft_GrSurfaceOrigin, false);
Brian Salomonc320b152018-02-20 14:05:36 -0500181 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400182 kTopLeft_GrSurfaceOrigin, true);
Brian Salomonc320b152018-02-20 14:05:36 -0500183 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400184 kBottomLeft_GrSurfaceOrigin, false);
Brian Salomonc320b152018-02-20 14:05:36 -0500185 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400186 kBottomLeft_GrSurfaceOrigin, true);
187
188 // BGRA
Brian Salomonc320b152018-02-20 14:05:36 -0500189 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400190 kTopLeft_GrSurfaceOrigin, false);
Brian Salomonc320b152018-02-20 14:05:36 -0500191 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400192 kTopLeft_GrSurfaceOrigin, true);
Brian Salomonc320b152018-02-20 14:05:36 -0500193 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400194 kBottomLeft_GrSurfaceOrigin, false);
Brian Salomonc320b152018-02-20 14:05:36 -0500195 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400196 kBottomLeft_GrSurfaceOrigin, true);
197}
198
199#endif