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 "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"
13 #include "src/gpu/SkGr.h"
14 #include "tools/gpu/ProxyUtils.h"
15
16 namespace sk_gpu_test {
17
MakeTextureProxyFromData(GrContext * context,GrRenderable renderable,int width,int height,GrColorType colorType,SkAlphaType alphaType,GrSurfaceOrigin origin,const void * data,size_t rowBytes)18 sk_sp<GrTextureProxy> MakeTextureProxyFromData(GrContext* context,
19 GrRenderable renderable,
20 int width,
21 int height,
22 GrColorType colorType, SkAlphaType alphaType,
23 GrSurfaceOrigin origin,
24 const void* data, size_t rowBytes) {
25 if (context->priv().abandoned()) {
26 return nullptr;
27 }
28
29 const GrCaps* caps = context->priv().caps();
30
31 const GrBackendFormat format = caps->getDefaultBackendFormat(colorType, renderable);
32 if (!format.isValid()) {
33 return nullptr;
34 }
35
36 sk_sp<GrTextureProxy> proxy;
37 if (kBottomLeft_GrSurfaceOrigin == origin) {
38 // We (soon will) only support using kBottomLeft with wrapped textures.
39 auto backendTex = context->createBackendTexture(
40 width, height, format, SkColors::kTransparent, GrMipMapped::kNo, renderable,
41 GrProtected::kNo);
42 if (!backendTex.isValid()) {
43 return nullptr;
44 }
45
46 // Adopt ownership so our caller doesn't have to worry about deleting the backend texture.
47 if (GrRenderable::kYes == renderable) {
48 proxy = context->priv().proxyProvider()->wrapRenderableBackendTexture(
49 backendTex, origin, 1, colorType, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo,
50 nullptr, nullptr);
51 } else {
52 proxy = context->priv().proxyProvider()->wrapBackendTexture(
53 backendTex, colorType, origin, kAdopt_GrWrapOwnership,
54 GrWrapCacheable::kNo, kRW_GrIOType);
55 }
56
57 if (!proxy) {
58 context->deleteBackendTexture(backendTex);
59 return nullptr;
60 }
61
62 } else {
63 GrSurfaceDesc desc;
64 desc.fConfig = GrColorTypeToPixelConfig(colorType);
65 desc.fWidth = width;
66 desc.fHeight = height;
67 proxy = context->priv().proxyProvider()->createProxy(format, desc, renderable, 1, origin,
68 SkBackingFit::kExact, SkBudgeted::kYes,
69 GrProtected::kNo);
70 if (!proxy) {
71 return nullptr;
72 }
73 }
74
75 auto sContext = context->priv().makeWrappedSurfaceContext(proxy, colorType, alphaType, nullptr);
76 if (!sContext) {
77 return nullptr;
78 }
79 if (!sContext->writePixels({colorType, alphaType, nullptr, width, height}, data, rowBytes,
80 {0, 0}, context)) {
81 return nullptr;
82 }
83 return proxy;
84 }
85
86 } // namespace sk_gpu_test
87