blob: 4921f5bbbeb97d04ee7d49064a21ac6a276c1787 [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"
Ben Wagnereed61282018-04-17 14:14:51 -040018#include "SkImageInfo.h"
19#include "SkPoint.h"
20#include "SkRect.h"
21#include "SkRefCnt.h"
22#include "SkTemplates.h"
bsalomonb8fea972016-02-16 07:34:17 -080023#include "SkUtils.h"
Ben Wagnereed61282018-04-17 14:14:51 -040024#include "Test.h"
25
26#include <utility>
bsalomonb8fea972016-02-16 07:34:17 -080027
bsalomon68d91342016-04-12 09:59:58 -070028DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070029 GrContext* context = ctxInfo.grContext();
bsalomonb8fea972016-02-16 07:34:17 -080030 static const int kW = 10;
31 static const int kH = 10;
32 static const size_t kRowBytes = sizeof(uint32_t) * kW;
33
bsalomonb8fea972016-02-16 07:34:17 -080034 SkAutoTMalloc<uint32_t> srcPixels(kW * kH);
35 for (int i = 0; i < kW * kH; ++i) {
36 srcPixels.get()[i] = i;
37 }
38
39 SkAutoTMalloc<uint32_t> dstPixels(kW * kH);
40 for (int i = 0; i < kW * kH; ++i) {
41 dstPixels.get()[i] = ~i;
42 }
43
44 static const SkIRect kSrcRects[] {
45 { 0, 0, kW , kH },
46 {-1, -1, kW+1, kH+1},
47 { 1, 1, kW-1, kH-1},
48 { 5, 5, 6 , 6 },
49 };
50
51 static const SkIPoint kDstPoints[] {
52 { 0 , 0 },
53 { 1 , 1 },
54 { kW/2, kH/4},
55 { kW-1, kH-1},
56 { kW , kH },
57 { kW+1, kH+2},
58 {-1 , -1 },
59 };
60
Robert Phillipsd46697a2017-01-25 12:10:37 -050061 const SkImageInfo ii = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
62
bsalomonb8fea972016-02-16 07:34:17 -080063 SkAutoTMalloc<uint32_t> read(kW * kH);
64
65 for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
66 for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
Brian Salomon58389b92018-03-07 13:01:25 -050067 for (auto sRT : {true, false}) {
68 for (auto dRT : {true, false}) {
bsalomonb8fea972016-02-16 07:34:17 -080069 for (auto srcRect : kSrcRects) {
70 for (auto dstPoint : kDstPoints) {
Brian Salomon58389b92018-03-07 13:01:25 -050071 auto src = sk_gpu_test::MakeTextureProxyFromData(
72 context, sRT, kW, kH, ii.colorType(), sOrigin, srcPixels.get(),
73 kRowBytes);
74 auto dst = sk_gpu_test::MakeTextureProxyFromData(
75 context, dRT, kW, kH, ii.colorType(), dOrigin, dstPixels.get(),
76 kRowBytes);
bsalomonb8fea972016-02-16 07:34:17 -080077 if (!src || !dst) {
78 ERRORF(reporter,
79 "Could not create surfaces for copy surface test.");
80 continue;
81 }
82
Robert Phillipsd46697a2017-01-25 12:10:37 -050083 sk_sp<GrSurfaceContext> dstContext =
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050084 context->contextPriv().makeWrappedSurfaceContext(std::move(dst));
Robert Phillipse2f7d182016-12-15 09:23:05 -050085
Robert Phillipsd46697a2017-01-25 12:10:37 -050086 bool result = dstContext->copy(src.get(), srcRect, dstPoint);
bsalomonb8fea972016-02-16 07:34:17 -080087
88 bool expectedResult = true;
89 SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
90 dstPoint.fY - srcRect.fTop };
91 SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
92 dstPoint.fY,
93 srcRect.width(),
94 srcRect.height());
95
96 SkIRect copiedSrcRect;
97 if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
98 expectedResult = false;
99 } else {
100 // If the src rect was clipped, apply same clipping to each side of
101 // copied dst rect.
102 copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
103 copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
104 copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
105 copiedDstRect.fBottom -= copiedSrcRect.fBottom - srcRect.fBottom;
106 }
107 if (copiedDstRect.isEmpty() ||
108 !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
109 expectedResult = false;
110 }
111 // To make the copied src rect correct we would apply any dst clipping
112 // back to the src rect, but we don't use it again so don't bother.
113 if (expectedResult != result) {
114 ERRORF(reporter, "Expected return value %d from copySurface, got "
115 "%d.", expectedResult, result);
116 continue;
117 }
118
119 if (!expectedResult || !result) {
120 continue;
121 }
122
123 sk_memset32(read.get(), 0, kW * kH);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500124 if (!dstContext->readPixels(ii, read.get(), kRowBytes, 0, 0)) {
bsalomonb8fea972016-02-16 07:34:17 -0800125 ERRORF(reporter, "Error calling readPixels");
126 continue;
127 }
128
129 bool abort = false;
130 // Validate that pixels inside copiedDstRect received the correct value
131 // from src and that those outside were not modified.
132 for (int y = 0; y < kH && !abort; ++y) {
133 for (int x = 0; x < kW; ++x) {
134 uint32_t r = read.get()[y * kW + x];
135 if (copiedDstRect.contains(x, y)) {
136 int sx = x - dstOffset.fX;
137 int sy = y - dstOffset.fY;
138 uint32_t s = srcPixels.get()[sy * kW + sx];
139 if (s != r) {
140 ERRORF(reporter, "Expected dst %d,%d to contain "
141 "0x%08x copied from src location %d,%d. Got "
142 "0x%08x", x, y, s, sx, sy, r);
143 abort = true;
144 break;
145 }
146 } else {
147 uint32_t d = dstPixels.get()[y * kW + x];
148 if (d != r) {
149 ERRORF(reporter, "Expected dst %d,%d to be unmodified ("
150 "0x%08x). Got 0x%08x", x, y, d, r);
151 abort = true;
152 break;
153 }
154 }
155 }
156 }
157 }
158 }
159 }
160 }
161 }
162 }
163}