blob: 9e77a91ee6691000c51b32d834a994dd1e770638 [file] [log] [blame]
Brian Salomon58389b92018-03-07 13:01:25 -05001/*
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 Salomon02bd2952018-03-07 15:20:21 -05009#include "GrBackendSurface.h"
Brian Salomon58389b92018-03-07 13:01:25 -050010#include "GrContextPriv.h"
11#include "GrDrawingManager.h"
Brian Salomon02bd2952018-03-07 15:20:21 -050012#include "GrGpu.h"
Brian Salomon58389b92018-03-07 13:01:25 -050013#include "GrProxyProvider.h"
14
15namespace sk_gpu_test {
16
17sk_sp<GrTextureProxy> MakeTextureProxyFromData(GrContext* context, bool isRT, int width, int height,
Robert Phillips646f6372018-09-25 09:31:10 -040018 GrColorType colorType, GrSRGBEncoded srgbEncoded,
Brian Salomon58389b92018-03-07 13:01:25 -050019 GrSurfaceOrigin origin, const void* data,
20 size_t rowBytes) {
Khushalc421ca12018-06-26 14:38:34 -070021 if (context->abandoned()) {
Robert Phillips6d363702018-06-25 08:53:09 -040022 return nullptr;
23 }
24
Brian Salomon02bd2952018-03-07 15:20:21 -050025 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 Phillips646f6372018-09-25 09:31:10 -040029 nullptr, width, height, colorType, isRT, GrMipMapped::kNo);
Brian Salomon02bd2952018-03-07 15:20:21 -050030 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(
39 backendTex, origin, kAdopt_GrWrapOwnership);
40 }
41
42 if (!proxy) {
Brian Salomon26102cb2018-03-09 09:33:19 -050043 context->contextPriv().getGpu()->deleteTestingOnlyBackendTexture(backendTex);
Brian Salomon02bd2952018-03-07 15:20:21 -050044 return nullptr;
45 }
46
47 } else {
Robert Phillips646f6372018-09-25 09:31:10 -040048 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, srgbEncoded);
49 if (!context->contextPriv().caps()->isConfigTexturable(config)) {
50 return nullptr;
51 }
52
Brian Salomon02bd2952018-03-07 15:20:21 -050053 GrSurfaceDesc desc;
54 desc.fConfig = config;
55 desc.fWidth = width;
56 desc.fHeight = height;
57 desc.fFlags = isRT ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
58 proxy = context->contextPriv().proxyProvider()->createProxy(
59 desc, origin, SkBackingFit::kExact, SkBudgeted::kYes);
60 if (!proxy) {
61 return nullptr;
62 }
Brian Salomon58389b92018-03-07 13:01:25 -050063 }
64 auto sContext = context->contextPriv().makeWrappedSurfaceContext(proxy, nullptr);
65 if (!sContext) {
66 return nullptr;
67 }
Robert Phillips646f6372018-09-25 09:31:10 -040068 if (!context->contextPriv().writeSurfacePixels(sContext.get(), 0, 0, width, height, colorType,
69 nullptr, data, rowBytes)) {
Brian Salomon58389b92018-03-07 13:01:25 -050070 return nullptr;
71 }
72 return proxy;
73}
74
75} // namespace sk_gpu_test