blob: 10bb6fec6a9d6c773684a9492cc7e5e4f8c5b332 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrBackendSurface.h"
9#include "src/gpu/GrContextPriv.h"
10#include "src/gpu/GrDrawingManager.h"
11#include "src/gpu/GrGpu.h"
12#include "src/gpu/GrProxyProvider.h"
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040013#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "tools/gpu/ProxyUtils.h"
Brian Salomon58389b92018-03-07 13:01:25 -050015
16namespace sk_gpu_test {
17
Brian Salomone7499c72019-06-24 12:12:36 -040018sk_sp<GrTextureProxy> MakeTextureProxyFromData(GrContext* context,
19 GrRenderable renderable,
20 int width,
21 int height,
22 GrColorType colorType, SkAlphaType alphaType,
23 GrSRGBEncoded srgbEncoded,
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040024 GrSurfaceOrigin origin,
25 const void* data, size_t rowBytes) {
Robert Phillips69893702019-02-22 11:16:30 -050026 if (context->priv().abandoned()) {
Robert Phillips6d363702018-06-25 08:53:09 -040027 return nullptr;
28 }
29
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040030 const GrCaps* caps = context->priv().caps();
31
32 const GrBackendFormat format = caps->getBackendFormatFromGrColorType(colorType, srgbEncoded);
33 if (!format.isValid()) {
34 return nullptr;
35 }
36
Brian Salomon02bd2952018-03-07 15:20:21 -050037 sk_sp<GrTextureProxy> proxy;
38 if (kBottomLeft_GrSurfaceOrigin == origin) {
39 // We (soon will) only support using kBottomLeft with wrapped textures.
Robert Phillips4bdd36f2019-06-04 11:03:06 -040040 auto backendTex = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -040041 width, height, format, SkColors::kTransparent, GrMipMapped::kNo, renderable);
Brian Salomon02bd2952018-03-07 15:20:21 -050042 if (!backendTex.isValid()) {
43 return nullptr;
44 }
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040045
Brian Salomon02bd2952018-03-07 15:20:21 -050046 // Adopt ownership so our caller doesn't have to worry about deleting the backend texture.
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040047 if (GrRenderable::kYes == renderable) {
Robert Phillips9da87e02019-02-04 13:26:26 -050048 proxy = context->priv().proxyProvider()->wrapRenderableBackendTexture(
Greg Daniel8ce79912019-02-05 10:08:43 -050049 backendTex, origin, 1, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, nullptr,
50 nullptr);
Brian Salomon02bd2952018-03-07 15:20:21 -050051 } else {
Robert Phillips9da87e02019-02-04 13:26:26 -050052 proxy = context->priv().proxyProvider()->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050053 backendTex, origin, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Brian Salomon02bd2952018-03-07 15:20:21 -050054 }
55
56 if (!proxy) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -040057 context->deleteBackendTexture(backendTex);
Brian Salomon02bd2952018-03-07 15:20:21 -050058 return nullptr;
59 }
60
61 } else {
Robert Phillips646f6372018-09-25 09:31:10 -040062 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, srgbEncoded);
Robert Phillips9da87e02019-02-04 13:26:26 -050063 if (!context->priv().caps()->isConfigTexturable(config)) {
Robert Phillips646f6372018-09-25 09:31:10 -040064 return nullptr;
65 }
66
Brian Salomon02bd2952018-03-07 15:20:21 -050067 GrSurfaceDesc desc;
68 desc.fConfig = config;
69 desc.fWidth = width;
70 desc.fHeight = height;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040071 desc.fFlags = GrRenderable::kYes == renderable ? kRenderTarget_GrSurfaceFlag
72 : kNone_GrSurfaceFlags;
Robert Phillips9da87e02019-02-04 13:26:26 -050073 proxy = context->priv().proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -050074 format, desc, origin, SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomon02bd2952018-03-07 15:20:21 -050075 if (!proxy) {
76 return nullptr;
77 }
Brian Salomon58389b92018-03-07 13:01:25 -050078 }
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040079
Brian Salomond6287472019-06-24 15:50:07 -040080 auto sContext = context->priv().makeWrappedSurfaceContext(proxy, colorType, alphaType, nullptr);
Brian Salomon58389b92018-03-07 13:01:25 -050081 if (!sContext) {
82 return nullptr;
83 }
Greg Daniel6eb8c242019-06-05 10:22:24 -040084 if (!sContext->writePixels(context, 0, 0, width, height, colorType, nullptr, data, rowBytes)) {
Brian Salomon58389b92018-03-07 13:01:25 -050085 return nullptr;
86 }
87 return proxy;
88}
89
90} // namespace sk_gpu_test