Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | #include "GrBackendTextureImageGenerator.h" |
| 9 | |
| 10 | #include "GrContext.h" |
| 11 | #include "GrContextPriv.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 12 | #include "GrGpu.h" |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 13 | #include "GrResourceCache.h" |
| 14 | #include "GrResourceProvider.h" |
| 15 | #include "GrSemaphore.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 16 | #include "GrTexture.h" |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 17 | |
| 18 | #include "SkGr.h" |
| 19 | #include "SkMessageBus.h" |
| 20 | |
| 21 | GrBackendTextureImageGenerator::RefHelper::~RefHelper() { |
| 22 | SkASSERT(nullptr == fBorrowedTexture); |
| 23 | SkASSERT(SK_InvalidGenID == fBorrowingContextID); |
| 24 | |
| 25 | // Generator has been freed, and no one is borrowing the texture. Notify the original cache |
| 26 | // that it can free the last ref, so it happens on the correct thread. |
| 27 | GrGpuResourceFreedMessage msg { fOriginalTexture, fOwningContextID }; |
| 28 | SkMessageBus<GrGpuResourceFreedMessage>::Post(msg); |
| 29 | } |
| 30 | |
| 31 | // TODO: I copied this from SkImage_Gpu, perhaps we put a version of this somewhere else? |
| 32 | static GrBackendTexture make_backend_texture_from_handle(GrBackend backend, |
| 33 | int width, int height, |
| 34 | GrPixelConfig config, |
| 35 | GrBackendObject handle) { |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 36 | switch (backend) { |
| 37 | case kOpenGL_GrBackend: { |
| 38 | const GrGLTextureInfo* glInfo = (const GrGLTextureInfo*)(handle); |
| 39 | return GrBackendTexture(width, height, config, *glInfo); |
| 40 | } |
Mike Reed | d20b5c4 | 2017-06-14 06:03:10 -0400 | [diff] [blame] | 41 | #ifdef SK_VULKAN |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 42 | case kVulkan_GrBackend: { |
| 43 | const GrVkImageInfo* vkInfo = (const GrVkImageInfo*)(handle); |
| 44 | return GrBackendTexture(width, height, *vkInfo); |
| 45 | } |
Robert Phillips | fcd5fdd | 2017-06-14 01:43:29 +0000 | [diff] [blame] | 46 | #endif |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 47 | case kMock_GrBackend: { |
| 48 | const GrMockTextureInfo* mockInfo = (const GrMockTextureInfo*)(handle); |
| 49 | return GrBackendTexture(width, height, config, *mockInfo); |
| 50 | } |
| 51 | default: |
| 52 | return GrBackendTexture(); |
| 53 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | std::unique_ptr<SkImageGenerator> |
Robert Phillips | 7294b85 | 2017-08-01 13:51:44 +0000 | [diff] [blame] | 57 | GrBackendTextureImageGenerator::Make(sk_sp<GrTexture> texture, sk_sp<GrSemaphore> semaphore, |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 58 | SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) { |
| 59 | if (colorSpace && (!colorSpace->gammaCloseToSRGB() && !colorSpace->gammaIsLinear())) { |
| 60 | return nullptr; |
| 61 | } |
| 62 | |
| 63 | SkColorType colorType = kUnknown_SkColorType; |
| 64 | if (!GrPixelConfigToColorType(texture->config(), &colorType)) { |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
| 68 | GrContext* context = texture->getContext(); |
| 69 | |
| 70 | // Attach our texture to this context's resource cache. This ensures that deletion will happen |
| 71 | // in the correct thread/context. This adds the only ref to the texture that will persist from |
| 72 | // this point. That ref will be released when the generator's RefHelper is freed. |
| 73 | context->getResourceCache()->insertCrossContextGpuResource(texture.get()); |
| 74 | |
| 75 | GrBackend backend = context->contextPriv().getBackend(); |
| 76 | GrBackendTexture backendTexture = make_backend_texture_from_handle(backend, |
| 77 | texture->width(), |
| 78 | texture->height(), |
| 79 | texture->config(), |
| 80 | texture->getTextureHandle()); |
| 81 | |
| 82 | SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType, |
| 83 | std::move(colorSpace)); |
| 84 | return std::unique_ptr<SkImageGenerator>(new GrBackendTextureImageGenerator( |
Robert Phillips | 7294b85 | 2017-08-01 13:51:44 +0000 | [diff] [blame] | 85 | info, texture.get(), context->uniqueID(), std::move(semaphore), backendTexture)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(const SkImageInfo& info, |
| 89 | GrTexture* texture, |
| 90 | uint32_t owningContextID, |
| 91 | sk_sp<GrSemaphore> semaphore, |
| 92 | const GrBackendTexture& backendTex) |
| 93 | : INHERITED(info) |
| 94 | , fRefHelper(new RefHelper(texture, owningContextID)) |
| 95 | , fSemaphore(std::move(semaphore)) |
| 96 | , fLastBorrowingContextID(SK_InvalidGenID) |
| 97 | , fBackendTexture(backendTex) |
Robert Phillips | 7294b85 | 2017-08-01 13:51:44 +0000 | [diff] [blame] | 98 | , fSurfaceOrigin(texture->origin()) { } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 99 | |
| 100 | GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() { |
| 101 | fRefHelper->unref(); |
| 102 | } |
| 103 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 104 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 105 | |
| 106 | #if SK_SUPPORT_GPU |
| 107 | void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) { |
| 108 | RefHelper* refHelper = static_cast<RefHelper*>(ctx); |
| 109 | SkASSERT(refHelper); |
| 110 | |
| 111 | // Release texture so another context can use it |
| 112 | refHelper->fBorrowedTexture = nullptr; |
| 113 | refHelper->fBorrowingContextID = SK_InvalidGenID; |
| 114 | refHelper->unref(); |
| 115 | } |
| 116 | |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 117 | sk_sp<GrTextureProxy> GrBackendTextureImageGenerator::onGenerateTexture( |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 118 | GrContext* context, const SkImageInfo& info, const SkIPoint& origin, |
| 119 | SkTransferFunctionBehavior) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 120 | SkASSERT(context); |
| 121 | |
| 122 | if (context->contextPriv().getBackend() != fBackendTexture.backend()) { |
| 123 | return nullptr; |
| 124 | } |
| 125 | |
| 126 | sk_sp<GrTexture> tex; |
| 127 | |
| 128 | if (fRefHelper->fBorrowingContextID == context->uniqueID()) { |
| 129 | // If a client re-draws the same image multiple times, the texture we return will be cached |
| 130 | // and re-used. If they draw a subset, though, we may be re-called. In that case, we want |
| 131 | // to re-use the borrowed texture we've previously created. |
| 132 | tex = sk_ref_sp(fRefHelper->fBorrowedTexture); |
| 133 | SkASSERT(tex); |
| 134 | } else { |
| 135 | // The texture is available or borrwed by another context. Try for exclusive access. |
| 136 | uint32_t expectedID = SK_InvalidGenID; |
| 137 | if (!fRefHelper->fBorrowingContextID.compare_exchange(&expectedID, context->uniqueID())) { |
| 138 | // Some other context is currently borrowing the texture. We aren't allowed to use it. |
| 139 | return nullptr; |
| 140 | } else { |
| 141 | // Wait on a semaphore when a new context has just started borrowing the texture. This |
| 142 | // is conservative, but shouldn't be too expensive. |
| 143 | if (fSemaphore && fLastBorrowingContextID != context->uniqueID()) { |
| 144 | context->getGpu()->waitSemaphore(fSemaphore); |
| 145 | fLastBorrowingContextID = context->uniqueID(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // We just gained access to the texture. If we're on the original context, we could use the |
| 150 | // original texture, but we'd have no way of detecting that it's no longer in-use. So we |
| 151 | // always make a wrapped copy, where the release proc informs us that the context is done |
| 152 | // with it. This is unfortunate - we'll have two texture objects referencing the same GPU |
| 153 | // object. However, no client can ever see the original texture, so this should be safe. |
Robert Phillips | 7294b85 | 2017-08-01 13:51:44 +0000 | [diff] [blame] | 154 | tex = context->resourceProvider()->wrapBackendTexture(fBackendTexture, fSurfaceOrigin, |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 155 | kBorrow_GrWrapOwnership); |
| 156 | if (!tex) { |
| 157 | fRefHelper->fBorrowingContextID = SK_InvalidGenID; |
| 158 | return nullptr; |
| 159 | } |
| 160 | fRefHelper->fBorrowedTexture = tex.get(); |
| 161 | |
| 162 | tex->setRelease(ReleaseRefHelper_TextureReleaseProc, fRefHelper); |
| 163 | fRefHelper->ref(); |
| 164 | } |
| 165 | |
| 166 | SkASSERT(fRefHelper->fBorrowingContextID == context->uniqueID()); |
| 167 | |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 168 | sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(tex), fSurfaceOrigin); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 169 | |
| 170 | if (0 == origin.fX && 0 == origin.fY && |
| 171 | info.width() == fBackendTexture.width() && info.height() == fBackendTexture.height()) { |
| 172 | // If the caller wants the entire texture, we're done |
| 173 | return proxy; |
| 174 | } else { |
| 175 | // Otherwise, make a copy of the requested subset. Make sure our temporary is renderable, |
| 176 | // because Vulkan will want to do the copy as a draw. |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 177 | GrSurfaceDesc desc; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 178 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 179 | desc.fOrigin = proxy->origin(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 180 | desc.fWidth = info.width(); |
| 181 | desc.fHeight = info.height(); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 182 | desc.fConfig = proxy->config(); |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 183 | desc.fIsMipMapped = proxy->isMipMapped(); |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 184 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 185 | sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeDeferredSurfaceContext( |
| 186 | desc, SkBackingFit::kExact, SkBudgeted::kYes)); |
| 187 | if (!sContext) { |
| 188 | return nullptr; |
| 189 | } |
| 190 | |
| 191 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height()); |
| 192 | if (!sContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) { |
| 193 | return nullptr; |
| 194 | } |
| 195 | |
| 196 | return sContext->asTextureProxyRef(); |
| 197 | } |
| 198 | } |
| 199 | #endif |