blob: 9d10b1fda54f1b8c3122f19a771bd55d935b1830 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/gl/GrGLGpu.h"
21#include "src/gpu/gl/GrGLUtil.h"
Greg Daniel84ea0492019-06-05 16:52:02 -040022#include "tools/gpu/ProxyUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tools/gpu/gl/GLTestContext.h"
bsalomone5286e02016-01-14 09:24:09 -080024
Brian Salomon739c5bf2016-11-07 09:53:44 -050025// skbug.com/5932
Robert Phillipsd46697a2017-01-25 12:10:37 -050026static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context,
Brian Salomond6287472019-06-24 15:50:07 -040027 sk_sp<GrTextureProxy> rectProxy, GrColorType colorType,
28 uint32_t expectedPixelValues[]) {
Brian Salomonbf6b9792019-08-21 09:38:10 -040029 auto rtContext = context->priv().makeDeferredRenderTargetContext(
30 SkBackingFit::kExact, rectProxy->width(), rectProxy->height(), colorType, nullptr);
Brian Salomon2bbdcc42017-09-07 12:36:34 -040031 for (auto filter : {GrSamplerState::Filter::kNearest,
32 GrSamplerState::Filter::kBilerp,
33 GrSamplerState::Filter::kMipMap}) {
Brian Osman9a9baae2018-11-05 15:06:26 -050034 rtContext->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xDDCCBBAA),
35 GrRenderTargetContext::CanClearFullscreen::kYes);
Brian Osman2240be92017-10-18 13:15:13 -040036 auto fp = GrSimpleTextureEffect::Make(rectProxy, SkMatrix::I(), filter);
Brian Salomon739c5bf2016-11-07 09:53:44 -050037 GrPaint paint;
38 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
39 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomon82f44312017-01-11 13:42:54 -050040 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Robert Phillips26c90e02017-03-14 14:39:29 -040041 test_read_pixels(reporter, rtContext.get(), expectedPixelValues,
Robert Phillips3500b772017-01-27 10:11:42 -050042 "RectangleTexture-basic-draw");
Brian Salomon739c5bf2016-11-07 09:53:44 -050043 }
44}
45
Robert Phillips26c90e02017-03-14 14:39:29 -040046static void test_clear(skiatest::Reporter* reporter, GrSurfaceContext* rectContext) {
Robert Phillipsd46697a2017-01-25 12:10:37 -050047 if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) {
bsalomone5286e02016-01-14 09:24:09 -080048 // Clear the whole thing.
49 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
Brian Osman9a9baae2018-11-05 15:06:26 -050050 rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(color0),
51 GrRenderTargetContext::CanClearFullscreen::kNo);
bsalomone5286e02016-01-14 09:24:09 -080052
Robert Phillipsd46697a2017-01-25 12:10:37 -050053 int w = rtc->width();
54 int h = rtc->height();
bsalomone5286e02016-01-14 09:24:09 -080055 int pixelCnt = w * h;
56 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
57
58 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -080059 uint32_t expectedColor0 = 0;
Mike Kleine72e7732018-06-14 14:41:22 -040060 uint8_t* expectedBytes0 = reinterpret_cast<uint8_t*>(&expectedColor0);
bsalomone5286e02016-01-14 09:24:09 -080061 expectedBytes0[0] = GrColorUnpackR(color0);
62 expectedBytes0[1] = GrColorUnpackG(color0);
63 expectedBytes0[2] = GrColorUnpackB(color0);
64 expectedBytes0[3] = GrColorUnpackA(color0);
Robert Phillipsd46697a2017-01-25 12:10:37 -050065 for (int i = 0; i < rtc->width() * rtc->height(); ++i) {
bsalomone5286e02016-01-14 09:24:09 -080066 expectedPixels.get()[i] = expectedColor0;
67 }
68
69 // Clear the the top to a different color.
70 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
71 SkIRect rect = SkIRect::MakeWH(w, h/2);
Brian Osman9a9baae2018-11-05 15:06:26 -050072 rtc->clear(&rect, SkPMColor4f::FromBytes_RGBA(color1),
73 GrRenderTargetContext::CanClearFullscreen::kNo);
bsalomone5286e02016-01-14 09:24:09 -080074
benjaminwagner2a641ee2016-01-15 06:21:18 -080075 uint32_t expectedColor1 = 0;
Mike Kleine72e7732018-06-14 14:41:22 -040076 uint8_t* expectedBytes1 = reinterpret_cast<uint8_t*>(&expectedColor1);
bsalomone5286e02016-01-14 09:24:09 -080077 expectedBytes1[0] = GrColorUnpackR(color1);
78 expectedBytes1[1] = GrColorUnpackG(color1);
79 expectedBytes1[2] = GrColorUnpackB(color1);
80 expectedBytes1[3] = GrColorUnpackA(color1);
81
82 for (int y = 0; y < h/2; ++y) {
83 for (int x = 0; x < w; ++x) {
84 expectedPixels.get()[y * h + x] = expectedColor1;
85 }
86 }
87
Robert Phillips26c90e02017-03-14 14:39:29 -040088 test_read_pixels(reporter, rtc, expectedPixels.get(), "RectangleTexture-clear");
bsalomone5286e02016-01-14 09:24:09 -080089 }
90}
91
Greg Daniel46cfbc62019-06-07 11:43:30 -040092static void test_copy_to_surface(skiatest::Reporter* reporter,
93 GrContext* context,
94 GrSurfaceContext* dstContext,
95 const char* testName) {
96
97 int pixelCnt = dstContext->width() * dstContext->height();
98 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
99 for (int y = 0; y < dstContext->width(); ++y) {
100 for (int x = 0; x < dstContext->height(); ++x) {
101 pixels.get()[y * dstContext->width() + x] =
102 SkColorToPremulGrColor(SkColorSetARGB(2*y, y, x, x * y));
103 }
104 }
105
106 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
107 auto origin = dstContext->asSurfaceProxy()->origin();
108 auto src = sk_gpu_test::MakeTextureProxyFromData(
Brian Salomone7499c72019-06-24 12:12:36 -0400109 context, renderable, dstContext->width(), dstContext->height(),
110 kRGBA_8888_SkColorType, kPremul_SkAlphaType, origin, pixels.get(), 0);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400111 // If this assert ever fails we can add a fallback to do copy as draw, but until then we can
112 // be more restrictive.
113 SkAssertResult(dstContext->testCopy(src.get()));
114 test_read_pixels(reporter, dstContext, pixels.get(), testName);
115 }
116}
117
bsalomon758586c2016-04-06 14:02:39 -0700118DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700119 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500120 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
bsalomon8b7451a2016-05-11 06:33:06 -0700121 sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
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
125 GrColor pixels[kWidth * kHeight];
126 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 }
131
Greg Daniel7ef28f32017-04-20 16:41:55 +0000132 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
133 bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin;
134
bsalomone5286e02016-01-14 09:24:09 -0800135 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
136 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
137 pixels);
138
139 if (!rectTexID) {
140 return;
141 }
142
143 // Let GrContext know that we messed with the GL context directly.
144 context->resetContext();
145
146 // Wrap the rectangle texture ID in a GrTexture
147 GrGLTextureInfo rectangleInfo;
148 rectangleInfo.fID = rectTexID;
149 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
Greg Danielfa55f2e2019-06-13 15:21:38 -0400150 rectangleInfo.fFormat = GR_GL_RGBA8;
bsalomone5286e02016-01-14 09:24:09 -0800151
Greg Daniel108bb232018-07-03 16:18:29 -0400152 GrBackendTexture rectangleTex(kWidth, kHeight, GrMipMapped::kNo, rectangleInfo);
bsalomone5286e02016-01-14 09:24:09 -0800153
154 GrColor refPixels[kWidth * kHeight];
bsalomone5286e02016-01-14 09:24:09 -0800155 for (int y = 0; y < kHeight; ++y) {
156 for (int x = 0; x < kWidth; ++x) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000157 int y0 = useBLOrigin ? kHeight - y - 1 : y;
bsalomone5286e02016-01-14 09:24:09 -0800158 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
159 }
160 }
161
Brian Salomonc67c31c2018-12-06 10:00:03 -0500162 sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400163 rectangleTex, GrColorType::kRGBA_8888, origin,
164 kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500165
Robert Phillips26caf892017-01-27 10:58:31 -0500166 if (!rectProxy) {
167 ERRORF(reporter, "Error creating proxy for rectangle texture.");
168 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
169 continue;
bsalomone5286e02016-01-14 09:24:09 -0800170 }
171
Greg Daniel09c94002018-06-08 22:11:51 +0000172 SkASSERT(rectProxy->mipMapped() == GrMipMapped::kNo);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400173 SkASSERT(rectProxy->peekTexture()->texturePriv().mipMapped() == GrMipMapped::kNo);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400174
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400175 SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle);
176 SkASSERT(rectProxy->peekTexture()->texturePriv().textureType() ==
Brian Salomon7226c232018-07-30 13:13:17 -0400177 GrTextureType::kRectangle);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400178 SkASSERT(rectProxy->hasRestrictedSampling());
179 SkASSERT(rectProxy->peekTexture()->texturePriv().hasRestrictedSampling());
Robert Phillipsabf7b762018-03-21 12:13:37 -0400180
Brian Salomond6287472019-06-24 15:50:07 -0400181 test_basic_draw_as_src(reporter, context, rectProxy, GrColorType::kRGBA_8888, refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800182
Robert Phillips3500b772017-01-27 10:11:42 -0500183 // Test copy to both a texture and RT
Brian Salomond6287472019-06-24 15:50:07 -0400184 test_copy_from_surface(reporter, context, rectProxy.get(), GrColorType::kRGBA_8888,
185 refPixels, "RectangleTexture-copy-from");
Brian Salomon739c5bf2016-11-07 09:53:44 -0500186
Brian Salomonbf6b9792019-08-21 09:38:10 -0400187 auto rectContext = context->priv().makeWrappedSurfaceContext(
Brian Salomond6287472019-06-24 15:50:07 -0400188 std::move(rectProxy), GrColorType::kRGBA_8888, kPremul_SkAlphaType);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500189 SkASSERT(rectContext);
bsalomone5286e02016-01-14 09:24:09 -0800190
Robert Phillips26c90e02017-03-14 14:39:29 -0400191 test_read_pixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read");
bsalomone5286e02016-01-14 09:24:09 -0800192
Robert Phillips69893702019-02-22 11:16:30 -0500193 test_copy_to_surface(reporter, context, rectContext.get(), "RectangleTexture-copy-to");
bsalomone5286e02016-01-14 09:24:09 -0800194
Robert Phillips26c90e02017-03-14 14:39:29 -0400195 test_write_pixels(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
199 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
200 }
201}