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