Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 1 | /* |
| 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 Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 14 | #include "vk/GrVkVulkan.h" |
| 15 | |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 16 | #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 | |
| 27 | using sk_gpu_test::GrContextFactory; |
| 28 | |
| 29 | static const int DEV_W = 16, DEV_H = 16; |
| 30 | |
| 31 | class TestDrawable : public SkDrawable { |
| 32 | public: |
| 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; |
| 70 | attachment.colorAttachment = vkInfo.fImageAttachmentIndex; |
| 71 | 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, |
| 90 | const SkMatrix& matrix) override { |
| 91 | if (backendApi != GrBackendApi::kVulkan) { |
| 92 | return nullptr; |
| 93 | } |
| 94 | std::unique_ptr<DrawHandler> draw(new DrawHandler(fInterface, fWidth, fHeight)); |
| 95 | return std::move(draw); |
| 96 | } |
| 97 | |
| 98 | SkRect onGetBounds() override { |
| 99 | return SkRect::MakeLTRB(fWidth / 2, 0, fWidth, fHeight); |
| 100 | } |
| 101 | |
| 102 | void onDraw(SkCanvas*) override { |
| 103 | SkASSERT(false); |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | const GrVkInterface* fInterface; |
| 108 | int32_t fWidth; |
| 109 | int32_t fHeight; |
| 110 | |
| 111 | typedef SkDrawable INHERITED; |
| 112 | }; |
| 113 | |
| 114 | void draw_drawable_test(skiatest::Reporter* reporter, GrContext* context) { |
| 115 | GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu()); |
| 116 | |
| 117 | const SkImageInfo ii = SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType, |
| 118 | kPremul_SkAlphaType); |
| 119 | sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, |
| 120 | ii, 0, kTopLeft_GrSurfaceOrigin, nullptr)); |
| 121 | SkCanvas* canvas = surface->getCanvas(); |
| 122 | canvas->clear(SK_ColorBLUE); |
| 123 | |
| 124 | sk_sp<TestDrawable> drawable(new TestDrawable(gpu->vkInterface(), DEV_W, DEV_H)); |
| 125 | canvas->drawDrawable(drawable.get()); |
| 126 | |
| 127 | SkPaint paint; |
| 128 | paint.setColor(SK_ColorGREEN); |
| 129 | SkIRect rect = SkIRect::MakeLTRB(0, DEV_H/2, DEV_W, DEV_H); |
| 130 | canvas->drawIRect(rect, paint); |
| 131 | |
| 132 | // read pixels |
| 133 | SkBitmap bitmap; |
| 134 | bitmap.allocPixels(ii); |
| 135 | canvas->readPixels(bitmap, 0, 0); |
| 136 | |
| 137 | const uint32_t* canvasPixels = static_cast<const uint32_t*>(bitmap.getPixels()); |
| 138 | bool failureFound = false; |
| 139 | SkPMColor expectedPixel; |
| 140 | for (int cy = 0; cy < DEV_H || failureFound; ++cy) { |
| 141 | for (int cx = 0; cx < DEV_W || failureFound; ++cx) { |
| 142 | SkPMColor canvasPixel = canvasPixels[cy * DEV_W + cx]; |
| 143 | if (cy < DEV_H / 2) { |
| 144 | if (cx < DEV_W / 2) { |
| 145 | expectedPixel = 0xFFFF0000; // Blue |
| 146 | } else { |
| 147 | expectedPixel = 0xFF0000FF; // Red |
| 148 | } |
| 149 | } else { |
| 150 | expectedPixel = 0xFF00FF00; // Green |
| 151 | } |
| 152 | if (expectedPixel != canvasPixel) { |
| 153 | failureFound = true; |
| 154 | ERRORF(reporter, "Wrong color at %d, %d. Got 0x%08x when we expected 0x%08x", |
| 155 | cx, cy, canvasPixel, expectedPixel); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | |
| 162 | DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkDrawableTest, reporter, ctxInfo) { |
| 163 | draw_drawable_test(reporter, ctxInfo.grContext()); |
| 164 | } |
| 165 | |
| 166 | #endif |