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