1 /*
2 * Copyright 2016 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 #include "include/core/SkImageInfo.h"
9 #include "include/core/SkPoint.h"
10 #include "include/core/SkRect.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkTypes.h"
13 #include "include/gpu/GrContext.h"
14 #include "include/gpu/GrTypes.h"
15 #include "include/private/GrTypesPriv.h"
16 #include "include/private/SkTemplates.h"
17 #include "src/core/SkUtils.h"
18 #include "src/gpu/GrCaps.h"
19 #include "src/gpu/GrContextPriv.h"
20 #include "src/gpu/GrImageInfo.h"
21 #include "src/gpu/GrRenderTargetContext.h"
22 #include "src/gpu/GrSurfaceContext.h"
23 #include "src/gpu/GrSurfaceProxy.h"
24 #include "src/gpu/GrTextureProxy.h"
25 #include "src/gpu/SkGr.h"
26 #include "tests/Test.h"
27 #include "tools/gpu/GrContextFactory.h"
28 #include "tools/gpu/ProxyUtils.h"
29
30 #include <initializer_list>
31 #include <utility>
32
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface,reporter,ctxInfo)33 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) {
34 GrContext* context = ctxInfo.grContext();
35 static const int kW = 10;
36 static const int kH = 10;
37 static const size_t kRowBytes = sizeof(uint32_t) * kW;
38
39 SkAutoTMalloc<uint32_t> srcPixels(kW * kH);
40 for (int i = 0; i < kW * kH; ++i) {
41 srcPixels.get()[i] = i;
42 }
43
44 SkAutoTMalloc<uint32_t> dstPixels(kW * kH);
45 for (int i = 0; i < kW * kH; ++i) {
46 dstPixels.get()[i] = ~i;
47 }
48
49 static const SkIRect kSrcRects[] {
50 { 0, 0, kW , kH },
51 {-1, -1, kW+1, kH+1},
52 { 1, 1, kW-1, kH-1},
53 { 5, 5, 6 , 6 },
54 };
55
56 static const SkIPoint kDstPoints[] {
57 { 0 , 0 },
58 { 1 , 1 },
59 { kW/2, kH/4},
60 { kW-1, kH-1},
61 { kW , kH },
62 { kW+1, kH+2},
63 {-1 , -1 },
64 };
65
66 static const SkImageInfo kImageInfos[] {
67 SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
68 SkImageInfo::Make(kW, kH, kBGRA_8888_SkColorType, kPremul_SkAlphaType)
69 };
70
71 SkAutoTMalloc<uint32_t> read(kW * kH);
72
73 for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
74 for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
75 for (auto sRenderable : {GrRenderable::kYes, GrRenderable::kNo}) {
76 for (auto dRenderable : {GrRenderable::kYes, GrRenderable::kNo}) {
77 for (auto srcRect : kSrcRects) {
78 for (auto dstPoint : kDstPoints) {
79 for (auto ii: kImageInfos) {
80 auto src = sk_gpu_test::MakeTextureProxyFromData(
81 context, sRenderable, sOrigin, ii, srcPixels.get(),
82 kRowBytes);
83 auto dst = sk_gpu_test::MakeTextureProxyFromData(
84 context, dRenderable, dOrigin, ii, dstPixels.get(),
85 kRowBytes);
86
87 // Should always work if the color type is RGBA, but may not work
88 // for BGRA
89 if (ii.colorType() == kRGBA_8888_SkColorType) {
90 if (!src || !dst) {
91 ERRORF(reporter,
92 "Could not create surfaces for copy surface test.");
93 continue;
94 }
95 } else {
96 if (!context->defaultBackendFormat(
97 kBGRA_8888_SkColorType, GrRenderable::kNo).isValid()) {
98 continue;
99 }
100 if (!src || !dst) {
101 ERRORF(reporter,
102 "Could not create surfaces for copy surface test.");
103 continue;
104 }
105 }
106
107 GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
108 GrSwizzle dstSwizzle = context->priv().caps()->getReadSwizzle(
109 dst->backendFormat(), grColorType);
110 GrSurfaceProxyView dstView(std::move(dst), dOrigin, dstSwizzle);
111 auto dstContext = GrSurfaceContext::Make(context,
112 std::move(dstView),
113 grColorType,
114 ii.alphaType(), nullptr);
115
116 bool result = false;
117 if (sOrigin == dOrigin) {
118 result = dstContext->testCopy(src.get(), sOrigin, srcRect,
119 dstPoint);
120 } else if (dRenderable == GrRenderable::kYes) {
121 SkASSERT(dstContext->asRenderTargetContext());
122 GrSwizzle srcSwizzle = context->priv().caps()->getReadSwizzle(
123 src->backendFormat(), grColorType);
124 GrSurfaceProxyView view(std::move(src), sOrigin, srcSwizzle);
125 result = dstContext->asRenderTargetContext()->blitTexture(
126 std::move(view), srcRect, dstPoint);
127 }
128
129 bool expectedResult = true;
130 SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
131 dstPoint.fY - srcRect.fTop };
132 SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
133 dstPoint.fY,
134 srcRect.width(),
135 srcRect.height());
136
137 SkIRect copiedSrcRect;
138 if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
139 expectedResult = false;
140 } else {
141 // If the src rect was clipped, apply same clipping to each side
142 // of copied dst rect.
143 copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
144 copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
145 copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
146 copiedDstRect.fBottom -= copiedSrcRect.fBottom -
147 srcRect.fBottom;
148 }
149 if (copiedDstRect.isEmpty() ||
150 !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
151 expectedResult = false;
152 }
153 if (sOrigin != dOrigin && dRenderable == GrRenderable::kNo) {
154 expectedResult = false;
155 }
156
157 // To make the copied src rect correct we would apply any dst
158 // clipping back to the src rect, but we don't use it again so
159 // don't bother.
160 if (expectedResult != result) {
161 ERRORF(reporter, "Expected return value %d from copySurface, "
162 "got %d.", expectedResult, result);
163 continue;
164 }
165
166 if (!expectedResult || !result) {
167 continue;
168 }
169
170 sk_memset32(read.get(), 0, kW * kH);
171 if (!dstContext->readPixels(ii, read.get(), kRowBytes, {0, 0})) {
172 ERRORF(reporter, "Error calling readPixels");
173 continue;
174 }
175
176 bool abort = false;
177 // Validate that pixels inside copiedDstRect received the correct
178 // value from src and that those outside were not modified.
179 for (int y = 0; y < kH && !abort; ++y) {
180 for (int x = 0; x < kW; ++x) {
181 uint32_t r = read.get()[y * kW + x];
182 if (copiedDstRect.contains(x, y)) {
183 int sx = x - dstOffset.fX;
184 int sy = y - dstOffset.fY;
185 uint32_t s = srcPixels.get()[sy * kW + sx];
186 if (s != r) {
187 ERRORF(reporter, "Expected dst %d,%d to contain "
188 "0x%08x copied from src location %d,%d. Got "
189 "0x%08x", x, y, s, sx, sy, r);
190 abort = true;
191 break;
192 }
193 } else {
194 uint32_t d = dstPixels.get()[y * kW + x];
195 if (d != r) {
196 ERRORF(reporter, "Expected dst %d,%d to be "
197 "unmodified (0x%08x). Got 0x%08x",
198 x, y, d, r);
199 abort = true;
200 break;
201 }
202 }
203 }
204 }
205 }
206 }
207 }
208 }
209 }
210 }
211 }
212 }
213