blob: d805c2b4f8e674710ca20b74c15fb00dc464f08b [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrSurfacePriv.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040017#include "src/gpu/GrTexture.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050018#include "src/gpu/SkGr.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080019#ifdef SK_GL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/gl/GrGLGpu.h"
21#include "src/gpu/gl/GrGLUtil.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080022#endif
Greg Daniel84ea0492019-06-05 16:52:02 -040023#include "tools/gpu/ProxyUtils.h"
bsalomone5286e02016-01-14 09:24:09 -080024
Brian Salomon739c5bf2016-11-07 09:53:44 -050025// skbug.com/5932
Robert Phillips6d344c32020-07-06 10:56:46 -040026static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrRecordingContext* context,
Greg Daniel124486b2020-02-11 11:54:55 -050027 GrSurfaceProxyView rectView, GrColorType colorType,
Brian Salomonfc118442019-11-22 19:09:27 -050028 SkAlphaType alphaType, uint32_t expectedPixelValues[]) {
Greg Daniele20fcad2020-01-08 11:52:34 -050029 auto rtContext = GrRenderTargetContext::Make(
Greg Daniel124486b2020-02-11 11:54:55 -050030 context, colorType, nullptr, SkBackingFit::kExact, rectView.proxy()->dimensions());
Brian Salomone69b9ef2020-07-22 11:18:06 -040031 for (auto filter : {GrSamplerState::Filter::kNearest, GrSamplerState::Filter::kLinear}) {
32 for (auto mm : {GrSamplerState::MipmapMode::kNone, GrSamplerState::MipmapMode::kLinear}) {
33 rtContext->clear(SkPMColor4f::FromBytes_RGBA(0xDDCCBBAA));
34 auto fp = GrTextureEffect::Make(rectView, alphaType, SkMatrix::I(), filter, mm);
35 GrPaint paint;
36 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
37 paint.setColorFragmentProcessor(std::move(fp));
38 rtContext->drawPaint(nullptr, std::move(paint), SkMatrix::I());
39 TestReadPixels(reporter, rtContext.get(), expectedPixelValues,
40 "RectangleTexture-basic-draw");
41 }
Brian Salomon739c5bf2016-11-07 09:53:44 -050042 }
43}
44
Robert Phillips26c90e02017-03-14 14:39:29 -040045static void test_clear(skiatest::Reporter* reporter, GrSurfaceContext* rectContext) {
Robert Phillipsd46697a2017-01-25 12:10:37 -050046 if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) {
bsalomone5286e02016-01-14 09:24:09 -080047 // Clear the whole thing.
48 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
Michael Ludwig81d41722020-05-26 16:57:38 -040049 rtc->clear(SkPMColor4f::FromBytes_RGBA(color0));
bsalomone5286e02016-01-14 09:24:09 -080050
Robert Phillipsd46697a2017-01-25 12:10:37 -050051 int w = rtc->width();
52 int h = rtc->height();
bsalomone5286e02016-01-14 09:24:09 -080053 int pixelCnt = w * h;
54 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
55
56 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -080057 uint32_t expectedColor0 = 0;
Mike Kleine72e7732018-06-14 14:41:22 -040058 uint8_t* expectedBytes0 = reinterpret_cast<uint8_t*>(&expectedColor0);
bsalomone5286e02016-01-14 09:24:09 -080059 expectedBytes0[0] = GrColorUnpackR(color0);
60 expectedBytes0[1] = GrColorUnpackG(color0);
61 expectedBytes0[2] = GrColorUnpackB(color0);
62 expectedBytes0[3] = GrColorUnpackA(color0);
Robert Phillipsd46697a2017-01-25 12:10:37 -050063 for (int i = 0; i < rtc->width() * rtc->height(); ++i) {
bsalomone5286e02016-01-14 09:24:09 -080064 expectedPixels.get()[i] = expectedColor0;
65 }
66
67 // Clear the the top to a different color.
68 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
69 SkIRect rect = SkIRect::MakeWH(w, h/2);
Michael Ludwig81d41722020-05-26 16:57:38 -040070 rtc->clear(rect, SkPMColor4f::FromBytes_RGBA(color1));
bsalomone5286e02016-01-14 09:24:09 -080071
benjaminwagner2a641ee2016-01-15 06:21:18 -080072 uint32_t expectedColor1 = 0;
Mike Kleine72e7732018-06-14 14:41:22 -040073 uint8_t* expectedBytes1 = reinterpret_cast<uint8_t*>(&expectedColor1);
bsalomone5286e02016-01-14 09:24:09 -080074 expectedBytes1[0] = GrColorUnpackR(color1);
75 expectedBytes1[1] = GrColorUnpackG(color1);
76 expectedBytes1[2] = GrColorUnpackB(color1);
77 expectedBytes1[3] = GrColorUnpackA(color1);
78
79 for (int y = 0; y < h/2; ++y) {
80 for (int x = 0; x < w; ++x) {
81 expectedPixels.get()[y * h + x] = expectedColor1;
82 }
83 }
84
Brian Salomon28a8f282019-10-24 20:07:39 -040085 TestReadPixels(reporter, rtc, expectedPixels.get(), "RectangleTexture-clear");
bsalomone5286e02016-01-14 09:24:09 -080086 }
87}
88
Greg Daniel46cfbc62019-06-07 11:43:30 -040089static void test_copy_to_surface(skiatest::Reporter* reporter,
Robert Phillips00f78de2020-07-01 16:09:43 -040090 GrDirectContext* context,
Greg Daniel46cfbc62019-06-07 11:43:30 -040091 GrSurfaceContext* dstContext,
92 const char* testName) {
93
94 int pixelCnt = dstContext->width() * dstContext->height();
95 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
96 for (int y = 0; y < dstContext->width(); ++y) {
97 for (int x = 0; x < dstContext->height(); ++x) {
98 pixels.get()[y * dstContext->width() + x] =
99 SkColorToPremulGrColor(SkColorSetARGB(2*y, y, x, x * y));
100 }
101 }
102
103 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
Greg Danielb8d84f82020-02-13 14:25:00 -0500104 auto origin = dstContext->origin();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400105 auto src = sk_gpu_test::MakeTextureProxyFromData(
Brian Salomon4eda7102019-10-21 15:04:52 -0400106 context, renderable, origin,
107 {GrColorType::kRGBA_8888, kPremul_SkAlphaType, nullptr, dstContext->width(),
108 dstContext->height()},
109 pixels.get(), 0);
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 Salomonc5243782020-04-02 12:50:34 -0400112 SkAssertResult(dstContext->testCopy(src.get()));
Brian Salomon28a8f282019-10-24 20:07:39 -0400113 TestReadPixels(reporter, 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) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400119 auto direct = ctxInfo.directContext();
120
121 GrProxyProvider* proxyProvider = direct->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 }) {
135 bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin;
136
Brian Salomon0f396992020-06-19 19:51:21 -0400137 auto format = GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_RECTANGLE);
Robert Phillips6d344c32020-07-06 10:56:46 -0400138 GrBackendTexture rectangleTex = direct->createBackendTexture(kWidth,
139 kHeight,
140 format,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400141 GrMipmapped::kNo,
Robert Phillips6d344c32020-07-06 10:56:46 -0400142 GrRenderable::kYes);
Brian Salomon0f396992020-06-19 19:51:21 -0400143 if (!rectangleTex.isValid()) {
144 continue;
bsalomone5286e02016-01-14 09:24:09 -0800145 }
146
Robert Phillips6d344c32020-07-06 10:56:46 -0400147 if (!direct->updateBackendTexture(rectangleTex, &pm, 1, nullptr, nullptr)) {
Brian Salomon0f396992020-06-19 19:51:21 -0400148 continue;
149 }
bsalomone5286e02016-01-14 09:24:09 -0800150
151 GrColor refPixels[kWidth * kHeight];
bsalomone5286e02016-01-14 09:24:09 -0800152 for (int y = 0; y < kHeight; ++y) {
153 for (int x = 0; x < kWidth; ++x) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000154 int y0 = useBLOrigin ? kHeight - y - 1 : y;
bsalomone5286e02016-01-14 09:24:09 -0800155 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
156 }
157 }
158
Brian Salomonc67c31c2018-12-06 10:00:03 -0500159 sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400160 rectangleTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500161
Robert Phillips26caf892017-01-27 10:58:31 -0500162 if (!rectProxy) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400163 direct->deleteBackendTexture(rectangleTex);
Robert Phillips26caf892017-01-27 10:58:31 -0500164 continue;
bsalomone5286e02016-01-14 09:24:09 -0800165 }
166
Brian Salomon8c82a872020-07-21 12:09:58 -0400167 SkASSERT(rectProxy->mipmapped() == GrMipmapped::kNo);
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400168 SkASSERT(rectProxy->peekTexture()->mipmapped() == GrMipmapped::kNo);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400169
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400170 SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle);
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400171 SkASSERT(rectProxy->peekTexture()->textureType() == GrTextureType::kRectangle);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400172 SkASSERT(rectProxy->hasRestrictedSampling());
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400173 SkASSERT(rectProxy->peekTexture()->hasRestrictedSampling());
Robert Phillipsabf7b762018-03-21 12:13:37 -0400174
Robert Phillips6d344c32020-07-06 10:56:46 -0400175 GrSwizzle swizzle = direct->priv().caps()->getReadSwizzle(rectangleTex.getBackendFormat(),
176 GrColorType::kRGBA_8888);
Greg Daniel124486b2020-02-11 11:54:55 -0500177 GrSurfaceProxyView view(rectProxy, origin, swizzle);
178
Robert Phillips6d344c32020-07-06 10:56:46 -0400179 test_basic_draw_as_src(reporter, direct, view, GrColorType::kRGBA_8888,
Brian Salomonfc118442019-11-22 19:09:27 -0500180 kPremul_SkAlphaType, refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800181
Robert Phillips3500b772017-01-27 10:11:42 -0500182 // Test copy to both a texture and RT
Robert Phillips6d344c32020-07-06 10:56:46 -0400183 TestCopyFromSurface(reporter, direct, rectProxy.get(), origin, GrColorType::kRGBA_8888,
Greg Daniel40903af2020-01-30 14:55:05 -0500184 refPixels, "RectangleTexture-copy-from");
Brian Salomon739c5bf2016-11-07 09:53:44 -0500185
Robert Phillips6d344c32020-07-06 10:56:46 -0400186 auto rectContext = GrSurfaceContext::Make(direct, std::move(view),
Greg Danielbfa19c42019-12-19 16:41:40 -0500187 GrColorType::kRGBA_8888, kPremul_SkAlphaType,
188 nullptr);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500189 SkASSERT(rectContext);
bsalomone5286e02016-01-14 09:24:09 -0800190
Brian Salomon28a8f282019-10-24 20:07:39 -0400191 TestReadPixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read");
bsalomone5286e02016-01-14 09:24:09 -0800192
Robert Phillips6d344c32020-07-06 10:56:46 -0400193 test_copy_to_surface(reporter, direct, rectContext.get(), "RectangleTexture-copy-to");
bsalomone5286e02016-01-14 09:24:09 -0800194
Brian Salomon28a8f282019-10-24 20:07:39 -0400195 TestWritePixels(reporter, rectContext.get(), true, "RectangleTexture-write");
Robert Phillipsd46697a2017-01-25 12:10:37 -0500196
Robert Phillips26c90e02017-03-14 14:39:29 -0400197 test_clear(reporter, rectContext.get());
bsalomone5286e02016-01-14 09:24:09 -0800198
Robert Phillips6d344c32020-07-06 10:56:46 -0400199 direct->deleteBackendTexture(rectangleTex);
bsalomone5286e02016-01-14 09:24:09 -0800200 }
201}
John Rosascoa9b348f2019-11-08 13:18:15 -0800202#endif