Repository navigation
Expand file tree
/
Copy pathCopySurfaceTest.cpp
More file actions
215 lines (193 loc) · 10.4 KB
/
Copy pathCopySurfaceTest.cpp
File metadata and controls
215 lines (193 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkAlphaType.h"
#include "include/core/SkColorType.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkTypes.h"
#include "include/gpu/GrBackendSurface.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/GrTypes.h"
#include "include/private/base/SkTemplates.h"
#include "src/core/SkOpts.h"
#include "src/gpu/ganesh/GrDirectContextPriv.h"
#include "src/gpu/ganesh/GrImageInfo.h"
#include "src/gpu/ganesh/GrPixmap.h"
#include "src/gpu/ganesh/GrSurfaceProxyView.h"
#include "src/gpu/ganesh/SurfaceContext.h"
#include "src/gpu/ganesh/SurfaceFillContext.h"
#include "tests/CtsEnforcement.h"
#include "tests/Test.h"
#include "tools/gpu/ProxyUtils.h"
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <memory>
#include <utility>
using namespace skia_private;
struct GrContextOptions;
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(CopySurface,
reporter,
ctxInfo,
CtsEnforcement::kApiLevel_T) {
auto dContext = ctxInfo.directContext();
static const int kW = 10;
static const int kH = 10;
static const size_t kRowBytes = sizeof(uint32_t) * kW;
AutoTMalloc<uint32_t> srcPixels(kW * kH);
for (int i = 0; i < kW * kH; ++i) {
srcPixels.get()[i] = i;
}
AutoTMalloc<uint32_t> dstPixels(kW * kH);
for (int i = 0; i < kW * kH; ++i) {
dstPixels.get()[i] = ~i;
}
static const SkIRect kSrcRects[] {
{ 0, 0, kW , kH },
{-1, -1, kW+1, kH+1},
{ 1, 1, kW-1, kH-1},
{ 5, 5, 6 , 6 },
};
static const SkIPoint kDstPoints[] {
{ 0 , 0 },
{ 1 , 1 },
{ kW/2, kH/4},
{ kW-1, kH-1},
{ kW , kH },
{ kW+1, kH+2},
{-1 , -1 },
};
static const SkImageInfo kImageInfos[] {
SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
SkImageInfo::Make(kW, kH, kBGRA_8888_SkColorType, kPremul_SkAlphaType)
};
AutoTMalloc<uint32_t> read(kW * kH);
for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
for (auto sRenderable : {GrRenderable::kYes, GrRenderable::kNo}) {
for (auto dRenderable : {GrRenderable::kYes, GrRenderable::kNo}) {
for (const SkIRect& srcRect : kSrcRects) {
for (const SkIPoint& dstPoint : kDstPoints) {
for (const SkImageInfo& ii: kImageInfos) {
GrCPixmap srcPM(ii, srcPixels.get(), kRowBytes);
GrPixmap dstPM(ii, dstPixels.get(), kRowBytes);
auto srcView = sk_gpu_test::MakeTextureProxyViewFromData(
dContext, sRenderable, sOrigin, srcPM);
auto dstView = sk_gpu_test::MakeTextureProxyViewFromData(
dContext, dRenderable, dOrigin, dstPM);
// Should always work if the color type is RGBA, but may not work
// for BGRA
if (ii.colorType() == kRGBA_8888_SkColorType) {
if (!srcView || !dstView) {
ERRORF(reporter,
"Could not create surfaces for copy surface test.");
continue;
}
} else {
if (!dContext->defaultBackendFormat(
kBGRA_8888_SkColorType, GrRenderable::kNo).isValid()) {
continue;
}
if (!srcView || !dstView) {
ERRORF(reporter,
"Could not create surfaces for copy surface test.");
continue;
}
}
auto dstContext = dContext->priv().makeSC(std::move(dstView),
ii.colorInfo());
bool result = false;
if (sOrigin == dOrigin) {
result = dstContext->testCopy(srcView.refProxy(),
srcRect,
dstPoint);
} else if (dRenderable == GrRenderable::kYes) {
SkASSERT(dstContext->asFillContext());
result = dstContext->asFillContext()->blitTexture(
std::move(srcView), srcRect, dstPoint);
}
bool expectedResult = true;
SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
dstPoint.fY - srcRect.fTop };
SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
dstPoint.fY,
srcRect.width(),
srcRect.height());
SkIRect copiedSrcRect;
if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
expectedResult = false;
} else {
// If the src rect was clipped, apply same clipping to each side
// of copied dst rect.
copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
copiedDstRect.fBottom -= copiedSrcRect.fBottom -
srcRect.fBottom;
}
if (copiedDstRect.isEmpty() ||
!copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
expectedResult = false;
}
if (sOrigin != dOrigin && dRenderable == GrRenderable::kNo) {
expectedResult = false;
}
// To make the copied src rect correct we would apply any dst
// clipping back to the src rect, but we don't use it again so
// don't bother.
if (expectedResult != result) {
ERRORF(reporter, "Expected return value %d from copySurface, "
"got %d.", expectedResult, result);
continue;
}
if (!expectedResult || !result) {
continue;
}
sk_memset32(read.get(), 0, kW * kH);
GrPixmap readPM(ii, read.get(), kRowBytes);
if (!dstContext->readPixels(dContext, readPM, {0, 0})) {
ERRORF(reporter, "Error calling readPixels");
continue;
}
bool abort = false;
// Validate that pixels inside copiedDstRect received the correct
// value from src and that those outside were not modified.
for (int y = 0; y < kH && !abort; ++y) {
for (int x = 0; x < kW; ++x) {
uint32_t r = read.get()[y * kW + x];
if (copiedDstRect.contains(x, y)) {
int sx = x - dstOffset.fX;
int sy = y - dstOffset.fY;
uint32_t s = srcPixels.get()[sy * kW + sx];
if (s != r) {
ERRORF(reporter, "Expected dst %d,%d to contain "
"0x%08x copied from src location %d,%d. Got "
"0x%08x", x, y, s, sx, sy, r);
abort = true;
break;
}
} else {
uint32_t d = dstPixels.get()[y * kW + x];
if (d != r) {
ERRORF(reporter, "Expected dst %d,%d to be "
"unmodified (0x%08x). Got 0x%08x",
x, y, d, r);
abort = true;
break;
}
}
}
}
}
}
}
}
}
}
}
}