blob: 9574208564ee70639bc413d58d7f93612d479a28 [file] [log] [blame]
Brian Salomon2a4f9832018-03-03 22:43:43 -05001
bsalomonb8fea972016-02-16 07:34:17 -08002/*
3 * Copyright 2016 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include <initializer_list>
10#include "Test.h"
11
12#if SK_SUPPORT_GPU
13#include "GrContext.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050014#include "GrContextPriv.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040015#include "GrTextureProxy.h"
Brian Salomon58389b92018-03-07 13:01:25 -050016#include "ProxyUtils.h"
bsalomonb8fea972016-02-16 07:34:17 -080017
18#include "SkUtils.h"
19
bsalomon68d91342016-04-12 09:59:58 -070020DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070021 GrContext* context = ctxInfo.grContext();
bsalomonb8fea972016-02-16 07:34:17 -080022 static const int kW = 10;
23 static const int kH = 10;
24 static const size_t kRowBytes = sizeof(uint32_t) * kW;
25
bsalomonb8fea972016-02-16 07:34:17 -080026 SkAutoTMalloc<uint32_t> srcPixels(kW * kH);
27 for (int i = 0; i < kW * kH; ++i) {
28 srcPixels.get()[i] = i;
29 }
30
31 SkAutoTMalloc<uint32_t> dstPixels(kW * kH);
32 for (int i = 0; i < kW * kH; ++i) {
33 dstPixels.get()[i] = ~i;
34 }
35
36 static const SkIRect kSrcRects[] {
37 { 0, 0, kW , kH },
38 {-1, -1, kW+1, kH+1},
39 { 1, 1, kW-1, kH-1},
40 { 5, 5, 6 , 6 },
41 };
42
43 static const SkIPoint kDstPoints[] {
44 { 0 , 0 },
45 { 1 , 1 },
46 { kW/2, kH/4},
47 { kW-1, kH-1},
48 { kW , kH },
49 { kW+1, kH+2},
50 {-1 , -1 },
51 };
52
Robert Phillipsd46697a2017-01-25 12:10:37 -050053 const SkImageInfo ii = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
54
bsalomonb8fea972016-02-16 07:34:17 -080055 SkAutoTMalloc<uint32_t> read(kW * kH);
56
57 for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
58 for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
Brian Salomon58389b92018-03-07 13:01:25 -050059 for (auto sRT : {true, false}) {
60 for (auto dRT : {true, false}) {
bsalomonb8fea972016-02-16 07:34:17 -080061 for (auto srcRect : kSrcRects) {
62 for (auto dstPoint : kDstPoints) {
Brian Salomon58389b92018-03-07 13:01:25 -050063 auto src = sk_gpu_test::MakeTextureProxyFromData(
64 context, sRT, kW, kH, ii.colorType(), sOrigin, srcPixels.get(),
65 kRowBytes);
66 auto dst = sk_gpu_test::MakeTextureProxyFromData(
67 context, dRT, kW, kH, ii.colorType(), dOrigin, dstPixels.get(),
68 kRowBytes);
bsalomonb8fea972016-02-16 07:34:17 -080069 if (!src || !dst) {
70 ERRORF(reporter,
71 "Could not create surfaces for copy surface test.");
72 continue;
73 }
74
Robert Phillipsd46697a2017-01-25 12:10:37 -050075 sk_sp<GrSurfaceContext> dstContext =
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050076 context->contextPriv().makeWrappedSurfaceContext(std::move(dst));
Robert Phillipse2f7d182016-12-15 09:23:05 -050077
Robert Phillipsd46697a2017-01-25 12:10:37 -050078 bool result = dstContext->copy(src.get(), srcRect, dstPoint);
bsalomonb8fea972016-02-16 07:34:17 -080079
80 bool expectedResult = true;
81 SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
82 dstPoint.fY - srcRect.fTop };
83 SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
84 dstPoint.fY,
85 srcRect.width(),
86 srcRect.height());
87
88 SkIRect copiedSrcRect;
89 if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
90 expectedResult = false;
91 } else {
92 // If the src rect was clipped, apply same clipping to each side of
93 // copied dst rect.
94 copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
95 copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
96 copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
97 copiedDstRect.fBottom -= copiedSrcRect.fBottom - srcRect.fBottom;
98 }
99 if (copiedDstRect.isEmpty() ||
100 !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
101 expectedResult = false;
102 }
103 // To make the copied src rect correct we would apply any dst clipping
104 // back to the src rect, but we don't use it again so don't bother.
105 if (expectedResult != result) {
106 ERRORF(reporter, "Expected return value %d from copySurface, got "
107 "%d.", expectedResult, result);
108 continue;
109 }
110
111 if (!expectedResult || !result) {
112 continue;
113 }
114
115 sk_memset32(read.get(), 0, kW * kH);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500116 if (!dstContext->readPixels(ii, read.get(), kRowBytes, 0, 0)) {
bsalomonb8fea972016-02-16 07:34:17 -0800117 ERRORF(reporter, "Error calling readPixels");
118 continue;
119 }
120
121 bool abort = false;
122 // Validate that pixels inside copiedDstRect received the correct value
123 // from src and that those outside were not modified.
124 for (int y = 0; y < kH && !abort; ++y) {
125 for (int x = 0; x < kW; ++x) {
126 uint32_t r = read.get()[y * kW + x];
127 if (copiedDstRect.contains(x, y)) {
128 int sx = x - dstOffset.fX;
129 int sy = y - dstOffset.fY;
130 uint32_t s = srcPixels.get()[sy * kW + sx];
131 if (s != r) {
132 ERRORF(reporter, "Expected dst %d,%d to contain "
133 "0x%08x copied from src location %d,%d. Got "
134 "0x%08x", x, y, s, sx, sy, r);
135 abort = true;
136 break;
137 }
138 } else {
139 uint32_t d = dstPixels.get()[y * kW + x];
140 if (d != r) {
141 ERRORF(reporter, "Expected dst %d,%d to be unmodified ("
142 "0x%08x). Got 0x%08x", x, y, d, r);
143 abort = true;
144 break;
145 }
146 }
147 }
148 }
149 }
150 }
151 }
152 }
153 }
154 }
155}
156#endif