ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 11 | #include "GrContext.h" |
| 12 | #include "GrGpu.h" |
| 13 | #include "GrTextureStripAtlas.h" |
| 14 | #include "GrTypes.h" |
| 15 | #include "SkGpuDevice.h" |
| 16 | |
| 17 | // This tests that GrTextureStripAtlas flushes pending IO on the texture it acquires. |
| 18 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTextureStripAtlasFlush, reporter, ctxInfo) { |
| 19 | GrContext* context = ctxInfo.grContext(); |
| 20 | GrSurfaceDesc desc; |
| 21 | desc.fWidth = 32; |
| 22 | desc.fHeight = 32; |
| 23 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 24 | GrTexture* texture = context->textureProvider()->createTexture(desc, SkBudgeted::kYes, |
| 25 | nullptr, 0); |
| 26 | |
| 27 | GrSurfaceDesc targetDesc = desc; |
| 28 | targetDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 29 | GrTexture* target = context->textureProvider()->createTexture(targetDesc, SkBudgeted::kYes, |
| 30 | nullptr, 0); |
| 31 | |
| 32 | SkAutoTMalloc<uint32_t> pixels(desc.fWidth * desc.fHeight); |
| 33 | memset(pixels.get(), 0xFF, sizeof(uint32_t) * desc.fWidth * desc.fHeight); |
| 34 | texture->writePixels(0, 0, desc.fWidth, desc.fHeight, kRGBA_8888_GrPixelConfig, pixels.get()); |
| 35 | |
| 36 | // Add a pending read to the texture, and then make it available for reuse. |
| 37 | context->copySurface(target, texture); |
| 38 | texture->unref(); |
| 39 | |
| 40 | // Create an atlas with parameters that allow it to reuse the texture. |
| 41 | GrTextureStripAtlas::Desc atlasDesc; |
| 42 | atlasDesc.fContext = context; |
| 43 | atlasDesc.fConfig = desc.fConfig; |
| 44 | atlasDesc.fWidth = desc.fWidth; |
| 45 | atlasDesc.fHeight = desc.fHeight; |
| 46 | atlasDesc.fRowHeight = 1; |
| 47 | GrTextureStripAtlas* atlas = GrTextureStripAtlas::GetAtlas(atlasDesc); |
| 48 | |
| 49 | // Write to the atlas' texture. |
| 50 | SkImageInfo info = SkImageInfo::MakeN32(desc.fWidth, desc.fHeight, kPremul_SkAlphaType); |
| 51 | size_t rowBytes = desc.fWidth * GrBytesPerPixel(desc.fConfig); |
| 52 | SkBitmap bitmap; |
| 53 | bitmap.allocPixels(info, rowBytes); |
| 54 | memset(bitmap.getPixels(), 1, rowBytes * desc.fHeight); |
ajuma | 8c99eab | 2016-08-24 12:09:12 -0700 | [diff] [blame] | 55 | int row = atlas->lockRow(bitmap); |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 56 | if (!context->caps()->preferVRAMUseOverFlushes()) |
| 57 | REPORTER_ASSERT(reporter, texture == atlas->getTexture()); |
| 58 | |
| 59 | // The atlas' use of its texture shouldn't change which pixels got copied to the target. |
| 60 | SkAutoTMalloc<uint32_t> actualPixels(desc.fWidth * desc.fHeight); |
| 61 | bool success = target->readPixels(0, 0, desc.fWidth, desc.fHeight, kRGBA_8888_GrPixelConfig, |
| 62 | actualPixels.get()); |
| 63 | REPORTER_ASSERT(reporter, success); |
| 64 | REPORTER_ASSERT(reporter, |
| 65 | !memcmp(pixels.get(), actualPixels.get(), |
| 66 | sizeof(uint32_t) * desc.fWidth * desc.fHeight)); |
| 67 | target->unref(); |
ajuma | 8c99eab | 2016-08-24 12:09:12 -0700 | [diff] [blame] | 68 | atlas->unlockRow(row); |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | #endif |