blob: ce8d1edc4f27338872369944127a914bbb0986e8 [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
65void basic_transfer_test(skiatest::Reporter* reporter, GrContext* context, GrPixelConfig config,
66 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
100 // create texture
101 GrSurfaceDesc desc;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400102 desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
103 desc.fOrigin = origin;
104 desc.fWidth = kTextureWidth;
105 desc.fHeight = kTextureHeight;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400106 desc.fConfig = config;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400107 desc.fSampleCnt = 0;
Robert Phillips6be756b2018-01-16 15:07:54 -0500108 sk_sp<GrTexture> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400109
110 //////////////////////////
111 // transfer full data
112
113 bool result;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500114 result = gpu->transferPixels(tex.get(), 0, 0, kTextureWidth, kTextureHeight,
115 config, buffer.get(), 0, rowBytes);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400116 REPORTER_ASSERT(reporter, result);
117
118 memset(dstBuffer.get(), 0xCDCD, size);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500119 result = gpu->readPixels(tex.get(), origin, 0, 0, kTextureWidth, kTextureHeight,
120 config, dstBuffer.get(), rowBytes);
Jim Van Verth52fb02e2017-06-27 10:36:56 -0400121 if (result) {
122 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_values(srcBuffer,
123 dstBuffer,
124 kTextureWidth,
125 kTextureHeight,
126 kBufferWidth,
127 kBufferHeight,
128 origin));
129 }
Robert Phillips16d8ec62017-07-27 16:16:25 -0400130
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400131 //////////////////////////
132 // transfer partial data
133
134 const int kLeft = 2;
135 const int kTop = 10;
136 const int kWidth = 10;
137 const int kHeight = 2;
138
139 // change color of subrectangle
140 fill_transfer_data(kLeft, kTop, kWidth, kHeight, kBufferWidth, srcBuffer.get());
141 data = buffer->map();
142 memcpy(data, srcBuffer.get(), size);
143 buffer->unmap();
144
145 size_t offset = sizeof(GrColor)*(kTop*kBufferWidth + kLeft);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500146 result = gpu->transferPixels(tex.get(), kLeft, kTop, kWidth, kHeight, config,
147 buffer.get(), offset, rowBytes);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400148 REPORTER_ASSERT(reporter, result);
149
150 memset(dstBuffer.get(), 0xCDCD, size);
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500151 result = gpu->readPixels(tex.get(), origin, 0, 0, kTextureWidth, kTextureHeight,
152 config, dstBuffer.get(), rowBytes);
Jim Van Verth52fb02e2017-06-27 10:36:56 -0400153 if (result) {
154 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_values(srcBuffer,
155 dstBuffer,
156 kTextureWidth,
157 kTextureHeight,
158 kBufferWidth,
159 kBufferHeight,
160 origin));
161 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400162}
163
164DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsTest, reporter, ctxInfo) {
165 // RGBA
166 basic_transfer_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig,
167 kTopLeft_GrSurfaceOrigin, false);
168 basic_transfer_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig,
169 kTopLeft_GrSurfaceOrigin, true);
170 basic_transfer_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig,
171 kBottomLeft_GrSurfaceOrigin, false);
172 basic_transfer_test(reporter, ctxInfo.grContext(), kRGBA_8888_GrPixelConfig,
173 kBottomLeft_GrSurfaceOrigin, true);
174
175 // BGRA
176 basic_transfer_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig,
177 kTopLeft_GrSurfaceOrigin, false);
178 basic_transfer_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig,
179 kTopLeft_GrSurfaceOrigin, true);
180 basic_transfer_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig,
181 kBottomLeft_GrSurfaceOrigin, false);
182 basic_transfer_test(reporter, ctxInfo.grContext(), kBGRA_8888_GrPixelConfig,
183 kBottomLeft_GrSurfaceOrigin, true);
184}
185
186#endif