blob: 79a411ffd1e64cc675e7ebc282588e94ab162972 [file] [log] [blame]
bsalomonb8fea972016-02-16 07:34:17 -08001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkImageInfo.h"
9#include "include/core/SkPoint.h"
10#include "include/core/SkRect.h"
11#include "include/core/SkRefCnt.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040012#include "include/core/SkTypes.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040013#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/gpu/GrTypes.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040015#include "include/private/GrTypesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkTemplates.h"
Kevin Lubick3a9a7992021-11-03 08:49:00 -040017#include "src/core/SkOpts.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040018#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040019#include "src/gpu/GrDirectContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040020#include "src/gpu/GrImageInfo.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040021#include "src/gpu/GrSurfaceProxy.h"
22#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/SkGr.h"
Robert Phillipsf3868622021-08-04 13:27:43 -040024#include "src/gpu/SurfaceFillContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "tests/Test.h"
26#include "tools/gpu/GrContextFactory.h"
27#include "tools/gpu/ProxyUtils.h"
Ben Wagnereed61282018-04-17 14:14:51 -040028
Ben Wagner9707a7e2019-05-06 17:17:19 -040029#include <initializer_list>
Ben Wagnereed61282018-04-17 14:14:51 -040030#include <utility>
bsalomonb8fea972016-02-16 07:34:17 -080031
bsalomon68d91342016-04-12 09:59:58 -070032DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) {
Adlai Hollerc95b5892020-08-11 12:02:22 -040033 auto dContext = ctxInfo.directContext();
Robert Phillips6d344c32020-07-06 10:56:46 -040034
bsalomonb8fea972016-02-16 07:34:17 -080035 static const int kW = 10;
36 static const int kH = 10;
37 static const size_t kRowBytes = sizeof(uint32_t) * kW;
38
bsalomonb8fea972016-02-16 07:34:17 -080039 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
Timothy Liange30739a2018-07-31 10:51:17 -040066 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 };
Robert Phillipsd46697a2017-01-25 12:10:37 -050070
bsalomonb8fea972016-02-16 07:34:17 -080071 SkAutoTMalloc<uint32_t> read(kW * kH);
72
73 for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
74 for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040075 for (auto sRenderable : {GrRenderable::kYes, GrRenderable::kNo}) {
76 for (auto dRenderable : {GrRenderable::kYes, GrRenderable::kNo}) {
John Stilesbd3ffa42020-07-30 20:24:57 -040077 for (const SkIRect& srcRect : kSrcRects) {
78 for (const SkIPoint& dstPoint : kDstPoints) {
79 for (const SkImageInfo& ii: kImageInfos) {
Brian Salomon5392c942021-03-30 16:14:37 -040080 GrCPixmap srcPM(ii, srcPixels.get(), kRowBytes);
81 GrPixmap dstPM(ii, dstPixels.get(), kRowBytes);
Brian Salomon652124c2020-08-12 14:06:50 -040082 auto srcView = sk_gpu_test::MakeTextureProxyViewFromData(
Brian Salomondd4087d2020-12-23 20:36:44 -050083 dContext, sRenderable, sOrigin, srcPM);
Brian Salomon652124c2020-08-12 14:06:50 -040084 auto dstView = sk_gpu_test::MakeTextureProxyViewFromData(
Brian Salomondd4087d2020-12-23 20:36:44 -050085 dContext, dRenderable, dOrigin, dstPM);
bsalomonb8fea972016-02-16 07:34:17 -080086
Timothy Liange30739a2018-07-31 10:51:17 -040087 // Should always work if the color type is RGBA, but may not work
88 // for BGRA
Timothy Liangdeba2122018-08-07 13:22:03 -040089 if (ii.colorType() == kRGBA_8888_SkColorType) {
Brian Salomon652124c2020-08-12 14:06:50 -040090 if (!srcView || !dstView) {
Timothy Liange30739a2018-07-31 10:51:17 -040091 ERRORF(reporter,
92 "Could not create surfaces for copy surface test.");
93 continue;
94 }
95 } else {
Adlai Hollerc95b5892020-08-11 12:02:22 -040096 if (!dContext->defaultBackendFormat(
Greg Daniel7bfc9132019-08-14 14:23:53 -040097 kBGRA_8888_SkColorType, GrRenderable::kNo).isValid()) {
Timothy Liange30739a2018-07-31 10:51:17 -040098 continue;
99 }
Brian Salomon652124c2020-08-12 14:06:50 -0400100 if (!srcView || !dstView) {
Timothy Liange30739a2018-07-31 10:51:17 -0400101 ERRORF(reporter,
102 "Could not create surfaces for copy surface test.");
103 continue;
104 }
105 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500106
Robert Phillips33bf2b52021-08-02 11:14:38 -0400107 auto dstContext = dContext->priv().makeSC(std::move(dstView),
108 ii.colorInfo());
bsalomonb8fea972016-02-16 07:34:17 -0800109
Greg Daniel46cfbc62019-06-07 11:43:30 -0400110 bool result = false;
111 if (sOrigin == dOrigin) {
Brian Salomon982127b2021-01-21 10:43:35 -0500112 result = dstContext->testCopy(srcView.refProxy(),
Brian Salomon652124c2020-08-12 14:06:50 -0400113 srcRect,
114 dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400115 } else if (dRenderable == GrRenderable::kYes) {
Brian Salomon590f5672020-12-16 11:44:47 -0500116 SkASSERT(dstContext->asFillContext());
117 result = dstContext->asFillContext()->blitTexture(
Brian Salomon652124c2020-08-12 14:06:50 -0400118 std::move(srcView), srcRect, dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400119 }
bsalomonb8fea972016-02-16 07:34:17 -0800120
Timothy Liange30739a2018-07-31 10:51:17 -0400121 bool expectedResult = true;
122 SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
123 dstPoint.fY - srcRect.fTop };
124 SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
125 dstPoint.fY,
126 srcRect.width(),
127 srcRect.height());
bsalomonb8fea972016-02-16 07:34:17 -0800128
Timothy Liange30739a2018-07-31 10:51:17 -0400129 SkIRect copiedSrcRect;
130 if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
131 expectedResult = false;
132 } else {
133 // If the src rect was clipped, apply same clipping to each side
134 // of copied dst rect.
135 copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
136 copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
137 copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
138 copiedDstRect.fBottom -= copiedSrcRect.fBottom -
139 srcRect.fBottom;
140 }
141 if (copiedDstRect.isEmpty() ||
142 !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
143 expectedResult = false;
144 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400145 if (sOrigin != dOrigin && dRenderable == GrRenderable::kNo) {
146 expectedResult = false;
147 }
148
Timothy Liange30739a2018-07-31 10:51:17 -0400149 // To make the copied src rect correct we would apply any dst
150 // clipping back to the src rect, but we don't use it again so
151 // don't bother.
152 if (expectedResult != result) {
153 ERRORF(reporter, "Expected return value %d from copySurface, "
154 "got %d.", expectedResult, result);
155 continue;
156 }
bsalomonb8fea972016-02-16 07:34:17 -0800157
Timothy Liange30739a2018-07-31 10:51:17 -0400158 if (!expectedResult || !result) {
159 continue;
160 }
bsalomonb8fea972016-02-16 07:34:17 -0800161
Timothy Liange30739a2018-07-31 10:51:17 -0400162 sk_memset32(read.get(), 0, kW * kH);
Brian Salomondd4087d2020-12-23 20:36:44 -0500163 GrPixmap readPM(ii, read.get(), kRowBytes);
164 if (!dstContext->readPixels(dContext, readPM, {0, 0})) {
Timothy Liange30739a2018-07-31 10:51:17 -0400165 ERRORF(reporter, "Error calling readPixels");
166 continue;
167 }
168
169 bool abort = false;
170 // Validate that pixels inside copiedDstRect received the correct
171 // value from src and that those outside were not modified.
172 for (int y = 0; y < kH && !abort; ++y) {
173 for (int x = 0; x < kW; ++x) {
174 uint32_t r = read.get()[y * kW + x];
175 if (copiedDstRect.contains(x, y)) {
176 int sx = x - dstOffset.fX;
177 int sy = y - dstOffset.fY;
178 uint32_t s = srcPixels.get()[sy * kW + sx];
179 if (s != r) {
180 ERRORF(reporter, "Expected dst %d,%d to contain "
181 "0x%08x copied from src location %d,%d. Got "
182 "0x%08x", x, y, s, sx, sy, r);
183 abort = true;
184 break;
185 }
186 } else {
187 uint32_t d = dstPixels.get()[y * kW + x];
188 if (d != r) {
189 ERRORF(reporter, "Expected dst %d,%d to be "
190 "unmodified (0x%08x). Got 0x%08x",
191 x, y, d, r);
192 abort = true;
193 break;
194 }
bsalomonb8fea972016-02-16 07:34:17 -0800195 }
196 }
197 }
198 }
199 }
200 }
201 }
202 }
203 }
204 }
205}