blob: b0faf45d70b7d1192fae27c79b1824a2508d736d [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);
Adlai Holler29405382020-07-20 16:02:05 -040034 return SkImage::MakeFromTexture(dContext,
Brian Salomon832936b2020-07-01 12:12:04 -040035 mbet->texture(),
36 origin,
37 src->colorType(),
38 src->alphaType(),
39 src->refColorSpace(),
Brian Salomonf9b00422020-10-08 16:00:14 -040040 ManagedBackendTexture::ReleaseProc,
41 mbet->releaseContext());
Brian Salomon63a0a752020-06-26 13:32:09 -040042}
43} // namespace sk_gpu_test