robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file |
| 6 | */ |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 7 | |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 8 | #include "SkSpecialImage.h" |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 9 | #include "SkBitmap.h" |
| 10 | #include "SkImage.h" |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 11 | #include "SkBitmapCache.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 12 | #include "SkCanvas.h" |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 13 | #include "SkImage_Base.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 14 | #include "SkSpecialSurface.h" |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 15 | #include "SkSurfacePriv.h" |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 16 | #include "SkPixelRef.h" |
| 17 | |
| 18 | #if SK_SUPPORT_GPU |
| 19 | #include "GrContext.h" |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 20 | #include "GrSurfaceContext.h" |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 21 | #include "GrTexture.h" |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 22 | #include "GrSamplerParams.h" |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 23 | #include "GrTextureProxy.h" |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 24 | #include "SkGr.h" |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 25 | #include "SkGrPriv.h" |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 26 | #include "SkImage_Gpu.h" |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 27 | #endif |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 28 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 29 | // Currently the raster imagefilters can only handle certain imageinfos. Call this to know if |
| 30 | // a given info is supported. |
| 31 | static bool valid_for_imagefilters(const SkImageInfo& info) { |
| 32 | // no support for other swizzles/depths yet |
| 33 | return info.colorType() == kN32_SkColorType; |
| 34 | } |
| 35 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | class SkSpecialImage_Base : public SkSpecialImage { |
| 38 | public: |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 39 | SkSpecialImage_Base(const SkIRect& subset, uint32_t uniqueID, const SkSurfaceProps* props) |
| 40 | : INHERITED(subset, uniqueID, props) { |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 41 | } |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 42 | ~SkSpecialImage_Base() override { } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 43 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 44 | virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 45 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 46 | virtual bool onGetROPixels(SkBitmap*) const = 0; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 47 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 48 | virtual GrContext* onGetContext() const { return nullptr; } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 49 | |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 50 | virtual SkColorSpace* onGetColorSpace() const = 0; |
| 51 | |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 52 | #if SK_SUPPORT_GPU |
Robert Phillips | 696b293 | 2017-02-08 12:49:00 +0000 | [diff] [blame] | 53 | virtual sk_sp<GrTexture> onAsTextureRef(GrContext* context) const = 0; |
| 54 | virtual sk_sp<GrTextureProxy> onAsTextureProxy(GrContext* context) const = 0; |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 55 | #endif |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 56 | |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 57 | virtual sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const = 0; |
| 58 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 59 | virtual sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps, |
| 60 | const SkISize& size, SkAlphaType at) const = 0; |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 61 | |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 62 | virtual sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const = 0; |
| 63 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 64 | virtual sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps, |
| 65 | const SkISize& size, SkAlphaType at) const = 0; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 66 | |
| 67 | private: |
| 68 | typedef SkSpecialImage INHERITED; |
| 69 | }; |
| 70 | |
| 71 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 72 | static inline const SkSpecialImage_Base* as_SIB(const SkSpecialImage* image) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 73 | return static_cast<const SkSpecialImage_Base*>(image); |
| 74 | } |
| 75 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 76 | SkSpecialImage::SkSpecialImage(const SkIRect& subset, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 77 | uint32_t uniqueID, |
| 78 | const SkSurfaceProps* props) |
| 79 | : fProps(SkSurfacePropsCopyOrDefault(props)) |
| 80 | , fSubset(subset) |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 81 | , fUniqueID(kNeedNewImageUniqueID_SpecialImage == uniqueID ? SkNextID::ImageID() : uniqueID) { |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 82 | } |
| 83 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 84 | sk_sp<SkSpecialImage> SkSpecialImage::makeTextureImage(GrContext* context) { |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 85 | #if SK_SUPPORT_GPU |
| 86 | if (!context) { |
| 87 | return nullptr; |
| 88 | } |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 89 | if (GrContext* curContext = as_SIB(this)->onGetContext()) { |
| 90 | return curContext == context ? sk_sp<SkSpecialImage>(SkRef(this)) : nullptr; |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | SkBitmap bmp; |
reed | cf5c846 | 2016-07-20 12:28:40 -0700 | [diff] [blame] | 94 | // At this point, we are definitely not texture-backed, so we must be raster or generator |
| 95 | // backed. If we remove the special-wrapping-an-image subclass, we may be able to assert that |
| 96 | // we are strictly raster-backed (i.e. generator images become raster when they are specialized) |
| 97 | // in which case getROPixels could turn into peekPixels... |
| 98 | if (!this->getROPixels(&bmp)) { |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 99 | return nullptr; |
| 100 | } |
| 101 | |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 102 | if (bmp.empty()) { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 103 | return SkSpecialImage::MakeFromRaster(SkIRect::MakeEmpty(), bmp, &this->props()); |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 106 | // TODO: this is a tight copy of 'bmp' but it doesn't have to be (given SkSpecialImage's |
| 107 | // semantics). Since this is cached though we would have to bake the fit into the cache key. |
| 108 | sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(context, bmp); |
| 109 | if (!proxy) { |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 110 | return nullptr; |
| 111 | } |
| 112 | |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 113 | const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height()); |
| 114 | |
| 115 | // GrMakeCachedBitmapProxy has uploaded only the specified subset of 'bmp' so we need not |
| 116 | // bother with SkBitmap::getSubset |
| 117 | return SkSpecialImage::MakeDeferredFromGpu(context, |
| 118 | rect, |
| 119 | this->uniqueID(), |
| 120 | std::move(proxy), |
| 121 | sk_ref_sp(this->getColorSpace()), |
| 122 | &this->props(), |
| 123 | this->alphaType()); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 124 | #else |
| 125 | return nullptr; |
| 126 | #endif |
| 127 | } |
| 128 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 129 | void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 130 | return as_SIB(this)->onDraw(canvas, x, y, paint); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 131 | } |
| 132 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 133 | bool SkSpecialImage::getROPixels(SkBitmap* bm) const { |
| 134 | return as_SIB(this)->onGetROPixels(bm); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 135 | } |
| 136 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 137 | bool SkSpecialImage::isTextureBacked() const { |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 138 | return SkToBool(as_SIB(this)->onGetContext()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 139 | } |
| 140 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 141 | GrContext* SkSpecialImage::getContext() const { |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 142 | return as_SIB(this)->onGetContext(); |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 143 | } |
| 144 | |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 145 | SkColorSpace* SkSpecialImage::getColorSpace() const { |
| 146 | return as_SIB(this)->onGetColorSpace(); |
| 147 | } |
| 148 | |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 149 | #if SK_SUPPORT_GPU |
Robert Phillips | 696b293 | 2017-02-08 12:49:00 +0000 | [diff] [blame] | 150 | sk_sp<GrTexture> SkSpecialImage::asTextureRef(GrContext* context) const { |
| 151 | return as_SIB(this)->onAsTextureRef(context); |
| 152 | } |
| 153 | |
| 154 | sk_sp<GrTextureProxy> SkSpecialImage::asTextureProxy(GrContext* context) const { |
| 155 | return as_SIB(this)->onAsTextureProxy(context); |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 156 | } |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 157 | #endif |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 158 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 159 | sk_sp<SkSpecialSurface> SkSpecialImage::makeSurface(const SkImageFilter::OutputProperties& outProps, |
| 160 | const SkISize& size, SkAlphaType at) const { |
| 161 | return as_SIB(this)->onMakeSurface(outProps, size, at); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 162 | } |
| 163 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 164 | sk_sp<SkSurface> SkSpecialImage::makeTightSurface(const SkImageFilter::OutputProperties& outProps, |
| 165 | const SkISize& size, SkAlphaType at) const { |
| 166 | return as_SIB(this)->onMakeTightSurface(outProps, size, at); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 167 | } |
| 168 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 169 | sk_sp<SkSpecialImage> SkSpecialImage::makeSubset(const SkIRect& subset) const { |
| 170 | return as_SIB(this)->onMakeSubset(subset); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 171 | } |
| 172 | |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 173 | sk_sp<SkImage> SkSpecialImage::makeTightSubset(const SkIRect& subset) const { |
| 174 | return as_SIB(this)->onMakeTightSubset(subset); |
| 175 | } |
| 176 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 177 | #ifdef SK_DEBUG |
| 178 | static bool rect_fits(const SkIRect& rect, int width, int height) { |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 179 | if (0 == width && 0 == height) { |
| 180 | SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == rect.fBottom); |
| 181 | return true; |
| 182 | } |
| 183 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 184 | return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight && |
| 185 | rect.fRight >= 0 && rect.fRight <= width && |
| 186 | rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom && |
| 187 | rect.fBottom >= 0 && rect.fBottom <= height; |
| 188 | } |
| 189 | #endif |
| 190 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 191 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromImage(const SkIRect& subset, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 192 | sk_sp<SkImage> image, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 193 | SkColorSpace* dstColorSpace, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 194 | const SkSurfaceProps* props) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 195 | SkASSERT(rect_fits(subset, image->width(), image->height())); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 196 | |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 197 | #if SK_SUPPORT_GPU |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 198 | if (sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef()) { |
| 199 | GrContext* context = ((SkImage_Gpu*) as_IB(image))->context(); |
| 200 | |
| 201 | return MakeDeferredFromGpu(context, subset, image->uniqueID(), std::move(proxy), |
| 202 | sk_ref_sp(as_IB(image)->onImageInfo().colorSpace()), props); |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 203 | } else |
| 204 | #endif |
| 205 | { |
| 206 | SkBitmap bm; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 207 | if (as_IB(image)->getROPixels(&bm, dstColorSpace)) { |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 208 | return MakeFromRaster(subset, bm, props); |
| 209 | } |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 210 | } |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 211 | return nullptr; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 215 | |
| 216 | class SkSpecialImage_Raster : public SkSpecialImage_Base { |
| 217 | public: |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 218 | SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm, const SkSurfaceProps* props) |
| 219 | : INHERITED(subset, bm.getGenerationID(), props) |
reed | bd2bd5c | 2016-07-25 14:26:02 -0700 | [diff] [blame] | 220 | , fBitmap(bm) |
| 221 | { |
| 222 | SkASSERT(bm.pixelRef()); |
| 223 | |
| 224 | // We have to lock now, while bm is still in scope, since it may have come from our |
| 225 | // cache, which means we need to keep it locked until we (the special) are done, since |
| 226 | // we cannot re-generate the cache entry (if bm came from a generator). |
| 227 | fBitmap.lockPixels(); |
| 228 | SkASSERT(fBitmap.getPixels()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 229 | } |
| 230 | |
brianosman | 80e9608 | 2016-08-16 07:09:47 -0700 | [diff] [blame] | 231 | SkAlphaType alphaType() const override { return fBitmap.alphaType(); } |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 232 | |
| 233 | size_t getSize() const override { return fBitmap.getSize(); } |
| 234 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 235 | void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 236 | SkRect dst = SkRect::MakeXYWH(x, y, |
| 237 | this->subset().width(), this->subset().height()); |
| 238 | |
| 239 | canvas->drawBitmapRect(fBitmap, this->subset(), |
| 240 | dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 241 | } |
| 242 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 243 | bool onGetROPixels(SkBitmap* bm) const override { |
| 244 | *bm = fBitmap; |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 248 | SkColorSpace* onGetColorSpace() const override { |
| 249 | return fBitmap.colorSpace(); |
| 250 | } |
| 251 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 252 | #if SK_SUPPORT_GPU |
Robert Phillips | 696b293 | 2017-02-08 12:49:00 +0000 | [diff] [blame] | 253 | sk_sp<GrTexture> onAsTextureRef(GrContext* context) const override { |
| 254 | if (context) { |
| 255 | return sk_ref_sp(GrRefCachedBitmapTexture(context, fBitmap, |
| 256 | GrSamplerParams::ClampNoFilter(), nullptr)); |
| 257 | } |
| 258 | |
| 259 | return nullptr; |
| 260 | } |
| 261 | |
| 262 | sk_sp<GrTextureProxy> onAsTextureProxy(GrContext* context) const override { |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 263 | if (context) { |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 264 | sk_sp<GrTexture> tex(sk_ref_sp(GrRefCachedBitmapTexture( |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 265 | context, fBitmap, GrSamplerParams::ClampNoFilter(), nullptr))); |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 266 | return GrSurfaceProxy::MakeWrapped(std::move(tex)); |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | return nullptr; |
| 270 | } |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 271 | #endif |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 272 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 273 | // TODO: The raster implementations of image filters all currently assume that the pixels are |
| 274 | // legacy N32. Until they actually check the format and operate on sRGB or F16 data appropriately, |
| 275 | // we can't enable this. (They will continue to produce incorrect results, but less-so). |
| 276 | #define RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16 0 |
| 277 | |
| 278 | sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps, |
| 279 | const SkISize& size, SkAlphaType at) const override { |
| 280 | #if RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16 |
| 281 | SkColorSpace* colorSpace = outProps.colorSpace(); |
| 282 | #else |
| 283 | SkColorSpace* colorSpace = nullptr; |
| 284 | #endif |
| 285 | SkColorType colorType = colorSpace && colorSpace->gammaIsLinear() |
| 286 | ? kRGBA_F16_SkColorType : kN32_SkColorType; |
| 287 | SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at, |
| 288 | sk_ref_sp(colorSpace)); |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 289 | return SkSpecialSurface::MakeRaster(info, nullptr); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 290 | } |
| 291 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 292 | sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override { |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 293 | SkBitmap subsetBM; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 294 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 295 | if (!fBitmap.extractSubset(&subsetBM, subset)) { |
| 296 | return nullptr; |
| 297 | } |
| 298 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 299 | return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(subset.width(), subset.height()), |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 300 | subsetBM, |
| 301 | &this->props()); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 302 | } |
| 303 | |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 304 | sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override { |
| 305 | SkBitmap subsetBM; |
| 306 | |
| 307 | if (!fBitmap.extractSubset(&subsetBM, subset)) { |
| 308 | return nullptr; |
| 309 | } |
| 310 | |
| 311 | return SkImage::MakeFromBitmap(subsetBM); |
| 312 | } |
| 313 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 314 | sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps, |
| 315 | const SkISize& size, SkAlphaType at) const override { |
| 316 | #if RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16 |
| 317 | SkColorSpace* colorSpace = outProps.colorSpace(); |
| 318 | #else |
| 319 | SkColorSpace* colorSpace = nullptr; |
| 320 | #endif |
| 321 | SkColorType colorType = colorSpace && colorSpace->gammaIsLinear() |
| 322 | ? kRGBA_F16_SkColorType : kN32_SkColorType; |
| 323 | SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at, |
| 324 | sk_ref_sp(colorSpace)); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 325 | return SkSurface::MakeRaster(info); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 326 | } |
| 327 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 328 | private: |
| 329 | SkBitmap fBitmap; |
| 330 | |
| 331 | typedef SkSpecialImage_Base INHERITED; |
| 332 | }; |
| 333 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 334 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromRaster(const SkIRect& subset, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 335 | const SkBitmap& bm, |
| 336 | const SkSurfaceProps* props) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 337 | SkASSERT(rect_fits(subset, bm.width(), bm.height())); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 338 | |
reed | bd2bd5c | 2016-07-25 14:26:02 -0700 | [diff] [blame] | 339 | if (!bm.pixelRef()) { |
| 340 | return nullptr; |
| 341 | } |
| 342 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 343 | const SkBitmap* srcBM = &bm; |
| 344 | SkBitmap tmpStorage; |
| 345 | // ImageFilters only handle N32 at the moment, so force our src to be that |
| 346 | if (!valid_for_imagefilters(bm.info())) { |
| 347 | if (!bm.copyTo(&tmpStorage, kN32_SkColorType)) { |
| 348 | return nullptr; |
| 349 | } |
| 350 | srcBM = &tmpStorage; |
| 351 | } |
| 352 | return sk_make_sp<SkSpecialImage_Raster>(subset, *srcBM, props); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | #if SK_SUPPORT_GPU |
| 356 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 357 | #include "GrTexture.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 358 | |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 359 | static sk_sp<SkImage> wrap_proxy_in_image(GrContext* context, GrTextureProxy* proxy, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 360 | SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) { |
| 361 | // TODO: add GrTextureProxy-backed SkImage_Gpus |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 362 | GrTexture* tex = proxy->instantiate(context->textureProvider()); |
| 363 | if (!tex) { |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 364 | return nullptr; |
| 365 | } |
| 366 | |
| 367 | return sk_make_sp<SkImage_Gpu>(proxy->width(), proxy->height(), |
| 368 | kNeedNewImageUniqueID, alphaType, |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 369 | sk_ref_sp(tex), |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 370 | std::move(colorSpace), SkBudgeted::kYes); |
| 371 | } |
| 372 | |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 373 | class SkSpecialImage_Gpu : public SkSpecialImage_Base { |
| 374 | public: |
| 375 | SkSpecialImage_Gpu(const SkIRect& subset, |
| 376 | uint32_t uniqueID, sk_sp<GrTexture> tex, SkAlphaType at, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 377 | sk_sp<SkColorSpace> colorSpace, const SkSurfaceProps* props) |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 378 | : INHERITED(subset, uniqueID, props) |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 379 | , fContext(tex->getContext()) |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 380 | , fTextureProxy(GrSurfaceProxy::MakeWrapped(std::move(tex))) |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 381 | , fAlphaType(at) |
| 382 | , fColorSpace(std::move(colorSpace)) |
| 383 | , fAddedRasterVersionToCache(false) { |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | SkSpecialImage_Gpu(GrContext* context, const SkIRect& subset, |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 387 | uint32_t uniqueID, sk_sp<GrTextureProxy> proxy, SkAlphaType at, |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 388 | sk_sp<SkColorSpace> colorSpace, const SkSurfaceProps* props) |
| 389 | : INHERITED(subset, uniqueID, props) |
| 390 | , fContext(context) |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 391 | , fTextureProxy(std::move(proxy)) |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 392 | , fAlphaType(at) |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 393 | , fColorSpace(std::move(colorSpace)) |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 394 | , fAddedRasterVersionToCache(false) { |
| 395 | } |
| 396 | |
| 397 | ~SkSpecialImage_Gpu() override { |
| 398 | if (fAddedRasterVersionToCache.load()) { |
| 399 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 400 | } |
| 401 | } |
| 402 | |
brianosman | 80e9608 | 2016-08-16 07:09:47 -0700 | [diff] [blame] | 403 | SkAlphaType alphaType() const override { return fAlphaType; } |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 404 | |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 405 | size_t getSize() const override { return fTextureProxy->gpuMemorySize(); } |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 406 | |
| 407 | void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
| 408 | SkRect dst = SkRect::MakeXYWH(x, y, |
| 409 | this->subset().width(), this->subset().height()); |
| 410 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 411 | // TODO: add GrTextureProxy-backed SkImage_Gpus |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 412 | GrTexture* tex = fTextureProxy->instantiate(fContext->textureProvider()); |
| 413 | if (!tex) { |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 414 | return; |
| 415 | } |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 416 | |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 417 | // TODO: In this instance we know we're going to draw a sub-portion of the backing |
| 418 | // texture into the canvas so it is okay to wrap it in an SkImage. This poses |
| 419 | // some problems for full deferral however in that when the deferred SkImage_Gpu |
| 420 | // instantiates itself it is going to have to either be okay with having a larger |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 421 | // than expected backing texture (unlikely) or the 'fit' of the SurfaceProxy needs |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 422 | // to be tightened (if it is deferred). |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 423 | auto img = sk_sp<SkImage>(new SkImage_Gpu(tex->width(), tex->height(), |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 424 | this->uniqueID(), fAlphaType, |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 425 | sk_ref_sp(tex), |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 426 | fColorSpace, SkBudgeted::kNo)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 427 | |
reed | 77d6f7d | 2016-07-13 12:24:48 -0700 | [diff] [blame] | 428 | canvas->drawImageRect(img, this->subset(), |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 429 | dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 432 | GrContext* onGetContext() const override { return fContext; } |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 433 | |
Robert Phillips | 696b293 | 2017-02-08 12:49:00 +0000 | [diff] [blame] | 434 | // This entry point should go away in favor of asTextureProxy |
| 435 | sk_sp<GrTexture> onAsTextureRef(GrContext* context) const override { |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 436 | return sk_ref_sp(fTextureProxy->instantiate(context->textureProvider())); |
Robert Phillips | 696b293 | 2017-02-08 12:49:00 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | sk_sp<GrTextureProxy> onAsTextureProxy(GrContext*) const override { |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 440 | return fTextureProxy; |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 441 | } |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 442 | |
| 443 | bool onGetROPixels(SkBitmap* dst) const override { |
| 444 | if (SkBitmapCache::Find(this->uniqueID(), dst)) { |
| 445 | SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 446 | SkASSERT(dst->isImmutable()); |
| 447 | SkASSERT(dst->getPixels()); |
| 448 | return true; |
| 449 | } |
| 450 | |
| 451 | SkImageInfo info = SkImageInfo::MakeN32(this->width(), this->height(), |
brianosman | 80e9608 | 2016-08-16 07:09:47 -0700 | [diff] [blame] | 452 | this->alphaType(), fColorSpace); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 453 | |
| 454 | if (!dst->tryAllocPixels(info)) { |
| 455 | return false; |
| 456 | } |
| 457 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 458 | // Reading back to an SkBitmap ends deferral |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 459 | GrTexture* texture = fTextureProxy->instantiate(fContext->textureProvider()); |
| 460 | if (!texture) { |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 461 | return false; |
| 462 | } |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 463 | |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 464 | if (!texture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig, |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 465 | dst->getPixels(), dst->rowBytes())) { |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 466 | return false; |
| 467 | } |
| 468 | |
| 469 | dst->pixelRef()->setImmutableWithID(this->uniqueID()); |
| 470 | SkBitmapCache::Add(this->uniqueID(), *dst); |
| 471 | fAddedRasterVersionToCache.store(true); |
| 472 | return true; |
| 473 | } |
| 474 | |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 475 | SkColorSpace* onGetColorSpace() const override { |
| 476 | return fColorSpace.get(); |
| 477 | } |
| 478 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 479 | sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps, |
| 480 | const SkISize& size, SkAlphaType at) const override { |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 481 | if (!fContext) { |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 482 | return nullptr; |
| 483 | } |
| 484 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 485 | SkColorSpace* colorSpace = outProps.colorSpace(); |
| 486 | return SkSpecialSurface::MakeRenderTarget( |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 487 | fContext, size.width(), size.height(), |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 488 | GrRenderableConfigForColorSpace(colorSpace), sk_ref_sp(colorSpace)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override { |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 492 | return SkSpecialImage::MakeDeferredFromGpu(fContext, |
| 493 | subset, |
| 494 | this->uniqueID(), |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 495 | fTextureProxy, |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 496 | fColorSpace, |
| 497 | &this->props(), |
| 498 | fAlphaType); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 501 | // TODO: move all the logic here into the subset-flavor GrSurfaceProxy::copy? |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 502 | sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override { |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 503 | // TODO: this is problematic since the surfaceProxy could be loose |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 504 | if (0 == subset.fLeft && 0 == subset.fTop && |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 505 | fTextureProxy->width() == subset.width() && |
| 506 | fTextureProxy->height() == subset.height()) { |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 507 | // The existing GrTexture is already tight so reuse it in the SkImage |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 508 | return wrap_proxy_in_image(fContext, fTextureProxy.get(), |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 509 | fAlphaType, fColorSpace); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 512 | sk_sp<GrTextureProxy> subsetProxy(GrSurfaceProxy::Copy(fContext, fTextureProxy.get(), |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 513 | subset, SkBudgeted::kYes)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 514 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 515 | return wrap_proxy_in_image(fContext, subsetProxy.get(), fAlphaType, fColorSpace); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 516 | } |
| 517 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 518 | sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps, |
| 519 | const SkISize& size, SkAlphaType at) const override { |
| 520 | SkColorSpace* colorSpace = outProps.colorSpace(); |
| 521 | SkColorType colorType = colorSpace && colorSpace->gammaIsLinear() |
| 522 | ? kRGBA_F16_SkColorType : kRGBA_8888_SkColorType; |
| 523 | SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at, |
| 524 | sk_ref_sp(colorSpace)); |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 525 | return SkSurface::MakeRenderTarget(fContext, SkBudgeted::kYes, info); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | private: |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 529 | GrContext* fContext; |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 530 | sk_sp<GrTextureProxy> fTextureProxy; |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 531 | const SkAlphaType fAlphaType; |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 532 | sk_sp<SkColorSpace> fColorSpace; |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 533 | mutable SkAtomic<bool> fAddedRasterVersionToCache; |
| 534 | |
| 535 | typedef SkSpecialImage_Base INHERITED; |
| 536 | }; |
| 537 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 538 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromGpu(const SkIRect& subset, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 539 | uint32_t uniqueID, |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 540 | sk_sp<GrTexture> tex, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 541 | sk_sp<SkColorSpace> colorSpace, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 542 | const SkSurfaceProps* props, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 543 | SkAlphaType at) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 544 | SkASSERT(rect_fits(subset, tex->width(), tex->height())); |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 545 | return sk_make_sp<SkSpecialImage_Gpu>(subset, uniqueID, std::move(tex), at, |
| 546 | std::move(colorSpace), props); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 547 | } |
| 548 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 549 | sk_sp<SkSpecialImage> SkSpecialImage::MakeDeferredFromGpu(GrContext* context, |
| 550 | const SkIRect& subset, |
| 551 | uint32_t uniqueID, |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame^] | 552 | sk_sp<GrTextureProxy> proxy, |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 553 | sk_sp<SkColorSpace> colorSpace, |
| 554 | const SkSurfaceProps* props, |
| 555 | SkAlphaType at) { |
| 556 | SkASSERT(rect_fits(subset, proxy->width(), proxy->height())); |
| 557 | return sk_make_sp<SkSpecialImage_Gpu>(context, subset, uniqueID, std::move(proxy), at, |
| 558 | std::move(colorSpace), props); |
| 559 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 560 | #endif |