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