skia2/tools/gpu/BackendTextureImageFactory.cpp
Adlai Holler 2940538ecd Reland "Remove more GrContext imports & usage"
This reverts commit 75c5168b41.

Reason for revert: Reverted image-cacherator test, for now

Original change's description:
> Revert "Remove more GrContext imports & usage"
>
> This reverts commit dd1395526d.
>
> Reason for revert: Broke chrome roll
>
> Original change's description:
> > Remove more GrContext imports & usage
> >
> > Sanity is coming soon!
> >
> > Change-Id: I109ebeef9efd7dbf4d76a13e1c05df36d59affbc
> > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/303661
> > Commit-Queue: Adlai Holler <adlai@google.com>
> > Reviewed-by: Robert Phillips <robertphillips@google.com>
>
> TBR=robertphillips@google.com,adlai@google.com
>
> Change-Id: I20d770058d4b54193b6cd2fdc9ca5a1e09f84309
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/304056
> Reviewed-by: Adlai Holler <adlai@google.com>
> Commit-Queue: Adlai Holler <adlai@google.com>

TBR=robertphillips@google.com,adlai@google.com


Change-Id: I940b9f74f7caaa8b4201c241f2a6242b7a24d2a4
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/304062
Reviewed-by: Robert Phillips <robertphillips@google.com>
Commit-Queue: Adlai Holler <adlai@google.com>
2020-07-21 11:36:05 +00:00

78 lines
2.8 KiB
C++

/*
* Copyright 2020 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "tools/gpu/BackendTextureImageFactory.h"
#include "include/core/SkImage.h"
#include "include/core/SkPixmap.h"
#include "include/gpu/GrBackendSurface.h"
#include "include/gpu/GrDirectContext.h"
#include "src/core/SkAutoPixmapStorage.h"
namespace {
class ManagedBackendTexture : public SkNVRefCnt<ManagedBackendTexture> {
public:
~ManagedBackendTexture() {
if (fDContext && fTexture.isValid()) {
fDContext->submit(true);
fDContext->deleteBackendTexture(fTexture);
}
}
static void Release(void* context) { static_cast<ManagedBackendTexture*>(context)->unref(); }
template <typename... Args>
static sk_sp<ManagedBackendTexture> Make(GrDirectContext* dContext, Args&&... args) {
sk_sp<ManagedBackendTexture> mbet(new ManagedBackendTexture);
mbet->fDContext = dContext;
mbet->fTexture = dContext->createBackendTexture(std::forward<Args>(args)..., Release,
mbet->refAndPassAsContext());
return mbet;
}
const GrBackendTexture& texture() { return fTexture; }
void* refAndPassAsContext() {
this->ref();
return static_cast<void*>(this);
}
private:
ManagedBackendTexture() = default;
GrDirectContext* fDContext = nullptr;
GrBackendTexture fTexture;
};
} // namespace
namespace sk_gpu_test {
sk_sp<SkImage> MakeBackendTextureImage(GrDirectContext* dContext,
const SkPixmap& pixmap,
GrRenderable renderable,
GrSurfaceOrigin origin) {
const SkPixmap* src = &pixmap;
SkAutoPixmapStorage temp;
if (origin == kBottomLeft_GrSurfaceOrigin) {
temp.alloc(src->info());
auto s = static_cast<const char*>(src->addr(0, pixmap.height() - 1));
auto d = static_cast<char*>(temp.writable_addr(0, 0));
for (int y = 0; y < temp.height(); ++y, s -= pixmap.rowBytes(), d += temp.rowBytes()) {
std::copy_n(s, temp.info().minRowBytes(), d);
}
src = &temp;
}
auto mbet = ManagedBackendTexture::Make(dContext, src, 1, renderable, GrProtected::kNo);
return SkImage::MakeFromTexture(dContext,
mbet->texture(),
origin,
src->colorType(),
src->alphaType(),
src->refColorSpace(),
ManagedBackendTexture::Release,
mbet->refAndPassAsContext());
}
} // namespace sk_gpu_test