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