blob: 82e69216f9ae5a7f909b9330daf2018b6eb32945 [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 Holler75c51682020-07-20 16:17:55 +000013#include "include/gpu/GrContext.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040014#include "src/core/SkAutoPixmapStorage.h"
15
16namespace {
17class ManagedBackendTexture : public SkNVRefCnt<ManagedBackendTexture> {
18public:
19 ~ManagedBackendTexture() {
Adlai Holler75c51682020-07-20 16:17:55 +000020 if (fContext && fTexture.isValid()) {
21 fContext->submit(true);
22 fContext->deleteBackendTexture(fTexture);
Brian Salomon63a0a752020-06-26 13:32:09 -040023 }
24 }
25
Brian Salomon832936b2020-07-01 12:12:04 -040026 static void Release(void* context) { static_cast<ManagedBackendTexture*>(context)->unref(); }
Brian Salomon63a0a752020-06-26 13:32:09 -040027
28 template <typename... Args>
Adlai Holler75c51682020-07-20 16:17:55 +000029 static sk_sp<ManagedBackendTexture> Make(GrContext* context, Args&&... args) {
Brian Salomon63a0a752020-06-26 13:32:09 -040030 sk_sp<ManagedBackendTexture> mbet(new ManagedBackendTexture);
Adlai Holler75c51682020-07-20 16:17:55 +000031 mbet->fContext = context;
32 mbet->fTexture = context->createBackendTexture(std::forward<Args>(args)..., Release,
33 mbet->refAndPassAsContext());
Brian Salomon63a0a752020-06-26 13:32:09 -040034 return mbet;
35 }
36
37 const GrBackendTexture& texture() { return fTexture; }
38
39 void* refAndPassAsContext() {
40 this->ref();
41 return static_cast<void*>(this);
42 }
43
44private:
45 ManagedBackendTexture() = default;
Adlai Holler75c51682020-07-20 16:17:55 +000046 GrContext* fContext = nullptr;
Brian Salomon63a0a752020-06-26 13:32:09 -040047 GrBackendTexture fTexture;
48};
49} // namespace
50
51namespace sk_gpu_test {
Adlai Holler75c51682020-07-20 16:17:55 +000052sk_sp<SkImage> MakeBackendTextureImage(GrContext* context,
Brian Salomon63a0a752020-06-26 13:32:09 -040053 const SkPixmap& pixmap,
54 GrRenderable renderable,
55 GrSurfaceOrigin origin) {
56 const SkPixmap* src = &pixmap;
57 SkAutoPixmapStorage temp;
58 if (origin == kBottomLeft_GrSurfaceOrigin) {
59 temp.alloc(src->info());
60 auto s = static_cast<const char*>(src->addr(0, pixmap.height() - 1));
61 auto d = static_cast<char*>(temp.writable_addr(0, 0));
62 for (int y = 0; y < temp.height(); ++y, s -= pixmap.rowBytes(), d += temp.rowBytes()) {
63 std::copy_n(s, temp.info().minRowBytes(), d);
64 }
65 src = &temp;
66 }
Adlai Holler75c51682020-07-20 16:17:55 +000067 auto mbet = ManagedBackendTexture::Make(context, src, 1, renderable, GrProtected::kNo);
68 return SkImage::MakeFromTexture(context,
Brian Salomon832936b2020-07-01 12:12:04 -040069 mbet->texture(),
70 origin,
71 src->colorType(),
72 src->alphaType(),
73 src->refColorSpace(),
74 ManagedBackendTexture::Release,
75 mbet->refAndPassAsContext());
Brian Salomon63a0a752020-06-26 13:32:09 -040076}
77} // namespace sk_gpu_test