Home | History | Annotate | Download | only in tests
      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 // This is a GPU-backend specific test. It relies on static intializers to work
      9 
     10 #include "SkTypes.h"
     11 
     12 #if SK_SUPPORT_GPU && defined(SK_VULKAN)
     13 
     14 #include "GrContextPriv.h"
     15 #include "GrContextFactory.h"
     16 #include "GrRenderTarget.h"
     17 #include "GrTest.h"
     18 #include "GrTexture.h"
     19 
     20 #include "Test.h"
     21 #include "vk/GrVkCaps.h"
     22 #include "vk/GrVkGpu.h"
     23 #include "vk/GrVkMemory.h"
     24 #include "vk/GrVkTypes.h"
     25 
     26 using sk_gpu_test::GrContextFactory;
     27 
     28 const int kW = 1024;
     29 const int kH = 1024;
     30 const GrPixelConfig kPixelConfig = kRGBA_8888_GrPixelConfig;
     31 
     32 void wrap_tex_test(skiatest::Reporter* reporter, GrContext* context) {
     33 
     34     GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
     35 
     36     GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
     37                                                                            kPixelConfig, false,
     38                                                                            GrMipMapped::kNo);
     39     const GrVkImageInfo* imageInfo = origBackendTex.getVkImageInfo();
     40 
     41     sk_sp<GrTexture> tex = gpu->wrapBackendTexture(origBackendTex, kBorrow_GrWrapOwnership);
     42     REPORTER_ASSERT(reporter, tex);
     43 
     44     // image is null
     45     {
     46         GrVkImageInfo backendCopy = *imageInfo;
     47         backendCopy.fImage = VK_NULL_HANDLE;
     48         GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
     49         tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership);
     50         REPORTER_ASSERT(reporter, !tex);
     51         tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership);
     52         REPORTER_ASSERT(reporter, !tex);
     53     }
     54 
     55     // alloc is null
     56     {
     57         GrVkImageInfo backendCopy = *imageInfo;
     58         backendCopy.fAlloc = GrVkAlloc();
     59         GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
     60         tex = gpu->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership);
     61         REPORTER_ASSERT(reporter, !tex);
     62         tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership);
     63         REPORTER_ASSERT(reporter, !tex);
     64     }
     65 
     66     // check adopt creation
     67     {
     68         GrVkImageInfo backendCopy = *imageInfo;
     69         GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
     70         tex = gpu->wrapBackendTexture(backendTex, kAdopt_GrWrapOwnership);
     71 
     72         REPORTER_ASSERT(reporter, tex);
     73     }
     74 
     75     gpu->deleteTestingOnlyBackendTexture(&origBackendTex, true);
     76 }
     77 
     78 void wrap_rt_test(skiatest::Reporter* reporter, GrContext* context) {
     79     GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
     80 
     81     GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
     82                                                                            kPixelConfig, true,
     83                                                                            GrMipMapped::kNo);
     84     const GrVkImageInfo* imageInfo = origBackendTex.getVkImageInfo();
     85 
     86     GrBackendRenderTarget origBackendRT(kW, kH, 1, 0, *imageInfo);
     87 
     88     sk_sp<GrRenderTarget> rt = gpu->wrapBackendRenderTarget(origBackendRT);
     89     REPORTER_ASSERT(reporter, rt);
     90 
     91     // image is null
     92     {
     93         GrVkImageInfo backendCopy = *imageInfo;
     94         backendCopy.fImage = VK_NULL_HANDLE;
     95         GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
     96         rt = gpu->wrapBackendRenderTarget(backendRT);
     97         REPORTER_ASSERT(reporter, !rt);
     98     }
     99 
    100     // alloc is null
    101     {
    102         GrVkImageInfo backendCopy = *imageInfo;
    103         backendCopy.fAlloc = GrVkAlloc();
    104         // can wrap null alloc
    105         GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
    106         rt = gpu->wrapBackendRenderTarget(backendRT);
    107         REPORTER_ASSERT(reporter, rt);
    108     }
    109 
    110     // When we wrapBackendRenderTarget it is always borrowed, so we must make sure to free the
    111     // resource when we're done.
    112     gpu->deleteTestingOnlyBackendTexture(&origBackendTex);
    113 }
    114 
    115 void wrap_trt_test(skiatest::Reporter* reporter, GrContext* context) {
    116     GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
    117 
    118     GrBackendTexture origBackendTex = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH,
    119                                                                            kPixelConfig, true,
    120                                                                            GrMipMapped::kNo);
    121     const GrVkImageInfo* imageInfo = origBackendTex.getVkImageInfo();
    122 
    123     sk_sp<GrTexture> tex =
    124             gpu->wrapRenderableBackendTexture(origBackendTex, 1, kBorrow_GrWrapOwnership);
    125     REPORTER_ASSERT(reporter, tex);
    126 
    127     // image is null
    128     {
    129         GrVkImageInfo backendCopy = *imageInfo;
    130         backendCopy.fImage = VK_NULL_HANDLE;
    131         GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
    132         tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
    133         REPORTER_ASSERT(reporter, !tex);
    134         tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
    135         REPORTER_ASSERT(reporter, !tex);
    136     }
    137 
    138     // alloc is null
    139     {
    140         GrVkImageInfo backendCopy = *imageInfo;
    141         backendCopy.fAlloc = GrVkAlloc();
    142         GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
    143         tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
    144         REPORTER_ASSERT(reporter, !tex);
    145         tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
    146         REPORTER_ASSERT(reporter, !tex);
    147     }
    148 
    149     // check adopt creation
    150     {
    151         GrVkImageInfo backendCopy = *imageInfo;
    152         GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
    153         tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kAdopt_GrWrapOwnership);
    154         REPORTER_ASSERT(reporter, tex);
    155     }
    156 
    157     gpu->deleteTestingOnlyBackendTexture(&origBackendTex, true);
    158 }
    159 
    160 DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests, reporter, ctxInfo) {
    161     wrap_tex_test(reporter, ctxInfo.grContext());
    162     wrap_rt_test(reporter, ctxInfo.grContext());
    163     wrap_trt_test(reporter, ctxInfo.grContext());
    164 }
    165 
    166 #endif
    167