blob: 2def782f3822f94b647f1d7e6196aaf44a34b684 [file] [log] [blame]
ajuma95243eb2016-08-24 08:19:02 -07001/*
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"
Robert Phillipse2f7d182016-12-15 09:23:05 -050012#include "GrContextPriv.h"
ajuma95243eb2016-08-24 08:19:02 -070013#include "GrGpu.h"
14#include "GrTextureStripAtlas.h"
15#include "GrTypes.h"
ajuma95243eb2016-08-24 08:19:02 -070016
17// This tests that GrTextureStripAtlas flushes pending IO on the texture it acquires.
18DEF_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;
ajuma95243eb2016-08-24 08:19:02 -070024
Robert Phillipse2f7d182016-12-15 09:23:05 -050025 sk_sp<GrSurfaceProxy> srcProxy;
ajuma95243eb2016-08-24 08:19:02 -070026
Robert Phillipse2f7d182016-12-15 09:23:05 -050027 {
28 SkAutoTMalloc<uint32_t> pixels(desc.fWidth * desc.fHeight);
29 memset(pixels.get(), 0xFF, sizeof(uint32_t) * desc.fWidth * desc.fHeight);
ajuma95243eb2016-08-24 08:19:02 -070030
Robert Phillipse2f7d182016-12-15 09:23:05 -050031 srcProxy = GrSurfaceProxy::MakeDeferred(*context->caps(), context->textureProvider(),
32 desc, SkBudgeted::kYes,
33 pixels.get(), 0);
34 }
35
36 // Add a pending read to the src texture, and then make it available for reuse.
37 sk_sp<GrSurfaceProxy> targetProxy;
38 GrSurface* srcSurface;
39
40 {
41 GrSurfaceDesc targetDesc = desc;
42 targetDesc.fFlags = kRenderTarget_GrSurfaceFlag;
43
44 // We can't use GrSurfaceProxy::Copy bc we may be changing the dst proxy type
45 sk_sp<GrSurfaceContext> dstContext(context->contextPriv().makeDeferredSurfaceContext(
46 targetDesc,
47 SkBackingFit::kExact,
48 SkBudgeted::kYes));
49 REPORTER_ASSERT(reporter, dstContext);
50
51 if (!dstContext->copy(srcProxy.get())) {
52 return;
53 }
54
55 targetProxy = sk_ref_sp(dstContext->asDeferredSurface());
56
57 srcSurface = srcProxy->instantiate(context->textureProvider());
58 srcProxy.reset();
59 }
ajuma95243eb2016-08-24 08:19:02 -070060
61 // Create an atlas with parameters that allow it to reuse the texture.
Robert Phillipse2f7d182016-12-15 09:23:05 -050062 GrTextureStripAtlas* atlas;
63
64 {
65 GrTextureStripAtlas::Desc atlasDesc;
66 atlasDesc.fContext = context;
67 atlasDesc.fConfig = desc.fConfig;
68 atlasDesc.fWidth = desc.fWidth;
69 atlasDesc.fHeight = desc.fHeight;
70 atlasDesc.fRowHeight = 1;
71 atlas = GrTextureStripAtlas::GetAtlas(atlasDesc);
72 }
ajuma95243eb2016-08-24 08:19:02 -070073
74 // Write to the atlas' texture.
Robert Phillipse2f7d182016-12-15 09:23:05 -050075 int lockedRow;
76
77 {
78 SkImageInfo info = SkImageInfo::MakeN32(desc.fWidth, desc.fHeight, kPremul_SkAlphaType);
79 size_t rowBytes = desc.fWidth * GrBytesPerPixel(desc.fConfig);
80 SkBitmap bitmap;
81 bitmap.allocPixels(info, rowBytes);
82 memset(bitmap.getPixels(), 1, rowBytes * desc.fHeight);
83 lockedRow = atlas->lockRow(bitmap);
84 }
ajuma95243eb2016-08-24 08:19:02 -070085
86 // The atlas' use of its texture shouldn't change which pixels got copied to the target.
Robert Phillipse2f7d182016-12-15 09:23:05 -050087 {
88 SkAutoTMalloc<uint8_t> actualPixels(sizeof(uint32_t) * desc.fWidth * desc.fHeight);
89
90 // TODO: move readPixels to GrSurfaceProxy?
91 GrSurface* surf = targetProxy->instantiate(context->textureProvider());
92
93 bool success = surf->readPixels(0, 0, desc.fWidth, desc.fHeight,
94 kRGBA_8888_GrPixelConfig, actualPixels.get());
95 REPORTER_ASSERT(reporter, success);
96
97 bool good = true;
98
99 const uint8_t* bytes = actualPixels.get();
100 for (size_t i = 0; i < sizeof(uint32_t) * desc.fWidth * desc.fHeight; ++i, ++bytes) {
101 if (0xFF != *bytes) {
102 good = false;
103 break;
104 }
105 }
106
107 REPORTER_ASSERT(reporter, good);
108 }
109
110 if (!context->caps()->preferVRAMUseOverFlushes()) {
111 // This is kindof dodgy since we released it!
112 REPORTER_ASSERT(reporter, srcSurface == atlas->getTexture());
113 }
114
115 atlas->unlockRow(lockedRow);
ajuma95243eb2016-08-24 08:19:02 -0700116}
117
118#endif