blob: d55b882f7ee715bf893b64af879e7c90abd21c50 [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
12#include "GrContext.h"
robertphillips4fd74ae2016-08-03 14:26:53 -070013#include "GrContextPriv.h"
Brian Osman11052242016-10-27 14:47:55 -040014#include "GrRenderTargetContext.h"
bsalomone5286e02016-01-14 09:24:09 -080015#include "gl/GrGLGpu.h"
16#include "gl/GrGLUtil.h"
bsalomon273c0f52016-03-31 10:59:06 -070017#include "gl/GLTestContext.h"
bsalomone5286e02016-01-14 09:24:09 -080018
Brian Salomon739c5bf2016-11-07 09:53:44 -050019// skbug.com/5932
Robert Phillipsd46697a2017-01-25 12:10:37 -050020static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context,
21 sk_sp<GrSurfaceProxy> rectProxy, uint32_t expectedPixelValues[]) {
Brian Salomon739c5bf2016-11-07 09:53:44 -050022 sk_sp<GrRenderTargetContext> rtContext(
Robert Phillipsd46697a2017-01-25 12:10:37 -050023 context->makeRenderTargetContext(SkBackingFit::kExact, rectProxy->width(),
24 rectProxy->height(), rectProxy->config(),
Brian Salomon739c5bf2016-11-07 09:53:44 -050025 nullptr));
Brian Salomon514baff2016-11-17 15:17:07 -050026 for (auto filter : {GrSamplerParams::kNone_FilterMode,
27 GrSamplerParams::kBilerp_FilterMode,
28 GrSamplerParams::kMipMap_FilterMode}) {
Brian Salomon739c5bf2016-11-07 09:53:44 -050029 rtContext->clear(nullptr, 0xDDCCBBAA, true);
Robert Phillipsd46697a2017-01-25 12:10:37 -050030 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(
31 context,
32 sk_ref_sp(rectProxy->asTextureProxy()),
33 nullptr,
34 SkMatrix::I(), filter));
Brian Salomon739c5bf2016-11-07 09:53:44 -050035 GrPaint paint;
36 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
37 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomon82f44312017-01-11 13:42:54 -050038 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Robert Phillips3500b772017-01-27 10:11:42 -050039 test_read_pixels(reporter, context, rtContext.get(), expectedPixelValues,
40 "RectangleTexture-basic-draw");
Brian Salomon739c5bf2016-11-07 09:53:44 -050041 }
42}
43
bsalomone5286e02016-01-14 09:24:09 -080044static void test_clear(skiatest::Reporter* reporter, GrContext* context,
Robert Phillipsd46697a2017-01-25 12:10:37 -050045 GrSurfaceContext* rectContext) {
46 if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) {
bsalomone5286e02016-01-14 09:24:09 -080047 // Clear the whole thing.
48 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
Brian Osman11052242016-10-27 14:47:55 -040049 rtc->clear(nullptr, color0, false);
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;
bsalomone5286e02016-01-14 09:24:09 -080058 uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0);
59 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);
Brian Osman11052242016-10-27 14:47:55 -040070 rtc->clear(&rect, color1, false);
bsalomone5286e02016-01-14 09:24:09 -080071
benjaminwagner2a641ee2016-01-15 06:21:18 -080072 uint32_t expectedColor1 = 0;
bsalomone5286e02016-01-14 09:24:09 -080073 uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1);
74 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
Robert Phillips3500b772017-01-27 10:11:42 -050085 test_read_pixels(reporter, context, rtc, expectedPixels.get(), "RectangleTexture-clear");
bsalomone5286e02016-01-14 09:24:09 -080086 }
87}
88
bsalomon758586c2016-04-06 14:02:39 -070089DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070090 GrContext* context = ctxInfo.grContext();
91 sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
bsalomone5286e02016-01-14 09:24:09 -080092 static const int kWidth = 13;
93 static const int kHeight = 13;
94
95 GrColor pixels[kWidth * kHeight];
96 for (int y = 0; y < kHeight; ++y) {
97 for (int x = 0; x < kWidth; ++x) {
98 pixels[y * kWidth + x] = y * kWidth + x;
99 }
100 }
101
102 for (int origin = 0; origin < 2; ++origin) {
103 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
104 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
105 pixels);
106
107 if (!rectTexID) {
108 return;
109 }
110
111 // Let GrContext know that we messed with the GL context directly.
112 context->resetContext();
113
114 // Wrap the rectangle texture ID in a GrTexture
115 GrGLTextureInfo rectangleInfo;
116 rectangleInfo.fID = rectTexID;
117 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
118
119 GrBackendTextureDesc rectangleDesc;
120 rectangleDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
121 rectangleDesc.fConfig = kRGBA_8888_GrPixelConfig;
122 rectangleDesc.fWidth = kWidth;
123 rectangleDesc.fHeight = kHeight;
124 rectangleDesc.fOrigin = origin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
125 rectangleDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&rectangleInfo);
126
127 GrColor refPixels[kWidth * kHeight];
128 bool flipRef = rectangleDesc.fOrigin == kBottomLeft_GrSurfaceOrigin;
129 for (int y = 0; y < kHeight; ++y) {
130 for (int x = 0; x < kWidth; ++x) {
131 int y0 = flipRef ? kHeight - y - 1 : y;
132 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
133 }
134 }
135
Robert Phillipsd46697a2017-01-25 12:10:37 -0500136 sk_sp<GrSurfaceProxy> rectProxy;
137
138 {
139 sk_sp<GrTexture> rectangleTexture(
140 context->textureProvider()->wrapBackendTexture(rectangleDesc));
141 if (!rectangleTexture) {
142 ERRORF(reporter, "Error wrapping rectangle texture in GrTexture.");
143 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
144 continue;
145 }
146
147 rectProxy = GrSurfaceProxy::MakeWrapped(std::move(rectangleTexture));
148 if (!rectProxy) {
149 ERRORF(reporter, "Error creating proxy for rectangle texture.");
150 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
151 continue;
152 }
bsalomone5286e02016-01-14 09:24:09 -0800153 }
154
Robert Phillipsd46697a2017-01-25 12:10:37 -0500155 test_basic_draw_as_src(reporter, context, rectProxy, refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800156
Robert Phillips3500b772017-01-27 10:11:42 -0500157 // Test copy to both a texture and RT
158 test_copy_from_surface(reporter, context, rectProxy.get(), refPixels,
159 false, "RectangleTexture-copy-from");
Brian Salomon739c5bf2016-11-07 09:53:44 -0500160
Robert Phillipsd46697a2017-01-25 12:10:37 -0500161 sk_sp<GrSurfaceContext> rectContext = context->contextPriv().makeWrappedSurfaceContext(
162 std::move(rectProxy), nullptr);
163 SkASSERT(rectContext);
bsalomone5286e02016-01-14 09:24:09 -0800164
Robert Phillips3500b772017-01-27 10:11:42 -0500165 test_read_pixels(reporter, context, rectContext.get(), refPixels, "RectangleTexture-read");
bsalomone5286e02016-01-14 09:24:09 -0800166
Robert Phillips3500b772017-01-27 10:11:42 -0500167 test_copy_to_surface(reporter, context, rectContext.get(), "RectangleTexture-copy-to");
bsalomone5286e02016-01-14 09:24:09 -0800168
Robert Phillips3500b772017-01-27 10:11:42 -0500169 test_write_pixels(reporter, context, rectContext.get(), true, "RectangleTexture-write");
Robert Phillipsd46697a2017-01-25 12:10:37 -0500170
171 test_clear(reporter, context, rectContext.get());
bsalomone5286e02016-01-14 09:24:09 -0800172
173 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
174 }
175}
176
177#endif