blob: 0080474f7b825b5d2eed7a28259933a5d3ffde37 [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
Ben Wagnereed61282018-04-17 14:14:51 -04008#include "SkTypes.h"
bsalomonb8fea972016-02-16 07:34:17 -08009
bsalomonb8fea972016-02-16 07:34:17 -080010#include "GrContext.h"
Ben Wagnereed61282018-04-17 14:14:51 -040011#include "GrContextFactory.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050012#include "GrContextPriv.h"
Ben Wagnereed61282018-04-17 14:14:51 -040013#include "GrSurfaceContext.h"
14#include "GrSurfaceProxy.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040015#include "GrTextureProxy.h"
Ben Wagnereed61282018-04-17 14:14:51 -040016#include "GrTypes.h"
Brian Salomon58389b92018-03-07 13:01:25 -050017#include "ProxyUtils.h"
Timothy Liange30739a2018-07-31 10:51:17 -040018#include "SkGr.h"
Ben Wagnereed61282018-04-17 14:14:51 -040019#include "SkImageInfo.h"
20#include "SkPoint.h"
21#include "SkRect.h"
22#include "SkRefCnt.h"
23#include "SkTemplates.h"
bsalomonb8fea972016-02-16 07:34:17 -080024#include "SkUtils.h"
Ben Wagnereed61282018-04-17 14:14:51 -040025#include "Test.h"
26
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);
94 if (!context->contextPriv().caps()->isConfigTexturable(
95 config)) {
96 continue;
97 }
98 if (!src || !dst) {
99 ERRORF(reporter,
100 "Could not create surfaces for copy surface test.");
101 continue;
102 }
103 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500104
Timothy Liange30739a2018-07-31 10:51:17 -0400105 sk_sp<GrSurfaceContext> dstContext =
106 context->contextPriv().makeWrappedSurfaceContext(
107 std::move(dst));
bsalomonb8fea972016-02-16 07:34:17 -0800108
Timothy Liange30739a2018-07-31 10:51:17 -0400109 bool result = dstContext->copy(src.get(), srcRect, dstPoint);
bsalomonb8fea972016-02-16 07:34:17 -0800110
Timothy Liange30739a2018-07-31 10:51:17 -0400111 bool expectedResult = true;
112 SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
113 dstPoint.fY - srcRect.fTop };
114 SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
115 dstPoint.fY,
116 srcRect.width(),
117 srcRect.height());
bsalomonb8fea972016-02-16 07:34:17 -0800118
Timothy Liange30739a2018-07-31 10:51:17 -0400119 SkIRect copiedSrcRect;
120 if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
121 expectedResult = false;
122 } else {
123 // If the src rect was clipped, apply same clipping to each side
124 // of copied dst rect.
125 copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
126 copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
127 copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
128 copiedDstRect.fBottom -= copiedSrcRect.fBottom -
129 srcRect.fBottom;
130 }
131 if (copiedDstRect.isEmpty() ||
132 !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
133 expectedResult = false;
134 }
135 // To make the copied src rect correct we would apply any dst
136 // clipping back to the src rect, but we don't use it again so
137 // don't bother.
138 if (expectedResult != result) {
139 ERRORF(reporter, "Expected return value %d from copySurface, "
140 "got %d.", expectedResult, result);
141 continue;
142 }
bsalomonb8fea972016-02-16 07:34:17 -0800143
Timothy Liange30739a2018-07-31 10:51:17 -0400144 if (!expectedResult || !result) {
145 continue;
146 }
bsalomonb8fea972016-02-16 07:34:17 -0800147
Timothy Liange30739a2018-07-31 10:51:17 -0400148 sk_memset32(read.get(), 0, kW * kH);
149 if (!dstContext->readPixels(ii, read.get(), kRowBytes, 0, 0)) {
150 ERRORF(reporter, "Error calling readPixels");
151 continue;
152 }
153
154 bool abort = false;
155 // Validate that pixels inside copiedDstRect received the correct
156 // value from src and that those outside were not modified.
157 for (int y = 0; y < kH && !abort; ++y) {
158 for (int x = 0; x < kW; ++x) {
159 uint32_t r = read.get()[y * kW + x];
160 if (copiedDstRect.contains(x, y)) {
161 int sx = x - dstOffset.fX;
162 int sy = y - dstOffset.fY;
163 uint32_t s = srcPixels.get()[sy * kW + sx];
164 if (s != r) {
165 ERRORF(reporter, "Expected dst %d,%d to contain "
166 "0x%08x copied from src location %d,%d. Got "
167 "0x%08x", x, y, s, sx, sy, r);
168 abort = true;
169 break;
170 }
171 } else {
172 uint32_t d = dstPixels.get()[y * kW + x];
173 if (d != r) {
174 ERRORF(reporter, "Expected dst %d,%d to be "
175 "unmodified (0x%08x). Got 0x%08x",
176 x, y, d, r);
177 abort = true;
178 break;
179 }
bsalomonb8fea972016-02-16 07:34:17 -0800180 }
181 }
182 }
183 }
184 }
185 }
186 }
187 }
188 }
189 }
190}