blob: 17f19dbe4d6375b0c52256a6257a643e01cbe1af [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"
John Rosascoa9b348f2019-11-08 13:18:15 -080020#ifdef SK_GL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/gl/GrGLGpu.h"
22#include "src/gpu/gl/GrGLUtil.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080023#endif
Greg Daniel84ea0492019-06-05 16:52:02 -040024#include "tools/gpu/ProxyUtils.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080025#ifdef SK_GL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "tools/gpu/gl/GLTestContext.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080027#endif
bsalomone5286e02016-01-14 09:24:09 -080028
Brian Salomon739c5bf2016-11-07 09:53:44 -050029// skbug.com/5932
Robert Phillipsd46697a2017-01-25 12:10:37 -050030static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context,
Brian Salomond6287472019-06-24 15:50:07 -040031 sk_sp<GrTextureProxy> rectProxy, GrColorType colorType,
Brian Salomonfc118442019-11-22 19:09:27 -050032 SkAlphaType alphaType, uint32_t expectedPixelValues[]) {
Greg Daniele20fcad2020-01-08 11:52:34 -050033 auto rtContext = GrRenderTargetContext::Make(
34 context, colorType, nullptr, SkBackingFit::kExact, rectProxy->dimensions());
Greg Danield2ccbb52020-02-05 10:45:39 -050035 GrSurfaceOrigin origin = rectProxy->origin();
36 GrSwizzle swizzle = rectProxy->textureSwizzle();
37 GrSurfaceProxyView view(std::move(rectProxy), origin, swizzle);
Brian Salomon2bbdcc42017-09-07 12:36:34 -040038 for (auto filter : {GrSamplerState::Filter::kNearest,
39 GrSamplerState::Filter::kBilerp,
40 GrSamplerState::Filter::kMipMap}) {
Brian Osman9a9baae2018-11-05 15:06:26 -050041 rtContext->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xDDCCBBAA),
42 GrRenderTargetContext::CanClearFullscreen::kYes);
Greg Danield2ccbb52020-02-05 10:45:39 -050043 auto fp = GrTextureEffect::Make(view, alphaType, SkMatrix::I(), filter);
Brian Salomon739c5bf2016-11-07 09:53:44 -050044 GrPaint paint;
45 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
46 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomon82f44312017-01-11 13:42:54 -050047 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Brian Salomon28a8f282019-10-24 20:07:39 -040048 TestReadPixels(reporter, rtContext.get(), expectedPixelValues,
49 "RectangleTexture-basic-draw");
Brian Salomon739c5bf2016-11-07 09:53:44 -050050 }
51}
52
Robert Phillips26c90e02017-03-14 14:39:29 -040053static void test_clear(skiatest::Reporter* reporter, GrSurfaceContext* rectContext) {
Robert Phillipsd46697a2017-01-25 12:10:37 -050054 if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) {
bsalomone5286e02016-01-14 09:24:09 -080055 // Clear the whole thing.
56 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
Brian Osman9a9baae2018-11-05 15:06:26 -050057 rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(color0),
58 GrRenderTargetContext::CanClearFullscreen::kNo);
bsalomone5286e02016-01-14 09:24:09 -080059
Robert Phillipsd46697a2017-01-25 12:10:37 -050060 int w = rtc->width();
61 int h = rtc->height();
bsalomone5286e02016-01-14 09:24:09 -080062 int pixelCnt = w * h;
63 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
64
65 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -080066 uint32_t expectedColor0 = 0;
Mike Kleine72e7732018-06-14 14:41:22 -040067 uint8_t* expectedBytes0 = reinterpret_cast<uint8_t*>(&expectedColor0);
bsalomone5286e02016-01-14 09:24:09 -080068 expectedBytes0[0] = GrColorUnpackR(color0);
69 expectedBytes0[1] = GrColorUnpackG(color0);
70 expectedBytes0[2] = GrColorUnpackB(color0);
71 expectedBytes0[3] = GrColorUnpackA(color0);
Robert Phillipsd46697a2017-01-25 12:10:37 -050072 for (int i = 0; i < rtc->width() * rtc->height(); ++i) {
bsalomone5286e02016-01-14 09:24:09 -080073 expectedPixels.get()[i] = expectedColor0;
74 }
75
76 // Clear the the top to a different color.
77 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
78 SkIRect rect = SkIRect::MakeWH(w, h/2);
Brian Osman9a9baae2018-11-05 15:06:26 -050079 rtc->clear(&rect, SkPMColor4f::FromBytes_RGBA(color1),
80 GrRenderTargetContext::CanClearFullscreen::kNo);
bsalomone5286e02016-01-14 09:24:09 -080081
benjaminwagner2a641ee2016-01-15 06:21:18 -080082 uint32_t expectedColor1 = 0;
Mike Kleine72e7732018-06-14 14:41:22 -040083 uint8_t* expectedBytes1 = reinterpret_cast<uint8_t*>(&expectedColor1);
bsalomone5286e02016-01-14 09:24:09 -080084 expectedBytes1[0] = GrColorUnpackR(color1);
85 expectedBytes1[1] = GrColorUnpackG(color1);
86 expectedBytes1[2] = GrColorUnpackB(color1);
87 expectedBytes1[3] = GrColorUnpackA(color1);
88
89 for (int y = 0; y < h/2; ++y) {
90 for (int x = 0; x < w; ++x) {
91 expectedPixels.get()[y * h + x] = expectedColor1;
92 }
93 }
94
Brian Salomon28a8f282019-10-24 20:07:39 -040095 TestReadPixels(reporter, rtc, expectedPixels.get(), "RectangleTexture-clear");
bsalomone5286e02016-01-14 09:24:09 -080096 }
97}
98
Greg Daniel46cfbc62019-06-07 11:43:30 -040099static void test_copy_to_surface(skiatest::Reporter* reporter,
100 GrContext* context,
101 GrSurfaceContext* dstContext,
102 const char* testName) {
103
104 int pixelCnt = dstContext->width() * dstContext->height();
105 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
106 for (int y = 0; y < dstContext->width(); ++y) {
107 for (int x = 0; x < dstContext->height(); ++x) {
108 pixels.get()[y * dstContext->width() + x] =
109 SkColorToPremulGrColor(SkColorSetARGB(2*y, y, x, x * y));
110 }
111 }
112
113 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
114 auto origin = dstContext->asSurfaceProxy()->origin();
115 auto src = sk_gpu_test::MakeTextureProxyFromData(
Brian Salomon4eda7102019-10-21 15:04:52 -0400116 context, renderable, origin,
117 {GrColorType::kRGBA_8888, kPremul_SkAlphaType, nullptr, dstContext->width(),
118 dstContext->height()},
119 pixels.get(), 0);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400120 // If this assert ever fails we can add a fallback to do copy as draw, but until then we can
121 // be more restrictive.
122 SkAssertResult(dstContext->testCopy(src.get()));
Brian Salomon28a8f282019-10-24 20:07:39 -0400123 TestReadPixels(reporter, dstContext, pixels.get(), testName);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400124 }
125}
126
John Rosascoa9b348f2019-11-08 13:18:15 -0800127#ifdef SK_GL
bsalomon758586c2016-04-06 14:02:39 -0700128DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700129 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500130 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
bsalomon8b7451a2016-05-11 06:33:06 -0700131 sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400132 static const int kWidth = 16;
133 static const int kHeight = 16;
bsalomone5286e02016-01-14 09:24:09 -0800134
135 GrColor pixels[kWidth * kHeight];
136 for (int y = 0; y < kHeight; ++y) {
137 for (int x = 0; x < kWidth; ++x) {
138 pixels[y * kWidth + x] = y * kWidth + x;
139 }
140 }
141
Greg Daniel7ef28f32017-04-20 16:41:55 +0000142 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
143 bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin;
144
bsalomone5286e02016-01-14 09:24:09 -0800145 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
146 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
147 pixels);
148
149 if (!rectTexID) {
150 return;
151 }
152
153 // Let GrContext know that we messed with the GL context directly.
154 context->resetContext();
155
156 // Wrap the rectangle texture ID in a GrTexture
157 GrGLTextureInfo rectangleInfo;
158 rectangleInfo.fID = rectTexID;
159 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
Greg Danielfa55f2e2019-06-13 15:21:38 -0400160 rectangleInfo.fFormat = GR_GL_RGBA8;
bsalomone5286e02016-01-14 09:24:09 -0800161
Greg Daniel108bb232018-07-03 16:18:29 -0400162 GrBackendTexture rectangleTex(kWidth, kHeight, GrMipMapped::kNo, rectangleInfo);
bsalomone5286e02016-01-14 09:24:09 -0800163
164 GrColor refPixels[kWidth * kHeight];
bsalomone5286e02016-01-14 09:24:09 -0800165 for (int y = 0; y < kHeight; ++y) {
166 for (int x = 0; x < kWidth; ++x) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000167 int y0 = useBLOrigin ? kHeight - y - 1 : y;
bsalomone5286e02016-01-14 09:24:09 -0800168 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
169 }
170 }
171
Brian Salomonc67c31c2018-12-06 10:00:03 -0500172 sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400173 rectangleTex, GrColorType::kRGBA_8888, origin,
174 kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500175
Robert Phillips26caf892017-01-27 10:58:31 -0500176 if (!rectProxy) {
177 ERRORF(reporter, "Error creating proxy for rectangle texture.");
178 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
179 continue;
bsalomone5286e02016-01-14 09:24:09 -0800180 }
181
Greg Daniel09c94002018-06-08 22:11:51 +0000182 SkASSERT(rectProxy->mipMapped() == GrMipMapped::kNo);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400183 SkASSERT(rectProxy->peekTexture()->texturePriv().mipMapped() == GrMipMapped::kNo);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400184
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400185 SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle);
186 SkASSERT(rectProxy->peekTexture()->texturePriv().textureType() ==
Brian Salomon7226c232018-07-30 13:13:17 -0400187 GrTextureType::kRectangle);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400188 SkASSERT(rectProxy->hasRestrictedSampling());
189 SkASSERT(rectProxy->peekTexture()->texturePriv().hasRestrictedSampling());
Robert Phillipsabf7b762018-03-21 12:13:37 -0400190
Brian Salomonfc118442019-11-22 19:09:27 -0500191 test_basic_draw_as_src(reporter, context, rectProxy, GrColorType::kRGBA_8888,
192 kPremul_SkAlphaType, refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800193
Robert Phillips3500b772017-01-27 10:11:42 -0500194 // Test copy to both a texture and RT
Greg Daniel40903af2020-01-30 14:55:05 -0500195 TestCopyFromSurface(reporter, context, rectProxy.get(), origin, GrColorType::kRGBA_8888,
196 refPixels, "RectangleTexture-copy-from");
Brian Salomon739c5bf2016-11-07 09:53:44 -0500197
Greg Daniel3912a4b2020-01-14 09:56:04 -0500198 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(rectangleTex.getBackendFormat(),
199 GrColorType::kRGBA_8888);
200 GrSurfaceProxyView view(std::move(rectProxy), origin, swizzle);
201 auto rectContext = GrSurfaceContext::Make(context, std::move(view),
Greg Danielbfa19c42019-12-19 16:41:40 -0500202 GrColorType::kRGBA_8888, kPremul_SkAlphaType,
203 nullptr);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500204 SkASSERT(rectContext);
bsalomone5286e02016-01-14 09:24:09 -0800205
Brian Salomon28a8f282019-10-24 20:07:39 -0400206 TestReadPixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read");
bsalomone5286e02016-01-14 09:24:09 -0800207
Robert Phillips69893702019-02-22 11:16:30 -0500208 test_copy_to_surface(reporter, context, rectContext.get(), "RectangleTexture-copy-to");
bsalomone5286e02016-01-14 09:24:09 -0800209
Brian Salomon28a8f282019-10-24 20:07:39 -0400210 TestWritePixels(reporter, rectContext.get(), true, "RectangleTexture-write");
Robert Phillipsd46697a2017-01-25 12:10:37 -0500211
Robert Phillips26c90e02017-03-14 14:39:29 -0400212 test_clear(reporter, rectContext.get());
bsalomone5286e02016-01-14 09:24:09 -0800213
214 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
215 }
216}
John Rosascoa9b348f2019-11-08 13:18:15 -0800217#endif