Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [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 | #include "ProxyUtils.h" |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 9 | #include "GrBackendSurface.h" |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 10 | #include "GrContextPriv.h" |
| 11 | #include "GrDrawingManager.h" |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 12 | #include "GrGpu.h" |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 13 | #include "GrProxyProvider.h" |
| 14 | |
| 15 | namespace sk_gpu_test { |
| 16 | |
| 17 | sk_sp<GrTextureProxy> MakeTextureProxyFromData(GrContext* context, bool isRT, int width, int height, |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 18 | GrColorType colorType, GrSRGBEncoded srgbEncoded, |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 19 | GrSurfaceOrigin origin, const void* data, |
| 20 | size_t rowBytes) { |
Khushal | c421ca1 | 2018-06-26 14:38:34 -0700 | [diff] [blame] | 21 | if (context->abandoned()) { |
Robert Phillips | 6d36370 | 2018-06-25 08:53:09 -0400 | [diff] [blame] | 22 | return nullptr; |
| 23 | } |
| 24 | |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 25 | sk_sp<GrTextureProxy> proxy; |
| 26 | if (kBottomLeft_GrSurfaceOrigin == origin) { |
| 27 | // We (soon will) only support using kBottomLeft with wrapped textures. |
| 28 | auto backendTex = context->contextPriv().getGpu()->createTestingOnlyBackendTexture( |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 29 | nullptr, width, height, colorType, isRT, GrMipMapped::kNo); |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 30 | if (!backendTex.isValid()) { |
| 31 | return nullptr; |
| 32 | } |
| 33 | // Adopt ownership so our caller doesn't have to worry about deleting the backend texture. |
| 34 | if (isRT) { |
| 35 | proxy = context->contextPriv().proxyProvider()->wrapRenderableBackendTexture( |
| 36 | backendTex, origin, 1, kAdopt_GrWrapOwnership); |
| 37 | } else { |
| 38 | proxy = context->contextPriv().proxyProvider()->wrapBackendTexture( |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame^] | 39 | backendTex, origin, kAdopt_GrWrapOwnership, kRW_GrIOType); |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | if (!proxy) { |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 43 | context->contextPriv().getGpu()->deleteTestingOnlyBackendTexture(backendTex); |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 44 | return nullptr; |
| 45 | } |
| 46 | |
| 47 | } else { |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 48 | GrPixelConfig config = GrColorTypeToPixelConfig(colorType, srgbEncoded); |
| 49 | if (!context->contextPriv().caps()->isConfigTexturable(config)) { |
| 50 | return nullptr; |
| 51 | } |
| 52 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 53 | const GrBackendFormat format = |
| 54 | context->contextPriv().caps()->getBackendFormatFromGrColorType(colorType, |
| 55 | srgbEncoded); |
| 56 | if (!format.isValid()) { |
| 57 | return nullptr; |
| 58 | } |
| 59 | |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 60 | GrSurfaceDesc desc; |
| 61 | desc.fConfig = config; |
| 62 | desc.fWidth = width; |
| 63 | desc.fHeight = height; |
| 64 | desc.fFlags = isRT ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
| 65 | proxy = context->contextPriv().proxyProvider()->createProxy( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 66 | format, desc, origin, SkBackingFit::kExact, SkBudgeted::kYes); |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 67 | if (!proxy) { |
| 68 | return nullptr; |
| 69 | } |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 70 | } |
| 71 | auto sContext = context->contextPriv().makeWrappedSurfaceContext(proxy, nullptr); |
| 72 | if (!sContext) { |
| 73 | return nullptr; |
| 74 | } |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 75 | if (!context->contextPriv().writeSurfacePixels(sContext.get(), 0, 0, width, height, colorType, |
| 76 | nullptr, data, rowBytes)) { |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 77 | return nullptr; |
| 78 | } |
| 79 | return proxy; |
| 80 | } |
| 81 | |
| 82 | } // namespace sk_gpu_test |