blob: acdc2b9f5d5cbc61ae37d711264467de491f84f1 [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
10#if SK_SUPPORT_GPU
11#include "GrContext.h"
Ben Wagnereed61282018-04-17 14:14:51 -040012#include "GrContextFactory.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050013#include "GrContextPriv.h"
Ben Wagnereed61282018-04-17 14:14:51 -040014#include "GrSurfaceContext.h"
15#include "GrSurfaceProxy.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040016#include "GrTextureProxy.h"
Ben Wagnereed61282018-04-17 14:14:51 -040017#include "GrTypes.h"
Brian Salomon58389b92018-03-07 13:01:25 -050018#include "ProxyUtils.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
Robert Phillipsd46697a2017-01-25 12:10:37 -050062 const SkImageInfo ii = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
63
bsalomonb8fea972016-02-16 07:34:17 -080064 SkAutoTMalloc<uint32_t> read(kW * kH);
65
66 for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
67 for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
Brian Salomon58389b92018-03-07 13:01:25 -050068 for (auto sRT : {true, false}) {
69 for (auto dRT : {true, false}) {
bsalomonb8fea972016-02-16 07:34:17 -080070 for (auto srcRect : kSrcRects) {
71 for (auto dstPoint : kDstPoints) {
Brian Salomon58389b92018-03-07 13:01:25 -050072 auto src = sk_gpu_test::MakeTextureProxyFromData(
73 context, sRT, kW, kH, ii.colorType(), sOrigin, srcPixels.get(),
74 kRowBytes);
75 auto dst = sk_gpu_test::MakeTextureProxyFromData(
76 context, dRT, kW, kH, ii.colorType(), dOrigin, dstPixels.get(),
77 kRowBytes);
bsalomonb8fea972016-02-16 07:34:17 -080078 if (!src || !dst) {
79 ERRORF(reporter,
80 "Could not create surfaces for copy surface test.");
81 continue;
82 }
83
Robert Phillipsd46697a2017-01-25 12:10:37 -050084 sk_sp<GrSurfaceContext> dstContext =
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050085 context->contextPriv().makeWrappedSurfaceContext(std::move(dst));
Robert Phillipse2f7d182016-12-15 09:23:05 -050086
Robert Phillipsd46697a2017-01-25 12:10:37 -050087 bool result = dstContext->copy(src.get(), srcRect, dstPoint);
bsalomonb8fea972016-02-16 07:34:17 -080088
89 bool expectedResult = true;
90 SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
91 dstPoint.fY - srcRect.fTop };
92 SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
93 dstPoint.fY,
94 srcRect.width(),
95 srcRect.height());
96
97 SkIRect copiedSrcRect;
98 if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
99 expectedResult = false;
100 } else {
101 // If the src rect was clipped, apply same clipping to each side of
102 // copied dst rect.
103 copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
104 copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
105 copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
106 copiedDstRect.fBottom -= copiedSrcRect.fBottom - srcRect.fBottom;
107 }
108 if (copiedDstRect.isEmpty() ||
109 !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
110 expectedResult = false;
111 }
112 // To make the copied src rect correct we would apply any dst clipping
113 // back to the src rect, but we don't use it again so don't bother.
114 if (expectedResult != result) {
115 ERRORF(reporter, "Expected return value %d from copySurface, got "
116 "%d.", expectedResult, result);
117 continue;
118 }
119
120 if (!expectedResult || !result) {
121 continue;
122 }
123
124 sk_memset32(read.get(), 0, kW * kH);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500125 if (!dstContext->readPixels(ii, read.get(), kRowBytes, 0, 0)) {
bsalomonb8fea972016-02-16 07:34:17 -0800126 ERRORF(reporter, "Error calling readPixels");
127 continue;
128 }
129
130 bool abort = false;
131 // Validate that pixels inside copiedDstRect received the correct value
132 // from src and that those outside were not modified.
133 for (int y = 0; y < kH && !abort; ++y) {
134 for (int x = 0; x < kW; ++x) {
135 uint32_t r = read.get()[y * kW + x];
136 if (copiedDstRect.contains(x, y)) {
137 int sx = x - dstOffset.fX;
138 int sy = y - dstOffset.fY;
139 uint32_t s = srcPixels.get()[sy * kW + sx];
140 if (s != r) {
141 ERRORF(reporter, "Expected dst %d,%d to contain "
142 "0x%08x copied from src location %d,%d. Got "
143 "0x%08x", x, y, s, sx, sy, r);
144 abort = true;
145 break;
146 }
147 } else {
148 uint32_t d = dstPixels.get()[y * kW + x];
149 if (d != r) {
150 ERRORF(reporter, "Expected dst %d,%d to be unmodified ("
151 "0x%08x). Got 0x%08x", x, y, d, r);
152 abort = true;
153 break;
154 }
155 }
156 }
157 }
158 }
159 }
160 }
161 }
162 }
163 }
164}
165#endif