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 | |
| 23 | virtual bool onPeekPixels(SkPixmap*) const { return false; } |
| 24 | |
| 25 | virtual GrTexture* onPeekTexture() const { return nullptr; } |
| 26 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 27 | // Delete this entry point ASAP (see skbug.com/4965) |
| 28 | virtual bool getBitmap(SkBitmap* result) const = 0; |
| 29 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 30 | virtual SkSpecialSurface* onNewSurface(const SkImageInfo& info) const { return nullptr; } |
| 31 | |
| 32 | private: |
| 33 | typedef SkSpecialImage INHERITED; |
| 34 | }; |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame^] | 37 | static inline const SkSpecialImage_Base* as_SIB(const SkSpecialImage* image) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 38 | return static_cast<const SkSpecialImage_Base*>(image); |
| 39 | } |
| 40 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 41 | void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame^] | 42 | return as_SIB(this)->onDraw(canvas, x, y, paint); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | bool SkSpecialImage::peekPixels(SkPixmap* pixmap) const { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame^] | 46 | return as_SIB(this)->onPeekPixels(pixmap); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | GrTexture* SkSpecialImage::peekTexture() const { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame^] | 50 | return as_SIB(this)->onPeekTexture(); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | SkSpecialSurface* SkSpecialImage::newSurface(const SkImageInfo& info) const { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame^] | 54 | return as_SIB(this)->onNewSurface(info); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 55 | } |
| 56 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 57 | #if SK_SUPPORT_GPU |
| 58 | #include "SkGr.h" |
| 59 | #include "SkGrPixelRef.h" |
| 60 | #endif |
| 61 | |
| 62 | SkSpecialImage* SkSpecialImage::internal_fromBM(SkImageFilter::Proxy* proxy, |
| 63 | const SkBitmap& src) { |
| 64 | // Need to test offset case! (see skbug.com/4967) |
| 65 | if (src.getTexture()) { |
| 66 | return SkSpecialImage::NewFromGpu(proxy, |
| 67 | src.bounds(), |
| 68 | src.getGenerationID(), |
| 69 | src.getTexture()); |
| 70 | } |
| 71 | |
| 72 | return SkSpecialImage::NewFromRaster(proxy, src.bounds(), src); |
| 73 | } |
| 74 | |
| 75 | bool SkSpecialImage::internal_getBM(SkBitmap* result) { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame^] | 76 | const SkSpecialImage_Base* ib = as_SIB(this); |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 77 | |
| 78 | // TODO: need to test offset case! (see skbug.com/4967) |
| 79 | return ib->getBitmap(result); |
| 80 | } |
| 81 | |
| 82 | SkImageFilter::Proxy* SkSpecialImage::internal_getProxy() { |
| 83 | SkASSERT(fProxy); |
| 84 | return fProxy; |
| 85 | } |
| 86 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 87 | /////////////////////////////////////////////////////////////////////////////// |
| 88 | #include "SkImage.h" |
| 89 | #if SK_SUPPORT_GPU |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 90 | #include "SkGrPriv.h" |
| 91 | #endif |
| 92 | |
| 93 | class SkSpecialImage_Image : public SkSpecialImage_Base { |
| 94 | public: |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 95 | SkSpecialImage_Image(SkImageFilter::Proxy* proxy, const SkIRect& subset, const SkImage* image) |
| 96 | : INHERITED(proxy, subset, image->uniqueID()) |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 97 | , fImage(SkRef(image)) { |
| 98 | } |
| 99 | |
| 100 | ~SkSpecialImage_Image() override { } |
| 101 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 102 | bool isOpaque() const override { return fImage->isOpaque(); } |
| 103 | |
| 104 | size_t getSize() const override { |
| 105 | #if SK_SUPPORT_GPU |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame^] | 106 | if (GrTexture* texture = as_IB(fImage.get())->peekTexture()) { |
| 107 | return texture->gpuMemorySize(); |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 108 | } else |
| 109 | #endif |
| 110 | { |
| 111 | SkImageInfo info; |
| 112 | size_t rowBytes; |
| 113 | |
| 114 | if (fImage->peekPixels(&info, &rowBytes)) { |
| 115 | return info.height() * rowBytes; |
| 116 | } |
| 117 | } |
| 118 | return 0; |
| 119 | } |
| 120 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 121 | void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 122 | SkRect dst = SkRect::MakeXYWH(x, y, this->subset().width(), this->subset().height()); |
| 123 | |
| 124 | canvas->drawImageRect(fImage, this->subset(), |
| 125 | dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 126 | } |
| 127 | |
| 128 | bool onPeekPixels(SkPixmap* pixmap) const override { |
| 129 | return fImage->peekPixels(pixmap); |
| 130 | } |
| 131 | |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame^] | 132 | GrTexture* onPeekTexture() const override { return as_IB(fImage.get())->peekTexture(); } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 133 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 134 | bool getBitmap(SkBitmap* result) const override { |
| 135 | return false; |
| 136 | } |
| 137 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 138 | SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
| 139 | #if SK_SUPPORT_GPU |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame^] | 140 | GrTexture* texture = as_IB(fImage.get())->peekTexture(); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 141 | if (texture) { |
| 142 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); |
| 143 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 144 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 145 | return SkSpecialSurface::NewRenderTarget(this->proxy(), texture->getContext(), desc); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 146 | } |
| 147 | #endif |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 148 | return SkSpecialSurface::NewRaster(this->proxy(), info, nullptr); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | private: |
| 152 | SkAutoTUnref<const SkImage> fImage; |
| 153 | |
| 154 | typedef SkSpecialImage_Base INHERITED; |
| 155 | }; |
| 156 | |
| 157 | #ifdef SK_DEBUG |
| 158 | static bool rect_fits(const SkIRect& rect, int width, int height) { |
| 159 | return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight && |
| 160 | rect.fRight >= 0 && rect.fRight <= width && |
| 161 | rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom && |
| 162 | rect.fBottom >= 0 && rect.fBottom <= height; |
| 163 | } |
| 164 | #endif |
| 165 | |
| 166 | SkSpecialImage* SkSpecialImage::NewFromImage(const SkIRect& subset, const SkImage* image) { |
| 167 | SkASSERT(rect_fits(subset, image->width(), image->height())); |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 168 | return new SkSpecialImage_Image(nullptr, subset, image); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /////////////////////////////////////////////////////////////////////////////// |
| 172 | #include "SkBitmap.h" |
| 173 | #include "SkImageInfo.h" |
| 174 | #include "SkPixelRef.h" |
| 175 | |
| 176 | class SkSpecialImage_Raster : public SkSpecialImage_Base { |
| 177 | public: |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 178 | SkSpecialImage_Raster(SkImageFilter::Proxy* proxy, const SkIRect& subset, const SkBitmap& bm) |
| 179 | : INHERITED(proxy, subset, bm.getGenerationID()) |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 180 | , fBitmap(bm) { |
| 181 | if (bm.pixelRef()->isPreLocked()) { |
| 182 | // we only preemptively lock if there is no chance of triggering something expensive |
| 183 | // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already. |
| 184 | fBitmap.lockPixels(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | ~SkSpecialImage_Raster() override { } |
| 189 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 190 | bool isOpaque() const override { return fBitmap.isOpaque(); } |
| 191 | |
| 192 | size_t getSize() const override { return fBitmap.getSize(); } |
| 193 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 194 | void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 195 | SkRect dst = SkRect::MakeXYWH(x, y, |
| 196 | this->subset().width(), this->subset().height()); |
| 197 | |
| 198 | canvas->drawBitmapRect(fBitmap, this->subset(), |
| 199 | dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 200 | } |
| 201 | |
| 202 | bool onPeekPixels(SkPixmap* pixmap) const override { |
| 203 | const SkImageInfo info = fBitmap.info(); |
| 204 | if ((kUnknown_SkColorType == info.colorType()) || !fBitmap.getPixels()) { |
| 205 | return false; |
| 206 | } |
| 207 | const void* pixels = fBitmap.getPixels(); |
| 208 | if (pixels) { |
| 209 | if (pixmap) { |
| 210 | pixmap->reset(info, pixels, fBitmap.rowBytes()); |
| 211 | } |
| 212 | return true; |
| 213 | } |
| 214 | return false; |
| 215 | } |
| 216 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 217 | bool getBitmap(SkBitmap* result) const override { |
| 218 | *result = fBitmap; |
| 219 | return true; |
| 220 | } |
| 221 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 222 | SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 223 | return SkSpecialSurface::NewRaster(this->proxy(), info, nullptr); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | private: |
| 227 | SkBitmap fBitmap; |
| 228 | |
| 229 | typedef SkSpecialImage_Base INHERITED; |
| 230 | }; |
| 231 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 232 | SkSpecialImage* SkSpecialImage::NewFromRaster(SkImageFilter::Proxy* proxy, |
| 233 | const SkIRect& subset, |
| 234 | const SkBitmap& bm) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 235 | SkASSERT(nullptr == bm.getTexture()); |
| 236 | SkASSERT(rect_fits(subset, bm.width(), bm.height())); |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 237 | return new SkSpecialImage_Raster(proxy, subset, bm); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | #if SK_SUPPORT_GPU |
| 241 | /////////////////////////////////////////////////////////////////////////////// |
| 242 | #include "GrTexture.h" |
| 243 | |
| 244 | class SkSpecialImage_Gpu : public SkSpecialImage_Base { |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 245 | public: |
| 246 | SkSpecialImage_Gpu(SkImageFilter::Proxy* proxy, const SkIRect& subset, |
| 247 | uint32_t uniqueID, GrTexture* tex, SkAlphaType at) |
| 248 | : INHERITED(proxy, subset, uniqueID) |
| 249 | , fTexture(SkRef(tex)) |
| 250 | , fAlphaType(at) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | ~SkSpecialImage_Gpu() override { } |
| 254 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 255 | bool isOpaque() const override { |
| 256 | return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType; |
| 257 | } |
| 258 | |
| 259 | size_t getSize() const override { return fTexture->gpuMemorySize(); } |
| 260 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 261 | void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 262 | SkRect dst = SkRect::MakeXYWH(x, y, |
| 263 | this->subset().width(), this->subset().height()); |
| 264 | |
| 265 | SkBitmap bm; |
| 266 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 267 | GrWrapTextureInBitmap(fTexture, |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 268 | fTexture->width(), fTexture->height(), this->isOpaque(), &bm); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 269 | |
| 270 | canvas->drawBitmapRect(bm, this->subset(), |
| 271 | dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 272 | } |
| 273 | |
| 274 | GrTexture* onPeekTexture() const override { return fTexture; } |
| 275 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 276 | bool getBitmap(SkBitmap* result) const override { |
| 277 | const SkImageInfo info = GrMakeInfoFromTexture(fTexture, |
| 278 | this->width(), this->height(), |
| 279 | this->isOpaque()); |
| 280 | if (!result->setInfo(info)) { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | result->setPixelRef(new SkGrPixelRef(info, fTexture))->unref(); |
| 285 | return true; |
| 286 | } |
| 287 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 288 | SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
| 289 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); |
| 290 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 291 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 292 | return SkSpecialSurface::NewRenderTarget(this->proxy(), fTexture->getContext(), desc); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | private: |
| 296 | SkAutoTUnref<GrTexture> fTexture; |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 297 | const SkAlphaType fAlphaType; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 298 | |
| 299 | typedef SkSpecialImage_Base INHERITED; |
| 300 | }; |
| 301 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 302 | SkSpecialImage* SkSpecialImage::NewFromGpu(SkImageFilter::Proxy* proxy, |
| 303 | const SkIRect& subset, |
| 304 | uint32_t uniqueID, |
| 305 | GrTexture* tex, |
| 306 | SkAlphaType at) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 307 | SkASSERT(rect_fits(subset, tex->width(), tex->height())); |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 308 | return new SkSpecialImage_Gpu(proxy, subset, uniqueID, tex, at); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | #else |
| 312 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 313 | SkSpecialImage* SkSpecialImage::NewFromGpu(SkImageFilter::Proxy* proxy, |
| 314 | const SkIRect& subset, |
| 315 | uint32_t uniqueID, |
| 316 | GrTexture* tex, |
| 317 | SkAlphaType at) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 318 | return nullptr; |
| 319 | } |
| 320 | |
| 321 | #endif |