bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 1 | /* |
| 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" |
| 9 | #if SK_SUPPORT_GPU |
| 10 | #include "GrContext.h" |
| 11 | #include "GrDrawContext.h" |
| 12 | #include "gl/GrGLGpu.h" |
| 13 | #include "gl/GrGLUtil.h" |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 14 | #include "gl/GLTestContext.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 15 | |
| 16 | static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context, |
| 17 | GrTexture* rectangleTexture, uint32_t expectedPixelValues[]) { |
| 18 | int pixelCnt = rectangleTexture->width() * rectangleTexture->height(); |
| 19 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 20 | memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt); |
| 21 | bool read = rectangleTexture->readPixels(0, 0, rectangleTexture->width(), |
| 22 | rectangleTexture->height(), kRGBA_8888_GrPixelConfig, |
| 23 | pixels.get()); |
| 24 | if (!read) { |
| 25 | ERRORF(reporter, "Error reading rectangle texture."); |
| 26 | } |
| 27 | for (int i = 0; i < pixelCnt; ++i) { |
| 28 | if (pixels.get()[i] != expectedPixelValues[i]) { |
| 29 | ERRORF(reporter, "Error, rectangle texture pixel value %d should be 0x%08x," |
| 30 | " got 0x%08x.", i, expectedPixelValues[i], pixels.get()[i]); |
| 31 | break; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context, |
| 37 | GrTexture* rectangleTexture) { |
| 38 | int pixelCnt = rectangleTexture->width() * rectangleTexture->height(); |
| 39 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 40 | for (int y = 0; y < rectangleTexture->width(); ++y) { |
| 41 | for (int x = 0; x < rectangleTexture->height(); ++x) { |
| 42 | pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(x, y, x + y, x * y); |
| 43 | } |
| 44 | } |
| 45 | bool write = rectangleTexture->writePixels(0, 0, rectangleTexture->width(), |
| 46 | rectangleTexture->height(), kRGBA_8888_GrPixelConfig, |
| 47 | pixels.get()); |
| 48 | if (!write) { |
| 49 | ERRORF(reporter, "Error writing to rectangle texture."); |
| 50 | } |
| 51 | test_read_pixels(reporter, context, rectangleTexture, pixels.get()); |
| 52 | } |
| 53 | |
| 54 | static void test_copy_surface_src(skiatest::Reporter* reporter, GrContext* context, |
| 55 | GrTexture* rectangleTexture, uint32_t expectedPixelValues[]) { |
| 56 | GrSurfaceDesc copyDstDesc; |
| 57 | copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 58 | copyDstDesc.fWidth = rectangleTexture->width(); |
| 59 | copyDstDesc.fHeight = rectangleTexture->height(); |
| 60 | copyDstDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 61 | SkAutoTUnref<GrTexture> dst(context->textureProvider()->createTexture( |
| 62 | copyDstDesc, SkBudgeted::kYes)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 63 | context->copySurface(dst, rectangleTexture); |
| 64 | test_read_pixels(reporter, context, dst, expectedPixelValues); |
| 65 | } |
| 66 | |
| 67 | static void test_copy_surface_dst(skiatest::Reporter* reporter, GrContext* context, |
| 68 | GrTexture* rectangleTexture) { |
| 69 | int pixelCnt = rectangleTexture->width() * rectangleTexture->height(); |
| 70 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 71 | for (int y = 0; y < rectangleTexture->width(); ++y) { |
| 72 | for (int x = 0; x < rectangleTexture->height(); ++x) { |
| 73 | pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(y, x, x * y, x *+ y); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | GrSurfaceDesc copySrcDesc; |
| 78 | copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 79 | copySrcDesc.fWidth = rectangleTexture->width(); |
| 80 | copySrcDesc.fHeight = rectangleTexture->height(); |
| 81 | copySrcDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 82 | SkAutoTUnref<GrTexture> src(context->textureProvider()->createTexture( |
| 83 | copySrcDesc, SkBudgeted::kYes, pixels.get(), 0)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 84 | |
| 85 | context->copySurface(rectangleTexture, src); |
| 86 | test_read_pixels(reporter, context, rectangleTexture, pixels.get()); |
| 87 | } |
| 88 | |
| 89 | static void test_clear(skiatest::Reporter* reporter, GrContext* context, |
| 90 | GrTexture* rectangleTexture) { |
| 91 | if (rectangleTexture->asRenderTarget()) { |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 92 | sk_sp<GrDrawContext> dc( |
robertphillips | 6738c70 | 2016-07-27 12:13:51 -0700 | [diff] [blame^] | 93 | context->makeDrawContext(sk_ref_sp(rectangleTexture->asRenderTarget()), |
| 94 | nullptr)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 95 | if (!dc) { |
| 96 | ERRORF(reporter, "Could not get GrDrawContext for rectangle texture."); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | // Clear the whole thing. |
| 101 | GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD); |
| 102 | dc->clear(nullptr, color0, false); |
| 103 | |
| 104 | int w = rectangleTexture->width(); |
| 105 | int h = rectangleTexture->height(); |
| 106 | int pixelCnt = w * h; |
| 107 | SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt); |
| 108 | |
| 109 | // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different. |
benjaminwagner | 2a641ee | 2016-01-15 06:21:18 -0800 | [diff] [blame] | 110 | uint32_t expectedColor0 = 0; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 111 | uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0); |
| 112 | expectedBytes0[0] = GrColorUnpackR(color0); |
| 113 | expectedBytes0[1] = GrColorUnpackG(color0); |
| 114 | expectedBytes0[2] = GrColorUnpackB(color0); |
| 115 | expectedBytes0[3] = GrColorUnpackA(color0); |
| 116 | for (int i = 0; i < rectangleTexture->width() * rectangleTexture->height(); ++i) { |
| 117 | expectedPixels.get()[i] = expectedColor0; |
| 118 | } |
| 119 | |
| 120 | // Clear the the top to a different color. |
| 121 | GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4); |
| 122 | SkIRect rect = SkIRect::MakeWH(w, h/2); |
| 123 | dc->clear(&rect, color1, false); |
| 124 | |
benjaminwagner | 2a641ee | 2016-01-15 06:21:18 -0800 | [diff] [blame] | 125 | uint32_t expectedColor1 = 0; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 126 | uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1); |
| 127 | expectedBytes1[0] = GrColorUnpackR(color1); |
| 128 | expectedBytes1[1] = GrColorUnpackG(color1); |
| 129 | expectedBytes1[2] = GrColorUnpackB(color1); |
| 130 | expectedBytes1[3] = GrColorUnpackA(color1); |
| 131 | |
| 132 | for (int y = 0; y < h/2; ++y) { |
| 133 | for (int x = 0; x < w; ++x) { |
| 134 | expectedPixels.get()[y * h + x] = expectedColor1; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | test_read_pixels(reporter, context, rectangleTexture, expectedPixels.get()); |
| 139 | } |
| 140 | } |
| 141 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 142 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 143 | GrContext* context = ctxInfo.grContext(); |
| 144 | sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 145 | static const int kWidth = 13; |
| 146 | static const int kHeight = 13; |
| 147 | |
| 148 | GrColor pixels[kWidth * kHeight]; |
| 149 | for (int y = 0; y < kHeight; ++y) { |
| 150 | for (int x = 0; x < kWidth; ++x) { |
| 151 | pixels[y * kWidth + x] = y * kWidth + x; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | for (int origin = 0; origin < 2; ++origin) { |
| 156 | GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA, |
| 157 | GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, |
| 158 | pixels); |
| 159 | |
| 160 | if (!rectTexID) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | // Let GrContext know that we messed with the GL context directly. |
| 165 | context->resetContext(); |
| 166 | |
| 167 | // Wrap the rectangle texture ID in a GrTexture |
| 168 | GrGLTextureInfo rectangleInfo; |
| 169 | rectangleInfo.fID = rectTexID; |
| 170 | rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE; |
| 171 | |
| 172 | GrBackendTextureDesc rectangleDesc; |
| 173 | rectangleDesc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 174 | rectangleDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 175 | rectangleDesc.fWidth = kWidth; |
| 176 | rectangleDesc.fHeight = kHeight; |
| 177 | rectangleDesc.fOrigin = origin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
| 178 | rectangleDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&rectangleInfo); |
| 179 | |
| 180 | GrColor refPixels[kWidth * kHeight]; |
| 181 | bool flipRef = rectangleDesc.fOrigin == kBottomLeft_GrSurfaceOrigin; |
| 182 | for (int y = 0; y < kHeight; ++y) { |
| 183 | for (int x = 0; x < kWidth; ++x) { |
| 184 | int y0 = flipRef ? kHeight - y - 1 : y; |
| 185 | refPixels[y * kWidth + x] = pixels[y0 * kWidth + x]; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | SkAutoTUnref<GrTexture> rectangleTexture( |
| 190 | context->textureProvider()->wrapBackendTexture(rectangleDesc)); |
| 191 | if (!rectangleTexture) { |
| 192 | ERRORF(reporter, "Error wrapping rectangle texture in GrTexture."); |
| 193 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 194 | continue; |
| 195 | } |
| 196 | |
| 197 | test_read_pixels(reporter, context, rectangleTexture, refPixels); |
| 198 | |
| 199 | test_copy_surface_src(reporter, context, rectangleTexture, refPixels); |
| 200 | |
| 201 | test_copy_surface_dst(reporter, context, rectangleTexture); |
| 202 | |
| 203 | test_write_pixels(reporter, context, rectangleTexture); |
| 204 | |
| 205 | test_clear(reporter, context, rectangleTexture); |
| 206 | |
| 207 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | #endif |