2018-10-19 17:54:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This is a GPU-backend specific test. It relies on static intializers to work
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2018-10-19 17:54:56 +00:00
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU && defined(SK_VULKAN)
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/gpu/vk/GrVkVulkan.h"
|
|
|
|
|
|
|
|
#include "include/core/SkDrawable.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/gpu/GrBackendDrawableInfo.h"
|
2020-07-06 14:56:46 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/vk/GrVkGpu.h"
|
|
|
|
#include "src/gpu/vk/GrVkInterface.h"
|
|
|
|
#include "src/gpu/vk/GrVkMemory.h"
|
|
|
|
#include "src/gpu/vk/GrVkSecondaryCBDrawContext.h"
|
|
|
|
#include "src/gpu/vk/GrVkUtil.h"
|
|
|
|
#include "tests/Test.h"
|
|
|
|
#include "tools/gpu/GrContextFactory.h"
|
2018-10-19 17:54:56 +00:00
|
|
|
|
|
|
|
using sk_gpu_test::GrContextFactory;
|
|
|
|
|
|
|
|
static const int DEV_W = 16, DEV_H = 16;
|
|
|
|
|
|
|
|
class TestDrawable : public SkDrawable {
|
|
|
|
public:
|
2020-07-22 13:35:49 +00:00
|
|
|
TestDrawable(const GrVkInterface* interface, GrDirectContext* dContext,
|
|
|
|
int32_t width, int32_t height)
|
2018-10-19 17:54:56 +00:00
|
|
|
: INHERITED()
|
|
|
|
, fInterface(interface)
|
2020-07-22 13:35:49 +00:00
|
|
|
, fDContext(dContext)
|
2018-10-19 17:54:56 +00:00
|
|
|
, fWidth(width)
|
|
|
|
, fHeight(height) {}
|
|
|
|
|
|
|
|
~TestDrawable() override {}
|
|
|
|
|
2019-01-03 22:35:54 +00:00
|
|
|
class DrawHandlerBasic : public GpuDrawHandler {
|
2018-10-19 17:54:56 +00:00
|
|
|
public:
|
2019-01-03 22:35:54 +00:00
|
|
|
DrawHandlerBasic(const GrVkInterface* interface, int32_t width, int32_t height)
|
2018-10-19 17:54:56 +00:00
|
|
|
: INHERITED()
|
|
|
|
, fInterface(interface)
|
|
|
|
, fWidth(width)
|
|
|
|
, fHeight(height) {}
|
2019-01-03 22:35:54 +00:00
|
|
|
~DrawHandlerBasic() override {}
|
2018-10-19 17:54:56 +00:00
|
|
|
|
|
|
|
void draw(const GrBackendDrawableInfo& info) override {
|
|
|
|
GrVkDrawableInfo vkInfo;
|
|
|
|
SkAssertResult(info.getVkDrawableInfo(&vkInfo));
|
|
|
|
|
|
|
|
// Clear to Red
|
|
|
|
VkClearColorValue vkColor;
|
|
|
|
vkColor.float32[0] = 1.0f; // r
|
|
|
|
vkColor.float32[1] = 0.0f; // g
|
|
|
|
vkColor.float32[2] = 0.0f; // b
|
|
|
|
vkColor.float32[3] = 1.0f; // a
|
|
|
|
|
|
|
|
// Clear right half of render target
|
|
|
|
VkClearRect clearRect;
|
|
|
|
clearRect.rect.offset = { fWidth / 2, 0 };
|
|
|
|
clearRect.rect.extent = { (uint32_t)fWidth / 2, (uint32_t)fHeight };
|
|
|
|
clearRect.baseArrayLayer = 0;
|
|
|
|
clearRect.layerCount = 1;
|
|
|
|
|
|
|
|
VkClearAttachment attachment;
|
|
|
|
attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
2018-12-05 16:01:58 +00:00
|
|
|
attachment.colorAttachment = vkInfo.fColorAttachmentIndex;
|
2018-10-19 17:54:56 +00:00
|
|
|
attachment.clearValue.color = vkColor;
|
|
|
|
|
|
|
|
GR_VK_CALL(fInterface, CmdClearAttachments(vkInfo.fSecondaryCommandBuffer,
|
|
|
|
1,
|
|
|
|
&attachment,
|
|
|
|
1,
|
|
|
|
&clearRect));
|
|
|
|
vkInfo.fDrawBounds->offset = { fWidth / 2, 0 };
|
|
|
|
vkInfo.fDrawBounds->extent = { (uint32_t)fWidth / 2, (uint32_t)fHeight };
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
const GrVkInterface* fInterface;
|
|
|
|
int32_t fWidth;
|
|
|
|
int32_t fHeight;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GpuDrawHandler;
|
2018-10-19 17:54:56 +00:00
|
|
|
};
|
|
|
|
|
2019-01-10 18:19:06 +00:00
|
|
|
typedef void (*DrawProc)(TestDrawable*, const SkMatrix&, const SkIRect&,
|
|
|
|
const SkImageInfo&, const GrVkDrawableInfo&);
|
2019-01-03 22:35:54 +00:00
|
|
|
typedef void (*SubmitProc)(TestDrawable*);
|
|
|
|
|
2020-07-23 17:54:35 +00:00
|
|
|
// Exercises the exporting of a secondary command buffer from one context and then importing
|
|
|
|
// it into a second context. We then draw to the secondary command buffer from the second
|
|
|
|
// context.
|
2019-01-03 22:35:54 +00:00
|
|
|
class DrawHandlerImport : public GpuDrawHandler {
|
|
|
|
public:
|
2019-01-10 18:19:06 +00:00
|
|
|
DrawHandlerImport(TestDrawable* td, DrawProc drawProc, SubmitProc submitProc,
|
|
|
|
const SkMatrix& matrix,
|
|
|
|
const SkIRect& clipBounds,
|
|
|
|
const SkImageInfo& bufferInfo)
|
2019-01-03 22:35:54 +00:00
|
|
|
: INHERITED()
|
|
|
|
, fTestDrawable(td)
|
|
|
|
, fDrawProc(drawProc)
|
2019-01-10 18:19:06 +00:00
|
|
|
, fSubmitProc(submitProc)
|
|
|
|
, fMatrix(matrix)
|
|
|
|
, fClipBounds(clipBounds)
|
|
|
|
, fBufferInfo(bufferInfo) {}
|
2019-01-03 22:35:54 +00:00
|
|
|
~DrawHandlerImport() override {
|
|
|
|
fSubmitProc(fTestDrawable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void draw(const GrBackendDrawableInfo& info) override {
|
|
|
|
GrVkDrawableInfo vkInfo;
|
|
|
|
SkAssertResult(info.getVkDrawableInfo(&vkInfo));
|
|
|
|
|
2019-01-10 18:19:06 +00:00
|
|
|
fDrawProc(fTestDrawable, fMatrix, fClipBounds, fBufferInfo, vkInfo);
|
2019-01-03 22:35:54 +00:00
|
|
|
}
|
|
|
|
private:
|
2019-01-10 18:19:06 +00:00
|
|
|
TestDrawable* fTestDrawable;
|
|
|
|
DrawProc fDrawProc;
|
|
|
|
SubmitProc fSubmitProc;
|
|
|
|
const SkMatrix fMatrix;
|
|
|
|
const SkIRect fClipBounds;
|
|
|
|
const SkImageInfo fBufferInfo;
|
2019-01-03 22:35:54 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GpuDrawHandler;
|
2019-01-03 22:35:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Helper function to test drawing to a secondary command buffer that we imported into the
|
2020-07-23 17:54:35 +00:00
|
|
|
// context using a GrVkSecondaryCBDrawContext.
|
2019-01-10 18:19:06 +00:00
|
|
|
static void ImportDraw(TestDrawable* td, const SkMatrix& matrix, const SkIRect& clipBounds,
|
|
|
|
const SkImageInfo& bufferInfo, const GrVkDrawableInfo& info) {
|
2020-07-22 13:35:49 +00:00
|
|
|
td->fDrawContext = GrVkSecondaryCBDrawContext::Make(td->fDContext, bufferInfo,
|
|
|
|
info, nullptr);
|
2019-01-03 22:35:54 +00:00
|
|
|
if (!td->fDrawContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkCanvas* canvas = td->fDrawContext->getCanvas();
|
2019-01-10 18:19:06 +00:00
|
|
|
canvas->clipRect(SkRect::Make(clipBounds));
|
|
|
|
canvas->setMatrix(matrix);
|
|
|
|
|
2019-01-03 22:35:54 +00:00
|
|
|
SkIRect rect = SkIRect::MakeXYWH(td->fWidth/2, 0, td->fWidth/4, td->fHeight);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawIRect(rect, paint);
|
|
|
|
|
|
|
|
// Draw to an offscreen target so that we end up with a mix of "real" secondary command
|
|
|
|
// buffers and the imported secondary command buffer.
|
2020-07-22 13:35:49 +00:00
|
|
|
sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(td->fDContext, SkBudgeted::kYes,
|
2019-01-10 18:19:06 +00:00
|
|
|
bufferInfo);
|
2019-01-03 22:35:54 +00:00
|
|
|
surf->getCanvas()->clear(SK_ColorRED);
|
|
|
|
|
|
|
|
SkRect dstRect = SkRect::MakeXYWH(3*td->fWidth/4, 0, td->fWidth/4, td->fHeight);
|
|
|
|
SkIRect srcRect = SkIRect::MakeWH(td->fWidth/4, td->fHeight);
|
|
|
|
canvas->drawImageRect(surf->makeImageSnapshot(), srcRect, dstRect, &paint);
|
|
|
|
|
|
|
|
td->fDrawContext->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to test waiting for the imported secondary command buffer to be submitted on
|
2020-07-23 17:54:35 +00:00
|
|
|
// its original context and then cleaning up the GrVkSecondaryCBDrawContext from this context.
|
2019-01-03 22:35:54 +00:00
|
|
|
static void ImportSubmitted(TestDrawable* td) {
|
|
|
|
// Typical use case here would be to create a fence that we submit to the gpu and then wait
|
|
|
|
// on before releasing the GrVkSecondaryCBDrawContext resources. To simulate that for this
|
|
|
|
// test (and since we are running single threaded anyways), we will just force a sync of
|
|
|
|
// the gpu and cpu here.
|
2020-07-22 13:35:49 +00:00
|
|
|
td->fDContext->priv().getGpu()->testingOnly_flushGpuAndSync();
|
2019-01-03 22:35:54 +00:00
|
|
|
|
|
|
|
td->fDrawContext->releaseResources();
|
2020-07-23 17:54:35 +00:00
|
|
|
// We release the context here manually to test that we waited long enough before
|
2019-01-03 22:35:54 +00:00
|
|
|
// releasing the GrVkSecondaryCBDrawContext. This simulates when a client is able to delete
|
2020-07-23 17:54:35 +00:00
|
|
|
// the context it used to imported the secondary command buffer. If we had released the
|
|
|
|
// context's resources earlier (before waiting on the gpu above), we would get vulkan
|
2019-01-03 22:35:54 +00:00
|
|
|
// validation layer errors saying we freed some vulkan objects while they were still in use
|
|
|
|
// on the GPU.
|
2020-07-22 13:35:49 +00:00
|
|
|
td->fDContext->releaseResourcesAndAbandonContext();
|
2019-01-03 22:35:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-19 17:54:56 +00:00
|
|
|
std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi backendApi,
|
2018-12-17 15:28:47 +00:00
|
|
|
const SkMatrix& matrix,
|
2019-01-10 18:19:06 +00:00
|
|
|
const SkIRect& clipBounds,
|
|
|
|
const SkImageInfo& bufferInfo) override {
|
2018-10-19 17:54:56 +00:00
|
|
|
if (backendApi != GrBackendApi::kVulkan) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-01-03 22:35:54 +00:00
|
|
|
std::unique_ptr<GpuDrawHandler> draw;
|
2020-07-22 13:35:49 +00:00
|
|
|
if (fDContext) {
|
2020-08-03 17:21:46 +00:00
|
|
|
draw = std::make_unique<DrawHandlerImport>(this, ImportDraw, ImportSubmitted, matrix,
|
|
|
|
clipBounds, bufferInfo);
|
2019-01-03 22:35:54 +00:00
|
|
|
} else {
|
2020-08-03 17:21:46 +00:00
|
|
|
draw = std::make_unique<DrawHandlerBasic>(fInterface, fWidth, fHeight);
|
2019-01-03 22:35:54 +00:00
|
|
|
}
|
|
|
|
return draw;
|
2018-10-19 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkRect onGetBounds() override {
|
|
|
|
return SkRect::MakeLTRB(fWidth / 2, 0, fWidth, fHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas*) override {
|
|
|
|
SkASSERT(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const GrVkInterface* fInterface;
|
2020-07-22 13:35:49 +00:00
|
|
|
GrDirectContext* fDContext;
|
2019-01-03 22:35:54 +00:00
|
|
|
sk_sp<GrVkSecondaryCBDrawContext> fDrawContext;
|
|
|
|
int32_t fWidth;
|
|
|
|
int32_t fHeight;
|
2018-10-19 17:54:56 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = SkDrawable;
|
2018-10-19 17:54:56 +00:00
|
|
|
};
|
|
|
|
|
2020-07-22 13:35:49 +00:00
|
|
|
void draw_drawable_test(skiatest::Reporter* reporter,
|
|
|
|
GrDirectContext* dContext,
|
|
|
|
GrDirectContext* childDContext) {
|
|
|
|
GrVkGpu* gpu = static_cast<GrVkGpu*>(dContext->priv().getGpu());
|
2018-10-19 17:54:56 +00:00
|
|
|
|
|
|
|
const SkImageInfo ii = SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType,
|
|
|
|
kPremul_SkAlphaType);
|
2020-07-22 13:35:49 +00:00
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo,
|
2018-10-19 17:54:56 +00:00
|
|
|
ii, 0, kTopLeft_GrSurfaceOrigin, nullptr));
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
canvas->clear(SK_ColorBLUE);
|
|
|
|
|
2020-07-22 13:35:49 +00:00
|
|
|
sk_sp<TestDrawable> drawable(new TestDrawable(gpu->vkInterface(), childDContext, DEV_W, DEV_H));
|
2018-10-19 17:54:56 +00:00
|
|
|
canvas->drawDrawable(drawable.get());
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
SkIRect rect = SkIRect::MakeLTRB(0, DEV_H/2, DEV_W, DEV_H);
|
|
|
|
canvas->drawIRect(rect, paint);
|
|
|
|
|
|
|
|
// read pixels
|
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.allocPixels(ii);
|
|
|
|
canvas->readPixels(bitmap, 0, 0);
|
|
|
|
|
|
|
|
const uint32_t* canvasPixels = static_cast<const uint32_t*>(bitmap.getPixels());
|
|
|
|
bool failureFound = false;
|
|
|
|
SkPMColor expectedPixel;
|
2019-01-03 22:35:54 +00:00
|
|
|
for (int cy = 0; cy < DEV_H && !failureFound; ++cy) {
|
|
|
|
for (int cx = 0; cx < DEV_W && !failureFound; ++cx) {
|
2018-10-19 17:54:56 +00:00
|
|
|
SkPMColor canvasPixel = canvasPixels[cy * DEV_W + cx];
|
|
|
|
if (cy < DEV_H / 2) {
|
|
|
|
if (cx < DEV_W / 2) {
|
|
|
|
expectedPixel = 0xFFFF0000; // Blue
|
|
|
|
} else {
|
|
|
|
expectedPixel = 0xFF0000FF; // Red
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
expectedPixel = 0xFF00FF00; // Green
|
|
|
|
}
|
|
|
|
if (expectedPixel != canvasPixel) {
|
|
|
|
failureFound = true;
|
|
|
|
ERRORF(reporter, "Wrong color at %d, %d. Got 0x%08x when we expected 0x%08x",
|
|
|
|
cx, cy, canvasPixel, expectedPixel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkDrawableTest, reporter, ctxInfo) {
|
2020-07-06 14:56:46 +00:00
|
|
|
draw_drawable_test(reporter, ctxInfo.directContext(), nullptr);
|
2019-01-03 22:35:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_GPUTEST(VkDrawableImportTest, reporter, options) {
|
|
|
|
for (int typeInt = 0; typeInt < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++typeInt) {
|
|
|
|
sk_gpu_test::GrContextFactory::ContextType contextType =
|
|
|
|
(sk_gpu_test::GrContextFactory::ContextType) typeInt;
|
|
|
|
if (contextType != sk_gpu_test::GrContextFactory::kVulkan_ContextType) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sk_gpu_test::GrContextFactory factory(options);
|
2019-06-26 02:07:56 +00:00
|
|
|
sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo(contextType);
|
2019-01-03 22:35:54 +00:00
|
|
|
skiatest::ReporterContext ctx(
|
|
|
|
reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType)));
|
2020-07-06 14:56:46 +00:00
|
|
|
if (ctxInfo.directContext()) {
|
2019-01-03 22:35:54 +00:00
|
|
|
sk_gpu_test::ContextInfo child =
|
2020-07-01 20:09:43 +00:00
|
|
|
factory.getSharedContextInfo(ctxInfo.directContext(), 0);
|
2020-07-06 14:56:46 +00:00
|
|
|
if (!child.directContext()) {
|
2019-01-03 22:35:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-06 14:56:46 +00:00
|
|
|
draw_drawable_test(reporter, ctxInfo.directContext(), child.directContext());
|
2019-01-03 22:35:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-19 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|