Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/vk/GrVkSecondaryCBDrawContext.h" |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkImageInfo.h" |
| 11 | #include "include/core/SkSurfaceCharacterization.h" |
| 12 | #include "include/gpu/GrContext.h" |
| 13 | #include "include/gpu/vk/GrVkTypes.h" |
| 14 | #include "include/private/SkDeferredDisplayList.h" |
| 15 | #include "src/core/SkSurfacePriv.h" |
| 16 | #include "src/gpu/GrContextPriv.h" |
| 17 | #include "src/gpu/GrContextThreadSafeProxyPriv.h" |
| 18 | #include "src/gpu/GrRenderTargetContext.h" |
| 19 | #include "src/gpu/SkGpuDevice.h" |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 20 | |
| 21 | sk_sp<GrVkSecondaryCBDrawContext> GrVkSecondaryCBDrawContext::Make(GrContext* ctx, |
| 22 | const SkImageInfo& imageInfo, |
| 23 | const GrVkDrawableInfo& vkInfo, |
| 24 | const SkSurfaceProps* props) { |
| 25 | if (!ctx) { |
| 26 | return nullptr; |
| 27 | } |
| 28 | |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 29 | if (ctx->backend() != GrBackendApi::kVulkan) { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 30 | return nullptr; |
| 31 | } |
| 32 | |
| 33 | sk_sp<GrRenderTargetContext> rtc( |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 34 | ctx->priv().makeVulkanSecondaryCBRenderTargetContext(imageInfo, vkInfo, props)); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 35 | |
| 36 | int width = rtc->width(); |
| 37 | int height = rtc->height(); |
| 38 | |
| 39 | sk_sp<SkGpuDevice> device(SkGpuDevice::Make(ctx, std::move(rtc), width, height, |
| 40 | SkGpuDevice::kUninit_InitContents)); |
| 41 | if (!device) { |
| 42 | return nullptr; |
| 43 | } |
| 44 | |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 45 | return sk_sp<GrVkSecondaryCBDrawContext>(new GrVkSecondaryCBDrawContext(std::move(device), |
| 46 | props)); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 47 | } |
| 48 | |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 49 | GrVkSecondaryCBDrawContext::GrVkSecondaryCBDrawContext(sk_sp<SkGpuDevice> device, |
| 50 | const SkSurfaceProps* props) |
| 51 | : fDevice(device) |
| 52 | , fProps(SkSurfacePropsCopyOrDefault(props)) {} |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 53 | |
| 54 | GrVkSecondaryCBDrawContext::~GrVkSecondaryCBDrawContext() { |
| 55 | SkASSERT(!fDevice); |
| 56 | SkASSERT(!fCachedCanvas.get()); |
| 57 | } |
| 58 | |
| 59 | SkCanvas* GrVkSecondaryCBDrawContext::getCanvas() { |
| 60 | if (!fCachedCanvas) { |
| 61 | fCachedCanvas = std::unique_ptr<SkCanvas>(new SkCanvas(fDevice)); |
| 62 | } |
| 63 | return fCachedCanvas.get(); |
| 64 | } |
| 65 | |
| 66 | void GrVkSecondaryCBDrawContext::flush() { |
| 67 | fDevice->flush(); |
| 68 | } |
| 69 | |
Greg Daniel | 3d92a49 | 2019-01-07 12:59:03 -0500 | [diff] [blame] | 70 | bool GrVkSecondaryCBDrawContext::wait(int numSemaphores, |
| 71 | const GrBackendSemaphore waitSemaphores[]) { |
| 72 | return fDevice->wait(numSemaphores, waitSemaphores); |
| 73 | } |
| 74 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 75 | void GrVkSecondaryCBDrawContext::releaseResources() { |
| 76 | fCachedCanvas.reset(); |
| 77 | fDevice.reset(); |
| 78 | } |
| 79 | |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 80 | bool GrVkSecondaryCBDrawContext::characterize(SkSurfaceCharacterization* characterization) const { |
| 81 | GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext(); |
| 82 | GrContext* ctx = fDevice->context(); |
| 83 | |
| 84 | int maxResourceCount; |
| 85 | size_t maxResourceBytes; |
| 86 | ctx->getResourceCacheLimits(&maxResourceCount, &maxResourceBytes); |
| 87 | |
| 88 | // We current don't support textured GrVkSecondaryCBDrawContexts. |
| 89 | SkASSERT(!rtc->asTextureProxy()); |
| 90 | |
| 91 | // TODO: the addition of colorType to the surfaceContext should remove this calculation |
| 92 | SkColorType ct; |
| 93 | if (!GrPixelConfigToColorType(rtc->colorSpaceInfo().config(), &ct)) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | SkImageInfo ii = SkImageInfo::Make(rtc->width(), rtc->height(), ct, kPremul_SkAlphaType, |
| 98 | rtc->colorSpaceInfo().refColorSpace()); |
| 99 | |
| 100 | characterization->set(ctx->threadSafeProxy(), maxResourceBytes, ii, rtc->origin(), |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 101 | rtc->colorSpaceInfo().config(), rtc->numSamples(), |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 102 | SkSurfaceCharacterization::Textureable(false), |
| 103 | SkSurfaceCharacterization::MipMapped(false), |
| 104 | SkSurfaceCharacterization::UsesGLFBO0(false), |
| 105 | SkSurfaceCharacterization::VulkanSecondaryCBCompatible(true), |
| 106 | this->props()); |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | bool GrVkSecondaryCBDrawContext::isCompatible( |
| 112 | const SkSurfaceCharacterization& characterization) const { |
| 113 | GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext(); |
| 114 | GrContext* ctx = fDevice->context(); |
| 115 | |
| 116 | if (!characterization.isValid()) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | if (!characterization.vulkanSecondaryCBCompatible()) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | // As long as the current state in the context allows for greater or equal resources, |
| 125 | // we allow the DDL to be replayed. |
| 126 | // DDL TODO: should we just remove the resource check and ignore the cache limits on playback? |
| 127 | int maxResourceCount; |
| 128 | size_t maxResourceBytes; |
| 129 | ctx->getResourceCacheLimits(&maxResourceCount, &maxResourceBytes); |
| 130 | |
| 131 | if (characterization.isTextureable()) { |
| 132 | // We don't support textureable DDL when rendering to a GrVkSecondaryCBDrawContext. |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | if (characterization.usesGLFBO0()) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | // TODO: the addition of colorType to the surfaceContext should remove this calculation |
| 141 | SkColorType rtcColorType; |
| 142 | if (!GrPixelConfigToColorType(rtc->colorSpaceInfo().config(), &rtcColorType)) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | return characterization.contextInfo() && characterization.contextInfo()->priv().matches(ctx) && |
| 147 | characterization.cacheMaxResourceBytes() <= maxResourceBytes && |
| 148 | characterization.origin() == rtc->origin() && |
| 149 | characterization.config() == rtc->colorSpaceInfo().config() && |
| 150 | characterization.width() == rtc->width() && |
| 151 | characterization.height() == rtc->height() && |
| 152 | characterization.colorType() == rtcColorType && |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 153 | characterization.sampleCount() == rtc->numSamples() && |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 154 | SkColorSpace::Equals(characterization.colorSpace(), |
| 155 | rtc->colorSpaceInfo().colorSpace()) && |
| 156 | characterization.surfaceProps() == rtc->surfaceProps(); |
| 157 | } |
| 158 | |
| 159 | bool GrVkSecondaryCBDrawContext::draw(SkDeferredDisplayList* ddl) { |
| 160 | if (!ddl || !this->isCompatible(ddl->characterization())) { |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext(); |
| 165 | GrContext* ctx = fDevice->context(); |
| 166 | |
| 167 | ctx->priv().copyOpListsFromDDL(ddl, rtc->asRenderTargetProxy()); |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | |