1/*
2 * Copyright 2020 Google LLC
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 "tools/gpu/BackendTextureImageFactory.h"
9
10#include "include/core/SkImage.h"
11#include "include/core/SkPixmap.h"
12#include "include/gpu/GrBackendSurface.h"
13#include "include/gpu/GrDirectContext.h"
14#include "src/core/SkAutoPixmapStorage.h"
15#include "tools/gpu/ManagedBackendTexture.h"
16
17namespace sk_gpu_test {
18sk_sp<SkImage> MakeBackendTextureImage(GrDirectContext* dContext,
19                                       const SkPixmap& pixmap,
20                                       GrRenderable renderable,
21                                       GrSurfaceOrigin origin) {
22    auto mbet = ManagedBackendTexture::MakeWithData(dContext,
23                                                    pixmap,
24                                                    origin,
25                                                    renderable,
26                                                    GrProtected::kNo);
27    if (!mbet) {
28        return nullptr;
29    }
30    return SkImage::MakeFromTexture(dContext,
31                                    mbet->texture(),
32                                    origin,
33                                    pixmap.colorType(),
34                                    pixmap.alphaType(),
35                                    pixmap.refColorSpace(),
36                                    ManagedBackendTexture::ReleaseProc,
37                                    mbet->releaseContext());
38}
39
40sk_sp<SkImage> MakeBackendTextureImage(GrDirectContext* dContext,
41                                       const SkImageInfo& info,
42                                       SkColor4f color,
43                                       GrMipmapped mipmapped,
44                                       GrRenderable renderable,
45                                       GrSurfaceOrigin origin) {
46    if (info.alphaType() == kOpaque_SkAlphaType) {
47        color = color.makeOpaque();
48    } else if (info.alphaType() == kPremul_SkAlphaType) {
49        auto pmColor = color.premul();
50        color = {pmColor.fR, pmColor.fG, pmColor.fB, pmColor.fA};
51    }
52    auto mbet = ManagedBackendTexture::MakeWithData(dContext,
53                                                    info.width(),
54                                                    info.height(),
55                                                    info.colorType(),
56                                                    color,
57                                                    mipmapped,
58                                                    renderable,
59                                                    GrProtected::kNo);
60    if (!mbet) {
61        return nullptr;
62    }
63    return SkImage::MakeFromTexture(dContext,
64                                    mbet->texture(),
65                                    origin,
66                                    info.colorType(),
67                                    info.alphaType(),
68                                    info.refColorSpace(),
69                                    ManagedBackendTexture::ReleaseProc,
70                                    mbet->releaseContext());
71}
72
73}  // namespace sk_gpu_test
74