blob: 9c5bc7292bda92e0bae17f963448c7a5f90d6018 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrContextPriv.h"
13#include "src/gpu/GrProxyProvider.h"
14#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040015#include "src/gpu/GrSurfaceContextPriv.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040016#include "src/gpu/GrTexture.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050017#include "src/gpu/SkGr.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080018#ifdef SK_GL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/gl/GrGLGpu.h"
20#include "src/gpu/gl/GrGLUtil.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080021#endif
Greg Daniel84ea0492019-06-05 16:52:02 -040022#include "tools/gpu/ProxyUtils.h"
bsalomone5286e02016-01-14 09:24:09 -080023
Brian Salomon739c5bf2016-11-07 09:53:44 -050024// skbug.com/5932
Adlai Hollercf0d08e2020-08-11 12:36:10 +000025static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrRecordingContext* context,
Greg Daniel124486b2020-02-11 11:54:55 -050026 GrSurfaceProxyView rectView, GrColorType colorType,
Brian Salomonfc118442019-11-22 19:09:27 -050027 SkAlphaType alphaType, uint32_t expectedPixelValues[]) {
Greg Daniele20fcad2020-01-08 11:52:34 -050028 auto rtContext = GrRenderTargetContext::Make(
Adlai Hollercf0d08e2020-08-11 12:36:10 +000029 context, colorType, nullptr, SkBackingFit::kExact, rectView.proxy()->dimensions());
Brian Salomone69b9ef2020-07-22 11:18:06 -040030 for (auto filter : {GrSamplerState::Filter::kNearest, GrSamplerState::Filter::kLinear}) {
31 for (auto mm : {GrSamplerState::MipmapMode::kNone, GrSamplerState::MipmapMode::kLinear}) {
32 rtContext->clear(SkPMColor4f::FromBytes_RGBA(0xDDCCBBAA));
33 auto fp = GrTextureEffect::Make(rectView, alphaType, SkMatrix::I(), filter, mm);
34 GrPaint paint;
35 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
36 paint.setColorFragmentProcessor(std::move(fp));
37 rtContext->drawPaint(nullptr, std::move(paint), SkMatrix::I());
Adlai Hollercf0d08e2020-08-11 12:36:10 +000038 TestReadPixels(reporter, rtContext.get(), expectedPixelValues,
Brian Salomone69b9ef2020-07-22 11:18:06 -040039 "RectangleTexture-basic-draw");
40 }
Brian Salomon739c5bf2016-11-07 09:53:44 -050041 }
42}
43
Adlai Hollercf0d08e2020-08-11 12:36:10 +000044static void test_clear(skiatest::Reporter* reporter, GrSurfaceContext* rectContext) {
Robert Phillipsd46697a2017-01-25 12:10:37 -050045 if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) {
bsalomone5286e02016-01-14 09:24:09 -080046 // Clear the whole thing.
47 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
Michael Ludwig81d41722020-05-26 16:57:38 -040048 rtc->clear(SkPMColor4f::FromBytes_RGBA(color0));
bsalomone5286e02016-01-14 09:24:09 -080049
Robert Phillipsd46697a2017-01-25 12:10:37 -050050 int w = rtc->width();
51 int h = rtc->height();
bsalomone5286e02016-01-14 09:24:09 -080052 int pixelCnt = w * h;
53 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
54
55 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -080056 uint32_t expectedColor0 = 0;
Mike Kleine72e7732018-06-14 14:41:22 -040057 uint8_t* expectedBytes0 = reinterpret_cast<uint8_t*>(&expectedColor0);
bsalomone5286e02016-01-14 09:24:09 -080058 expectedBytes0[0] = GrColorUnpackR(color0);
59 expectedBytes0[1] = GrColorUnpackG(color0);
60 expectedBytes0[2] = GrColorUnpackB(color0);
61 expectedBytes0[3] = GrColorUnpackA(color0);
Robert Phillipsd46697a2017-01-25 12:10:37 -050062 for (int i = 0; i < rtc->width() * rtc->height(); ++i) {
bsalomone5286e02016-01-14 09:24:09 -080063 expectedPixels.get()[i] = expectedColor0;
64 }
65
66 // Clear the the top to a different color.
67 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
68 SkIRect rect = SkIRect::MakeWH(w, h/2);
Michael Ludwig81d41722020-05-26 16:57:38 -040069 rtc->clear(rect, SkPMColor4f::FromBytes_RGBA(color1));
bsalomone5286e02016-01-14 09:24:09 -080070
benjaminwagner2a641ee2016-01-15 06:21:18 -080071 uint32_t expectedColor1 = 0;
Mike Kleine72e7732018-06-14 14:41:22 -040072 uint8_t* expectedBytes1 = reinterpret_cast<uint8_t*>(&expectedColor1);
bsalomone5286e02016-01-14 09:24:09 -080073 expectedBytes1[0] = GrColorUnpackR(color1);
74 expectedBytes1[1] = GrColorUnpackG(color1);
75 expectedBytes1[2] = GrColorUnpackB(color1);
76 expectedBytes1[3] = GrColorUnpackA(color1);
77
78 for (int y = 0; y < h/2; ++y) {
79 for (int x = 0; x < w; ++x) {
80 expectedPixels.get()[y * h + x] = expectedColor1;
81 }
82 }
83
Adlai Hollercf0d08e2020-08-11 12:36:10 +000084 TestReadPixels(reporter, rtc, expectedPixels.get(), "RectangleTexture-clear");
bsalomone5286e02016-01-14 09:24:09 -080085 }
86}
87
Greg Daniel46cfbc62019-06-07 11:43:30 -040088static void test_copy_to_surface(skiatest::Reporter* reporter,
Adlai Hollercf0d08e2020-08-11 12:36:10 +000089 GrDirectContext* context,
Greg Daniel46cfbc62019-06-07 11:43:30 -040090 GrSurfaceContext* dstContext,
91 const char* testName) {
92
93 int pixelCnt = dstContext->width() * dstContext->height();
94 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
95 for (int y = 0; y < dstContext->width(); ++y) {
96 for (int x = 0; x < dstContext->height(); ++x) {
97 pixels.get()[y * dstContext->width() + x] =
98 SkColorToPremulGrColor(SkColorSetARGB(2*y, y, x, x * y));
99 }
100 }
101
102 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
Greg Danielb8d84f82020-02-13 14:25:00 -0500103 auto origin = dstContext->origin();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400104 auto src = sk_gpu_test::MakeTextureProxyFromData(
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000105 context, renderable, origin,
Brian Salomon4eda7102019-10-21 15:04:52 -0400106 {GrColorType::kRGBA_8888, kPremul_SkAlphaType, nullptr, dstContext->width(),
107 dstContext->height()},
108 pixels.get(), 0);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400109 // If this assert ever fails we can add a fallback to do copy as draw, but until then we can
110 // be more restrictive.
Brian Salomonc5243782020-04-02 12:50:34 -0400111 SkAssertResult(dstContext->testCopy(src.get()));
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000112 TestReadPixels(reporter, dstContext, pixels.get(), testName);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400113 }
114}
115
John Rosascoa9b348f2019-11-08 13:18:15 -0800116#ifdef SK_GL
bsalomon758586c2016-04-06 14:02:39 -0700117DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000118 auto direct = ctxInfo.directContext();
Robert Phillips6d344c32020-07-06 10:56:46 -0400119
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000120 GrProxyProvider* proxyProvider = direct->priv().proxyProvider();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400121 static const int kWidth = 16;
122 static const int kHeight = 16;
bsalomone5286e02016-01-14 09:24:09 -0800123
Brian Salomon0f396992020-06-19 19:51:21 -0400124 uint32_t pixels[kWidth * kHeight];
bsalomone5286e02016-01-14 09:24:09 -0800125 for (int y = 0; y < kHeight; ++y) {
126 for (int x = 0; x < kWidth; ++x) {
127 pixels[y * kWidth + x] = y * kWidth + x;
128 }
129 }
Brian Salomon0f396992020-06-19 19:51:21 -0400130 auto ii = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
131 SkPixmap pm(ii, pixels, sizeof(uint32_t)*kWidth);
bsalomone5286e02016-01-14 09:24:09 -0800132
Greg Daniel7ef28f32017-04-20 16:41:55 +0000133 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
134 bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin;
135
Brian Salomon0f396992020-06-19 19:51:21 -0400136 auto format = GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_RECTANGLE);
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000137 GrBackendTexture rectangleTex = direct->createBackendTexture(kWidth,
138 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
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000146 if (!direct->updateBackendTexture(rectangleTex, &pm, 1, 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) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000153 int y0 = useBLOrigin ? kHeight - y - 1 : y;
bsalomone5286e02016-01-14 09:24:09 -0800154 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
155 }
156 }
157
Brian Salomonc67c31c2018-12-06 10:00:03 -0500158 sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400159 rectangleTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500160
Robert Phillips26caf892017-01-27 10:58:31 -0500161 if (!rectProxy) {
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000162 direct->deleteBackendTexture(rectangleTex);
Robert Phillips26caf892017-01-27 10:58:31 -0500163 continue;
bsalomone5286e02016-01-14 09:24:09 -0800164 }
165
Brian Salomon8c82a872020-07-21 12:09:58 -0400166 SkASSERT(rectProxy->mipmapped() == GrMipmapped::kNo);
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400167 SkASSERT(rectProxy->peekTexture()->mipmapped() == GrMipmapped::kNo);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400168
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400169 SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle);
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400170 SkASSERT(rectProxy->peekTexture()->textureType() == GrTextureType::kRectangle);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400171 SkASSERT(rectProxy->hasRestrictedSampling());
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400172 SkASSERT(rectProxy->peekTexture()->hasRestrictedSampling());
Robert Phillipsabf7b762018-03-21 12:13:37 -0400173
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000174 GrSwizzle swizzle = direct->priv().caps()->getReadSwizzle(rectangleTex.getBackendFormat(),
175 GrColorType::kRGBA_8888);
Greg Daniel124486b2020-02-11 11:54:55 -0500176 GrSurfaceProxyView view(rectProxy, origin, swizzle);
177
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000178 test_basic_draw_as_src(reporter, direct, view, GrColorType::kRGBA_8888,
Brian Salomonfc118442019-11-22 19:09:27 -0500179 kPremul_SkAlphaType, refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800180
Robert Phillips3500b772017-01-27 10:11:42 -0500181 // Test copy to both a texture and RT
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000182 TestCopyFromSurface(reporter, direct, rectProxy.get(), origin, GrColorType::kRGBA_8888,
Greg Daniel40903af2020-01-30 14:55:05 -0500183 refPixels, "RectangleTexture-copy-from");
Brian Salomon739c5bf2016-11-07 09:53:44 -0500184
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000185 auto rectContext = GrSurfaceContext::Make(direct, std::move(view),
Greg Danielbfa19c42019-12-19 16:41:40 -0500186 GrColorType::kRGBA_8888, kPremul_SkAlphaType,
187 nullptr);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500188 SkASSERT(rectContext);
bsalomone5286e02016-01-14 09:24:09 -0800189
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000190 TestReadPixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read");
bsalomone5286e02016-01-14 09:24:09 -0800191
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000192 test_copy_to_surface(reporter, direct, rectContext.get(), "RectangleTexture-copy-to");
bsalomone5286e02016-01-14 09:24:09 -0800193
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000194 TestWritePixels(reporter, rectContext.get(), true, "RectangleTexture-write");
Robert Phillipsd46697a2017-01-25 12:10:37 -0500195
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000196 test_clear(reporter, rectContext.get());
bsalomone5286e02016-01-14 09:24:09 -0800197
Adlai Hollercf0d08e2020-08-11 12:36:10 +0000198 direct->deleteBackendTexture(rectangleTex);
bsalomone5286e02016-01-14 09:24:09 -0800199 }
200}
John Rosascoa9b348f2019-11-08 13:18:15 -0800201#endif