blob: e17dfac14b0cdfba1827b8d4071a6f7c3e0a66c2 [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;
Brian Salomonc320b152018-02-20 14:05:36 -0500104 desc.fWidth = kTextureWidth;
105 desc.fHeight = kTextureHeight;
106 desc.fConfig = GrColorTypeToPixelConfig(colorType, srgbEncoding);
107 desc.fSampleCnt = 1;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400108
Brian Salomonc320b152018-02-20 14:05:36 -0500109 if (kUnknown_GrPixelConfig == desc.fConfig) {
110 SkASSERT(GrSRGBEncoded::kYes == srgbEncoding);
111 continue;
112 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400113
Brian Salomonc320b152018-02-20 14:05:36 -0500114 if (!context->caps()->isConfigTexturable(desc.fConfig) ||
115 (renderTarget && !context->caps()->isConfigRenderable(desc.fConfig))) {
116 continue;
117 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400118
Brian Salomonc320b152018-02-20 14:05:36 -0500119 sk_sp<GrTexture> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
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);
130 result = gpu->readPixels(tex.get(), origin, 0, 0, kTextureWidth, kTextureHeight, colorType,
131 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,
138 kBufferHeight,
139 origin));
140 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400141
Brian Salomonc320b152018-02-20 14:05:36 -0500142 //////////////////////////
143 // transfer partial data
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400144
Brian Salomonc320b152018-02-20 14:05:36 -0500145 const int kLeft = 2;
146 const int kTop = 10;
147 const int kWidth = 10;
148 const int kHeight = 2;
149
150 // change color of subrectangle
151 fill_transfer_data(kLeft, kTop, kWidth, kHeight, kBufferWidth, srcBuffer.get());
152 data = buffer->map();
153 memcpy(data, srcBuffer.get(), size);
154 buffer->unmap();
155
156 size_t offset = sizeof(GrColor) * (kTop * kBufferWidth + kLeft);
157 result = gpu->transferPixels(tex.get(), kLeft, kTop, kWidth, kHeight, colorType,
158 buffer.get(), offset, rowBytes);
159 REPORTER_ASSERT(reporter, result);
160
161 memset(dstBuffer.get(), 0xCDCD, size);
162 result = gpu->readPixels(tex.get(), origin, 0, 0, kTextureWidth, kTextureHeight, colorType,
163 dstBuffer.get(), rowBytes);
164 if (result) {
165 REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_values(srcBuffer,
166 dstBuffer,
167 kTextureWidth,
168 kTextureHeight,
169 kBufferWidth,
170 kBufferHeight,
171 origin));
172 }
Jim Van Verth52fb02e2017-06-27 10:36:56 -0400173 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400174}
175
176DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsTest, reporter, ctxInfo) {
177 // RGBA
Brian Salomonc320b152018-02-20 14:05:36 -0500178 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400179 kTopLeft_GrSurfaceOrigin, false);
Brian Salomonc320b152018-02-20 14:05:36 -0500180 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400181 kTopLeft_GrSurfaceOrigin, true);
Brian Salomonc320b152018-02-20 14:05:36 -0500182 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400183 kBottomLeft_GrSurfaceOrigin, false);
Brian Salomonc320b152018-02-20 14:05:36 -0500184 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400185 kBottomLeft_GrSurfaceOrigin, true);
186
187 // BGRA
Brian Salomonc320b152018-02-20 14:05:36 -0500188 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400189 kTopLeft_GrSurfaceOrigin, false);
Brian Salomonc320b152018-02-20 14:05:36 -0500190 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400191 kTopLeft_GrSurfaceOrigin, true);
Brian Salomonc320b152018-02-20 14:05:36 -0500192 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400193 kBottomLeft_GrSurfaceOrigin, false);
Brian Salomonc320b152018-02-20 14:05:36 -0500194 basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400195 kBottomLeft_GrSurfaceOrigin, true);
196}
197
198#endif