blob: 700f64f5b5d931d640fe79a10198c66ea05e616e [file] [log] [blame]
Brian Salomon63a0a752020-06-26 13:32:09 -04001/*
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"
Adlai Holler29405382020-07-20 16:02:05 -040013#include "include/gpu/GrDirectContext.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040014#include "src/core/SkAutoPixmapStorage.h"
Brian Salomonf9b00422020-10-08 16:00:14 -040015#include "tools/gpu/ManagedBackendTexture.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040016
17namespace sk_gpu_test {
Adlai Holler29405382020-07-20 16:02:05 -040018sk_sp<SkImage> MakeBackendTextureImage(GrDirectContext* dContext,
Brian Salomon63a0a752020-06-26 13:32:09 -040019 const SkPixmap& pixmap,
20 GrRenderable renderable,
21 GrSurfaceOrigin origin) {
22 const SkPixmap* src = &pixmap;
23 SkAutoPixmapStorage temp;
24 if (origin == kBottomLeft_GrSurfaceOrigin) {
25 temp.alloc(src->info());
26 auto s = static_cast<const char*>(src->addr(0, pixmap.height() - 1));
27 auto d = static_cast<char*>(temp.writable_addr(0, 0));
28 for (int y = 0; y < temp.height(); ++y, s -= pixmap.rowBytes(), d += temp.rowBytes()) {
29 std::copy_n(s, temp.info().minRowBytes(), d);
30 }
31 src = &temp;
32 }
Brian Salomonf9b00422020-10-08 16:00:14 -040033 auto mbet = ManagedBackendTexture::MakeWithData(dContext, src, 1, renderable, GrProtected::kNo);
Brian Salomon88d7e622020-11-05 11:11:20 -050034 if (!mbet) {
35 return nullptr;
36 }
Adlai Holler29405382020-07-20 16:02:05 -040037 return SkImage::MakeFromTexture(dContext,
Brian Salomon832936b2020-07-01 12:12:04 -040038 mbet->texture(),
39 origin,
40 src->colorType(),
41 src->alphaType(),
42 src->refColorSpace(),
Brian Salomonf9b00422020-10-08 16:00:14 -040043 ManagedBackendTexture::ReleaseProc,
44 mbet->releaseContext());
Brian Salomon63a0a752020-06-26 13:32:09 -040045}
Brian Salomon72050802020-10-12 20:45:06 -040046
47sk_sp<SkImage> MakeBackendTextureImage(GrDirectContext* dContext,
48 const SkImageInfo& info,
49 SkColor4f color,
50 GrMipmapped mipmapped,
51 GrRenderable renderable,
52 GrSurfaceOrigin origin) {
53 if (info.alphaType() == kOpaque_SkAlphaType) {
54 color = color.makeOpaque();
55 } else if (info.alphaType() == kPremul_SkAlphaType) {
56 auto pmColor = color.premul();
57 color = {pmColor.fR, pmColor.fG, pmColor.fB, pmColor.fA};
58 }
59 auto mbet = ManagedBackendTexture::MakeWithData(dContext,
60 info.width(),
61 info.height(),
62 info.colorType(),
63 color,
64 mipmapped,
65 renderable,
66 GrProtected::kNo);
Brian Salomon88d7e622020-11-05 11:11:20 -050067 if (!mbet) {
68 return nullptr;
69 }
Brian Salomon72050802020-10-12 20:45:06 -040070 return SkImage::MakeFromTexture(dContext,
71 mbet->texture(),
72 origin,
73 info.colorType(),
74 info.alphaType(),
75 info.refColorSpace(),
76 ManagedBackendTexture::ReleaseProc,
77 mbet->releaseContext());
78}
79
Brian Salomon63a0a752020-06-26 13:32:09 -040080} // namespace sk_gpu_test