blob: b3eb1747dd3d9a6c91c26a005c417e3bc351a30e [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/gpu/GrContext.h"
14#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"
17#include "src/core/SkUtils.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040018#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040020#include "src/gpu/GrImageInfo.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040021#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040023#include "src/gpu/GrSurfaceProxy.h"
24#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/SkGr.h"
26#include "tests/Test.h"
27#include "tools/gpu/GrContextFactory.h"
28#include "tools/gpu/ProxyUtils.h"
Ben Wagnereed61282018-04-17 14:14:51 -040029
Ben Wagner9707a7e2019-05-06 17:17:19 -040030#include <initializer_list>
Ben Wagnereed61282018-04-17 14:14:51 -040031#include <utility>
bsalomonb8fea972016-02-16 07:34:17 -080032
bsalomon68d91342016-04-12 09:59:58 -070033DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070034 GrContext* context = ctxInfo.grContext();
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}) {
bsalomonb8fea972016-02-16 07:34:17 -080077 for (auto srcRect : kSrcRects) {
78 for (auto dstPoint : kDstPoints) {
Timothy Liange30739a2018-07-31 10:51:17 -040079 for (auto ii: kImageInfos) {
80 auto src = sk_gpu_test::MakeTextureProxyFromData(
Brian Salomon4eda7102019-10-21 15:04:52 -040081 context, sRenderable, sOrigin, ii, srcPixels.get(),
82 kRowBytes);
Timothy Liange30739a2018-07-31 10:51:17 -040083 auto dst = sk_gpu_test::MakeTextureProxyFromData(
Brian Salomon4eda7102019-10-21 15:04:52 -040084 context, dRenderable, dOrigin, ii, dstPixels.get(),
85 kRowBytes);
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) {
Timothy Liange30739a2018-07-31 10:51:17 -040090 if (!src || !dst) {
91 ERRORF(reporter,
92 "Could not create surfaces for copy surface test.");
93 continue;
94 }
95 } else {
Greg Daniel7bfc9132019-08-14 14:23:53 -040096 if (!context->defaultBackendFormat(
97 kBGRA_8888_SkColorType, GrRenderable::kNo).isValid()) {
Timothy Liange30739a2018-07-31 10:51:17 -040098 continue;
99 }
100 if (!src || !dst) {
101 ERRORF(reporter,
102 "Could not create surfaces for copy surface test.");
103 continue;
104 }
105 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500106
Greg Danielc594e622019-10-15 14:01:49 -0400107 GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500108 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),
Greg Danielbfa19c42019-12-19 16:41:40 -0500113 grColorType,
114 ii.alphaType(), nullptr);
bsalomonb8fea972016-02-16 07:34:17 -0800115
Greg Daniel46cfbc62019-06-07 11:43:30 -0400116 bool result = false;
117 if (sOrigin == dOrigin) {
Brian Salomonc5243782020-04-02 12:50:34 -0400118 result = dstContext->testCopy(src.get(), srcRect, dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400119 } else if (dRenderable == GrRenderable::kYes) {
120 SkASSERT(dstContext->asRenderTargetContext());
Greg Daniel573312e2020-02-07 17:22:35 -0500121 GrSwizzle srcSwizzle = context->priv().caps()->getReadSwizzle(
122 src->backendFormat(), grColorType);
123 GrSurfaceProxyView view(std::move(src), sOrigin, srcSwizzle);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400124 result = dstContext->asRenderTargetContext()->blitTexture(
Greg Daniel573312e2020-02-07 17:22:35 -0500125 std::move(view), srcRect, dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400126 }
bsalomonb8fea972016-02-16 07:34:17 -0800127
Timothy Liange30739a2018-07-31 10:51:17 -0400128 bool expectedResult = true;
129 SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
130 dstPoint.fY - srcRect.fTop };
131 SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
132 dstPoint.fY,
133 srcRect.width(),
134 srcRect.height());
bsalomonb8fea972016-02-16 07:34:17 -0800135
Timothy Liange30739a2018-07-31 10:51:17 -0400136 SkIRect copiedSrcRect;
137 if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
138 expectedResult = false;
139 } else {
140 // If the src rect was clipped, apply same clipping to each side
141 // of copied dst rect.
142 copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
143 copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
144 copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
145 copiedDstRect.fBottom -= copiedSrcRect.fBottom -
146 srcRect.fBottom;
147 }
148 if (copiedDstRect.isEmpty() ||
149 !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
150 expectedResult = false;
151 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400152 if (sOrigin != dOrigin && dRenderable == GrRenderable::kNo) {
153 expectedResult = false;
154 }
155
Timothy Liange30739a2018-07-31 10:51:17 -0400156 // To make the copied src rect correct we would apply any dst
157 // clipping back to the src rect, but we don't use it again so
158 // don't bother.
159 if (expectedResult != result) {
160 ERRORF(reporter, "Expected return value %d from copySurface, "
161 "got %d.", expectedResult, result);
162 continue;
163 }
bsalomonb8fea972016-02-16 07:34:17 -0800164
Timothy Liange30739a2018-07-31 10:51:17 -0400165 if (!expectedResult || !result) {
166 continue;
167 }
bsalomonb8fea972016-02-16 07:34:17 -0800168
Timothy Liange30739a2018-07-31 10:51:17 -0400169 sk_memset32(read.get(), 0, kW * kH);
Brian Salomon1d435302019-07-01 13:05:28 -0400170 if (!dstContext->readPixels(ii, read.get(), kRowBytes, {0, 0})) {
Timothy Liange30739a2018-07-31 10:51:17 -0400171 ERRORF(reporter, "Error calling readPixels");
172 continue;
173 }
174
175 bool abort = false;
176 // Validate that pixels inside copiedDstRect received the correct
177 // value from src and that those outside were not modified.
178 for (int y = 0; y < kH && !abort; ++y) {
179 for (int x = 0; x < kW; ++x) {
180 uint32_t r = read.get()[y * kW + x];
181 if (copiedDstRect.contains(x, y)) {
182 int sx = x - dstOffset.fX;
183 int sy = y - dstOffset.fY;
184 uint32_t s = srcPixels.get()[sy * kW + sx];
185 if (s != r) {
186 ERRORF(reporter, "Expected dst %d,%d to contain "
187 "0x%08x copied from src location %d,%d. Got "
188 "0x%08x", x, y, s, sx, sy, r);
189 abort = true;
190 break;
191 }
192 } else {
193 uint32_t d = dstPixels.get()[y * kW + x];
194 if (d != r) {
195 ERRORF(reporter, "Expected dst %d,%d to be "
196 "unmodified (0x%08x). Got 0x%08x",
197 x, y, d, r);
198 abort = true;
199 break;
200 }
bsalomonb8fea972016-02-16 07:34:17 -0800201 }
202 }
203 }
204 }
205 }
206 }
207 }
208 }
209 }
210 }
211}