• 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/GrContext.h"
12 #include "src/gpu/GrClip.h"
13 #include "src/gpu/GrContextPriv.h"
14 #include "src/gpu/GrProxyProvider.h"
15 #include "src/gpu/GrRenderTargetContext.h"
16 #include "src/gpu/GrSurfaceContextPriv.h"
17 #include "src/gpu/GrSurfacePriv.h"
18 #include "src/gpu/GrTexturePriv.h"
19 #include "src/gpu/SkGr.h"
20 #include "src/gpu/gl/GrGLGpu.h"
21 #include "src/gpu/gl/GrGLUtil.h"
22 #include "tools/gpu/ProxyUtils.h"
23 #include "tools/gpu/gl/GLTestContext.h"
24 
25 // skbug.com/5932
test_basic_draw_as_src(skiatest::Reporter * reporter,GrContext * context,sk_sp<GrTextureProxy> rectProxy,GrColorType colorType,uint32_t expectedPixelValues[])26 static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context,
27                                    sk_sp<GrTextureProxy> rectProxy, GrColorType colorType,
28                                    uint32_t expectedPixelValues[]) {
29     sk_sp<GrRenderTargetContext> rtContext(context->priv().makeDeferredRenderTargetContext(
30             SkBackingFit::kExact, rectProxy->width(), rectProxy->height(), colorType, nullptr));
31     for (auto filter : {GrSamplerState::Filter::kNearest,
32                         GrSamplerState::Filter::kBilerp,
33                         GrSamplerState::Filter::kMipMap}) {
34         rtContext->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xDDCCBBAA),
35                          GrRenderTargetContext::CanClearFullscreen::kYes);
36         auto fp = GrSimpleTextureEffect::Make(rectProxy, SkMatrix::I(), filter);
37         GrPaint paint;
38         paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
39         paint.addColorFragmentProcessor(std::move(fp));
40         rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
41         test_read_pixels(reporter, rtContext.get(), expectedPixelValues,
42                          "RectangleTexture-basic-draw");
43     }
44 }
45 
test_clear(skiatest::Reporter * reporter,GrSurfaceContext * rectContext)46 static void test_clear(skiatest::Reporter* reporter, GrSurfaceContext* rectContext) {
47     if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) {
48         // Clear the whole thing.
49         GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
50         rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(color0),
51                    GrRenderTargetContext::CanClearFullscreen::kNo);
52 
53         int w = rtc->width();
54         int h = rtc->height();
55         int pixelCnt = w * h;
56         SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
57 
58         // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
59         uint32_t expectedColor0 = 0;
60         uint8_t* expectedBytes0 = reinterpret_cast<uint8_t*>(&expectedColor0);
61         expectedBytes0[0] = GrColorUnpackR(color0);
62         expectedBytes0[1] = GrColorUnpackG(color0);
63         expectedBytes0[2] = GrColorUnpackB(color0);
64         expectedBytes0[3] = GrColorUnpackA(color0);
65         for (int i = 0; i < rtc->width() * rtc->height(); ++i) {
66             expectedPixels.get()[i] = expectedColor0;
67         }
68 
69         // Clear the the top to a different color.
70         GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
71         SkIRect rect = SkIRect::MakeWH(w, h/2);
72         rtc->clear(&rect, SkPMColor4f::FromBytes_RGBA(color1),
73                    GrRenderTargetContext::CanClearFullscreen::kNo);
74 
75         uint32_t expectedColor1 = 0;
76         uint8_t* expectedBytes1 = reinterpret_cast<uint8_t*>(&expectedColor1);
77         expectedBytes1[0] = GrColorUnpackR(color1);
78         expectedBytes1[1] = GrColorUnpackG(color1);
79         expectedBytes1[2] = GrColorUnpackB(color1);
80         expectedBytes1[3] = GrColorUnpackA(color1);
81 
82         for (int y = 0; y < h/2; ++y) {
83             for (int x = 0; x < w; ++x) {
84                 expectedPixels.get()[y * h + x] = expectedColor1;
85             }
86         }
87 
88         test_read_pixels(reporter, rtc, expectedPixels.get(), "RectangleTexture-clear");
89     }
90 }
91 
test_copy_to_surface(skiatest::Reporter * reporter,GrContext * context,GrSurfaceContext * dstContext,const char * testName)92 static void test_copy_to_surface(skiatest::Reporter* reporter,
93                                  GrContext* context,
94                                  GrSurfaceContext* dstContext,
95                                  const char* testName) {
96 
97     int pixelCnt = dstContext->width() * dstContext->height();
98     SkAutoTMalloc<uint32_t> pixels(pixelCnt);
99     for (int y = 0; y < dstContext->width(); ++y) {
100         for (int x = 0; x < dstContext->height(); ++x) {
101             pixels.get()[y * dstContext->width() + x] =
102                 SkColorToPremulGrColor(SkColorSetARGB(2*y, y, x, x * y));
103         }
104     }
105 
106     for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
107         auto origin = dstContext->asSurfaceProxy()->origin();
108         auto src = sk_gpu_test::MakeTextureProxyFromData(
109                 context, renderable, dstContext->width(), dstContext->height(),
110                 kRGBA_8888_SkColorType, kPremul_SkAlphaType, origin, pixels.get(), 0);
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(src.get()));
114         test_read_pixels(reporter, dstContext, pixels.get(), testName);
115     }
116 }
117 
DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture,reporter,ctxInfo)118 DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
119     GrContext* context = ctxInfo.grContext();
120     GrProxyProvider* proxyProvider = context->priv().proxyProvider();
121     sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
122     static const int kWidth = 16;
123     static const int kHeight = 16;
124 
125     GrColor pixels[kWidth * kHeight];
126     for (int y = 0; y < kHeight; ++y) {
127         for (int x = 0; x < kWidth; ++x) {
128             pixels[y * kWidth + x] = y * kWidth + x;
129         }
130     }
131 
132     for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
133         bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin;
134 
135         GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
136                                                                GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
137                                                                pixels);
138 
139         if (!rectTexID) {
140             return;
141         }
142 
143         // Let GrContext know that we messed with the GL context directly.
144         context->resetContext();
145 
146         // Wrap the rectangle texture ID in a GrTexture
147         GrGLTextureInfo rectangleInfo;
148         rectangleInfo.fID = rectTexID;
149         rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
150         rectangleInfo.fFormat = GR_GL_RGBA8;
151 
152         GrBackendTexture rectangleTex(kWidth, kHeight, GrMipMapped::kNo, rectangleInfo);
153 
154         GrColor refPixels[kWidth * kHeight];
155         for (int y = 0; y < kHeight; ++y) {
156             for (int x = 0; x < kWidth; ++x) {
157                 int y0 = useBLOrigin ? kHeight - y - 1 : y;
158                 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
159             }
160         }
161 
162         sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture(
163                 rectangleTex, GrColorType::kRGBA_8888, origin,
164                 kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
165 
166         if (!rectProxy) {
167             ERRORF(reporter, "Error creating proxy for rectangle texture.");
168             GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
169             continue;
170         }
171 
172         SkASSERT(rectProxy->mipMapped() == GrMipMapped::kNo);
173         SkASSERT(rectProxy->peekTexture()->texturePriv().mipMapped() == GrMipMapped::kNo);
174 
175         SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle);
176         SkASSERT(rectProxy->peekTexture()->texturePriv().textureType() ==
177                  GrTextureType::kRectangle);
178         SkASSERT(rectProxy->hasRestrictedSampling());
179         SkASSERT(rectProxy->peekTexture()->texturePriv().hasRestrictedSampling());
180 
181         test_basic_draw_as_src(reporter, context, rectProxy, GrColorType::kRGBA_8888, refPixels);
182 
183         // Test copy to both a texture and RT
184         test_copy_from_surface(reporter, context, rectProxy.get(), GrColorType::kRGBA_8888,
185                                refPixels, "RectangleTexture-copy-from");
186 
187         sk_sp<GrSurfaceContext> rectContext = context->priv().makeWrappedSurfaceContext(
188                 std::move(rectProxy), GrColorType::kRGBA_8888, kPremul_SkAlphaType);
189         SkASSERT(rectContext);
190 
191         test_read_pixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read");
192 
193         test_copy_to_surface(reporter, context, rectContext.get(), "RectangleTexture-copy-to");
194 
195         test_write_pixels(reporter, rectContext.get(), true, "RectangleTexture-write");
196 
197         test_clear(reporter, rectContext.get());
198 
199         GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
200     }
201 }
202