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"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrContextPriv.h"
|
|
|
|
#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:
|
2019-01-03 22:35:54 +00:00
|
|
|
TestDrawable(const GrVkInterface* interface, GrContext* context, int32_t width, int32_t height)
|
2018-10-19 17:54:56 +00:00
|
|
|
: INHERITED()
|
|
|
|
, fInterface(interface)
|
2019-01-03 22:35:54 +00:00
|
|
|
, fContext(context)
|
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;
|
|
|
|
|
|
|
|
typedef GpuDrawHandler INHERITED;
|
|
|
|
};
|
|
|
|
|
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*);
|
|
|
|
|
|
|
|
// Exercises the exporting of a secondary command buffer from one GrContext and then importing
|
|
|
|
// it into a second GrContext. We then draw to the secondary command buffer from the second
|
|
|
|
// GrContext.
|
|
|
|
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
|
|
|
|
|
|
|
typedef GpuDrawHandler INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Helper function to test drawing to a secondary command buffer that we imported into the
|
|
|
|
// GrContext 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) {
|
|
|
|
td->fDrawContext = GrVkSecondaryCBDrawContext::Make(td->fContext, 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.
|
|
|
|
sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(td->fContext, 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
|
|
|
|
// its original context and then cleaning up the GrVkSecondaryCBDrawContext from this GrContext.
|
|
|
|
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.
|
2019-02-04 18:26:26 +00:00
|
|
|
td->fContext->priv().getGpu()->testingOnly_flushGpuAndSync();
|
2019-01-03 22:35:54 +00:00
|
|
|
|
|
|
|
td->fDrawContext->releaseResources();
|
|
|
|
// We release the GrContext here manually to test that we waited long enough before
|
|
|
|
// releasing the GrVkSecondaryCBDrawContext. This simulates when a client is able to delete
|
|
|
|
// the GrContext it used to imported the secondary command buffer. If we had released the
|
|
|
|
// GrContext's resources earlier (before waiting on the gpu above), we would get vulkan
|
|
|
|
// validation layer errors saying we freed some vulkan objects while they were still in use
|
|
|
|
// on the GPU.
|
|
|
|
td->fContext->releaseResourcesAndAbandonContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
if (fContext) {
|
2019-01-10 18:19:06 +00:00
|
|
|
draw.reset(new DrawHandlerImport(this, ImportDraw, ImportSubmitted, matrix,
|
|
|
|
clipBounds, bufferInfo));
|
2019-01-03 22:35:54 +00:00
|
|
|
} else {
|
|
|
|
draw.reset(new DrawHandlerBasic(fInterface, fWidth, fHeight));
|
|
|
|
}
|
|
|
|
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;
|
2019-01-03 22:35:54 +00:00
|
|
|
GrContext* fContext;
|
|
|
|
sk_sp<GrVkSecondaryCBDrawContext> fDrawContext;
|
|
|
|
int32_t fWidth;
|
|
|
|
int32_t fHeight;
|
2018-10-19 17:54:56 +00:00
|
|
|
|
|
|
|
typedef SkDrawable INHERITED;
|
|
|
|
};
|
|
|
|
|
2019-01-03 22:35:54 +00:00
|
|
|
void draw_drawable_test(skiatest::Reporter* reporter, GrContext* context, GrContext* childContext) {
|
2019-02-04 18:26:26 +00:00
|
|
|
GrVkGpu* gpu = static_cast<GrVkGpu*>(context->priv().getGpu());
|
2018-10-19 17:54:56 +00:00
|
|
|
|
|
|
|
const SkImageInfo ii = SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType,
|
|
|
|
kPremul_SkAlphaType);
|
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo,
|
|
|
|
ii, 0, kTopLeft_GrSurfaceOrigin, nullptr));
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
canvas->clear(SK_ColorBLUE);
|
|
|
|
|
2019-01-03 22:35:54 +00:00
|
|
|
sk_sp<TestDrawable> drawable(new TestDrawable(gpu->vkInterface(), childContext, 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
|