blob: 0ff94aa4544cba5970110399e62064adecd29e1b [file] [log] [blame]
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001/*
2 * Copyright 2018 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
Greg Daniel54bfb182018-11-20 17:12:36 -050014#include "vk/GrVkVulkan.h"
15
Greg Daniel64cc9aa2018-10-19 13:54:56 -040016#include "GrBackendDrawableInfo.h"
17#include "GrContextFactory.h"
18#include "GrContextPriv.h"
19#include "SkDrawable.h"
20#include "SkSurface.h"
21#include "Test.h"
22#include "vk/GrVkGpu.h"
23#include "vk/GrVkInterface.h"
24#include "vk/GrVkMemory.h"
25#include "vk/GrVkUtil.h"
26
27using sk_gpu_test::GrContextFactory;
28
29static const int DEV_W = 16, DEV_H = 16;
30
31class TestDrawable : public SkDrawable {
32public:
33 TestDrawable(const GrVkInterface* interface, int32_t width, int32_t height)
34 : INHERITED()
35 , fInterface(interface)
36 , fWidth(width)
37 , fHeight(height) {}
38
39 ~TestDrawable() override {}
40
41 class DrawHandler : public GpuDrawHandler {
42 public:
43 DrawHandler(const GrVkInterface* interface, int32_t width, int32_t height)
44 : INHERITED()
45 , fInterface(interface)
46 , fWidth(width)
47 , fHeight(height) {}
48 ~DrawHandler() override {}
49
50 void draw(const GrBackendDrawableInfo& info) override {
51 GrVkDrawableInfo vkInfo;
52 SkAssertResult(info.getVkDrawableInfo(&vkInfo));
53
54 // Clear to Red
55 VkClearColorValue vkColor;
56 vkColor.float32[0] = 1.0f; // r
57 vkColor.float32[1] = 0.0f; // g
58 vkColor.float32[2] = 0.0f; // b
59 vkColor.float32[3] = 1.0f; // a
60
61 // Clear right half of render target
62 VkClearRect clearRect;
63 clearRect.rect.offset = { fWidth / 2, 0 };
64 clearRect.rect.extent = { (uint32_t)fWidth / 2, (uint32_t)fHeight };
65 clearRect.baseArrayLayer = 0;
66 clearRect.layerCount = 1;
67
68 VkClearAttachment attachment;
69 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Greg Danielb353eeb2018-12-05 11:01:58 -050070 attachment.colorAttachment = vkInfo.fColorAttachmentIndex;
Greg Daniel64cc9aa2018-10-19 13:54:56 -040071 attachment.clearValue.color = vkColor;
72
73 GR_VK_CALL(fInterface, CmdClearAttachments(vkInfo.fSecondaryCommandBuffer,
74 1,
75 &attachment,
76 1,
77 &clearRect));
78 vkInfo.fDrawBounds->offset = { fWidth / 2, 0 };
79 vkInfo.fDrawBounds->extent = { (uint32_t)fWidth / 2, (uint32_t)fHeight };
80 }
81 private:
82 const GrVkInterface* fInterface;
83 int32_t fWidth;
84 int32_t fHeight;
85
86 typedef GpuDrawHandler INHERITED;
87 };
88
89 std::unique_ptr<GpuDrawHandler> onSnapGpuDrawHandler(GrBackendApi backendApi,
Greg Danielb2a259c2018-12-17 10:28:47 -050090 const SkMatrix& matrix,
91 const SkIRect& clipBounds) override {
Greg Daniel64cc9aa2018-10-19 13:54:56 -040092 if (backendApi != GrBackendApi::kVulkan) {
93 return nullptr;
94 }
95 std::unique_ptr<DrawHandler> draw(new DrawHandler(fInterface, fWidth, fHeight));
96 return std::move(draw);
97 }
98
99 SkRect onGetBounds() override {
100 return SkRect::MakeLTRB(fWidth / 2, 0, fWidth, fHeight);
101 }
102
103 void onDraw(SkCanvas*) override {
104 SkASSERT(false);
105 }
106
107private:
108 const GrVkInterface* fInterface;
109 int32_t fWidth;
110 int32_t fHeight;
111
112 typedef SkDrawable INHERITED;
113};
114
115void draw_drawable_test(skiatest::Reporter* reporter, GrContext* context) {
116 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
117
118 const SkImageInfo ii = SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType,
119 kPremul_SkAlphaType);
120 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo,
121 ii, 0, kTopLeft_GrSurfaceOrigin, nullptr));
122 SkCanvas* canvas = surface->getCanvas();
123 canvas->clear(SK_ColorBLUE);
124
125 sk_sp<TestDrawable> drawable(new TestDrawable(gpu->vkInterface(), DEV_W, DEV_H));
126 canvas->drawDrawable(drawable.get());
127
128 SkPaint paint;
129 paint.setColor(SK_ColorGREEN);
130 SkIRect rect = SkIRect::MakeLTRB(0, DEV_H/2, DEV_W, DEV_H);
131 canvas->drawIRect(rect, paint);
132
133 // read pixels
134 SkBitmap bitmap;
135 bitmap.allocPixels(ii);
136 canvas->readPixels(bitmap, 0, 0);
137
138 const uint32_t* canvasPixels = static_cast<const uint32_t*>(bitmap.getPixels());
139 bool failureFound = false;
140 SkPMColor expectedPixel;
141 for (int cy = 0; cy < DEV_H || failureFound; ++cy) {
142 for (int cx = 0; cx < DEV_W || failureFound; ++cx) {
143 SkPMColor canvasPixel = canvasPixels[cy * DEV_W + cx];
144 if (cy < DEV_H / 2) {
145 if (cx < DEV_W / 2) {
146 expectedPixel = 0xFFFF0000; // Blue
147 } else {
148 expectedPixel = 0xFF0000FF; // Red
149 }
150 } else {
151 expectedPixel = 0xFF00FF00; // Green
152 }
153 if (expectedPixel != canvasPixel) {
154 failureFound = true;
155 ERRORF(reporter, "Wrong color at %d, %d. Got 0x%08x when we expected 0x%08x",
156 cx, cy, canvasPixel, expectedPixel);
157 }
158 }
159 }
160}
161
162
163DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkDrawableTest, reporter, ctxInfo) {
164 draw_drawable_test(reporter, ctxInfo.grContext());
165}
166
167#endif