| 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/SkColorSpace.h" |
| 11 |
#include "include/core/SkImage.h" |
| 12 |
#include "include/core/SkPixmap.h" |
| 13 |
#include "src/core/SkAutoPixmapStorage.h" |
| 14 |
#include "tools/gpu/ManagedBackendTexture.h" |
| 15 |
|
| 16 |
#ifdef SK_GANESH |
| 17 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 18 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 19 |
#include "include/gpu/ganesh/SkImageGanesh.h" |
| 20 |
#endif |
| 21 |
|
| 22 |
#ifdef SK_GRAPHITE |
| 23 |
#include "include/core/SkBitmap.h" |
| 24 |
#include "include/gpu/graphite/Image.h" |
| 25 |
#include "include/gpu/graphite/Recorder.h" |
| 26 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
namespace sk_gpu_test { |
| 30 |
#ifdef SK_GANESH |
| 31 |
sk_sp<SkImage> MakeBackendTextureImage(GrDirectContext* dContext, |
| 32 |
const SkPixmap& pixmap, |
| 33 |
Renderable renderable, |
| 34 |
GrSurfaceOrigin origin, |
| 35 |
Protected isProtected) { |
| 36 |
auto mbet = ManagedBackendTexture::MakeWithData(dContext, |
| 37 |
pixmap, |
| 38 |
origin, |
| 39 |
renderable, |
| 40 |
isProtected); |
| 41 |
if (!mbet) { |
| 42 |
return nullptr; |
| 43 |
} |
| 44 |
return SkImages::BorrowTextureFrom(dContext, |
| 45 |
mbet->texture(), |
| 46 |
origin, |
| 47 |
pixmap.colorType(), |
| 48 |
pixmap.alphaType(), |
| 49 |
pixmap.refColorSpace(), |
| 50 |
ManagedBackendTexture::ReleaseProc, |
| 51 |
mbet->releaseContext()); |
| 52 |
} |
| 53 |
|
| 54 |
sk_sp<SkImage> MakeBackendTextureImage(GrDirectContext* dContext, |
| 55 |
const SkImageInfo& info, |
| 56 |
SkColor4f color, |
| 57 |
Mipmapped mipmapped, |
| 58 |
Renderable renderable, |
| 59 |
GrSurfaceOrigin origin, |
| 60 |
Protected isProtected) { |
| 61 |
if (info.alphaType() == kOpaque_SkAlphaType) { |
| 62 |
color = color.makeOpaque(); |
| 63 |
} else if (info.alphaType() == kPremul_SkAlphaType) { |
| 64 |
auto pmColor = color.premul(); |
| 65 |
color = {pmColor.fR, pmColor.fG, pmColor.fB, pmColor.fA}; |
| 66 |
} |
| 67 |
auto mbet = ManagedBackendTexture::MakeWithData(dContext, |
| 68 |
info.width(), |
| 69 |
info.height(), |
| 70 |
info.colorType(), |
| 71 |
color, |
| 72 |
mipmapped, |
| 73 |
renderable, |
| 74 |
isProtected); |
| 75 |
if (!mbet) { |
| 76 |
return nullptr; |
| 77 |
} |
| 78 |
return SkImages::BorrowTextureFrom(dContext, |
| 79 |
mbet->texture(), |
| 80 |
origin, |
| 81 |
info.colorType(), |
| 82 |
info.alphaType(), |
| 83 |
info.refColorSpace(), |
| 84 |
ManagedBackendTexture::ReleaseProc, |
| 85 |
mbet->releaseContext()); |
| 86 |
} |
| 87 |
#endif // SK_GANESH |
| 88 |
|
| 89 |
#ifdef SK_GRAPHITE |
| 90 |
using Recorder = skgpu::graphite::Recorder; |
| 91 |
sk_sp<SkImage> MakeBackendTextureImage(Recorder* recorder, |
| 92 |
const SkPixmap& pixmap, |
| 93 |
skgpu::Mipmapped isMipmapped, |
| 94 |
Renderable isRenderable, |
| 95 |
Origin origin, |
| 96 |
Protected isProtected) { |
| 97 |
auto mbet = ManagedGraphiteTexture::MakeFromPixmap(recorder, |
| 98 |
pixmap, |
| 99 |
isMipmapped, |
| 100 |
isRenderable, |
| 101 |
isProtected); |
| 102 |
if (!mbet) { |
| 103 |
return nullptr; |
| 104 |
} |
| 105 |
|
| 106 |
return SkImages::WrapTexture(recorder, |
| 107 |
mbet->texture(), |
| 108 |
pixmap.colorType(), |
| 109 |
pixmap.alphaType(), |
| 110 |
pixmap.refColorSpace(), |
| 111 |
origin, |
| 112 |
sk_gpu_test::ManagedGraphiteTexture::ImageReleaseProc, |
| 113 |
mbet->releaseContext()); |
| 114 |
} |
| 115 |
|
| 116 |
sk_sp<SkImage> MakeBackendTextureImage(Recorder* recorder, |
| 117 |
const SkImageInfo& ii, |
| 118 |
SkColor4f color, |
| 119 |
skgpu::Mipmapped isMipmapped, |
| 120 |
Renderable isRenderable, |
| 121 |
Origin origin, |
| 122 |
Protected isProtected) { |
| 123 |
if (ii.alphaType() == kOpaque_SkAlphaType) { |
| 124 |
color = color.makeOpaque(); |
| 125 |
} |
| 126 |
|
| 127 |
SkBitmap bitmap; |
| 128 |
bitmap.allocPixels(ii); |
| 129 |
|
| 130 |
bitmap.eraseColor(color); |
| 131 |
|
| 132 |
return MakeBackendTextureImage(recorder, bitmap.pixmap(), isMipmapped, isRenderable, |
| 133 |
origin, isProtected); |
| 134 |
} |
| 135 |
#endif // SK_GRAPHITE |
| 136 |
|
| 137 |
} // namespace sk_gpu_test |