• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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 "tests/Test.h"
9 #include "tests/TestUtils.h"
10 
11 #include "include/gpu/GrDirectContext.h"
12 #include "src/gpu/GrDirectContextPriv.h"
13 #include "src/gpu/GrProxyProvider.h"
14 #include "src/gpu/GrTexture.h"
15 #include "src/gpu/SkGr.h"
16 #include "src/gpu/SurfaceFillContext.h"
17 #include "src/gpu/effects/GrTextureEffect.h"
18 #ifdef SK_GL
19 #include "src/gpu/gl/GrGLGpu.h"
20 #include "src/gpu/gl/GrGLUtil.h"
21 #endif
22 #include "tools/gpu/ProxyUtils.h"
23 
24 // skbug.com/5932
test_basic_draw_as_src(skiatest::Reporter * reporter,GrDirectContext * dContext,GrSurfaceProxyView rectView,GrColorType colorType,SkAlphaType alphaType,uint32_t expectedPixelValues[])25 static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrDirectContext* dContext,
26                                    GrSurfaceProxyView rectView, GrColorType colorType,
27                                    SkAlphaType alphaType, uint32_t expectedPixelValues[]) {
28     auto sfc = dContext->priv().makeSFC(
29             {colorType, kPremul_SkAlphaType, nullptr, rectView.dimensions()});
30     for (auto filter : {GrSamplerState::Filter::kNearest, GrSamplerState::Filter::kLinear}) {
31         for (auto mm : {GrSamplerState::MipmapMode::kNone, GrSamplerState::MipmapMode::kLinear}) {
32             sfc->clear(SkPMColor4f::FromBytes_RGBA(0xDDCCBBAA));
33             auto fp = GrTextureEffect::Make(rectView, alphaType, SkMatrix::I(), filter, mm);
34             sfc->fillWithFP(std::move(fp));
35             TestReadPixels(reporter, dContext, sfc.get(), expectedPixelValues,
36                            "RectangleTexture-basic-draw");
37         }
38     }
39 }
40 
test_clear(skiatest::Reporter * reporter,GrDirectContext * dContext,skgpu::SurfaceContext * rectContext)41 static void test_clear(skiatest::Reporter* reporter, GrDirectContext* dContext,
42                        skgpu::SurfaceContext* rectContext) {
43     if (auto sfc = rectContext->asFillContext()) {
44         // Clear the whole thing.
45         GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
46         sfc->clear(SkPMColor4f::FromBytes_RGBA(color0));
47 
48         int w = sfc->width();
49         int h = sfc->height();
50         int pixelCnt = w * h;
51         SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
52 
53         // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
54         uint32_t expectedColor0 = 0;
55         uint8_t* expectedBytes0 = reinterpret_cast<uint8_t*>(&expectedColor0);
56         expectedBytes0[0] = GrColorUnpackR(color0);
57         expectedBytes0[1] = GrColorUnpackG(color0);
58         expectedBytes0[2] = GrColorUnpackB(color0);
59         expectedBytes0[3] = GrColorUnpackA(color0);
60         for (int i = 0; i < sfc->width() * sfc->height(); ++i) {
61             expectedPixels.get()[i] = expectedColor0;
62         }
63 
64         // Clear the the top to a different color.
65         GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
66         SkIRect rect = SkIRect::MakeWH(w, h/2);
67         sfc->clear(rect, SkPMColor4f::FromBytes_RGBA(color1));
68 
69         uint32_t expectedColor1 = 0;
70         uint8_t* expectedBytes1 = reinterpret_cast<uint8_t*>(&expectedColor1);
71         expectedBytes1[0] = GrColorUnpackR(color1);
72         expectedBytes1[1] = GrColorUnpackG(color1);
73         expectedBytes1[2] = GrColorUnpackB(color1);
74         expectedBytes1[3] = GrColorUnpackA(color1);
75 
76         for (int y = 0; y < h/2; ++y) {
77             for (int x = 0; x < w; ++x) {
78                 expectedPixels.get()[y * h + x] = expectedColor1;
79             }
80         }
81 
82         TestReadPixels(reporter, dContext, sfc, expectedPixels.get(), "RectangleTexture-clear");
83     }
84 }
85 
test_copy_to_surface(skiatest::Reporter * reporter,GrDirectContext * dContext,skgpu::SurfaceContext * dstContext,const char * testName)86 static void test_copy_to_surface(skiatest::Reporter* reporter,
87                                  GrDirectContext* dContext,
88                                  skgpu::SurfaceContext* dstContext,
89                                  const char* testName) {
90 
91     int pixelCnt = dstContext->width() * dstContext->height();
92     SkAutoTMalloc<uint32_t> pixels(pixelCnt);
93     for (int y = 0; y < dstContext->width(); ++y) {
94         for (int x = 0; x < dstContext->height(); ++x) {
95             pixels.get()[y * dstContext->width() + x] =
96                 SkColorToPremulGrColor(SkColorSetARGB(2*y, y, x, x * y));
97         }
98     }
99 
100     for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
101         auto origin = dstContext->origin();
102         GrImageInfo info(GrColorType::kRGBA_8888,
103                          kPremul_SkAlphaType,
104                          nullptr,
105                          dstContext->dimensions());
106         GrCPixmap pixmap(info, pixels.get(), dstContext->width()*sizeof(uint32_t));
107         auto srcView = sk_gpu_test::MakeTextureProxyViewFromData(dContext,
108                                                                  renderable,
109                                                                  origin,
110                                                                  pixmap);
111         // If this assert ever fails we can add a fallback to do copy as draw, but until then we can
112         // be more restrictive.
113         SkAssertResult(dstContext->testCopy(srcView.refProxy()));
114         TestReadPixels(reporter, dContext, dstContext, pixels.get(), testName);
115     }
116 }
117 
118 #ifdef SK_GL
DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture,reporter,ctxInfo)119 DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
120     auto dContext = ctxInfo.directContext();
121 
122     GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
123     static const int kWidth = 16;
124     static const int kHeight = 16;
125 
126     uint32_t pixels[kWidth * kHeight];
127     for (int y = 0; y < kHeight; ++y) {
128         for (int x = 0; x < kWidth; ++x) {
129             pixels[y * kWidth + x] = y * kWidth + x;
130         }
131     }
132     auto ii = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
133     SkPixmap pm(ii, pixels, sizeof(uint32_t)*kWidth);
134 
135     for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
136 
137         auto format = GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_RECTANGLE);
138         GrBackendTexture rectangleTex = dContext->createBackendTexture(kWidth,
139                                                                        kHeight,
140                                                                        format,
141                                                                        GrMipmapped::kNo,
142                                                                        GrRenderable::kYes);
143         if (!rectangleTex.isValid()) {
144             continue;
145         }
146 
147         if (!dContext->updateBackendTexture(rectangleTex, &pm, 1, origin, nullptr, nullptr)) {
148             continue;
149         }
150 
151         GrColor refPixels[kWidth * kHeight];
152         for (int y = 0; y < kHeight; ++y) {
153             for (int x = 0; x < kWidth; ++x) {
154                 refPixels[y * kWidth + x] = pixels[y * kWidth + x];
155             }
156         }
157 
158         sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture(
159                 rectangleTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
160 
161         if (!rectProxy) {
162             dContext->deleteBackendTexture(rectangleTex);
163             continue;
164         }
165 
166         SkASSERT(rectProxy->mipmapped() == GrMipmapped::kNo);
167         SkASSERT(rectProxy->peekTexture()->mipmapped() == GrMipmapped::kNo);
168 
169         SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle);
170         SkASSERT(rectProxy->peekTexture()->textureType() == GrTextureType::kRectangle);
171         SkASSERT(rectProxy->hasRestrictedSampling());
172         SkASSERT(rectProxy->peekTexture()->hasRestrictedSampling());
173 
174         GrImageInfo grII = ii;
175         GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(rectangleTex.getBackendFormat(),
176                                                                     grII.colorType());
177         GrSurfaceProxyView view(rectProxy, origin, swizzle);
178 
179         test_basic_draw_as_src(reporter, dContext, view, grII.colorType(), kPremul_SkAlphaType,
180                                refPixels);
181 
182         // Test copy to both a texture and RT
183         TestCopyFromSurface(reporter, dContext, rectProxy, origin, grII.colorType(), refPixels,
184                             "RectangleTexture-copy-from");
185 
186         auto rectContext = dContext->priv().makeSC(std::move(view), grII.colorInfo());
187         SkASSERT(rectContext);
188 
189         TestReadPixels(reporter, dContext, rectContext.get(), refPixels, "RectangleTexture-read");
190 
191         test_copy_to_surface(reporter, dContext, rectContext.get(), "RectangleTexture-copy-to");
192 
193         TestWritePixels(reporter, dContext, rectContext.get(), true, "RectangleTexture-write");
194 
195         test_clear(reporter, dContext, rectContext.get());
196 
197         dContext->deleteBackendTexture(rectangleTex);
198     }
199 }
200 #endif
201