aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/ImageTest.cpp
diff options
context:
space:
mode:
authorGravatar Eric Karl <ericrk@chromium.org>2017-10-12 12:44:50 -0700
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-10-12 20:05:31 +0000
commit914a36b248ffb538874483d86759254838866dd7 (patch)
treeea6954b9b3fc41bf87a3be1c30d5fd447656b28a /tests/ImageTest.cpp
parent708ec81d7a9bba12cd7e574b5c5ae80b2ad77919 (diff)
MakeBackendTextureFromSkImage
Creates a static function on SkImage which converts the SkImage to a GrBackendTexture. The texture is unowned by Skia, and must be deleted by the caller. Allows for a no-copy / no-conversion fast path if the provided image is unowned (unique()) and texture backed. Change-Id: I8a48f9cc39de792725cd72057d98cd1c4594daab Reviewed-on: https://skia-review.googlesource.com/52440 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Eric Karl <ericrk@chromium.org>
Diffstat (limited to 'tests/ImageTest.cpp')
-rw-r--r--tests/ImageTest.cpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/tests/ImageTest.cpp b/tests/ImageTest.cpp
index a35d56caf3..8eecbbd0a5 100644
--- a/tests/ImageTest.cpp
+++ b/tests/ImageTest.cpp
@@ -1132,6 +1132,94 @@ DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredTextureImage, reporter, ctxInfo) {
context->flush();
}
}
+
+static uint32_t GetIdForBackendObject(GrContext* ctx, GrBackendObject object) {
+ if (!object) {
+ return 0;
+ }
+
+ if (ctx->contextPriv().getBackend() != kOpenGL_GrBackend) {
+ return 0;
+ }
+
+ return reinterpret_cast<const GrGLTextureInfo*>(object)->fID;
+}
+
+static uint32_t GetIdForBackendTexture(GrBackendTexture texture) {
+ if (!texture.isValid()) {
+ return 0;
+ }
+
+ if (texture.backend() != kOpenGL_GrBackend) {
+ return 0;
+ }
+
+ return texture.getGLTextureInfo()->fID;
+}
+
+DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(makeBackendTexture, reporter, ctxInfo) {
+ GrContext* context = ctxInfo.grContext();
+ sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
+ sk_sp<GrContextThreadSafeProxy> proxy = context->threadSafeProxy();
+
+ GrContextFactory otherFactory;
+ ContextInfo otherContextInfo = otherFactory.getContextInfo(ctxInfo.type());
+
+ testContext->makeCurrent();
+ REPORTER_ASSERT(reporter, proxy);
+ auto createLarge = [context] {
+ return create_image_large(context->caps()->maxTextureSize());
+ };
+ struct {
+ std::function<sk_sp<SkImage> ()> fImageFactory;
+ bool fExpectation;
+ bool fCanTakeDirectly;
+ } testCases[] = {
+ { create_image, true, false },
+ { create_codec_image, true, false },
+ { create_data_image, true, false },
+ { create_picture_image, true, false },
+ { [context] { return create_gpu_image(context); }, true, true },
+ // Create a texture image in a another GrContext.
+ { [testContext, otherContextInfo] {
+ otherContextInfo.testContext()->makeCurrent();
+ sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.grContext());
+ testContext->makeCurrent();
+ return otherContextImage;
+ }, false, false },
+ // Create an image that is too large to be texture backed.
+ { createLarge, false, false }
+ };
+
+ for (auto testCase : testCases) {
+ sk_sp<SkImage> image(testCase.fImageFactory());
+ if (!image) {
+ ERRORF(reporter, "Failed to create image!");
+ continue;
+ }
+
+ uint32_t originalID = GetIdForBackendObject(context, image->getTextureHandle(true, nullptr));
+ GrBackendTexture texture;
+ SkImage::BackendTextureReleaseProc proc;
+ bool result =
+ SkImage::MakeBackendTextureFromSkImage(context, std::move(image), &texture, &proc);
+ if (result != testCase.fExpectation) {
+ static const char *const kFS[] = { "fail", "succeed" };
+ ERRORF(reporter, "This image was expected to %s but did not.",
+ kFS[testCase.fExpectation]);
+ }
+
+ bool tookDirectly = result && originalID == GetIdForBackendTexture(texture);
+ if (testCase.fCanTakeDirectly != tookDirectly) {
+ static const char *const kExpectedState[] = { "not expected", "expected" };
+ ERRORF(reporter, "This backend texture was %s to be taken directly.",
+ kExpectedState[testCase.fCanTakeDirectly]);
+ }
+
+ testContext->makeCurrent();
+ context->flush();
+ }
+}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////