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