blob: abaf04c24b9e265560c203bb586c559fe48911fc [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());
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 Salomonb8f098d2020-01-07 11:15:44 -050040 auto fp = GrTextureEffect::Make(rectProxy, alphaType, 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());
Brian Salomon28a8f282019-10-24 20:07:39 -040045 TestReadPixels(reporter, rtContext.get(), expectedPixelValues,
46 "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
Brian Salomon28a8f282019-10-24 20:07:39 -040092 TestReadPixels(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(
Brian Salomon4eda7102019-10-21 15:04:52 -0400113 context, renderable, origin,
114 {GrColorType::kRGBA_8888, kPremul_SkAlphaType, nullptr, dstContext->width(),
115 dstContext->height()},
116 pixels.get(), 0);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400117 // If this assert ever fails we can add a fallback to do copy as draw, but until then we can
118 // be more restrictive.
119 SkAssertResult(dstContext->testCopy(src.get()));
Brian Salomon28a8f282019-10-24 20:07:39 -0400120 TestReadPixels(reporter, dstContext, pixels.get(), testName);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400121 }
122}
123
John Rosascoa9b348f2019-11-08 13:18:15 -0800124#ifdef SK_GL
bsalomon758586c2016-04-06 14:02:39 -0700125DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700126 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500127 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
bsalomon8b7451a2016-05-11 06:33:06 -0700128 sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400129 static const int kWidth = 16;
130 static const int kHeight = 16;
bsalomone5286e02016-01-14 09:24:09 -0800131
132 GrColor pixels[kWidth * kHeight];
133 for (int y = 0; y < kHeight; ++y) {
134 for (int x = 0; x < kWidth; ++x) {
135 pixels[y * kWidth + x] = y * kWidth + x;
136 }
137 }
138
Greg Daniel7ef28f32017-04-20 16:41:55 +0000139 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
140 bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin;
141
bsalomone5286e02016-01-14 09:24:09 -0800142 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
143 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
144 pixels);
145
146 if (!rectTexID) {
147 return;
148 }
149
150 // Let GrContext know that we messed with the GL context directly.
151 context->resetContext();
152
153 // Wrap the rectangle texture ID in a GrTexture
154 GrGLTextureInfo rectangleInfo;
155 rectangleInfo.fID = rectTexID;
156 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
Greg Danielfa55f2e2019-06-13 15:21:38 -0400157 rectangleInfo.fFormat = GR_GL_RGBA8;
bsalomone5286e02016-01-14 09:24:09 -0800158
Greg Daniel108bb232018-07-03 16:18:29 -0400159 GrBackendTexture rectangleTex(kWidth, kHeight, GrMipMapped::kNo, rectangleInfo);
bsalomone5286e02016-01-14 09:24:09 -0800160
161 GrColor refPixels[kWidth * kHeight];
bsalomone5286e02016-01-14 09:24:09 -0800162 for (int y = 0; y < kHeight; ++y) {
163 for (int x = 0; x < kWidth; ++x) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000164 int y0 = useBLOrigin ? kHeight - y - 1 : y;
bsalomone5286e02016-01-14 09:24:09 -0800165 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
166 }
167 }
168
Brian Salomonc67c31c2018-12-06 10:00:03 -0500169 sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400170 rectangleTex, GrColorType::kRGBA_8888, origin,
171 kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500172
Robert Phillips26caf892017-01-27 10:58:31 -0500173 if (!rectProxy) {
174 ERRORF(reporter, "Error creating proxy for rectangle texture.");
175 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
176 continue;
bsalomone5286e02016-01-14 09:24:09 -0800177 }
178
Greg Daniel09c94002018-06-08 22:11:51 +0000179 SkASSERT(rectProxy->mipMapped() == GrMipMapped::kNo);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400180 SkASSERT(rectProxy->peekTexture()->texturePriv().mipMapped() == GrMipMapped::kNo);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400181
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400182 SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle);
183 SkASSERT(rectProxy->peekTexture()->texturePriv().textureType() ==
Brian Salomon7226c232018-07-30 13:13:17 -0400184 GrTextureType::kRectangle);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400185 SkASSERT(rectProxy->hasRestrictedSampling());
186 SkASSERT(rectProxy->peekTexture()->texturePriv().hasRestrictedSampling());
Robert Phillipsabf7b762018-03-21 12:13:37 -0400187
Brian Salomonfc118442019-11-22 19:09:27 -0500188 test_basic_draw_as_src(reporter, context, rectProxy, GrColorType::kRGBA_8888,
189 kPremul_SkAlphaType, refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800190
Robert Phillips3500b772017-01-27 10:11:42 -0500191 // Test copy to both a texture and RT
Brian Salomon28a8f282019-10-24 20:07:39 -0400192 TestCopyFromSurface(reporter, context, rectProxy.get(), GrColorType::kRGBA_8888, refPixels,
193 "RectangleTexture-copy-from");
Brian Salomon739c5bf2016-11-07 09:53:44 -0500194
Greg Daniel3912a4b2020-01-14 09:56:04 -0500195 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(rectangleTex.getBackendFormat(),
196 GrColorType::kRGBA_8888);
197 GrSurfaceProxyView view(std::move(rectProxy), origin, swizzle);
198 auto rectContext = GrSurfaceContext::Make(context, std::move(view),
Greg Danielbfa19c42019-12-19 16:41:40 -0500199 GrColorType::kRGBA_8888, kPremul_SkAlphaType,
200 nullptr);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500201 SkASSERT(rectContext);
bsalomone5286e02016-01-14 09:24:09 -0800202
Brian Salomon28a8f282019-10-24 20:07:39 -0400203 TestReadPixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read");
bsalomone5286e02016-01-14 09:24:09 -0800204
Robert Phillips69893702019-02-22 11:16:30 -0500205 test_copy_to_surface(reporter, context, rectContext.get(), "RectangleTexture-copy-to");
bsalomone5286e02016-01-14 09:24:09 -0800206
Brian Salomon28a8f282019-10-24 20:07:39 -0400207 TestWritePixels(reporter, rectContext.get(), true, "RectangleTexture-write");
Robert Phillipsd46697a2017-01-25 12:10:37 -0500208
Robert Phillips26c90e02017-03-14 14:39:29 -0400209 test_clear(reporter, rectContext.get());
bsalomone5286e02016-01-14 09:24:09 -0800210
211 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
212 }
213}
John Rosascoa9b348f2019-11-08 13:18:15 -0800214#endif