blob: 7ca72fc2008accb984380007dc075d13009819cb [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
8#include "Test.h"
Robert Phillips3500b772017-01-27 10:11:42 -05009#include "TestUtils.h"
10
Brian Salomonc65aec92017-03-09 09:03:58 -050011#include "GrClip.h"
bsalomone5286e02016-01-14 09:24:09 -080012#include "GrContext.h"
robertphillips4fd74ae2016-08-03 14:26:53 -070013#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050014#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040015#include "GrRenderTargetContext.h"
Robert Phillipsabf7b762018-03-21 12:13:37 -040016#include "GrSurfacePriv.h"
Greg Daniel09c94002018-06-08 22:11:51 +000017#include "GrTexturePriv.h"
Robert Phillipsabf7b762018-03-21 12:13:37 -040018#include "GrTextureProxyPriv.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050019#include "gl/GLTestContext.h"
bsalomone5286e02016-01-14 09:24:09 -080020#include "gl/GrGLGpu.h"
21#include "gl/GrGLUtil.h"
bsalomone5286e02016-01-14 09:24:09 -080022
Brian Salomon739c5bf2016-11-07 09:53:44 -050023// skbug.com/5932
Robert Phillipsd46697a2017-01-25 12:10:37 -050024static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context,
Greg Daniel7ef28f32017-04-20 16:41:55 +000025 sk_sp<GrTextureProxy> rectProxy, uint32_t expectedPixelValues[]) {
Greg Daniel4065d452018-11-16 15:43:41 -050026 GrBackendFormat format = rectProxy->backendFormat().makeTexture2D();
27 SkASSERT(format.isValid());
Robert Phillips0c4b7b12018-03-06 08:20:37 -050028 sk_sp<GrRenderTargetContext> rtContext(context->contextPriv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -050029 format,
Robert Phillips0c4b7b12018-03-06 08:20:37 -050030 SkBackingFit::kExact, rectProxy->width(),
Robert Phillipsdd3b3f42017-04-24 10:57:28 -040031 rectProxy->height(), rectProxy->config(),
32 nullptr));
Brian Salomon2bbdcc42017-09-07 12:36:34 -040033 for (auto filter : {GrSamplerState::Filter::kNearest,
34 GrSamplerState::Filter::kBilerp,
35 GrSamplerState::Filter::kMipMap}) {
Brian Osman9a9baae2018-11-05 15:06:26 -050036 rtContext->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xDDCCBBAA),
37 GrRenderTargetContext::CanClearFullscreen::kYes);
Brian Osman2240be92017-10-18 13:15:13 -040038 auto fp = GrSimpleTextureEffect::Make(rectProxy, SkMatrix::I(), filter);
Brian Salomon739c5bf2016-11-07 09:53:44 -050039 GrPaint paint;
40 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
41 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomon82f44312017-01-11 13:42:54 -050042 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Robert Phillips26c90e02017-03-14 14:39:29 -040043 test_read_pixels(reporter, rtContext.get(), expectedPixelValues,
Robert Phillips3500b772017-01-27 10:11:42 -050044 "RectangleTexture-basic-draw");
Brian Salomon739c5bf2016-11-07 09:53:44 -050045 }
46}
47
Robert Phillips26c90e02017-03-14 14:39:29 -040048static void test_clear(skiatest::Reporter* reporter, GrSurfaceContext* rectContext) {
Robert Phillipsd46697a2017-01-25 12:10:37 -050049 if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) {
bsalomone5286e02016-01-14 09:24:09 -080050 // Clear the whole thing.
51 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
Brian Osman9a9baae2018-11-05 15:06:26 -050052 rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(color0),
53 GrRenderTargetContext::CanClearFullscreen::kNo);
bsalomone5286e02016-01-14 09:24:09 -080054
Robert Phillipsd46697a2017-01-25 12:10:37 -050055 int w = rtc->width();
56 int h = rtc->height();
bsalomone5286e02016-01-14 09:24:09 -080057 int pixelCnt = w * h;
58 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
59
60 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -080061 uint32_t expectedColor0 = 0;
Mike Kleine72e7732018-06-14 14:41:22 -040062 uint8_t* expectedBytes0 = reinterpret_cast<uint8_t*>(&expectedColor0);
bsalomone5286e02016-01-14 09:24:09 -080063 expectedBytes0[0] = GrColorUnpackR(color0);
64 expectedBytes0[1] = GrColorUnpackG(color0);
65 expectedBytes0[2] = GrColorUnpackB(color0);
66 expectedBytes0[3] = GrColorUnpackA(color0);
Robert Phillipsd46697a2017-01-25 12:10:37 -050067 for (int i = 0; i < rtc->width() * rtc->height(); ++i) {
bsalomone5286e02016-01-14 09:24:09 -080068 expectedPixels.get()[i] = expectedColor0;
69 }
70
71 // Clear the the top to a different color.
72 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
73 SkIRect rect = SkIRect::MakeWH(w, h/2);
Brian Osman9a9baae2018-11-05 15:06:26 -050074 rtc->clear(&rect, SkPMColor4f::FromBytes_RGBA(color1),
75 GrRenderTargetContext::CanClearFullscreen::kNo);
bsalomone5286e02016-01-14 09:24:09 -080076
benjaminwagner2a641ee2016-01-15 06:21:18 -080077 uint32_t expectedColor1 = 0;
Mike Kleine72e7732018-06-14 14:41:22 -040078 uint8_t* expectedBytes1 = reinterpret_cast<uint8_t*>(&expectedColor1);
bsalomone5286e02016-01-14 09:24:09 -080079 expectedBytes1[0] = GrColorUnpackR(color1);
80 expectedBytes1[1] = GrColorUnpackG(color1);
81 expectedBytes1[2] = GrColorUnpackB(color1);
82 expectedBytes1[3] = GrColorUnpackA(color1);
83
84 for (int y = 0; y < h/2; ++y) {
85 for (int x = 0; x < w; ++x) {
86 expectedPixels.get()[y * h + x] = expectedColor1;
87 }
88 }
89
Robert Phillips26c90e02017-03-14 14:39:29 -040090 test_read_pixels(reporter, rtc, expectedPixels.get(), "RectangleTexture-clear");
bsalomone5286e02016-01-14 09:24:09 -080091 }
92}
93
bsalomon758586c2016-04-06 14:02:39 -070094DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070095 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -050096 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
bsalomon8b7451a2016-05-11 06:33:06 -070097 sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
bsalomone5286e02016-01-14 09:24:09 -080098 static const int kWidth = 13;
99 static const int kHeight = 13;
100
101 GrColor pixels[kWidth * kHeight];
102 for (int y = 0; y < kHeight; ++y) {
103 for (int x = 0; x < kWidth; ++x) {
104 pixels[y * kWidth + x] = y * kWidth + x;
105 }
106 }
107
Greg Daniel7ef28f32017-04-20 16:41:55 +0000108 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
109 bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin;
110
bsalomone5286e02016-01-14 09:24:09 -0800111 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
112 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
113 pixels);
114
115 if (!rectTexID) {
116 return;
117 }
118
119 // Let GrContext know that we messed with the GL context directly.
120 context->resetContext();
121
122 // Wrap the rectangle texture ID in a GrTexture
123 GrGLTextureInfo rectangleInfo;
124 rectangleInfo.fID = rectTexID;
125 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
126
Greg Daniel108bb232018-07-03 16:18:29 -0400127 GrBackendTexture rectangleTex(kWidth, kHeight, GrMipMapped::kNo, rectangleInfo);
128 rectangleTex.setPixelConfig(kRGBA_8888_GrPixelConfig);
bsalomone5286e02016-01-14 09:24:09 -0800129
130 GrColor refPixels[kWidth * kHeight];
bsalomone5286e02016-01-14 09:24:09 -0800131 for (int y = 0; y < kHeight; ++y) {
132 for (int x = 0; x < kWidth; ++x) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000133 int y0 = useBLOrigin ? kHeight - y - 1 : y;
bsalomone5286e02016-01-14 09:24:09 -0800134 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
135 }
136 }
137
Brian Salomonc67c31c2018-12-06 10:00:03 -0500138 sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500139 rectangleTex, origin, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500140
Robert Phillips26caf892017-01-27 10:58:31 -0500141 if (!rectProxy) {
142 ERRORF(reporter, "Error creating proxy for rectangle texture.");
143 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
144 continue;
bsalomone5286e02016-01-14 09:24:09 -0800145 }
146
Greg Daniel09c94002018-06-08 22:11:51 +0000147 SkASSERT(rectProxy->mipMapped() == GrMipMapped::kNo);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400148 SkASSERT(rectProxy->peekTexture()->texturePriv().mipMapped() == GrMipMapped::kNo);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400149
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400150 SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle);
151 SkASSERT(rectProxy->peekTexture()->texturePriv().textureType() ==
Brian Salomon7226c232018-07-30 13:13:17 -0400152 GrTextureType::kRectangle);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400153 SkASSERT(rectProxy->hasRestrictedSampling());
154 SkASSERT(rectProxy->peekTexture()->texturePriv().hasRestrictedSampling());
Robert Phillipsabf7b762018-03-21 12:13:37 -0400155
Robert Phillipsd46697a2017-01-25 12:10:37 -0500156 test_basic_draw_as_src(reporter, context, rectProxy, refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800157
Robert Phillips3500b772017-01-27 10:11:42 -0500158 // Test copy to both a texture and RT
159 test_copy_from_surface(reporter, context, rectProxy.get(), refPixels,
160 false, "RectangleTexture-copy-from");
Brian Salomon739c5bf2016-11-07 09:53:44 -0500161
Robert Phillipsd46697a2017-01-25 12:10:37 -0500162 sk_sp<GrSurfaceContext> rectContext = context->contextPriv().makeWrappedSurfaceContext(
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500163 std::move(rectProxy));
Robert Phillipsd46697a2017-01-25 12:10:37 -0500164 SkASSERT(rectContext);
bsalomone5286e02016-01-14 09:24:09 -0800165
Robert Phillips26c90e02017-03-14 14:39:29 -0400166 test_read_pixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read");
bsalomone5286e02016-01-14 09:24:09 -0800167
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500168 test_copy_to_surface(reporter, context->contextPriv().proxyProvider(),
Robert Phillips26c90e02017-03-14 14:39:29 -0400169 rectContext.get(), "RectangleTexture-copy-to");
bsalomone5286e02016-01-14 09:24:09 -0800170
Robert Phillips26c90e02017-03-14 14:39:29 -0400171 test_write_pixels(reporter, rectContext.get(), true, "RectangleTexture-write");
Robert Phillipsd46697a2017-01-25 12:10:37 -0500172
Robert Phillips26c90e02017-03-14 14:39:29 -0400173 test_clear(reporter, rectContext.get());
bsalomone5286e02016-01-14 09:24:09 -0800174
175 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
176 }
177}