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" |
| 23 | #include "SkGrPixelRef.h" |
| 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 | |
| 46 | virtual GrTexture* onPeekTexture() const { return nullptr; } |
| 47 | |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 48 | #if SK_SUPPORT_GPU |
| 49 | virtual sk_sp<GrTexture> onAsTextureRef(GrContext* context) const = 0; |
| 50 | #endif |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 51 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 52 | // Delete this entry point ASAP (see skbug.com/4965) |
robertphillips | ab01ccd | 2016-03-08 10:45:32 -0800 | [diff] [blame] | 53 | virtual bool getBitmapDeprecated(SkBitmap* result) const = 0; |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -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 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 57 | virtual sk_sp<SkSpecialSurface> onMakeSurface(const SkImageInfo& info) const = 0; |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 58 | |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 59 | virtual sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const = 0; |
| 60 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 61 | virtual sk_sp<SkSurface> onMakeTightSurface(const SkImageInfo& info) const = 0; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 62 | |
| 63 | private: |
| 64 | typedef SkSpecialImage INHERITED; |
| 65 | }; |
| 66 | |
| 67 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 68 | static inline const SkSpecialImage_Base* as_SIB(const SkSpecialImage* image) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 69 | return static_cast<const SkSpecialImage_Base*>(image); |
| 70 | } |
| 71 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 72 | SkSpecialImage::SkSpecialImage(const SkIRect& subset, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 73 | uint32_t uniqueID, |
| 74 | const SkSurfaceProps* props) |
| 75 | : fProps(SkSurfacePropsCopyOrDefault(props)) |
| 76 | , fSubset(subset) |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 77 | , fUniqueID(kNeedNewImageUniqueID_SpecialImage == uniqueID ? SkNextID::ImageID() : uniqueID) { |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 78 | } |
| 79 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 80 | sk_sp<SkSpecialImage> SkSpecialImage::makeTextureImage(GrContext* context) { |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 81 | #if SK_SUPPORT_GPU |
| 82 | if (!context) { |
| 83 | return nullptr; |
| 84 | } |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 85 | if (GrTexture* peek = as_SIB(this)->onPeekTexture()) { |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 86 | return peek->getContext() == context ? sk_sp<SkSpecialImage>(SkRef(this)) : nullptr; |
| 87 | } |
| 88 | |
| 89 | SkBitmap bmp; |
reed | cf5c846 | 2016-07-20 12:28:40 -0700 | [diff] [blame] | 90 | // At this point, we are definitely not texture-backed, so we must be raster or generator |
| 91 | // backed. If we remove the special-wrapping-an-image subclass, we may be able to assert that |
| 92 | // we are strictly raster-backed (i.e. generator images become raster when they are specialized) |
| 93 | // in which case getROPixels could turn into peekPixels... |
| 94 | if (!this->getROPixels(&bmp)) { |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 95 | return nullptr; |
| 96 | } |
| 97 | |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 98 | if (bmp.empty()) { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 99 | return SkSpecialImage::MakeFromRaster(SkIRect::MakeEmpty(), bmp, &this->props()); |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 100 | } |
| 101 | |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 102 | sk_sp<GrTexture> resultTex(GrRefCachedBitmapTexture(context, |
| 103 | bmp, |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 104 | GrTextureParams::ClampNoFilter(), |
| 105 | SkSourceGammaTreatment::kRespect)); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 106 | if (!resultTex) { |
| 107 | return nullptr; |
| 108 | } |
| 109 | |
| 110 | SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 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 | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 114 | resultTex, &this->props(), at); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 115 | #else |
| 116 | return nullptr; |
| 117 | #endif |
| 118 | } |
| 119 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 120 | void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 121 | return as_SIB(this)->onDraw(canvas, x, y, paint); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 122 | } |
| 123 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 124 | bool SkSpecialImage::getROPixels(SkBitmap* bm) const { |
| 125 | return as_SIB(this)->onGetROPixels(bm); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 126 | } |
| 127 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 128 | bool SkSpecialImage::isTextureBacked() const { |
| 129 | #if SK_SUPPORT_GPU |
| 130 | return as_SIB(this)->onPeekTexture() && as_SIB(this)->onPeekTexture()->getContext(); |
| 131 | #else |
| 132 | return false; |
| 133 | #endif |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 134 | } |
| 135 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 136 | GrContext* SkSpecialImage::getContext() const { |
| 137 | #if SK_SUPPORT_GPU |
| 138 | GrTexture* texture = as_SIB(this)->onPeekTexture(); |
| 139 | |
| 140 | if (texture) { |
| 141 | return texture->getContext(); |
| 142 | } |
| 143 | #endif |
| 144 | return nullptr; |
| 145 | } |
| 146 | |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 147 | #if SK_SUPPORT_GPU |
| 148 | sk_sp<GrTexture> SkSpecialImage::asTextureRef(GrContext* context) const { |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 149 | return as_SIB(this)->onAsTextureRef(context); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 150 | } |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 151 | #endif |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 152 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 153 | sk_sp<SkSpecialSurface> SkSpecialImage::makeSurface(const SkImageInfo& info) const { |
| 154 | return as_SIB(this)->onMakeSurface(info); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 155 | } |
| 156 | |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 157 | sk_sp<SkSurface> SkSpecialImage::makeTightSurface(const SkImageInfo& info) const { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 158 | return as_SIB(this)->onMakeTightSurface(info); |
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()) { |
| 190 | return MakeFromGpu(subset, image->uniqueID(), sk_ref_sp(texture), props); |
| 191 | } else |
| 192 | #endif |
| 193 | { |
| 194 | SkBitmap bm; |
| 195 | if (as_IB(image)->getROPixels(&bm)) { |
| 196 | return MakeFromRaster(subset, bm, props); |
| 197 | } |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 198 | } |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame^] | 199 | return nullptr; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 203 | |
| 204 | class SkSpecialImage_Raster : public SkSpecialImage_Base { |
| 205 | public: |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 206 | SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm, const SkSurfaceProps* props) |
| 207 | : INHERITED(subset, bm.getGenerationID(), props) |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 208 | , fBitmap(bm) { |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 209 | if (bm.pixelRef() && bm.pixelRef()->isPreLocked()) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 210 | // we only preemptively lock if there is no chance of triggering something expensive |
| 211 | // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already. |
| 212 | fBitmap.lockPixels(); |
| 213 | } |
| 214 | } |
| 215 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 216 | bool isOpaque() const override { return fBitmap.isOpaque(); } |
| 217 | |
| 218 | size_t getSize() const override { return fBitmap.getSize(); } |
| 219 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 220 | void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 221 | SkRect dst = SkRect::MakeXYWH(x, y, |
| 222 | this->subset().width(), this->subset().height()); |
| 223 | |
| 224 | canvas->drawBitmapRect(fBitmap, this->subset(), |
| 225 | dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 226 | } |
| 227 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 228 | bool onGetROPixels(SkBitmap* bm) const override { |
| 229 | *bm = fBitmap; |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 233 | #if SK_SUPPORT_GPU |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 234 | sk_sp<GrTexture> onAsTextureRef(GrContext* context) const override { |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 235 | if (context) { |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 236 | return sk_ref_sp(GrRefCachedBitmapTexture(context, |
| 237 | fBitmap, |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 238 | GrTextureParams::ClampNoFilter(), |
| 239 | SkSourceGammaTreatment::kRespect)); |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 240 | } |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 241 | |
| 242 | return nullptr; |
| 243 | } |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 244 | #endif |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 245 | |
| 246 | bool getBitmapDeprecated(SkBitmap* result) const override { |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 247 | *result = fBitmap; |
| 248 | return true; |
| 249 | } |
| 250 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 251 | sk_sp<SkSpecialSurface> onMakeSurface(const SkImageInfo& info) const override { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 252 | return SkSpecialSurface::MakeRaster(info, nullptr); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 253 | } |
| 254 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 255 | sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override { |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 256 | SkBitmap subsetBM; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 257 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 258 | if (!fBitmap.extractSubset(&subsetBM, subset)) { |
| 259 | return nullptr; |
| 260 | } |
| 261 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 262 | return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(subset.width(), subset.height()), |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 263 | subsetBM, |
| 264 | &this->props()); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 265 | } |
| 266 | |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 267 | sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override { |
| 268 | SkBitmap subsetBM; |
| 269 | |
| 270 | if (!fBitmap.extractSubset(&subsetBM, subset)) { |
| 271 | return nullptr; |
| 272 | } |
| 273 | |
| 274 | return SkImage::MakeFromBitmap(subsetBM); |
| 275 | } |
| 276 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 277 | sk_sp<SkSurface> onMakeTightSurface(const SkImageInfo& info) const override { |
| 278 | return SkSurface::MakeRaster(info); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 279 | } |
| 280 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 281 | private: |
| 282 | SkBitmap fBitmap; |
| 283 | |
| 284 | typedef SkSpecialImage_Base INHERITED; |
| 285 | }; |
| 286 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 287 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromRaster(const SkIRect& subset, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 288 | const SkBitmap& bm, |
| 289 | const SkSurfaceProps* props) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 290 | SkASSERT(nullptr == bm.getTexture()); |
| 291 | SkASSERT(rect_fits(subset, bm.width(), bm.height())); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 292 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 293 | const SkBitmap* srcBM = &bm; |
| 294 | SkBitmap tmpStorage; |
| 295 | // ImageFilters only handle N32 at the moment, so force our src to be that |
| 296 | if (!valid_for_imagefilters(bm.info())) { |
| 297 | if (!bm.copyTo(&tmpStorage, kN32_SkColorType)) { |
| 298 | return nullptr; |
| 299 | } |
| 300 | srcBM = &tmpStorage; |
| 301 | } |
| 302 | return sk_make_sp<SkSpecialImage_Raster>(subset, *srcBM, props); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | #if SK_SUPPORT_GPU |
| 306 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 307 | #include "GrTexture.h" |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 308 | #include "SkImage_Gpu.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 309 | |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 310 | class SkSpecialImage_Gpu : public SkSpecialImage_Base { |
| 311 | public: |
| 312 | SkSpecialImage_Gpu(const SkIRect& subset, |
| 313 | uint32_t uniqueID, sk_sp<GrTexture> tex, SkAlphaType at, |
| 314 | const SkSurfaceProps* props) |
| 315 | : INHERITED(subset, uniqueID, props) |
| 316 | , fTexture(std::move(tex)) |
| 317 | , fAlphaType(at) |
| 318 | , fAddedRasterVersionToCache(false) { |
| 319 | } |
| 320 | |
| 321 | ~SkSpecialImage_Gpu() override { |
| 322 | if (fAddedRasterVersionToCache.load()) { |
| 323 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | bool isOpaque() const override { |
| 328 | return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType; |
| 329 | } |
| 330 | |
| 331 | size_t getSize() const override { return fTexture->gpuMemorySize(); } |
| 332 | |
| 333 | void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
| 334 | SkRect dst = SkRect::MakeXYWH(x, y, |
| 335 | this->subset().width(), this->subset().height()); |
| 336 | |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 337 | // TODO: Supply correct color space after we're storing it here |
reed | 77d6f7d | 2016-07-13 12:24:48 -0700 | [diff] [blame] | 338 | auto img = sk_sp<SkImage>(new SkImage_Gpu(fTexture->width(), fTexture->height(), |
| 339 | this->uniqueID(), fAlphaType, fTexture.get(), |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 340 | nullptr, SkBudgeted::kNo)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 341 | |
reed | 77d6f7d | 2016-07-13 12:24:48 -0700 | [diff] [blame] | 342 | canvas->drawImageRect(img, this->subset(), |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 343 | dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 344 | } |
| 345 | |
| 346 | GrTexture* onPeekTexture() const override { return fTexture.get(); } |
| 347 | |
| 348 | sk_sp<GrTexture> onAsTextureRef(GrContext*) const override { return fTexture; } |
| 349 | |
| 350 | bool onGetROPixels(SkBitmap* dst) const override { |
| 351 | if (SkBitmapCache::Find(this->uniqueID(), dst)) { |
| 352 | SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 353 | SkASSERT(dst->isImmutable()); |
| 354 | SkASSERT(dst->getPixels()); |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | SkImageInfo info = SkImageInfo::MakeN32(this->width(), this->height(), |
| 359 | this->isOpaque() ? kOpaque_SkAlphaType |
| 360 | : kPremul_SkAlphaType); |
| 361 | |
| 362 | if (!dst->tryAllocPixels(info)) { |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig, |
| 367 | dst->getPixels(), dst->rowBytes())) { |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | dst->pixelRef()->setImmutableWithID(this->uniqueID()); |
| 372 | SkBitmapCache::Add(this->uniqueID(), *dst); |
| 373 | fAddedRasterVersionToCache.store(true); |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | bool getBitmapDeprecated(SkBitmap* result) const override { |
| 378 | const SkImageInfo info = GrMakeInfoFromTexture(fTexture.get(), |
| 379 | this->width(), this->height(), |
| 380 | this->isOpaque()); |
| 381 | if (!result->setInfo(info)) { |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | const SkImageInfo prInfo = info.makeWH(fTexture->width(), fTexture->height()); |
| 386 | |
| 387 | SkAutoTUnref<SkGrPixelRef> pixelRef(new SkGrPixelRef(prInfo, fTexture.get())); |
| 388 | result->setPixelRef(pixelRef, this->subset().fLeft, this->subset().fTop); |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | sk_sp<SkSpecialSurface> onMakeSurface(const SkImageInfo& info) const override { |
| 393 | if (!fTexture->getContext()) { |
| 394 | return nullptr; |
| 395 | } |
| 396 | |
robertphillips | 4df1656 | 2016-04-28 15:09:34 -0700 | [diff] [blame] | 397 | GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fTexture->getContext()->caps()); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 398 | |
robertphillips | 4df1656 | 2016-04-28 15:09:34 -0700 | [diff] [blame] | 399 | return SkSpecialSurface::MakeRenderTarget(fTexture->getContext(), |
| 400 | info.width(), info.height(), |
| 401 | config); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override { |
| 405 | return SkSpecialImage::MakeFromGpu(subset, |
| 406 | this->uniqueID(), |
| 407 | fTexture, |
| 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 |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 417 | // TODO: Supply correct color space after we're storing it here |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 418 | return sk_make_sp<SkImage_Gpu>(fTexture->width(), fTexture->height(), |
| 419 | kNeedNewImageUniqueID, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 420 | fAlphaType, fTexture.get(), nullptr, 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)); |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 433 | // TODO: Supply correct color space after we're storing it here |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 434 | return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 435 | fAlphaType, subTx.get(), nullptr, SkBudgeted::kYes); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | sk_sp<SkSurface> onMakeTightSurface(const SkImageInfo& info) const override { |
| 439 | return SkSurface::MakeRenderTarget(fTexture->getContext(), SkBudgeted::kYes, info); |
| 440 | } |
| 441 | |
| 442 | private: |
| 443 | sk_sp<GrTexture> fTexture; |
| 444 | const SkAlphaType fAlphaType; |
| 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 | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 453 | const SkSurfaceProps* props, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 454 | SkAlphaType at) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 455 | SkASSERT(rect_fits(subset, tex->width(), tex->height())); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 456 | return sk_make_sp<SkSpecialImage_Gpu>(subset, uniqueID, std::move(tex), at, props); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | #endif |