blob: 8133147196625af061a51d368559f722a5a5a37d [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
Robert Phillips00f78de2020-07-01 16:09:43 -040011#include "include/gpu/GrDirectContext.h"
Adlai Hollera0693042020-10-14 11:23:11 -040012#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrProxyProvider.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050014#include "src/gpu/GrSurfaceDrawContext.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040015#include "src/gpu/GrTexture.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050016#include "src/gpu/SkGr.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080017#ifdef SK_GL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/gl/GrGLGpu.h"
19#include "src/gpu/gl/GrGLUtil.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080020#endif
Greg Daniel84ea0492019-06-05 16:52:02 -040021#include "tools/gpu/ProxyUtils.h"
bsalomone5286e02016-01-14 09:24:09 -080022
Brian Salomon739c5bf2016-11-07 09:53:44 -050023// skbug.com/5932
Adlai Hollerc95b5892020-08-11 12:02:22 -040024static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrDirectContext* dContext,
Greg Daniel124486b2020-02-11 11:54:55 -050025 GrSurfaceProxyView rectView, GrColorType colorType,
Brian Salomonfc118442019-11-22 19:09:27 -050026 SkAlphaType alphaType, uint32_t expectedPixelValues[]) {
Brian Salomon590f5672020-12-16 11:44:47 -050027 auto fillContext = GrSurfaceFillContext::Make(
28 dContext, {colorType, kPremul_SkAlphaType, nullptr, rectView.dimensions()});
Brian Salomone69b9ef2020-07-22 11:18:06 -040029 for (auto filter : {GrSamplerState::Filter::kNearest, GrSamplerState::Filter::kLinear}) {
30 for (auto mm : {GrSamplerState::MipmapMode::kNone, GrSamplerState::MipmapMode::kLinear}) {
Brian Salomon590f5672020-12-16 11:44:47 -050031 fillContext->clear(SkPMColor4f::FromBytes_RGBA(0xDDCCBBAA));
Brian Salomone69b9ef2020-07-22 11:18:06 -040032 auto fp = GrTextureEffect::Make(rectView, alphaType, SkMatrix::I(), filter, mm);
Brian Salomon590f5672020-12-16 11:44:47 -050033 fillContext->fillWithFP(std::move(fp));
34 TestReadPixels(reporter, dContext, fillContext.get(), expectedPixelValues,
Brian Salomone69b9ef2020-07-22 11:18:06 -040035 "RectangleTexture-basic-draw");
36 }
Brian Salomon739c5bf2016-11-07 09:53:44 -050037 }
38}
39
Adlai Hollerc95b5892020-08-11 12:02:22 -040040static void test_clear(skiatest::Reporter* reporter, GrDirectContext* dContext,
41 GrSurfaceContext* rectContext) {
Brian Salomon590f5672020-12-16 11:44:47 -050042 if (GrSurfaceFillContext* sfc = rectContext->asFillContext()) {
bsalomone5286e02016-01-14 09:24:09 -080043 // Clear the whole thing.
44 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
Brian Salomon590f5672020-12-16 11:44:47 -050045 sfc->clear(SkPMColor4f::FromBytes_RGBA(color0));
bsalomone5286e02016-01-14 09:24:09 -080046
Brian Salomon590f5672020-12-16 11:44:47 -050047 int w = sfc->width();
48 int h = sfc->height();
bsalomone5286e02016-01-14 09:24:09 -080049 int pixelCnt = w * h;
50 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
51
52 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -080053 uint32_t expectedColor0 = 0;
Mike Kleine72e7732018-06-14 14:41:22 -040054 uint8_t* expectedBytes0 = reinterpret_cast<uint8_t*>(&expectedColor0);
bsalomone5286e02016-01-14 09:24:09 -080055 expectedBytes0[0] = GrColorUnpackR(color0);
56 expectedBytes0[1] = GrColorUnpackG(color0);
57 expectedBytes0[2] = GrColorUnpackB(color0);
58 expectedBytes0[3] = GrColorUnpackA(color0);
Brian Salomon590f5672020-12-16 11:44:47 -050059 for (int i = 0; i < sfc->width() * sfc->height(); ++i) {
bsalomone5286e02016-01-14 09:24:09 -080060 expectedPixels.get()[i] = expectedColor0;
61 }
62
63 // Clear the the top to a different color.
64 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
65 SkIRect rect = SkIRect::MakeWH(w, h/2);
Brian Salomon590f5672020-12-16 11:44:47 -050066 sfc->clear(rect, SkPMColor4f::FromBytes_RGBA(color1));
bsalomone5286e02016-01-14 09:24:09 -080067
benjaminwagner2a641ee2016-01-15 06:21:18 -080068 uint32_t expectedColor1 = 0;
Mike Kleine72e7732018-06-14 14:41:22 -040069 uint8_t* expectedBytes1 = reinterpret_cast<uint8_t*>(&expectedColor1);
bsalomone5286e02016-01-14 09:24:09 -080070 expectedBytes1[0] = GrColorUnpackR(color1);
71 expectedBytes1[1] = GrColorUnpackG(color1);
72 expectedBytes1[2] = GrColorUnpackB(color1);
73 expectedBytes1[3] = GrColorUnpackA(color1);
74
75 for (int y = 0; y < h/2; ++y) {
76 for (int x = 0; x < w; ++x) {
77 expectedPixels.get()[y * h + x] = expectedColor1;
78 }
79 }
80
Brian Salomon590f5672020-12-16 11:44:47 -050081 TestReadPixels(reporter, dContext, sfc, expectedPixels.get(), "RectangleTexture-clear");
bsalomone5286e02016-01-14 09:24:09 -080082 }
83}
84
Greg Daniel46cfbc62019-06-07 11:43:30 -040085static void test_copy_to_surface(skiatest::Reporter* reporter,
Adlai Hollerc95b5892020-08-11 12:02:22 -040086 GrDirectContext* dContext,
Greg Daniel46cfbc62019-06-07 11:43:30 -040087 GrSurfaceContext* dstContext,
88 const char* testName) {
89
90 int pixelCnt = dstContext->width() * dstContext->height();
91 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
92 for (int y = 0; y < dstContext->width(); ++y) {
93 for (int x = 0; x < dstContext->height(); ++x) {
94 pixels.get()[y * dstContext->width() + x] =
95 SkColorToPremulGrColor(SkColorSetARGB(2*y, y, x, x * y));
96 }
97 }
98
99 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
Greg Danielb8d84f82020-02-13 14:25:00 -0500100 auto origin = dstContext->origin();
Brian Salomondd4087d2020-12-23 20:36:44 -0500101 GrImageInfo info(GrColorType::kRGBA_8888,
102 kPremul_SkAlphaType,
103 nullptr,
104 dstContext->dimensions());
105 GrPixmap pixmap(info, pixels.get(), dstContext->width()*sizeof(uint32_t));
106 auto srcView = sk_gpu_test::MakeTextureProxyViewFromData(dContext,
107 renderable,
108 origin,
109 pixmap);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400110 // If this assert ever fails we can add a fallback to do copy as draw, but until then we can
111 // be more restrictive.
Brian Salomon982127b2021-01-21 10:43:35 -0500112 SkAssertResult(dstContext->testCopy(srcView.refProxy()));
Adlai Hollerc95b5892020-08-11 12:02:22 -0400113 TestReadPixels(reporter, dContext, dstContext, pixels.get(), testName);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400114 }
115}
116
John Rosascoa9b348f2019-11-08 13:18:15 -0800117#ifdef SK_GL
bsalomon758586c2016-04-06 14:02:39 -0700118DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400119 auto dContext = ctxInfo.directContext();
Robert Phillips6d344c32020-07-06 10:56:46 -0400120
Adlai Hollerc95b5892020-08-11 12:02:22 -0400121 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400122 static const int kWidth = 16;
123 static const int kHeight = 16;
bsalomone5286e02016-01-14 09:24:09 -0800124
Brian Salomon0f396992020-06-19 19:51:21 -0400125 uint32_t pixels[kWidth * kHeight];
bsalomone5286e02016-01-14 09:24:09 -0800126 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 }
Brian Salomon0f396992020-06-19 19:51:21 -0400131 auto ii = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
132 SkPixmap pm(ii, pixels, sizeof(uint32_t)*kWidth);
bsalomone5286e02016-01-14 09:24:09 -0800133
Greg Daniel7ef28f32017-04-20 16:41:55 +0000134 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000135
Brian Salomon0f396992020-06-19 19:51:21 -0400136 auto format = GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_RECTANGLE);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400137 GrBackendTexture rectangleTex = dContext->createBackendTexture(kWidth,
Brian Salomon14f99fc2020-12-07 12:19:47 -0500138 kHeight,
139 format,
140 GrMipmapped::kNo,
141 GrRenderable::kYes);
Brian Salomon0f396992020-06-19 19:51:21 -0400142 if (!rectangleTex.isValid()) {
143 continue;
bsalomone5286e02016-01-14 09:24:09 -0800144 }
145
Brian Salomonb5f880a2020-12-07 11:30:16 -0500146 if (!dContext->updateBackendTexture(rectangleTex, &pm, 1, origin, nullptr, nullptr)) {
Brian Salomon0f396992020-06-19 19:51:21 -0400147 continue;
148 }
bsalomone5286e02016-01-14 09:24:09 -0800149
150 GrColor refPixels[kWidth * kHeight];
bsalomone5286e02016-01-14 09:24:09 -0800151 for (int y = 0; y < kHeight; ++y) {
152 for (int x = 0; x < kWidth; ++x) {
Brian Salomonb5f880a2020-12-07 11:30:16 -0500153 refPixels[y * kWidth + x] = pixels[y * kWidth + x];
bsalomone5286e02016-01-14 09:24:09 -0800154 }
155 }
156
Brian Salomonc67c31c2018-12-06 10:00:03 -0500157 sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400158 rectangleTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500159
Robert Phillips26caf892017-01-27 10:58:31 -0500160 if (!rectProxy) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400161 dContext->deleteBackendTexture(rectangleTex);
Robert Phillips26caf892017-01-27 10:58:31 -0500162 continue;
bsalomone5286e02016-01-14 09:24:09 -0800163 }
164
Brian Salomon8c82a872020-07-21 12:09:58 -0400165 SkASSERT(rectProxy->mipmapped() == GrMipmapped::kNo);
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400166 SkASSERT(rectProxy->peekTexture()->mipmapped() == GrMipmapped::kNo);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400167
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400168 SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle);
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400169 SkASSERT(rectProxy->peekTexture()->textureType() == GrTextureType::kRectangle);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400170 SkASSERT(rectProxy->hasRestrictedSampling());
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400171 SkASSERT(rectProxy->peekTexture()->hasRestrictedSampling());
Robert Phillipsabf7b762018-03-21 12:13:37 -0400172
Brian Salomon14f99fc2020-12-07 12:19:47 -0500173 GrImageInfo grII = ii;
Adlai Hollerc95b5892020-08-11 12:02:22 -0400174 GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(rectangleTex.getBackendFormat(),
Brian Salomon14f99fc2020-12-07 12:19:47 -0500175 grII.colorType());
Greg Daniel124486b2020-02-11 11:54:55 -0500176 GrSurfaceProxyView view(rectProxy, origin, swizzle);
177
Brian Salomon14f99fc2020-12-07 12:19:47 -0500178 test_basic_draw_as_src(reporter, dContext, view, grII.colorType(), kPremul_SkAlphaType,
179 refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800180
Robert Phillips3500b772017-01-27 10:11:42 -0500181 // Test copy to both a texture and RT
Brian Salomon982127b2021-01-21 10:43:35 -0500182 TestCopyFromSurface(reporter, dContext, rectProxy, origin, grII.colorType(), refPixels,
183 "RectangleTexture-copy-from");
Brian Salomon739c5bf2016-11-07 09:53:44 -0500184
Brian Salomon14f99fc2020-12-07 12:19:47 -0500185 auto rectContext = GrSurfaceContext::Make(dContext, std::move(view), grII.colorInfo());
Robert Phillipsd46697a2017-01-25 12:10:37 -0500186 SkASSERT(rectContext);
bsalomone5286e02016-01-14 09:24:09 -0800187
Adlai Hollerc95b5892020-08-11 12:02:22 -0400188 TestReadPixels(reporter, dContext, rectContext.get(), refPixels, "RectangleTexture-read");
bsalomone5286e02016-01-14 09:24:09 -0800189
Adlai Hollerc95b5892020-08-11 12:02:22 -0400190 test_copy_to_surface(reporter, dContext, rectContext.get(), "RectangleTexture-copy-to");
bsalomone5286e02016-01-14 09:24:09 -0800191
Adlai Hollerc95b5892020-08-11 12:02:22 -0400192 TestWritePixels(reporter, dContext, rectContext.get(), true, "RectangleTexture-write");
Robert Phillipsd46697a2017-01-25 12:10:37 -0500193
Adlai Hollerc95b5892020-08-11 12:02:22 -0400194 test_clear(reporter, dContext, rectContext.get());
bsalomone5286e02016-01-14 09:24:09 -0800195
Adlai Hollerc95b5892020-08-11 12:02:22 -0400196 dContext->deleteBackendTexture(rectangleTex);
bsalomone5286e02016-01-14 09:24:09 -0800197 }
198}
John Rosascoa9b348f2019-11-08 13:18:15 -0800199#endif