blob: e444f1645343df394faa6a8ad2d9e2f7f0412ff8 [file] [log] [blame]
bsalomone5286e02016-01-14 09:24:09 -08001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
9#include "tests/TestUtils.h"
Robert Phillips3500b772017-01-27 10:11:42 -050010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#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"
Greg Daniel46cfbc62019-06-07 11:43:30 -040016#include "src/gpu/GrSurfaceContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrSurfacePriv.h"
18#include "src/gpu/GrTexturePriv.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050019#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/gl/GrGLGpu.h"
21#include "src/gpu/gl/GrGLUtil.h"
Greg Daniel84ea0492019-06-05 16:52:02 -040022#include "tools/gpu/ProxyUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tools/gpu/gl/GLTestContext.h"
bsalomone5286e02016-01-14 09:24:09 -080024
Brian Salomon739c5bf2016-11-07 09:53:44 -050025// skbug.com/5932
Robert Phillipsd46697a2017-01-25 12:10:37 -050026static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context,
Greg Daniel7ef28f32017-04-20 16:41:55 +000027 sk_sp<GrTextureProxy> rectProxy, uint32_t expectedPixelValues[]) {
Greg Daniel4065d452018-11-16 15:43:41 -050028 GrBackendFormat format = rectProxy->backendFormat().makeTexture2D();
29 SkASSERT(format.isValid());
Robert Phillips9da87e02019-02-04 13:26:26 -050030 sk_sp<GrRenderTargetContext> rtContext(context->priv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -050031 format,
Robert Phillips0c4b7b12018-03-06 08:20:37 -050032 SkBackingFit::kExact, rectProxy->width(),
Robert Phillipsdd3b3f42017-04-24 10:57:28 -040033 rectProxy->height(), rectProxy->config(),
34 nullptr));
Brian Salomon2bbdcc42017-09-07 12:36:34 -040035 for (auto filter : {GrSamplerState::Filter::kNearest,
36 GrSamplerState::Filter::kBilerp,
37 GrSamplerState::Filter::kMipMap}) {
Brian Osman9a9baae2018-11-05 15:06:26 -050038 rtContext->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xDDCCBBAA),
39 GrRenderTargetContext::CanClearFullscreen::kYes);
Brian Osman2240be92017-10-18 13:15:13 -040040 auto fp = GrSimpleTextureEffect::Make(rectProxy, SkMatrix::I(), filter);
Brian Salomon739c5bf2016-11-07 09:53:44 -050041 GrPaint paint;
42 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
43 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomon82f44312017-01-11 13:42:54 -050044 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Robert Phillips26c90e02017-03-14 14:39:29 -040045 test_read_pixels(reporter, rtContext.get(), expectedPixelValues,
Robert Phillips3500b772017-01-27 10:11:42 -050046 "RectangleTexture-basic-draw");
Brian Salomon739c5bf2016-11-07 09:53:44 -050047 }
48}
49
Robert Phillips26c90e02017-03-14 14:39:29 -040050static void test_clear(skiatest::Reporter* reporter, GrSurfaceContext* rectContext) {
Robert Phillipsd46697a2017-01-25 12:10:37 -050051 if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) {
bsalomone5286e02016-01-14 09:24:09 -080052 // Clear the whole thing.
53 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
Brian Osman9a9baae2018-11-05 15:06:26 -050054 rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(color0),
55 GrRenderTargetContext::CanClearFullscreen::kNo);
bsalomone5286e02016-01-14 09:24:09 -080056
Robert Phillipsd46697a2017-01-25 12:10:37 -050057 int w = rtc->width();
58 int h = rtc->height();
bsalomone5286e02016-01-14 09:24:09 -080059 int pixelCnt = w * h;
60 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
61
62 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -080063 uint32_t expectedColor0 = 0;
Mike Kleine72e7732018-06-14 14:41:22 -040064 uint8_t* expectedBytes0 = reinterpret_cast<uint8_t*>(&expectedColor0);
bsalomone5286e02016-01-14 09:24:09 -080065 expectedBytes0[0] = GrColorUnpackR(color0);
66 expectedBytes0[1] = GrColorUnpackG(color0);
67 expectedBytes0[2] = GrColorUnpackB(color0);
68 expectedBytes0[3] = GrColorUnpackA(color0);
Robert Phillipsd46697a2017-01-25 12:10:37 -050069 for (int i = 0; i < rtc->width() * rtc->height(); ++i) {
bsalomone5286e02016-01-14 09:24:09 -080070 expectedPixels.get()[i] = expectedColor0;
71 }
72
73 // Clear the the top to a different color.
74 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
75 SkIRect rect = SkIRect::MakeWH(w, h/2);
Brian Osman9a9baae2018-11-05 15:06:26 -050076 rtc->clear(&rect, SkPMColor4f::FromBytes_RGBA(color1),
77 GrRenderTargetContext::CanClearFullscreen::kNo);
bsalomone5286e02016-01-14 09:24:09 -080078
benjaminwagner2a641ee2016-01-15 06:21:18 -080079 uint32_t expectedColor1 = 0;
Mike Kleine72e7732018-06-14 14:41:22 -040080 uint8_t* expectedBytes1 = reinterpret_cast<uint8_t*>(&expectedColor1);
bsalomone5286e02016-01-14 09:24:09 -080081 expectedBytes1[0] = GrColorUnpackR(color1);
82 expectedBytes1[1] = GrColorUnpackG(color1);
83 expectedBytes1[2] = GrColorUnpackB(color1);
84 expectedBytes1[3] = GrColorUnpackA(color1);
85
86 for (int y = 0; y < h/2; ++y) {
87 for (int x = 0; x < w; ++x) {
88 expectedPixels.get()[y * h + x] = expectedColor1;
89 }
90 }
91
Robert Phillips26c90e02017-03-14 14:39:29 -040092 test_read_pixels(reporter, rtc, expectedPixels.get(), "RectangleTexture-clear");
bsalomone5286e02016-01-14 09:24:09 -080093 }
94}
95
Greg Daniel46cfbc62019-06-07 11:43:30 -040096static void test_copy_to_surface(skiatest::Reporter* reporter,
97 GrContext* context,
98 GrSurfaceContext* dstContext,
99 const char* testName) {
100
101 int pixelCnt = dstContext->width() * dstContext->height();
102 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
103 for (int y = 0; y < dstContext->width(); ++y) {
104 for (int x = 0; x < dstContext->height(); ++x) {
105 pixels.get()[y * dstContext->width() + x] =
106 SkColorToPremulGrColor(SkColorSetARGB(2*y, y, x, x * y));
107 }
108 }
109
110 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
111 auto origin = dstContext->asSurfaceProxy()->origin();
112 auto src = sk_gpu_test::MakeTextureProxyFromData(
113 context, renderable, dstContext->width(),
114 dstContext->height(), kRGBA_8888_SkColorType, origin, pixels.get(), 0);
115 // If this assert ever fails we can add a fallback to do copy as draw, but until then we can
116 // be more restrictive.
117 SkAssertResult(dstContext->testCopy(src.get()));
118 test_read_pixels(reporter, dstContext, pixels.get(), testName);
119 }
120}
121
bsalomon758586c2016-04-06 14:02:39 -0700122DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700123 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500124 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
bsalomon8b7451a2016-05-11 06:33:06 -0700125 sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400126 static const int kWidth = 16;
127 static const int kHeight = 16;
bsalomone5286e02016-01-14 09:24:09 -0800128
129 GrColor pixels[kWidth * kHeight];
130 for (int y = 0; y < kHeight; ++y) {
131 for (int x = 0; x < kWidth; ++x) {
132 pixels[y * kWidth + x] = y * kWidth + x;
133 }
134 }
135
Greg Daniel7ef28f32017-04-20 16:41:55 +0000136 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
137 bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin;
138
bsalomone5286e02016-01-14 09:24:09 -0800139 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
140 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
141 pixels);
142
143 if (!rectTexID) {
144 return;
145 }
146
147 // Let GrContext know that we messed with the GL context directly.
148 context->resetContext();
149
150 // Wrap the rectangle texture ID in a GrTexture
151 GrGLTextureInfo rectangleInfo;
152 rectangleInfo.fID = rectTexID;
153 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
Greg Danielfa55f2e2019-06-13 15:21:38 -0400154 rectangleInfo.fFormat = GR_GL_RGBA8;
bsalomone5286e02016-01-14 09:24:09 -0800155
Greg Daniel108bb232018-07-03 16:18:29 -0400156 GrBackendTexture rectangleTex(kWidth, kHeight, GrMipMapped::kNo, rectangleInfo);
157 rectangleTex.setPixelConfig(kRGBA_8888_GrPixelConfig);
bsalomone5286e02016-01-14 09:24:09 -0800158
159 GrColor refPixels[kWidth * kHeight];
bsalomone5286e02016-01-14 09:24:09 -0800160 for (int y = 0; y < kHeight; ++y) {
161 for (int x = 0; x < kWidth; ++x) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000162 int y0 = useBLOrigin ? kHeight - y - 1 : y;
bsalomone5286e02016-01-14 09:24:09 -0800163 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
164 }
165 }
166
Brian Salomonc67c31c2018-12-06 10:00:03 -0500167 sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500168 rectangleTex, origin, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500169
Robert Phillips26caf892017-01-27 10:58:31 -0500170 if (!rectProxy) {
171 ERRORF(reporter, "Error creating proxy for rectangle texture.");
172 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
173 continue;
bsalomone5286e02016-01-14 09:24:09 -0800174 }
175
Greg Daniel09c94002018-06-08 22:11:51 +0000176 SkASSERT(rectProxy->mipMapped() == GrMipMapped::kNo);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400177 SkASSERT(rectProxy->peekTexture()->texturePriv().mipMapped() == GrMipMapped::kNo);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400178
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400179 SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle);
180 SkASSERT(rectProxy->peekTexture()->texturePriv().textureType() ==
Brian Salomon7226c232018-07-30 13:13:17 -0400181 GrTextureType::kRectangle);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400182 SkASSERT(rectProxy->hasRestrictedSampling());
183 SkASSERT(rectProxy->peekTexture()->texturePriv().hasRestrictedSampling());
Robert Phillipsabf7b762018-03-21 12:13:37 -0400184
Robert Phillipsd46697a2017-01-25 12:10:37 -0500185 test_basic_draw_as_src(reporter, context, rectProxy, refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800186
Robert Phillips3500b772017-01-27 10:11:42 -0500187 // Test copy to both a texture and RT
188 test_copy_from_surface(reporter, context, rectProxy.get(), refPixels,
Greg Daniel46cfbc62019-06-07 11:43:30 -0400189 "RectangleTexture-copy-from");
Brian Salomon739c5bf2016-11-07 09:53:44 -0500190
Robert Phillips9da87e02019-02-04 13:26:26 -0500191 sk_sp<GrSurfaceContext> rectContext = context->priv().makeWrappedSurfaceContext(
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500192 std::move(rectProxy));
Robert Phillipsd46697a2017-01-25 12:10:37 -0500193 SkASSERT(rectContext);
bsalomone5286e02016-01-14 09:24:09 -0800194
Robert Phillips26c90e02017-03-14 14:39:29 -0400195 test_read_pixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read");
bsalomone5286e02016-01-14 09:24:09 -0800196
Robert Phillips69893702019-02-22 11:16:30 -0500197 test_copy_to_surface(reporter, context, rectContext.get(), "RectangleTexture-copy-to");
bsalomone5286e02016-01-14 09:24:09 -0800198
Robert Phillips26c90e02017-03-14 14:39:29 -0400199 test_write_pixels(reporter, rectContext.get(), true, "RectangleTexture-write");
Robert Phillipsd46697a2017-01-25 12:10:37 -0500200
Robert Phillips26c90e02017-03-14 14:39:29 -0400201 test_clear(reporter, rectContext.get());
bsalomone5286e02016-01-14 09:24:09 -0800202
203 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
204 }
205}