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 
fill_transfer_data(int left,int top,int width,int height,int bufferWidth,GrColor * data)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 
does_full_buffer_contain_correct_values(GrColor * srcBuffer,GrColor * dstBuffer,int width,int height,int bufferWidth,int bufferHeight,GrSurfaceOrigin origin)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 
basic_transfer_test(skiatest::Reporter * reporter,GrContext * context,GrColorType colorType,GrSurfaceOrigin origin,bool renderTarget)65 void basic_transfer_test(skiatest::Reporter* reporter, GrContext* context, GrColorType colorType,
66                          GrSurfaceOrigin origin, bool renderTarget) {
67     if (GrCaps::kNone_MapFlags == context->caps()->mapBufferFlags()) {
68         return;
69     }
70 
71     auto resourceProvider = context->contextPriv().resourceProvider();
72     GrGpu* gpu = context->contextPriv().getGpu();
73 
74     // 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;
88     sk_sp<GrBuffer> buffer(resourceProvider->createBuffer(size,
89                                                           kXferCpuToGpu_GrBufferType,
90                                                           kDynamic_GrAccessPattern,
91                                                           bufferFlags));
92     if (!buffer) {
93         return;
94     }
95 
96     void* data = buffer->map();
97     memcpy(data, srcBuffer.get(), size);
98     buffer->unmap();
99 
100     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;
109 
110         if (kUnknown_GrPixelConfig == desc.fConfig) {
111             SkASSERT(GrSRGBEncoded::kYes == srgbEncoding);
112             continue;
113         }
114 
115         if (!context->caps()->isConfigTexturable(desc.fConfig) ||
116             (renderTarget && !context->caps()->isConfigRenderable(desc.fConfig))) {
117             continue;
118         }
119 
120         sk_sp<GrTexture> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
121 
122         //////////////////////////
123         // transfer full data
124 
125         bool result;
126         result = gpu->transferPixels(tex.get(), 0, 0, kTextureWidth, kTextureHeight, colorType,
127                                      buffer.get(), 0, rowBytes);
128         REPORTER_ASSERT(reporter, result);
129 
130         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         }
142 
143         //////////////////////////
144         // transfer partial data
145 
146         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         }
174     }
175 }
176 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsTest,reporter,ctxInfo)177 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsTest, reporter, ctxInfo) {
178     // RGBA
179     basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888,
180                         kTopLeft_GrSurfaceOrigin, false);
181     basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888,
182                         kTopLeft_GrSurfaceOrigin, true);
183     basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888,
184                         kBottomLeft_GrSurfaceOrigin, false);
185     basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888,
186                         kBottomLeft_GrSurfaceOrigin, true);
187 
188     // BGRA
189     basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888,
190                         kTopLeft_GrSurfaceOrigin, false);
191     basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888,
192                         kTopLeft_GrSurfaceOrigin, true);
193     basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888,
194                         kBottomLeft_GrSurfaceOrigin, false);
195     basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888,
196                         kBottomLeft_GrSurfaceOrigin, true);
197 }
198 
199 #endif
200