blob: 0674d0428634c76d201d3c67f062bea820fd64df [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/SkTypes.h"
bsalomonb8fea972016-02-16 07:34:17 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkImageInfo.h"
11#include "include/core/SkPoint.h"
12#include "include/core/SkRect.h"
13#include "include/core/SkRefCnt.h"
14#include "include/gpu/GrContext.h"
15#include "include/gpu/GrTypes.h"
16#include "include/private/GrSurfaceProxy.h"
17#include "include/private/GrTextureProxy.h"
18#include "include/private/SkTemplates.h"
19#include "src/core/SkUtils.h"
20#include "src/gpu/GrContextPriv.h"
21#include "src/gpu/GrSurfaceContext.h"
22#include "src/gpu/SkGr.h"
23#include "tests/Test.h"
24#include "tools/gpu/GrContextFactory.h"
25#include "tools/gpu/ProxyUtils.h"
Ben Wagnereed61282018-04-17 14:14:51 -040026
27#include <utility>
bsalomonb8fea972016-02-16 07:34:17 -080028
bsalomon68d91342016-04-12 09:59:58 -070029DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070030 GrContext* context = ctxInfo.grContext();
bsalomonb8fea972016-02-16 07:34:17 -080031 static const int kW = 10;
32 static const int kH = 10;
33 static const size_t kRowBytes = sizeof(uint32_t) * kW;
34
bsalomonb8fea972016-02-16 07:34:17 -080035 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
Timothy Liange30739a2018-07-31 10:51:17 -040062 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 };
Robert Phillipsd46697a2017-01-25 12:10:37 -050066
bsalomonb8fea972016-02-16 07:34:17 -080067 SkAutoTMalloc<uint32_t> read(kW * kH);
68
69 for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
70 for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
Brian Salomon58389b92018-03-07 13:01:25 -050071 for (auto sRT : {true, false}) {
72 for (auto dRT : {true, false}) {
bsalomonb8fea972016-02-16 07:34:17 -080073 for (auto srcRect : kSrcRects) {
74 for (auto dstPoint : kDstPoints) {
Timothy Liange30739a2018-07-31 10:51:17 -040075 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);
bsalomonb8fea972016-02-16 07:34:17 -080082
Timothy Liange30739a2018-07-31 10:51:17 -040083 // Should always work if the color type is RGBA, but may not work
84 // for BGRA
Timothy Liangdeba2122018-08-07 13:22:03 -040085 if (ii.colorType() == kRGBA_8888_SkColorType) {
Timothy Liange30739a2018-07-31 10:51:17 -040086 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);
Robert Phillips9da87e02019-02-04 13:26:26 -050094 if (!context->priv().caps()->isConfigTexturable(config)) {
Timothy Liange30739a2018-07-31 10:51:17 -040095 continue;
96 }
97 if (!src || !dst) {
98 ERRORF(reporter,
99 "Could not create surfaces for copy surface test.");
100 continue;
101 }
102 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500103
Timothy Liange30739a2018-07-31 10:51:17 -0400104 sk_sp<GrSurfaceContext> dstContext =
Robert Phillips9da87e02019-02-04 13:26:26 -0500105 context->priv().makeWrappedSurfaceContext(std::move(dst));
bsalomonb8fea972016-02-16 07:34:17 -0800106
Timothy Liange30739a2018-07-31 10:51:17 -0400107 bool result = dstContext->copy(src.get(), srcRect, dstPoint);
bsalomonb8fea972016-02-16 07:34:17 -0800108
Timothy Liange30739a2018-07-31 10:51:17 -0400109 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());
bsalomonb8fea972016-02-16 07:34:17 -0800116
Timothy Liange30739a2018-07-31 10:51:17 -0400117 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 }
bsalomonb8fea972016-02-16 07:34:17 -0800141
Timothy Liange30739a2018-07-31 10:51:17 -0400142 if (!expectedResult || !result) {
143 continue;
144 }
bsalomonb8fea972016-02-16 07:34:17 -0800145
Timothy Liange30739a2018-07-31 10:51:17 -0400146 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 }
bsalomonb8fea972016-02-16 07:34:17 -0800178 }
179 }
180 }
181 }
182 }
183 }
184 }
185 }
186 }
187 }
188}