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