blob: cdcd8d7ac62e15d04f39f55c8e8d9f84e5e20720 [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"
Brian Osman11052242016-10-27 14:47:55 -040015#include "GrRenderTargetContext.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000016#include "GrTest.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050017#include "gl/GLTestContext.h"
bsalomone5286e02016-01-14 09:24:09 -080018#include "gl/GrGLGpu.h"
19#include "gl/GrGLUtil.h"
bsalomone5286e02016-01-14 09:24:09 -080020
Brian Salomon739c5bf2016-11-07 09:53:44 -050021// skbug.com/5932
Robert Phillipsd46697a2017-01-25 12:10:37 -050022static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context,
Greg Daniel7ef28f32017-04-20 16:41:55 +000023 sk_sp<GrTextureProxy> rectProxy, uint32_t expectedPixelValues[]) {
Brian Salomon739c5bf2016-11-07 09:53:44 -050024 sk_sp<GrRenderTargetContext> rtContext(
Robert Phillipsdd3b3f42017-04-24 10:57:28 -040025 context->makeDeferredRenderTargetContext(SkBackingFit::kExact, rectProxy->width(),
26 rectProxy->height(), rectProxy->config(),
27 nullptr));
Brian Salomon514baff2016-11-17 15:17:07 -050028 for (auto filter : {GrSamplerParams::kNone_FilterMode,
29 GrSamplerParams::kBilerp_FilterMode,
30 GrSamplerParams::kMipMap_FilterMode}) {
Brian Salomon739c5bf2016-11-07 09:53:44 -050031 rtContext->clear(nullptr, 0xDDCCBBAA, true);
Robert Phillipsd46697a2017-01-25 12:10:37 -050032 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(
Robert Phillips296b1cc2017-03-15 10:42:12 -040033 context->resourceProvider(),
Greg Daniel7ef28f32017-04-20 16:41:55 +000034 rectProxy,
Robert Phillipsd46697a2017-01-25 12:10:37 -050035 nullptr,
36 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 Osman11052242016-10-27 14:47:55 -040050 rtc->clear(nullptr, color0, false);
bsalomone5286e02016-01-14 09:24:09 -080051
Robert Phillipsd46697a2017-01-25 12:10:37 -050052 int w = rtc->width();
53 int h = rtc->height();
bsalomone5286e02016-01-14 09:24:09 -080054 int pixelCnt = w * h;
55 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
56
57 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -080058 uint32_t expectedColor0 = 0;
bsalomone5286e02016-01-14 09:24:09 -080059 uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0);
60 expectedBytes0[0] = GrColorUnpackR(color0);
61 expectedBytes0[1] = GrColorUnpackG(color0);
62 expectedBytes0[2] = GrColorUnpackB(color0);
63 expectedBytes0[3] = GrColorUnpackA(color0);
Robert Phillipsd46697a2017-01-25 12:10:37 -050064 for (int i = 0; i < rtc->width() * rtc->height(); ++i) {
bsalomone5286e02016-01-14 09:24:09 -080065 expectedPixels.get()[i] = expectedColor0;
66 }
67
68 // Clear the the top to a different color.
69 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
70 SkIRect rect = SkIRect::MakeWH(w, h/2);
Brian Osman11052242016-10-27 14:47:55 -040071 rtc->clear(&rect, color1, false);
bsalomone5286e02016-01-14 09:24:09 -080072
benjaminwagner2a641ee2016-01-15 06:21:18 -080073 uint32_t expectedColor1 = 0;
bsalomone5286e02016-01-14 09:24:09 -080074 uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1);
75 expectedBytes1[0] = GrColorUnpackR(color1);
76 expectedBytes1[1] = GrColorUnpackG(color1);
77 expectedBytes1[2] = GrColorUnpackB(color1);
78 expectedBytes1[3] = GrColorUnpackA(color1);
79
80 for (int y = 0; y < h/2; ++y) {
81 for (int x = 0; x < w; ++x) {
82 expectedPixels.get()[y * h + x] = expectedColor1;
83 }
84 }
85
Robert Phillips26c90e02017-03-14 14:39:29 -040086 test_read_pixels(reporter, rtc, expectedPixels.get(), "RectangleTexture-clear");
bsalomone5286e02016-01-14 09:24:09 -080087 }
88}
89
bsalomon758586c2016-04-06 14:02:39 -070090DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070091 GrContext* context = ctxInfo.grContext();
92 sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
bsalomone5286e02016-01-14 09:24:09 -080093 static const int kWidth = 13;
94 static const int kHeight = 13;
95
96 GrColor pixels[kWidth * kHeight];
97 for (int y = 0; y < kHeight; ++y) {
98 for (int x = 0; x < kWidth; ++x) {
99 pixels[y * kWidth + x] = y * kWidth + x;
100 }
101 }
102
Greg Daniel7ef28f32017-04-20 16:41:55 +0000103 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
104 bool useBLOrigin = kBottomLeft_GrSurfaceOrigin == origin;
105
bsalomone5286e02016-01-14 09:24:09 -0800106 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
107 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
108 pixels);
109
110 if (!rectTexID) {
111 return;
112 }
113
114 // Let GrContext know that we messed with the GL context directly.
115 context->resetContext();
116
117 // Wrap the rectangle texture ID in a GrTexture
118 GrGLTextureInfo rectangleInfo;
119 rectangleInfo.fID = rectTexID;
120 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
121
Greg Daniel7ef28f32017-04-20 16:41:55 +0000122 GrBackendTexture rectangleTex(kWidth, kHeight, kRGBA_8888_GrPixelConfig, &rectangleInfo);
bsalomone5286e02016-01-14 09:24:09 -0800123
124 GrColor refPixels[kWidth * kHeight];
bsalomone5286e02016-01-14 09:24:09 -0800125 for (int y = 0; y < kHeight; ++y) {
126 for (int x = 0; x < kWidth; ++x) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000127 int y0 = useBLOrigin ? kHeight - y - 1 : y;
bsalomone5286e02016-01-14 09:24:09 -0800128 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
129 }
130 }
131
Greg Daniel7ef28f32017-04-20 16:41:55 +0000132 sk_sp<GrTextureProxy> rectProxy = GrSurfaceProxy::MakeWrappedBackend(context,
133 rectangleTex,
134 origin);
Robert Phillips26caf892017-01-27 10:58:31 -0500135 if (!rectProxy) {
136 ERRORF(reporter, "Error creating proxy for rectangle texture.");
137 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
138 continue;
bsalomone5286e02016-01-14 09:24:09 -0800139 }
140
Robert Phillipsd46697a2017-01-25 12:10:37 -0500141 test_basic_draw_as_src(reporter, context, rectProxy, refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800142
Robert Phillips3500b772017-01-27 10:11:42 -0500143 // Test copy to both a texture and RT
144 test_copy_from_surface(reporter, context, rectProxy.get(), refPixels,
145 false, "RectangleTexture-copy-from");
Brian Salomon739c5bf2016-11-07 09:53:44 -0500146
Robert Phillipsd46697a2017-01-25 12:10:37 -0500147 sk_sp<GrSurfaceContext> rectContext = context->contextPriv().makeWrappedSurfaceContext(
148 std::move(rectProxy), nullptr);
149 SkASSERT(rectContext);
bsalomone5286e02016-01-14 09:24:09 -0800150
Robert Phillips26c90e02017-03-14 14:39:29 -0400151 test_read_pixels(reporter, rectContext.get(), refPixels, "RectangleTexture-read");
bsalomone5286e02016-01-14 09:24:09 -0800152
Robert Phillips26c90e02017-03-14 14:39:29 -0400153 test_copy_to_surface(reporter, context->resourceProvider(),
154 rectContext.get(), "RectangleTexture-copy-to");
bsalomone5286e02016-01-14 09:24:09 -0800155
Robert Phillips26c90e02017-03-14 14:39:29 -0400156 test_write_pixels(reporter, rectContext.get(), true, "RectangleTexture-write");
Robert Phillipsd46697a2017-01-25 12:10:37 -0500157
Robert Phillips26c90e02017-03-14 14:39:29 -0400158 test_clear(reporter, rectContext.get());
bsalomone5286e02016-01-14 09:24:09 -0800159
160 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
161 }
162}
163
164#endif