blob: 8167264fd47f4cf53fb0f1858ad35987d958ffb8 [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
Robert Phillips69893702019-02-22 11:16:30 -050017sk_sp<GrTextureProxy> MakeTextureProxyFromData(GrContext* context, bool isRT,
18 int width, int height,
Robert Phillips646f6372018-09-25 09:31:10 -040019 GrColorType colorType, GrSRGBEncoded srgbEncoded,
Brian Salomon58389b92018-03-07 13:01:25 -050020 GrSurfaceOrigin origin, const void* data,
21 size_t rowBytes) {
Robert Phillips69893702019-02-22 11:16:30 -050022 if (context->priv().abandoned()) {
Robert Phillips6d363702018-06-25 08:53:09 -040023 return nullptr;
24 }
25
Brian Salomon02bd2952018-03-07 15:20:21 -050026 sk_sp<GrTextureProxy> proxy;
27 if (kBottomLeft_GrSurfaceOrigin == origin) {
28 // We (soon will) only support using kBottomLeft with wrapped textures.
Robert Phillips9da87e02019-02-04 13:26:26 -050029 auto backendTex = context->priv().getGpu()->createTestingOnlyBackendTexture(
Robert Phillips646f6372018-09-25 09:31:10 -040030 nullptr, width, height, colorType, isRT, GrMipMapped::kNo);
Brian Salomon02bd2952018-03-07 15:20:21 -050031 if (!backendTex.isValid()) {
32 return nullptr;
33 }
34 // Adopt ownership so our caller doesn't have to worry about deleting the backend texture.
35 if (isRT) {
Robert Phillips9da87e02019-02-04 13:26:26 -050036 proxy = context->priv().proxyProvider()->wrapRenderableBackendTexture(
Greg Daniel8ce79912019-02-05 10:08:43 -050037 backendTex, origin, 1, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, nullptr,
38 nullptr);
Brian Salomon02bd2952018-03-07 15:20:21 -050039 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -050040 proxy = context->priv().proxyProvider()->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050041 backendTex, origin, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Brian Salomon02bd2952018-03-07 15:20:21 -050042 }
43
44 if (!proxy) {
Robert Phillips9da87e02019-02-04 13:26:26 -050045 context->priv().getGpu()->deleteTestingOnlyBackendTexture(backendTex);
Brian Salomon02bd2952018-03-07 15:20:21 -050046 return nullptr;
47 }
48
49 } else {
Robert Phillips646f6372018-09-25 09:31:10 -040050 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, srgbEncoded);
Robert Phillips9da87e02019-02-04 13:26:26 -050051 if (!context->priv().caps()->isConfigTexturable(config)) {
Robert Phillips646f6372018-09-25 09:31:10 -040052 return nullptr;
53 }
54
Greg Daniel4065d452018-11-16 15:43:41 -050055 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -050056 context->priv().caps()->getBackendFormatFromGrColorType(colorType,
57 srgbEncoded);
Greg Daniel4065d452018-11-16 15:43:41 -050058 if (!format.isValid()) {
59 return nullptr;
60 }
61
Brian Salomon02bd2952018-03-07 15:20:21 -050062 GrSurfaceDesc desc;
63 desc.fConfig = config;
64 desc.fWidth = width;
65 desc.fHeight = height;
66 desc.fFlags = isRT ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Robert Phillips9da87e02019-02-04 13:26:26 -050067 proxy = context->priv().proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -050068 format, desc, origin, SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomon02bd2952018-03-07 15:20:21 -050069 if (!proxy) {
70 return nullptr;
71 }
Brian Salomon58389b92018-03-07 13:01:25 -050072 }
Robert Phillips9da87e02019-02-04 13:26:26 -050073 auto sContext = context->priv().makeWrappedSurfaceContext(proxy, nullptr);
Brian Salomon58389b92018-03-07 13:01:25 -050074 if (!sContext) {
75 return nullptr;
76 }
Robert Phillips9da87e02019-02-04 13:26:26 -050077 if (!context->priv().writeSurfacePixels(sContext.get(), 0, 0, width, height, colorType,
78 nullptr, data, rowBytes)) {
Brian Salomon58389b92018-03-07 13:01:25 -050079 return nullptr;
80 }
81 return proxy;
82}
83
84} // namespace sk_gpu_test