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