blob: 1e325356983f8d61ffbd4e83e09e300e464c5bb0 [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"
17#include "src/core/SkUtils.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrSurfaceContext.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050022#include "src/gpu/GrSurfaceDrawContext.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) {
Adlai Hollerc95b5892020-08-11 12:02:22 -040034 auto dContext = ctxInfo.directContext();
Robert Phillips6d344c32020-07-06 10:56:46 -040035
bsalomonb8fea972016-02-16 07:34:17 -080036 static const int kW = 10;
37 static const int kH = 10;
38 static const size_t kRowBytes = sizeof(uint32_t) * kW;
39
bsalomonb8fea972016-02-16 07:34:17 -080040 SkAutoTMalloc<uint32_t> srcPixels(kW * kH);
41 for (int i = 0; i < kW * kH; ++i) {
42 srcPixels.get()[i] = i;
43 }
44
45 SkAutoTMalloc<uint32_t> dstPixels(kW * kH);
46 for (int i = 0; i < kW * kH; ++i) {
47 dstPixels.get()[i] = ~i;
48 }
49
50 static const SkIRect kSrcRects[] {
51 { 0, 0, kW , kH },
52 {-1, -1, kW+1, kH+1},
53 { 1, 1, kW-1, kH-1},
54 { 5, 5, 6 , 6 },
55 };
56
57 static const SkIPoint kDstPoints[] {
58 { 0 , 0 },
59 { 1 , 1 },
60 { kW/2, kH/4},
61 { kW-1, kH-1},
62 { kW , kH },
63 { kW+1, kH+2},
64 {-1 , -1 },
65 };
66
Timothy Liange30739a2018-07-31 10:51:17 -040067 static const SkImageInfo kImageInfos[] {
68 SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
69 SkImageInfo::Make(kW, kH, kBGRA_8888_SkColorType, kPremul_SkAlphaType)
70 };
Robert Phillipsd46697a2017-01-25 12:10:37 -050071
bsalomonb8fea972016-02-16 07:34:17 -080072 SkAutoTMalloc<uint32_t> read(kW * kH);
73
74 for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
75 for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040076 for (auto sRenderable : {GrRenderable::kYes, GrRenderable::kNo}) {
77 for (auto dRenderable : {GrRenderable::kYes, GrRenderable::kNo}) {
John Stilesbd3ffa42020-07-30 20:24:57 -040078 for (const SkIRect& srcRect : kSrcRects) {
79 for (const SkIPoint& dstPoint : kDstPoints) {
80 for (const SkImageInfo& ii: kImageInfos) {
Brian Salomondd4087d2020-12-23 20:36:44 -050081 GrPixmap srcPM(ii, srcPixels.get(), kRowBytes);
82 GrPixmap dstPM(ii, dstPixels.get(), kRowBytes);
Brian Salomon652124c2020-08-12 14:06:50 -040083 auto srcView = sk_gpu_test::MakeTextureProxyViewFromData(
Brian Salomondd4087d2020-12-23 20:36:44 -050084 dContext, sRenderable, sOrigin, srcPM);
Brian Salomon652124c2020-08-12 14:06:50 -040085 auto dstView = sk_gpu_test::MakeTextureProxyViewFromData(
Brian Salomondd4087d2020-12-23 20:36:44 -050086 dContext, dRenderable, dOrigin, dstPM);
bsalomonb8fea972016-02-16 07:34:17 -080087
Timothy Liange30739a2018-07-31 10:51:17 -040088 // Should always work if the color type is RGBA, but may not work
89 // for BGRA
Timothy Liangdeba2122018-08-07 13:22:03 -040090 if (ii.colorType() == kRGBA_8888_SkColorType) {
Brian Salomon652124c2020-08-12 14:06:50 -040091 if (!srcView || !dstView) {
Timothy Liange30739a2018-07-31 10:51:17 -040092 ERRORF(reporter,
93 "Could not create surfaces for copy surface test.");
94 continue;
95 }
96 } else {
Adlai Hollerc95b5892020-08-11 12:02:22 -040097 if (!dContext->defaultBackendFormat(
Greg Daniel7bfc9132019-08-14 14:23:53 -040098 kBGRA_8888_SkColorType, GrRenderable::kNo).isValid()) {
Timothy Liange30739a2018-07-31 10:51:17 -040099 continue;
100 }
Brian Salomon652124c2020-08-12 14:06:50 -0400101 if (!srcView || !dstView) {
Timothy Liange30739a2018-07-31 10:51:17 -0400102 ERRORF(reporter,
103 "Could not create surfaces for copy surface test.");
104 continue;
105 }
106 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500107
Adlai Hollerc95b5892020-08-11 12:02:22 -0400108 auto dstContext = GrSurfaceContext::Make(dContext,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500109 std::move(dstView),
Brian Salomon14f99fc2020-12-07 12:19:47 -0500110 ii.colorInfo());
bsalomonb8fea972016-02-16 07:34:17 -0800111
Greg Daniel46cfbc62019-06-07 11:43:30 -0400112 bool result = false;
113 if (sOrigin == dOrigin) {
Brian Salomon652124c2020-08-12 14:06:50 -0400114 result = dstContext->testCopy(srcView.proxy(),
115 srcRect,
116 dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400117 } else if (dRenderable == GrRenderable::kYes) {
Brian Salomon590f5672020-12-16 11:44:47 -0500118 SkASSERT(dstContext->asFillContext());
119 result = dstContext->asFillContext()->blitTexture(
Brian Salomon652124c2020-08-12 14:06:50 -0400120 std::move(srcView), srcRect, dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400121 }
bsalomonb8fea972016-02-16 07:34:17 -0800122
Timothy Liange30739a2018-07-31 10:51:17 -0400123 bool expectedResult = true;
124 SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
125 dstPoint.fY - srcRect.fTop };
126 SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
127 dstPoint.fY,
128 srcRect.width(),
129 srcRect.height());
bsalomonb8fea972016-02-16 07:34:17 -0800130
Timothy Liange30739a2018-07-31 10:51:17 -0400131 SkIRect copiedSrcRect;
132 if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
133 expectedResult = false;
134 } else {
135 // If the src rect was clipped, apply same clipping to each side
136 // of copied dst rect.
137 copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
138 copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
139 copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
140 copiedDstRect.fBottom -= copiedSrcRect.fBottom -
141 srcRect.fBottom;
142 }
143 if (copiedDstRect.isEmpty() ||
144 !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
145 expectedResult = false;
146 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400147 if (sOrigin != dOrigin && dRenderable == GrRenderable::kNo) {
148 expectedResult = false;
149 }
150
Timothy Liange30739a2018-07-31 10:51:17 -0400151 // To make the copied src rect correct we would apply any dst
152 // clipping back to the src rect, but we don't use it again so
153 // don't bother.
154 if (expectedResult != result) {
155 ERRORF(reporter, "Expected return value %d from copySurface, "
156 "got %d.", expectedResult, result);
157 continue;
158 }
bsalomonb8fea972016-02-16 07:34:17 -0800159
Timothy Liange30739a2018-07-31 10:51:17 -0400160 if (!expectedResult || !result) {
161 continue;
162 }
bsalomonb8fea972016-02-16 07:34:17 -0800163
Timothy Liange30739a2018-07-31 10:51:17 -0400164 sk_memset32(read.get(), 0, kW * kH);
Brian Salomondd4087d2020-12-23 20:36:44 -0500165 GrPixmap readPM(ii, read.get(), kRowBytes);
166 if (!dstContext->readPixels(dContext, readPM, {0, 0})) {
Timothy Liange30739a2018-07-31 10:51:17 -0400167 ERRORF(reporter, "Error calling readPixels");
168 continue;
169 }
170
171 bool abort = false;
172 // Validate that pixels inside copiedDstRect received the correct
173 // value from src and that those outside were not modified.
174 for (int y = 0; y < kH && !abort; ++y) {
175 for (int x = 0; x < kW; ++x) {
176 uint32_t r = read.get()[y * kW + x];
177 if (copiedDstRect.contains(x, y)) {
178 int sx = x - dstOffset.fX;
179 int sy = y - dstOffset.fY;
180 uint32_t s = srcPixels.get()[sy * kW + sx];
181 if (s != r) {
182 ERRORF(reporter, "Expected dst %d,%d to contain "
183 "0x%08x copied from src location %d,%d. Got "
184 "0x%08x", x, y, s, sx, sy, r);
185 abort = true;
186 break;
187 }
188 } else {
189 uint32_t d = dstPixels.get()[y * kW + x];
190 if (d != r) {
191 ERRORF(reporter, "Expected dst %d,%d to be "
192 "unmodified (0x%08x). Got 0x%08x",
193 x, y, d, r);
194 abort = true;
195 break;
196 }
bsalomonb8fea972016-02-16 07:34:17 -0800197 }
198 }
199 }
200 }
201 }
202 }
203 }
204 }
205 }
206 }
207}