• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #include "include/core/SkAlphaType.h"
9 #include "include/core/SkColorType.h"
10 #include "include/core/SkImageInfo.h"
11 #include "include/core/SkPoint.h"
12 #include "include/core/SkRect.h"
13 #include "include/core/SkTypes.h"
14 #include "include/gpu/GrBackendSurface.h"
15 #include "include/gpu/GrDirectContext.h"
16 #include "include/gpu/GrTypes.h"
17 #include "include/private/base/SkTemplates.h"
18 #include "src/core/SkOpts.h"
19 #include "src/gpu/ganesh/GrDirectContextPriv.h"
20 #include "src/gpu/ganesh/GrImageInfo.h"
21 #include "src/gpu/ganesh/GrPixmap.h"
22 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
23 #include "src/gpu/ganesh/SurfaceContext.h"
24 #include "src/gpu/ganesh/SurfaceFillContext.h"
25 #include "tests/CtsEnforcement.h"
26 #include "tests/Test.h"
27 #include "tools/gpu/ProxyUtils.h"
28 
29 #include <cstddef>
30 #include <cstdint>
31 #include <initializer_list>
32 #include <memory>
33 #include <utility>
34 
35 using namespace skia_private;
36 
37 struct GrContextOptions;
38 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(CopySurface,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)39 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(CopySurface,
40                                        reporter,
41                                        ctxInfo,
42                                        CtsEnforcement::kApiLevel_T) {
43     auto dContext = ctxInfo.directContext();
44 
45     static const int kW = 10;
46     static const int kH = 10;
47     static const size_t kRowBytes = sizeof(uint32_t) * kW;
48 
49     AutoTMalloc<uint32_t> srcPixels(kW * kH);
50     for (int i = 0; i < kW * kH; ++i) {
51         srcPixels.get()[i] = i;
52     }
53 
54     AutoTMalloc<uint32_t> dstPixels(kW * kH);
55     for (int i = 0; i < kW * kH; ++i) {
56         dstPixels.get()[i] = ~i;
57     }
58 
59     static const SkIRect kSrcRects[] {
60         { 0,  0, kW  , kH  },
61         {-1, -1, kW+1, kH+1},
62         { 1,  1, kW-1, kH-1},
63         { 5,  5, 6   , 6   },
64     };
65 
66     static const SkIPoint kDstPoints[] {
67         { 0   ,  0   },
68         { 1   ,  1   },
69         { kW/2,  kH/4},
70         { kW-1,  kH-1},
71         { kW  ,  kH  },
72         { kW+1,  kH+2},
73         {-1   , -1   },
74     };
75 
76     static const SkImageInfo kImageInfos[] {
77         SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
78         SkImageInfo::Make(kW, kH, kBGRA_8888_SkColorType, kPremul_SkAlphaType)
79     };
80 
81     AutoTMalloc<uint32_t> read(kW * kH);
82 
83     for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
84         for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
85             for (auto sRenderable : {GrRenderable::kYes, GrRenderable::kNo}) {
86                 for (auto dRenderable : {GrRenderable::kYes, GrRenderable::kNo}) {
87                     for (const SkIRect& srcRect : kSrcRects) {
88                         for (const SkIPoint& dstPoint : kDstPoints) {
89                             for (const SkImageInfo& ii: kImageInfos) {
90                                 GrCPixmap srcPM(ii, srcPixels.get(), kRowBytes);
91                                 GrPixmap  dstPM(ii, dstPixels.get(), kRowBytes);
92                                 auto srcView = sk_gpu_test::MakeTextureProxyViewFromData(
93                                         dContext, sRenderable, sOrigin, srcPM);
94                                 auto dstView = sk_gpu_test::MakeTextureProxyViewFromData(
95                                         dContext, dRenderable, dOrigin, dstPM);
96 
97                                 // Should always work if the color type is RGBA, but may not work
98                                 // for BGRA
99                                 if (ii.colorType() == kRGBA_8888_SkColorType) {
100                                     if (!srcView || !dstView) {
101                                         ERRORF(reporter,
102                                                "Could not create surfaces for copy surface test.");
103                                         continue;
104                                     }
105                                 } else {
106                                     if (!dContext->defaultBackendFormat(
107                                             kBGRA_8888_SkColorType, GrRenderable::kNo).isValid()) {
108                                         continue;
109                                     }
110                                     if (!srcView || !dstView) {
111                                         ERRORF(reporter,
112                                                "Could not create surfaces for copy surface test.");
113                                         continue;
114                                     }
115                                 }
116 
117                                 auto dstContext = dContext->priv().makeSC(std::move(dstView),
118                                                                           ii.colorInfo());
119 
120                                 bool result = false;
121                                 if (sOrigin == dOrigin) {
122                                     result = dstContext->testCopy(srcView.refProxy(),
123                                                                   srcRect,
124                                                                   dstPoint);
125                                 } else if (dRenderable == GrRenderable::kYes) {
126                                     SkASSERT(dstContext->asFillContext());
127                                     result = dstContext->asFillContext()->blitTexture(
128                                             std::move(srcView), srcRect, dstPoint);
129                                 }
130 
131                                 bool expectedResult = true;
132                                 SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
133                                                        dstPoint.fY - srcRect.fTop };
134                                 SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
135                                                                           dstPoint.fY,
136                                                                           srcRect.width(),
137                                                                           srcRect.height());
138 
139                                 SkIRect copiedSrcRect;
140                                 if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
141                                     expectedResult = false;
142                                 } else {
143                                     // If the src rect was clipped, apply same clipping to each side
144                                     // of copied dst rect.
145                                     copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
146                                     copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
147                                     copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
148                                     copiedDstRect.fBottom -= copiedSrcRect.fBottom -
149                                                              srcRect.fBottom;
150                                 }
151                                 if (copiedDstRect.isEmpty() ||
152                                     !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
153                                     expectedResult = false;
154                                 }
155                                 if (sOrigin != dOrigin && dRenderable == GrRenderable::kNo) {
156                                     expectedResult = false;
157                                 }
158 
159                                 // To make the copied src rect correct we would apply any dst
160                                 // clipping back to the src rect, but we don't use it again so
161                                 // don't bother.
162                                 if (expectedResult != result) {
163                                     ERRORF(reporter, "Expected return value %d from copySurface, "
164                                            "got %d.", expectedResult, result);
165                                     continue;
166                                 }
167 
168                                 if (!expectedResult || !result) {
169                                     continue;
170                                 }
171 
172                                 SkOpts::memset32(read.get(), 0, kW * kH);
173                                 GrPixmap readPM(ii, read.get(), kRowBytes);
174                                 if (!dstContext->readPixels(dContext, readPM, {0, 0})) {
175                                     ERRORF(reporter, "Error calling readPixels");
176                                     continue;
177                                 }
178 
179                                 bool abort = false;
180                                 // Validate that pixels inside copiedDstRect received the correct
181                                 // value from src and that those outside were not modified.
182                                 for (int y = 0; y < kH && !abort; ++y) {
183                                     for (int x = 0; x < kW; ++x) {
184                                         uint32_t r = read.get()[y * kW + x];
185                                         if (copiedDstRect.contains(x, y)) {
186                                             int sx = x - dstOffset.fX;
187                                             int sy = y - dstOffset.fY;
188                                             uint32_t s = srcPixels.get()[sy * kW + sx];
189                                             if (s != r) {
190                                                 ERRORF(reporter, "Expected dst %d,%d to contain "
191                                                        "0x%08x copied from src location %d,%d. Got "
192                                                        "0x%08x", x, y, s, sx, sy, r);
193                                                 abort = true;
194                                                 break;
195                                             }
196                                         } else {
197                                             uint32_t d = dstPixels.get()[y * kW + x];
198                                             if (d != r) {
199                                                 ERRORF(reporter, "Expected dst %d,%d to be "
200                                                        "unmodified (0x%08x). Got 0x%08x",
201                                                        x, y, d, r);
202                                                 abort = true;
203                                                 break;
204                                             }
205                                         }
206                                     }
207                                 }
208                             }
209                         }
210                     }
211                 }
212             }
213         }
214     }
215 }
216