• 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 "SkTypes.h"
9 
10 #include "GrContext.h"
11 #include "GrContextFactory.h"
12 #include "GrContextPriv.h"
13 #include "GrSurfaceContext.h"
14 #include "GrSurfaceProxy.h"
15 #include "GrTextureProxy.h"
16 #include "GrTypes.h"
17 #include "ProxyUtils.h"
18 #include "SkGr.h"
19 #include "SkImageInfo.h"
20 #include "SkPoint.h"
21 #include "SkRect.h"
22 #include "SkRefCnt.h"
23 #include "SkTemplates.h"
24 #include "SkUtils.h"
25 #include "Test.h"
26 
27 #include <utility>
28 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface,reporter,ctxInfo)29 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) {
30     GrContext* context = ctxInfo.grContext();
31     static const int kW = 10;
32     static const int kH = 10;
33     static const size_t kRowBytes = sizeof(uint32_t) * kW;
34 
35     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 
62     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     };
66 
67     SkAutoTMalloc<uint32_t> read(kW * kH);
68 
69     for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
70         for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
71             for (auto sRT : {true, false}) {
72                 for (auto dRT : {true, false}) {
73                     for (auto srcRect : kSrcRects) {
74                         for (auto dstPoint : kDstPoints) {
75                             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);
82 
83                                 // Should always work if the color type is RGBA, but may not work
84                                 // for BGRA
85                                 if (ii.colorType() == kRGBA_8888_SkColorType) {
86                                     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                                 }
104 
105                                 sk_sp<GrSurfaceContext> dstContext =
106                                         context->contextPriv().makeWrappedSurfaceContext(
107                                                 std::move(dst));
108 
109                                 bool result = dstContext->copy(src.get(), srcRect, dstPoint);
110 
111                                 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());
118 
119                                 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                                 }
143 
144                                 if (!expectedResult || !result) {
145                                     continue;
146                                 }
147 
148                                 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                                             }
180                                         }
181                                     }
182                                 }
183                             }
184                         }
185                     }
186                 }
187             }
188         }
189     }
190 }
191