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" |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 21 | #include "GrSamplerParams.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 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 104 | sk_sp<GrTexture> resultTex( |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame^] | 105 | GrRefCachedBitmapTexture(context, bmp, GrSamplerParams::ClampNoFilter())); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 106 | if (!resultTex) { |
| 107 | return nullptr; |
| 108 | } |
| 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(), |
brianosman | 80e9608 | 2016-08-16 07:09:47 -0700 | [diff] [blame] | 113 | this->alphaType()); |
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 { |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 128 | return SkToBool(as_SIB(this)->onGetContext()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 129 | } |
| 130 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 131 | GrContext* SkSpecialImage::getContext() const { |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 132 | return as_SIB(this)->onGetContext(); |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 133 | } |
| 134 | |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 135 | SkColorSpace* SkSpecialImage::getColorSpace() const { |
| 136 | return as_SIB(this)->onGetColorSpace(); |
| 137 | } |
| 138 | |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 139 | #if SK_SUPPORT_GPU |
| 140 | sk_sp<GrTexture> SkSpecialImage::asTextureRef(GrContext* context) const { |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 141 | return as_SIB(this)->onAsTextureRef(context); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 142 | } |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 143 | |
| 144 | sk_sp<GrTextureProxy> SkSpecialImage::asTextureProxy(GrContext* context) const { |
| 145 | return as_SIB(this)->onAsTextureProxy(context); |
| 146 | } |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 147 | #endif |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 148 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 149 | sk_sp<SkSpecialSurface> SkSpecialImage::makeSurface(const SkImageFilter::OutputProperties& outProps, |
| 150 | const SkISize& size, SkAlphaType at) const { |
| 151 | return as_SIB(this)->onMakeSurface(outProps, size, at); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 152 | } |
| 153 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 154 | sk_sp<SkSurface> SkSpecialImage::makeTightSurface(const SkImageFilter::OutputProperties& outProps, |
| 155 | const SkISize& size, SkAlphaType at) const { |
| 156 | return as_SIB(this)->onMakeTightSurface(outProps, size, at); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 157 | } |
| 158 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 159 | sk_sp<SkSpecialImage> SkSpecialImage::makeSubset(const SkIRect& subset) const { |
| 160 | return as_SIB(this)->onMakeSubset(subset); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 161 | } |
| 162 | |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 163 | sk_sp<SkImage> SkSpecialImage::makeTightSubset(const SkIRect& subset) const { |
| 164 | return as_SIB(this)->onMakeTightSubset(subset); |
| 165 | } |
| 166 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 167 | #ifdef SK_DEBUG |
| 168 | static bool rect_fits(const SkIRect& rect, int width, int height) { |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 169 | if (0 == width && 0 == height) { |
| 170 | SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == rect.fBottom); |
| 171 | return true; |
| 172 | } |
| 173 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 174 | return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight && |
| 175 | rect.fRight >= 0 && rect.fRight <= width && |
| 176 | rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom && |
| 177 | rect.fBottom >= 0 && rect.fBottom <= height; |
| 178 | } |
| 179 | #endif |
| 180 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 181 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromImage(const SkIRect& subset, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 182 | sk_sp<SkImage> image, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame^] | 183 | SkColorSpace* dstColorSpace, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 184 | const SkSurfaceProps* props) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 185 | SkASSERT(rect_fits(subset, image->width(), image->height())); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 186 | |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 187 | #if SK_SUPPORT_GPU |
| 188 | if (GrTexture* texture = as_IB(image)->peekTexture()) { |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 189 | return MakeFromGpu(subset, image->uniqueID(), sk_ref_sp(texture), |
| 190 | sk_ref_sp(as_IB(image)->onImageInfo().colorSpace()), props); |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 191 | } else |
| 192 | #endif |
| 193 | { |
| 194 | SkBitmap bm; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame^] | 195 | if (as_IB(image)->getROPixels(&bm, dstColorSpace)) { |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 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) |
reed | bd2bd5c | 2016-07-25 14:26:02 -0700 | [diff] [blame] | 208 | , fBitmap(bm) |
| 209 | { |
| 210 | SkASSERT(bm.pixelRef()); |
| 211 | |
| 212 | // We have to lock now, while bm is still in scope, since it may have come from our |
| 213 | // cache, which means we need to keep it locked until we (the special) are done, since |
| 214 | // we cannot re-generate the cache entry (if bm came from a generator). |
| 215 | fBitmap.lockPixels(); |
| 216 | SkASSERT(fBitmap.getPixels()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 217 | } |
| 218 | |
brianosman | 80e9608 | 2016-08-16 07:09:47 -0700 | [diff] [blame] | 219 | SkAlphaType alphaType() const override { return fBitmap.alphaType(); } |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 220 | |
| 221 | size_t getSize() const override { return fBitmap.getSize(); } |
| 222 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 223 | void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 224 | SkRect dst = SkRect::MakeXYWH(x, y, |
| 225 | this->subset().width(), this->subset().height()); |
| 226 | |
| 227 | canvas->drawBitmapRect(fBitmap, this->subset(), |
| 228 | dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 229 | } |
| 230 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 231 | bool onGetROPixels(SkBitmap* bm) const override { |
| 232 | *bm = fBitmap; |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 236 | SkColorSpace* onGetColorSpace() const override { |
| 237 | return fBitmap.colorSpace(); |
| 238 | } |
| 239 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 240 | #if SK_SUPPORT_GPU |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 241 | sk_sp<GrTexture> onAsTextureRef(GrContext* context) const override { |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 242 | if (context) { |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 243 | return sk_ref_sp( |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame^] | 244 | GrRefCachedBitmapTexture(context, fBitmap, GrSamplerParams::ClampNoFilter())); |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 245 | } |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 246 | |
| 247 | return nullptr; |
| 248 | } |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 249 | |
| 250 | sk_sp<GrTextureProxy> onAsTextureProxy(GrContext* context) const override { |
| 251 | if (context) { |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 252 | sk_sp<GrTexture> tex(sk_ref_sp(GrRefCachedBitmapTexture( |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame^] | 253 | context, fBitmap, GrSamplerParams::ClampNoFilter()))); |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 254 | sk_sp<GrSurfaceProxy> sProxy = GrSurfaceProxy::MakeWrapped(std::move(tex)); |
| 255 | return sk_ref_sp(sProxy->asTextureProxy()); |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | return nullptr; |
| 259 | } |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 260 | #endif |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 261 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 262 | // TODO: The raster implementations of image filters all currently assume that the pixels are |
| 263 | // legacy N32. Until they actually check the format and operate on sRGB or F16 data appropriately, |
| 264 | // we can't enable this. (They will continue to produce incorrect results, but less-so). |
| 265 | #define RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16 0 |
| 266 | |
| 267 | sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps, |
| 268 | const SkISize& size, SkAlphaType at) const override { |
| 269 | #if RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16 |
| 270 | SkColorSpace* colorSpace = outProps.colorSpace(); |
| 271 | #else |
| 272 | SkColorSpace* colorSpace = nullptr; |
| 273 | #endif |
| 274 | SkColorType colorType = colorSpace && colorSpace->gammaIsLinear() |
| 275 | ? kRGBA_F16_SkColorType : kN32_SkColorType; |
| 276 | SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at, |
| 277 | sk_ref_sp(colorSpace)); |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 278 | return SkSpecialSurface::MakeRaster(info, nullptr); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 279 | } |
| 280 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 281 | sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override { |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 282 | SkBitmap subsetBM; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 283 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 284 | if (!fBitmap.extractSubset(&subsetBM, subset)) { |
| 285 | return nullptr; |
| 286 | } |
| 287 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 288 | return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(subset.width(), subset.height()), |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 289 | subsetBM, |
| 290 | &this->props()); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 291 | } |
| 292 | |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 293 | sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override { |
| 294 | SkBitmap subsetBM; |
| 295 | |
| 296 | if (!fBitmap.extractSubset(&subsetBM, subset)) { |
| 297 | return nullptr; |
| 298 | } |
| 299 | |
| 300 | return SkImage::MakeFromBitmap(subsetBM); |
| 301 | } |
| 302 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 303 | sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps, |
| 304 | const SkISize& size, SkAlphaType at) const override { |
| 305 | #if RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16 |
| 306 | SkColorSpace* colorSpace = outProps.colorSpace(); |
| 307 | #else |
| 308 | SkColorSpace* colorSpace = nullptr; |
| 309 | #endif |
| 310 | SkColorType colorType = colorSpace && colorSpace->gammaIsLinear() |
| 311 | ? kRGBA_F16_SkColorType : kN32_SkColorType; |
| 312 | SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at, |
| 313 | sk_ref_sp(colorSpace)); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 314 | return SkSurface::MakeRaster(info); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 315 | } |
| 316 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 317 | private: |
| 318 | SkBitmap fBitmap; |
| 319 | |
| 320 | typedef SkSpecialImage_Base INHERITED; |
| 321 | }; |
| 322 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 323 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromRaster(const SkIRect& subset, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 324 | const SkBitmap& bm, |
| 325 | const SkSurfaceProps* props) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 326 | SkASSERT(rect_fits(subset, bm.width(), bm.height())); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 327 | |
reed | bd2bd5c | 2016-07-25 14:26:02 -0700 | [diff] [blame] | 328 | if (!bm.pixelRef()) { |
| 329 | return nullptr; |
| 330 | } |
| 331 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 332 | const SkBitmap* srcBM = &bm; |
| 333 | SkBitmap tmpStorage; |
| 334 | // ImageFilters only handle N32 at the moment, so force our src to be that |
| 335 | if (!valid_for_imagefilters(bm.info())) { |
| 336 | if (!bm.copyTo(&tmpStorage, kN32_SkColorType)) { |
| 337 | return nullptr; |
| 338 | } |
| 339 | srcBM = &tmpStorage; |
| 340 | } |
| 341 | return sk_make_sp<SkSpecialImage_Raster>(subset, *srcBM, props); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | #if SK_SUPPORT_GPU |
| 345 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 346 | #include "GrTexture.h" |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 347 | #include "SkImage_Gpu.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 348 | |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 349 | class SkSpecialImage_Gpu : public SkSpecialImage_Base { |
| 350 | public: |
| 351 | SkSpecialImage_Gpu(const SkIRect& subset, |
| 352 | uint32_t uniqueID, sk_sp<GrTexture> tex, SkAlphaType at, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 353 | sk_sp<SkColorSpace> colorSpace, const SkSurfaceProps* props) |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 354 | : INHERITED(subset, uniqueID, props) |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 355 | , fContext(tex->getContext()) |
| 356 | , fAlphaType(at) |
| 357 | , fColorSpace(std::move(colorSpace)) |
| 358 | , fAddedRasterVersionToCache(false) { |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 359 | fSurfaceProxy = GrSurfaceProxy::MakeWrapped(std::move(tex)); |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | SkSpecialImage_Gpu(GrContext* context, const SkIRect& subset, |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 363 | uint32_t uniqueID, sk_sp<GrSurfaceProxy> proxy, SkAlphaType at, |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 364 | sk_sp<SkColorSpace> colorSpace, const SkSurfaceProps* props) |
| 365 | : INHERITED(subset, uniqueID, props) |
| 366 | , fContext(context) |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 367 | , fSurfaceProxy(std::move(proxy)) |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 368 | , fAlphaType(at) |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 369 | , fColorSpace(std::move(colorSpace)) |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 370 | , fAddedRasterVersionToCache(false) { |
| 371 | } |
| 372 | |
| 373 | ~SkSpecialImage_Gpu() override { |
| 374 | if (fAddedRasterVersionToCache.load()) { |
| 375 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 376 | } |
| 377 | } |
| 378 | |
brianosman | 80e9608 | 2016-08-16 07:09:47 -0700 | [diff] [blame] | 379 | SkAlphaType alphaType() const override { return fAlphaType; } |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 380 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 381 | size_t getSize() const override { return fSurfaceProxy->gpuMemorySize(); } |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 382 | |
| 383 | void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
| 384 | SkRect dst = SkRect::MakeXYWH(x, y, |
| 385 | this->subset().width(), this->subset().height()); |
| 386 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 387 | // TODO: add GrTextureProxy-backed SkImage_Gpus |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 388 | GrSurface* surf = fSurfaceProxy->instantiate(fContext->textureProvider()); |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 389 | if (!surf) { |
| 390 | return; |
| 391 | } |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 392 | |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 393 | // TODO: In this instance we know we're going to draw a sub-portion of the backing |
| 394 | // texture into the canvas so it is okay to wrap it in an SkImage. This poses |
| 395 | // some problems for full deferral however in that when the deferred SkImage_Gpu |
| 396 | // instantiates itself it is going to have to either be okay with having a larger |
| 397 | // than expected backing texture (unlikely) or the 'fit' of the SurfaceProxy needs |
| 398 | // to be tightened (if it is deferred). |
| 399 | auto img = sk_sp<SkImage>(new SkImage_Gpu(surf->width(), surf->height(), |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 400 | this->uniqueID(), fAlphaType, |
| 401 | sk_ref_sp(surf->asTexture()), |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 402 | fColorSpace, SkBudgeted::kNo)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 403 | |
reed | 77d6f7d | 2016-07-13 12:24:48 -0700 | [diff] [blame] | 404 | canvas->drawImageRect(img, this->subset(), |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 405 | dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 408 | GrContext* onGetContext() const override { return fContext; } |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 409 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 410 | // This entry point should go away in favor of asTextureProxy |
| 411 | sk_sp<GrTexture> onAsTextureRef(GrContext* context) const override { |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 412 | GrSurface* surf = fSurfaceProxy->instantiate(context->textureProvider()); |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 413 | if (!surf) { |
| 414 | return nullptr; |
| 415 | } |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 416 | return sk_ref_sp(surf->asTexture()); |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | sk_sp<GrTextureProxy> onAsTextureProxy(GrContext*) const override { |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 420 | return sk_ref_sp(fSurfaceProxy->asTextureProxy()); |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 421 | } |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 422 | |
| 423 | bool onGetROPixels(SkBitmap* dst) const override { |
| 424 | if (SkBitmapCache::Find(this->uniqueID(), dst)) { |
| 425 | SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 426 | SkASSERT(dst->isImmutable()); |
| 427 | SkASSERT(dst->getPixels()); |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | SkImageInfo info = SkImageInfo::MakeN32(this->width(), this->height(), |
brianosman | 80e9608 | 2016-08-16 07:09:47 -0700 | [diff] [blame] | 432 | this->alphaType(), fColorSpace); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 433 | |
| 434 | if (!dst->tryAllocPixels(info)) { |
| 435 | return false; |
| 436 | } |
| 437 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 438 | // Reading back to an SkBitmap ends deferral |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 439 | GrSurface* surface = fSurfaceProxy->instantiate(fContext->textureProvider()); |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 440 | if (!surface) { |
| 441 | return false; |
| 442 | } |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 443 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 444 | if (!surface->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig, |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 445 | dst->getPixels(), dst->rowBytes())) { |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 446 | return false; |
| 447 | } |
| 448 | |
| 449 | dst->pixelRef()->setImmutableWithID(this->uniqueID()); |
| 450 | SkBitmapCache::Add(this->uniqueID(), *dst); |
| 451 | fAddedRasterVersionToCache.store(true); |
| 452 | return true; |
| 453 | } |
| 454 | |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 455 | SkColorSpace* onGetColorSpace() const override { |
| 456 | return fColorSpace.get(); |
| 457 | } |
| 458 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 459 | sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps, |
| 460 | const SkISize& size, SkAlphaType at) const override { |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 461 | if (!fContext) { |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 462 | return nullptr; |
| 463 | } |
| 464 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 465 | SkColorSpace* colorSpace = outProps.colorSpace(); |
| 466 | return SkSpecialSurface::MakeRenderTarget( |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 467 | fContext, size.width(), size.height(), |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 468 | GrRenderableConfigForColorSpace(colorSpace), sk_ref_sp(colorSpace)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override { |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 472 | return SkSpecialImage::MakeDeferredFromGpu(fContext, |
| 473 | subset, |
| 474 | this->uniqueID(), |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 475 | fSurfaceProxy, |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 476 | fColorSpace, |
| 477 | &this->props(), |
| 478 | fAlphaType); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override { |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 482 | // TODO: add GrTextureProxy-backed SkImage_Gpus |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 483 | GrSurface* surf = fSurfaceProxy->instantiate(fContext->textureProvider()); |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 484 | if (!surf) { |
| 485 | return nullptr; |
| 486 | } |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 487 | |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 488 | if (0 == subset.fLeft && 0 == subset.fTop && |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 489 | fSurfaceProxy->width() == subset.width() && |
| 490 | fSurfaceProxy->height() == subset.height()) { |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 491 | // The existing GrTexture is already tight so reuse it in the SkImage |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 492 | return sk_make_sp<SkImage_Gpu>(surf->width(), surf->height(), |
| 493 | kNeedNewImageUniqueID, fAlphaType, |
| 494 | sk_ref_sp(surf->asTexture()), |
| 495 | fColorSpace, SkBudgeted::kYes); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 498 | GrSurfaceDesc desc = fSurfaceProxy->desc(); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 499 | desc.fWidth = subset.width(); |
| 500 | desc.fHeight = subset.height(); |
| 501 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 502 | sk_sp<GrTexture> subTx(fContext->textureProvider()->createTexture(desc, SkBudgeted::kYes)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 503 | if (!subTx) { |
| 504 | return nullptr; |
| 505 | } |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 506 | fContext->copySurface(subTx.get(), surf, subset, SkIPoint::Make(0, 0)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 507 | return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 508 | fAlphaType, std::move(subTx), fColorSpace, SkBudgeted::kYes); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 509 | } |
| 510 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 511 | sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps, |
| 512 | const SkISize& size, SkAlphaType at) const override { |
| 513 | SkColorSpace* colorSpace = outProps.colorSpace(); |
| 514 | SkColorType colorType = colorSpace && colorSpace->gammaIsLinear() |
| 515 | ? kRGBA_F16_SkColorType : kRGBA_8888_SkColorType; |
| 516 | SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at, |
| 517 | sk_ref_sp(colorSpace)); |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 518 | return SkSurface::MakeRenderTarget(fContext, SkBudgeted::kYes, info); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | private: |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 522 | GrContext* fContext; |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 523 | sk_sp<GrSurfaceProxy> fSurfaceProxy; |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 524 | const SkAlphaType fAlphaType; |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 525 | sk_sp<SkColorSpace> fColorSpace; |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 526 | mutable SkAtomic<bool> fAddedRasterVersionToCache; |
| 527 | |
| 528 | typedef SkSpecialImage_Base INHERITED; |
| 529 | }; |
| 530 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 531 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromGpu(const SkIRect& subset, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 532 | uint32_t uniqueID, |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 533 | sk_sp<GrTexture> tex, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 534 | sk_sp<SkColorSpace> colorSpace, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 535 | const SkSurfaceProps* props, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 536 | SkAlphaType at) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 537 | SkASSERT(rect_fits(subset, tex->width(), tex->height())); |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 538 | return sk_make_sp<SkSpecialImage_Gpu>(subset, uniqueID, std::move(tex), at, |
| 539 | std::move(colorSpace), props); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 542 | sk_sp<SkSpecialImage> SkSpecialImage::MakeDeferredFromGpu(GrContext* context, |
| 543 | const SkIRect& subset, |
| 544 | uint32_t uniqueID, |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 545 | sk_sp<GrSurfaceProxy> proxy, |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 546 | sk_sp<SkColorSpace> colorSpace, |
| 547 | const SkSurfaceProps* props, |
| 548 | SkAlphaType at) { |
| 549 | SkASSERT(rect_fits(subset, proxy->width(), proxy->height())); |
| 550 | return sk_make_sp<SkSpecialImage_Gpu>(context, subset, uniqueID, std::move(proxy), at, |
| 551 | std::move(colorSpace), props); |
| 552 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 553 | #endif |