blob: 08cedd0be39733b19e23ec9ff67908ff24c2c217 [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
bsalomone5286e02016-01-14 09:24:09 -080011#if SK_SUPPORT_GPU
Brian Salomonc65aec92017-03-09 09:03:58 -050012#include "GrClip.h"
bsalomone5286e02016-01-14 09:24:09 -080013#include "GrContext.h"
robertphillips4fd74ae2016-08-03 14:26:53 -070014#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050015#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040016#include "GrRenderTargetContext.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000017#include "GrTest.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050018#include "gl/GLTestContext.h"
bsalomone5286e02016-01-14 09:24:09 -080019#include "gl/GrGLGpu.h"
20#include "gl/GrGLUtil.h"
bsalomone5286e02016-01-14 09:24:09 -080021
Brian Salomon739c5bf2016-11-07 09:53:44 -050022// skbug.com/5932
Robert Phillipsd46697a2017-01-25 12:10:37 -050023static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context,
Greg Daniel7ef28f32017-04-20 16:41:55 +000024 sk_sp<GrTextureProxy> rectProxy, uint32_t expectedPixelValues[]) {
Robert Phillips0c4b7b12018-03-06 08:20:37 -050025 sk_sp<GrRenderTargetContext> rtContext(context->contextPriv().makeDeferredRenderTargetContext(
26 SkBackingFit::kExact, rectProxy->width(),
Robert Phillipsdd3b3f42017-04-24 10:57:28 -040027 rectProxy->height(), rectProxy->config(),
28 nullptr));
Brian Salomon2bbdcc42017-09-07 12:36:34 -040029 for (auto filter : {GrSamplerState::Filter::kNearest,
30 GrSamplerState::Filter::kBilerp,
31 GrSamplerState::Filter::kMipMap}) {
Chris Dalton344e9032017-12-11 15:42:09 -070032 rtContext->clear(nullptr, 0xDDCCBBAA, GrRenderTargetContext::CanClearFullscreen::kYes);
Brian Osman2240be92017-10-18 13:15:13 -040033 auto fp = GrSimpleTextureEffect::Make(rectProxy, SkMatrix::I(), filter);
Brian Salomon739c5bf2016-11-07 09:53:44 -050034 GrPaint paint;
35 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
36 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomon82f44312017-01-11 13:42:54 -050037 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Robert Phillips26c90e02017-03-14 14:39:29 -040038 test_read_pixels(reporter, rtContext.get(), expectedPixelValues,
Robert Phillips3500b772017-01-27 10:11:42 -050039 "RectangleTexture-basic-draw");
Brian Salomon739c5bf2016-11-07 09:53:44 -050040 }
41}
42
Robert Phillips26c90e02017-03-14 14:39:29 -040043static void test_clear(skiatest::Reporter* reporter, GrSurfaceContext* rectContext) {
Robert Phillipsd46697a2017-01-25 12:10:37 -050044 if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) {
bsalomone5286e02016-01-14 09:24:09 -080045 // Clear the whole thing.
46 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
Chris Dalton344e9032017-12-11 15:42:09 -070047 rtc->clear(nullptr, color0, GrRenderTargetContext::CanClearFullscreen::kNo);
bsalomone5286e02016-01-14 09:24:09 -080048
Robert Phillipsd46697a2017-01-25 12:10:37 -050049 int w = rtc->width();
50 int h = rtc->height();
bsalomone5286e02016-01-14 09:24:09 -080051 int pixelCnt = w * h;
52 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
53
54 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -080055 uint32_t expectedColor0 = 0;
bsalomone5286e02016-01-14 09:24:09 -080056 uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0);
57 expectedBytes0[0] = GrColorUnpackR(color0);
58 expectedBytes0[1] = GrColorUnpackG(color0);
59 expectedBytes0[2] = GrColorUnpackB(color0);
60 expectedBytes0[3] = GrColorUnpackA(color0);
Robert Phillipsd46697a2017-01-25 12:10:37 -050061 for (int i = 0; i < rtc->width() * rtc->height(); ++i) {
bsalomone5286e02016-01-14 09:24:09 -080062 expectedPixels.get()[i] = expectedColor0;
63 }
64
65 // Clear the the top to a different color.
66 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
67 SkIRect rect = SkIRect::MakeWH(w, h/2);
Chris Dalton344e9032017-12-11 15:42:09 -070068 rtc->clear(&rect, color1, GrRenderTargetContext::CanClearFullscreen::kNo);
bsalomone5286e02016-01-14 09:24:09 -080069
benjaminwagner2a641ee2016-01-15 06:21:18 -080070 uint32_t expectedColor1 = 0;
bsalomone5286e02016-01-14 09:24:09 -080071 uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1);
72 expectedBytes1[0] = GrColorUnpackR(color1);
73 expectedBytes1[1] = GrColorUnpackG(color1);
74 expectedBytes1[2] = GrColorUnpackB(color1);
75 expectedBytes1[3] = GrColorUnpackA(color1);
76
77 for (int y = 0; y < h/2; ++y) {
78 for (int x = 0; x < w; ++x) {
79 expectedPixels.get()[y * h + x] = expectedColor1;
80 }
81 }
82
Robert Phillips26c90e02017-03-14 14:39:29 -040083 test_read_pixels(reporter, rtc, expectedPixels.get(), "RectangleTexture-clear");
bsalomone5286e02016-01-14 09:24:09 -080084 }
85}
86
bsalomon758586c2016-04-06 14:02:39 -070087DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070088 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -050089 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
bsalomon8b7451a2016-05-11 06:33:06 -070090 sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
bsalomone5286e02016-01-14 09:24:09 -080091 static const int kWidth = 13;
92 static const int kHeight = 13;
93
94 GrColor pixels[kWidth * kHeight];
95 for (int y = 0; y < kHeight; ++y) {
96 for (int x = 0; x < kWidth; ++x) {
97 pixels[y * kWidth + x] = y * kWidth + x;
98 }
99 }
100
Greg Daniel7ef28f32017-04-20 16:41:55 +0000101 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
102 bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin;
103
bsalomone5286e02016-01-14 09:24:09 -0800104 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
105 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
106 pixels);
107
108 if (!rectTexID) {
109 return;
110 }
111
112 // Let GrContext know that we messed with the GL context directly.
113 context->resetContext();
114
115 // Wrap the rectangle texture ID in a GrTexture
116 GrGLTextureInfo rectangleInfo;
117 rectangleInfo.fID = rectTexID;
118 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
119
Greg Daniel207282e2017-04-26 13:29:21 -0400120 GrBackendTexture rectangleTex(kWidth, kHeight, kRGBA_8888_GrPixelConfig, rectangleInfo);
bsalomone5286e02016-01-14 09:24:09 -0800121
122 GrColor refPixels[kWidth * kHeight];
bsalomone5286e02016-01-14 09:24:09 -0800123 for (int y = 0; y < kHeight; ++y) {
124 for (int x = 0; x < kWidth; ++x) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000125 int y0 = useBLOrigin ? kHeight - y - 1 : y;
bsalomone5286e02016-01-14 09:24:09 -0800126 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
127 }
128 }
129
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500130 sk_sp<GrTextureProxy> rectProxy = proxyProvider->createWrappedTextureProxy(
131 rectangleTex, origin);
132
Robert Phillips26caf892017-01-27 10:58:31 -0500133 if (!rectProxy) {
134 ERRORF(reporter, "Error creating proxy for rectangle texture.");
135 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
136 continue;
bsalomone5286e02016-01-14 09:24:09 -0800137 }
138
Robert Phillipsd46697a2017-01-25 12:10:37 -0500139 test_basic_draw_as_src(reporter, context, rectProxy, refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800140
Robert Phillips3500b772017-01-27 10:11:42 -0500141 // Test copy to both a texture and RT
142 test_copy_from_surface(reporter, context, rectProxy.get(), refPixels,
143 false, "RectangleTexture-copy-from");
Brian Salomon739c5bf2016-11-07 09:53:44 -0500144
Robert Phillipsd46697a2017-01-25 12:10:37 -0500145 sk_sp<GrSurfaceContext> rectContext = context->contextPriv().makeWrappedSurfaceContext(
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500146 std::move(rectProxy));
Robert Phillipsd46697a2017-01-25 12:10:37 -0500147 SkASSERT(rectContext);
bsalomone5286e02016-01-14 09:24:09 -0800148
Robert Phillips26c90e02017-03-14 14:39:29 -0400149 test_read_pixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read");
bsalomone5286e02016-01-14 09:24:09 -0800150
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500151 test_copy_to_surface(reporter, context->contextPriv().proxyProvider(),
Robert Phillips26c90e02017-03-14 14:39:29 -0400152 rectContext.get(), "RectangleTexture-copy-to");
bsalomone5286e02016-01-14 09:24:09 -0800153
Robert Phillips26c90e02017-03-14 14:39:29 -0400154 test_write_pixels(reporter, rectContext.get(), true, "RectangleTexture-write");
Robert Phillipsd46697a2017-01-25 12:10:37 -0500155
Robert Phillips26c90e02017-03-14 14:39:29 -0400156 test_clear(reporter, rectContext.get());
bsalomone5286e02016-01-14 09:24:09 -0800157
158 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
159 }
160}
161
162#endif