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 | */ |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame^] | 7 | #include "SkSpecialImage.h" |
| 8 | |
| 9 | #if SK_SUPPORT_GPU |
| 10 | #include "GrTexture.h" |
| 11 | #include "GrTextureParams.h" |
| 12 | #include "SkGr.h" |
| 13 | #endif |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 14 | |
| 15 | #include "SkCanvas.h" |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 16 | #include "SkImage_Base.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 17 | #include "SkSpecialSurface.h" |
| 18 | |
| 19 | /////////////////////////////////////////////////////////////////////////////// |
| 20 | class SkSpecialImage_Base : public SkSpecialImage { |
| 21 | public: |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 22 | SkSpecialImage_Base(SkImageFilter::Proxy* proxy, const SkIRect& subset, uint32_t uniqueID) |
| 23 | : INHERITED(proxy, subset, uniqueID) { |
| 24 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 25 | virtual ~SkSpecialImage_Base() { } |
| 26 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 27 | virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 28 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 29 | virtual bool onPeekPixels(SkPixmap*) const { return false; } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 30 | |
| 31 | virtual GrTexture* onPeekTexture() const { return nullptr; } |
| 32 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 33 | virtual bool testingOnlyOnGetROPixels(SkBitmap*) const = 0; |
| 34 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 35 | // Delete this entry point ASAP (see skbug.com/4965) |
robertphillips | ab01ccd | 2016-03-08 10:45:32 -0800 | [diff] [blame] | 36 | virtual bool getBitmapDeprecated(SkBitmap* result) const = 0; |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 37 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 38 | virtual sk_sp<SkSpecialSurface> onMakeSurface(const SkImageInfo& info) const = 0; |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 39 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 40 | virtual sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const = 0; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 41 | |
| 42 | private: |
| 43 | typedef SkSpecialImage INHERITED; |
| 44 | }; |
| 45 | |
| 46 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 47 | static inline const SkSpecialImage_Base* as_SIB(const SkSpecialImage* image) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 48 | return static_cast<const SkSpecialImage_Base*>(image); |
| 49 | } |
| 50 | |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame^] | 51 | sk_sp<SkSpecialImage> SkSpecialImage::makeTextureImage(SkImageFilter::Proxy* proxy, |
| 52 | GrContext* context) { |
| 53 | #if SK_SUPPORT_GPU |
| 54 | if (!context) { |
| 55 | return nullptr; |
| 56 | } |
| 57 | if (GrTexture* peek = as_SIB(this)->peekTexture()) { |
| 58 | return peek->getContext() == context ? sk_sp<SkSpecialImage>(SkRef(this)) : nullptr; |
| 59 | } |
| 60 | |
| 61 | SkBitmap bmp; |
| 62 | if (!this->internal_getBM(&bmp)) { |
| 63 | return nullptr; |
| 64 | } |
| 65 | |
| 66 | SkAutoTUnref<GrTexture> resultTex( |
| 67 | GrRefCachedBitmapTexture(context, bmp, GrTextureParams::ClampNoFilter())); |
| 68 | if (!resultTex) { |
| 69 | return nullptr; |
| 70 | } |
| 71 | |
| 72 | SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 73 | |
| 74 | return SkSpecialImage::MakeFromGpu(proxy, |
| 75 | SkIRect::MakeWH(resultTex->width(), resultTex->height()), |
| 76 | this->uniqueID(), |
| 77 | resultTex, at); |
| 78 | #else |
| 79 | return nullptr; |
| 80 | #endif |
| 81 | } |
| 82 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 83 | void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 84 | return as_SIB(this)->onDraw(canvas, x, y, paint); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 85 | } |
| 86 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 87 | bool SkSpecialImage::peekPixels(SkPixmap* pixmap) const { |
| 88 | return as_SIB(this)->onPeekPixels(pixmap); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | GrTexture* SkSpecialImage::peekTexture() const { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 92 | return as_SIB(this)->onPeekTexture(); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 93 | } |
| 94 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 95 | bool SkSpecialImage::testingOnlyGetROPixels(SkBitmap* result) const { |
| 96 | return as_SIB(this)->testingOnlyOnGetROPixels(result); |
| 97 | } |
| 98 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 99 | sk_sp<SkSpecialSurface> SkSpecialImage::makeSurface(const SkImageInfo& info) const { |
| 100 | return as_SIB(this)->onMakeSurface(info); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 101 | } |
| 102 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 103 | sk_sp<SkSpecialImage> SkSpecialImage::makeSubset(const SkIRect& subset) const { |
| 104 | return as_SIB(this)->onMakeSubset(subset); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 105 | } |
| 106 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 107 | #if SK_SUPPORT_GPU |
| 108 | #include "SkGr.h" |
| 109 | #include "SkGrPixelRef.h" |
| 110 | #endif |
| 111 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 112 | sk_sp<SkSpecialImage> SkSpecialImage::internal_fromBM(SkImageFilter::Proxy* proxy, |
| 113 | const SkBitmap& src) { |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 114 | // Need to test offset case! (see skbug.com/4967) |
| 115 | if (src.getTexture()) { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 116 | return SkSpecialImage::MakeFromGpu(proxy, |
| 117 | src.bounds(), |
| 118 | src.getGenerationID(), |
| 119 | src.getTexture()); |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 120 | } |
| 121 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 122 | return SkSpecialImage::MakeFromRaster(proxy, src.bounds(), src); |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | bool SkSpecialImage::internal_getBM(SkBitmap* result) { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 126 | const SkSpecialImage_Base* ib = as_SIB(this); |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 127 | |
| 128 | // TODO: need to test offset case! (see skbug.com/4967) |
robertphillips | ab01ccd | 2016-03-08 10:45:32 -0800 | [diff] [blame] | 129 | return ib->getBitmapDeprecated(result); |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 130 | } |
| 131 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 132 | SkImageFilter::Proxy* SkSpecialImage::internal_getProxy() const { |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 133 | return fProxy; |
| 134 | } |
| 135 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 136 | /////////////////////////////////////////////////////////////////////////////// |
| 137 | #include "SkImage.h" |
| 138 | #if SK_SUPPORT_GPU |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 139 | #include "SkGrPriv.h" |
| 140 | #endif |
| 141 | |
| 142 | class SkSpecialImage_Image : public SkSpecialImage_Base { |
| 143 | public: |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 144 | SkSpecialImage_Image(SkImageFilter::Proxy* proxy, |
| 145 | const SkIRect& subset, |
| 146 | sk_sp<SkImage> image) |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 147 | : INHERITED(proxy, subset, image->uniqueID()) |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 148 | , fImage(image) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | ~SkSpecialImage_Image() override { } |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 152 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 153 | bool isOpaque() const override { return fImage->isOpaque(); } |
| 154 | |
| 155 | size_t getSize() const override { |
| 156 | #if SK_SUPPORT_GPU |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 157 | if (GrTexture* texture = as_IB(fImage.get())->peekTexture()) { |
| 158 | return texture->gpuMemorySize(); |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 159 | } else |
| 160 | #endif |
| 161 | { |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 162 | SkPixmap pm; |
| 163 | if (fImage->peekPixels(&pm)) { |
| 164 | return pm.height() * pm.rowBytes(); |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 170 | void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 171 | SkRect dst = SkRect::MakeXYWH(x, y, this->subset().width(), this->subset().height()); |
| 172 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 173 | canvas->drawImageRect(fImage.get(), this->subset(), |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 174 | dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 175 | } |
| 176 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 177 | bool onPeekPixels(SkPixmap* pixmap) const override { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 178 | return fImage->peekPixels(pixmap); |
| 179 | } |
| 180 | |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 181 | GrTexture* onPeekTexture() const override { return as_IB(fImage.get())->peekTexture(); } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 182 | |
robertphillips | ab01ccd | 2016-03-08 10:45:32 -0800 | [diff] [blame] | 183 | bool getBitmapDeprecated(SkBitmap* result) const override { |
robertphillips | 6ac97b7 | 2016-03-09 05:17:10 -0800 | [diff] [blame] | 184 | #if SK_SUPPORT_GPU |
| 185 | if (GrTexture* texture = as_IB(fImage.get())->peekTexture()) { |
| 186 | const SkImageInfo info = GrMakeInfoFromTexture(texture, |
| 187 | fImage->width(), fImage->height(), |
| 188 | fImage->isOpaque()); |
| 189 | if (!result->setInfo(info)) { |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | result->setPixelRef(new SkGrPixelRef(info, texture))->unref(); |
| 194 | return true; |
| 195 | } |
| 196 | #endif |
| 197 | |
| 198 | return as_IB(fImage.get())->asBitmapForImageFilters(result); |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 199 | } |
| 200 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 201 | bool testingOnlyOnGetROPixels(SkBitmap* result) const override { |
robertphillips | 6ac97b7 | 2016-03-09 05:17:10 -0800 | [diff] [blame] | 202 | return fImage->asLegacyBitmap(result, SkImage::kRO_LegacyBitmapMode); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 203 | } |
| 204 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 205 | sk_sp<SkSpecialSurface> onMakeSurface(const SkImageInfo& info) const override { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 206 | #if SK_SUPPORT_GPU |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 207 | GrTexture* texture = as_IB(fImage.get())->peekTexture(); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 208 | if (texture) { |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 209 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 210 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 211 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 212 | return SkSpecialSurface::MakeRenderTarget(this->proxy(), texture->getContext(), desc); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 213 | } |
| 214 | #endif |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 215 | return SkSpecialSurface::MakeRaster(this->proxy(), info, nullptr); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 216 | } |
| 217 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 218 | sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override { |
| 219 | sk_sp<SkImage> subsetImg(fImage->makeSubset(subset)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 220 | if (!subsetImg) { |
| 221 | return nullptr; |
| 222 | } |
| 223 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 224 | return SkSpecialImage::MakeFromImage(this->internal_getProxy(), |
| 225 | SkIRect::MakeWH(subset.width(), subset.height()), |
| 226 | subsetImg); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 227 | } |
| 228 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 229 | private: |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 230 | sk_sp<SkImage> fImage; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 231 | |
| 232 | typedef SkSpecialImage_Base INHERITED; |
| 233 | }; |
| 234 | |
| 235 | #ifdef SK_DEBUG |
| 236 | static bool rect_fits(const SkIRect& rect, int width, int height) { |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 237 | if (0 == width && 0 == height) { |
| 238 | SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == rect.fBottom); |
| 239 | return true; |
| 240 | } |
| 241 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 242 | return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight && |
| 243 | rect.fRight >= 0 && rect.fRight <= width && |
| 244 | rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom && |
| 245 | rect.fBottom >= 0 && rect.fBottom <= height; |
| 246 | } |
| 247 | #endif |
| 248 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 249 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromImage(SkImageFilter::Proxy* proxy, |
| 250 | const SkIRect& subset, |
| 251 | sk_sp<SkImage> image) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 252 | SkASSERT(rect_fits(subset, image->width(), image->height())); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 253 | |
| 254 | return sk_make_sp<SkSpecialImage_Image>(proxy, subset, image); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /////////////////////////////////////////////////////////////////////////////// |
| 258 | #include "SkBitmap.h" |
| 259 | #include "SkImageInfo.h" |
| 260 | #include "SkPixelRef.h" |
| 261 | |
| 262 | class SkSpecialImage_Raster : public SkSpecialImage_Base { |
| 263 | public: |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 264 | SkSpecialImage_Raster(SkImageFilter::Proxy* proxy, const SkIRect& subset, const SkBitmap& bm) |
| 265 | : INHERITED(proxy, subset, bm.getGenerationID()) |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 266 | , fBitmap(bm) { |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 267 | if (bm.pixelRef() && bm.pixelRef()->isPreLocked()) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 268 | // we only preemptively lock if there is no chance of triggering something expensive |
| 269 | // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already. |
| 270 | fBitmap.lockPixels(); |
| 271 | } |
| 272 | } |
| 273 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 274 | SkSpecialImage_Raster(SkImageFilter::Proxy* proxy, |
| 275 | const SkIRect& subset, |
| 276 | const SkPixmap& pixmap, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 277 | RasterReleaseProc releaseProc, |
| 278 | ReleaseContext context) |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 279 | : INHERITED(proxy, subset, kNeedNewImageUniqueID_SpecialImage) { |
| 280 | fBitmap.installPixels(pixmap.info(), pixmap.writable_addr(), |
| 281 | pixmap.rowBytes(), pixmap.ctable(), |
| 282 | releaseProc, context); |
| 283 | } |
| 284 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 285 | ~SkSpecialImage_Raster() override { } |
| 286 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 287 | bool isOpaque() const override { return fBitmap.isOpaque(); } |
| 288 | |
| 289 | size_t getSize() const override { return fBitmap.getSize(); } |
| 290 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 291 | void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 292 | SkRect dst = SkRect::MakeXYWH(x, y, |
| 293 | this->subset().width(), this->subset().height()); |
| 294 | |
| 295 | canvas->drawBitmapRect(fBitmap, this->subset(), |
| 296 | dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 297 | } |
| 298 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 299 | bool onPeekPixels(SkPixmap* pixmap) const override { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 300 | const SkImageInfo info = fBitmap.info(); |
| 301 | if ((kUnknown_SkColorType == info.colorType()) || !fBitmap.getPixels()) { |
| 302 | return false; |
| 303 | } |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 304 | |
| 305 | return fBitmap.peekPixels(pixmap); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 306 | } |
| 307 | |
robertphillips | ab01ccd | 2016-03-08 10:45:32 -0800 | [diff] [blame] | 308 | bool getBitmapDeprecated(SkBitmap* result) const override { |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 309 | *result = fBitmap; |
| 310 | return true; |
| 311 | } |
| 312 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 313 | bool testingOnlyOnGetROPixels(SkBitmap* result) const override { |
| 314 | *result = fBitmap; |
| 315 | return true; |
| 316 | } |
| 317 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 318 | sk_sp<SkSpecialSurface> onMakeSurface(const SkImageInfo& info) const override { |
| 319 | return SkSpecialSurface::MakeRaster(this->proxy(), info, nullptr); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 320 | } |
| 321 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 322 | sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override { |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 323 | SkBitmap subsetBM; |
| 324 | |
| 325 | if (!fBitmap.extractSubset(&subsetBM, subset)) { |
| 326 | return nullptr; |
| 327 | } |
| 328 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 329 | return SkSpecialImage::MakeFromRaster(this->internal_getProxy(), |
| 330 | SkIRect::MakeWH(subset.width(), subset.height()), |
| 331 | subsetBM); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 332 | } |
| 333 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 334 | private: |
| 335 | SkBitmap fBitmap; |
| 336 | |
| 337 | typedef SkSpecialImage_Base INHERITED; |
| 338 | }; |
| 339 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 340 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromRaster(SkImageFilter::Proxy* proxy, |
| 341 | const SkIRect& subset, |
| 342 | const SkBitmap& bm) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 343 | SkASSERT(nullptr == bm.getTexture()); |
| 344 | SkASSERT(rect_fits(subset, bm.width(), bm.height())); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 345 | |
| 346 | return sk_make_sp<SkSpecialImage_Raster>(proxy, subset, bm); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 347 | } |
| 348 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 349 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromPixmap(SkImageFilter::Proxy* proxy, |
| 350 | const SkIRect& subset, |
| 351 | const SkPixmap& src, |
| 352 | RasterReleaseProc releaseProc, |
| 353 | ReleaseContext context) { |
| 354 | if (!src.addr()) { |
| 355 | return nullptr; |
| 356 | } |
| 357 | |
| 358 | return sk_make_sp<SkSpecialImage_Raster>(proxy, subset, src, releaseProc, context); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 362 | #if SK_SUPPORT_GPU |
| 363 | /////////////////////////////////////////////////////////////////////////////// |
| 364 | #include "GrTexture.h" |
| 365 | |
| 366 | class SkSpecialImage_Gpu : public SkSpecialImage_Base { |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 367 | public: |
| 368 | SkSpecialImage_Gpu(SkImageFilter::Proxy* proxy, const SkIRect& subset, |
| 369 | uint32_t uniqueID, GrTexture* tex, SkAlphaType at) |
| 370 | : INHERITED(proxy, subset, uniqueID) |
| 371 | , fTexture(SkRef(tex)) |
| 372 | , fAlphaType(at) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | ~SkSpecialImage_Gpu() override { } |
| 376 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 377 | bool isOpaque() const override { |
| 378 | return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType; |
| 379 | } |
| 380 | |
| 381 | size_t getSize() const override { return fTexture->gpuMemorySize(); } |
| 382 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 383 | void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 384 | SkRect dst = SkRect::MakeXYWH(x, y, |
| 385 | this->subset().width(), this->subset().height()); |
| 386 | |
| 387 | SkBitmap bm; |
| 388 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 389 | GrWrapTextureInBitmap(fTexture, |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 390 | fTexture->width(), fTexture->height(), this->isOpaque(), &bm); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 391 | |
| 392 | canvas->drawBitmapRect(bm, this->subset(), |
| 393 | dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 394 | } |
| 395 | |
| 396 | GrTexture* onPeekTexture() const override { return fTexture; } |
| 397 | |
robertphillips | ab01ccd | 2016-03-08 10:45:32 -0800 | [diff] [blame] | 398 | bool getBitmapDeprecated(SkBitmap* result) const override { |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 399 | const SkImageInfo info = GrMakeInfoFromTexture(fTexture, |
| 400 | this->width(), this->height(), |
| 401 | this->isOpaque()); |
| 402 | if (!result->setInfo(info)) { |
| 403 | return false; |
| 404 | } |
| 405 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 406 | const SkImageInfo prInfo = info.makeWH(fTexture->width(), fTexture->height()); |
| 407 | |
| 408 | SkAutoTUnref<SkGrPixelRef> pixelRef(new SkGrPixelRef(prInfo, fTexture)); |
| 409 | result->setPixelRef(pixelRef, this->subset().fLeft, this->subset().fTop); |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 410 | return true; |
| 411 | } |
| 412 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 413 | bool testingOnlyOnGetROPixels(SkBitmap* result) const override { |
| 414 | |
| 415 | const SkImageInfo info = SkImageInfo::MakeN32(this->width(), |
| 416 | this->height(), |
| 417 | this->isOpaque() ? kOpaque_SkAlphaType |
| 418 | : kPremul_SkAlphaType); |
| 419 | if (!result->tryAllocPixels(info)) { |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | if (!fTexture->readPixels(0, 0, result->width(), result->height(), kSkia8888_GrPixelConfig, |
| 424 | result->getPixels(), result->rowBytes())) { |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | result->pixelRef()->setImmutable(); |
| 429 | return true; |
| 430 | } |
| 431 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 432 | sk_sp<SkSpecialSurface> onMakeSurface(const SkImageInfo& info) const override { |
brianosman | c571c00 | 2016-03-17 13:01:26 -0700 | [diff] [blame] | 433 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 434 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 435 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 436 | return SkSpecialSurface::MakeRenderTarget(this->proxy(), fTexture->getContext(), desc); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 437 | } |
| 438 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 439 | sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override { |
| 440 | return SkSpecialImage::MakeFromGpu(this->internal_getProxy(), |
| 441 | subset, |
| 442 | this->uniqueID(), |
| 443 | fTexture, |
| 444 | fAlphaType); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 445 | } |
| 446 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 447 | private: |
| 448 | SkAutoTUnref<GrTexture> fTexture; |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 449 | const SkAlphaType fAlphaType; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 450 | |
| 451 | typedef SkSpecialImage_Base INHERITED; |
| 452 | }; |
| 453 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 454 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromGpu(SkImageFilter::Proxy* proxy, |
| 455 | const SkIRect& subset, |
| 456 | uint32_t uniqueID, |
| 457 | GrTexture* tex, |
| 458 | SkAlphaType at) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 459 | SkASSERT(rect_fits(subset, tex->width(), tex->height())); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 460 | return sk_make_sp<SkSpecialImage_Gpu>(proxy, subset, uniqueID, tex, at); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | #else |
| 464 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 465 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromGpu(SkImageFilter::Proxy* proxy, |
| 466 | const SkIRect& subset, |
| 467 | uint32_t uniqueID, |
| 468 | GrTexture* tex, |
| 469 | SkAlphaType at) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 470 | return nullptr; |
| 471 | } |
| 472 | |
| 473 | #endif |