Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file | 5 * found in the LICENSE file |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
| 9 #include "SkSpecialImage.h" | 9 #include "SkSpecialImage.h" |
| 10 #include "SkSpecialSurface.h" | 10 #include "SkSpecialSurface.h" |
| 11 | 11 |
| 12 /////////////////////////////////////////////////////////////////////////////// | 12 /////////////////////////////////////////////////////////////////////////////// |
| 13 class SkSpecialImage_Base : public SkSpecialImage { | 13 class SkSpecialImage_Base : public SkSpecialImage { |
| 14 public: | 14 public: |
| 15 SkSpecialImage_Base(const SkIRect& subset) : INHERITED(subset) { } | 15 SkSpecialImage_Base(const SkIRect& subset, uint32_t uniqueID) : INHERITED(su bset, uniqueID) {} |
| 16 virtual ~SkSpecialImage_Base() { } | 16 virtual ~SkSpecialImage_Base() { } |
| 17 | 17 |
| 18 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0; | 18 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0; |
| 19 | 19 |
| 20 virtual bool onPeekPixels(SkPixmap*) const { return false; } | 20 virtual bool onPeekPixels(SkPixmap*) const { return false; } |
| 21 | 21 |
| 22 virtual GrTexture* onPeekTexture() const { return nullptr; } | 22 virtual GrTexture* onPeekTexture() const { return nullptr; } |
| 23 | 23 |
| 24 // Delete this entry point ASAP | |
| 25 virtual bool getBitmap(SkBitmap* result) const = 0; | |
| 26 | |
| 24 virtual SkSpecialSurface* onNewSurface(const SkImageInfo& info) const { retu rn nullptr; } | 27 virtual SkSpecialSurface* onNewSurface(const SkImageInfo& info) const { retu rn nullptr; } |
| 25 | 28 |
| 26 private: | 29 private: |
| 27 typedef SkSpecialImage INHERITED; | 30 typedef SkSpecialImage INHERITED; |
| 28 }; | 31 }; |
| 29 | 32 |
| 30 /////////////////////////////////////////////////////////////////////////////// | 33 /////////////////////////////////////////////////////////////////////////////// |
| 31 static inline const SkSpecialImage_Base* as_IB(const SkSpecialImage* image) { | 34 static inline const SkSpecialImage_Base* as_IB(const SkSpecialImage* image) { |
| 32 return static_cast<const SkSpecialImage_Base*>(image); | 35 return static_cast<const SkSpecialImage_Base*>(image); |
| 33 } | 36 } |
| 34 | 37 |
| 35 void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPain t* paint) const { | 38 void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPain t* paint) const { |
| 36 return as_IB(this)->onDraw(canvas, x, y, paint); | 39 return as_IB(this)->onDraw(canvas, x, y, paint); |
| 37 } | 40 } |
| 38 | 41 |
| 39 bool SkSpecialImage::peekPixels(SkPixmap* pixmap) const { | 42 bool SkSpecialImage::peekPixels(SkPixmap* pixmap) const { |
| 40 return as_IB(this)->onPeekPixels(pixmap); | 43 return as_IB(this)->onPeekPixels(pixmap); |
| 41 } | 44 } |
| 42 | 45 |
| 43 GrTexture* SkSpecialImage::peekTexture() const { | 46 GrTexture* SkSpecialImage::peekTexture() const { |
| 44 return as_IB(this)->onPeekTexture(); | 47 return as_IB(this)->onPeekTexture(); |
| 45 } | 48 } |
| 46 | 49 |
| 47 SkSpecialSurface* SkSpecialImage::newSurface(const SkImageInfo& info) const { | 50 SkSpecialSurface* SkSpecialImage::newSurface(const SkImageInfo& info) const { |
| 48 return as_IB(this)->onNewSurface(info); | 51 return as_IB(this)->onNewSurface(info); |
| 49 } | 52 } |
| 50 | 53 |
| 54 #if SK_SUPPORT_GPU | |
| 55 #include "SkGr.h" | |
| 56 #include "SkGrPixelRef.h" | |
| 57 #endif | |
| 58 | |
| 59 SkSpecialImage* SkSpecialImage::internal_fromBM(const SkBitmap& src) { | |
| 60 // Need to test offset case! | |
| 61 if (src.getTexture()) { | |
| 62 return SkSpecialImage::NewFromGpu(src.bounds(), src.getGenerationID(), s rc.getTexture()); | |
| 63 } | |
| 64 | |
| 65 return SkSpecialImage::NewFromRaster(src.bounds(), src); | |
| 66 } | |
| 67 | |
| 68 bool SkSpecialImage::internal_getBM(SkBitmap* result) { | |
| 69 const SkSpecialImage_Base* ib = as_IB(this); | |
| 70 | |
| 71 // TODO: need to test offset case! | |
| 72 return ib->getBitmap(result); | |
| 73 } | |
| 74 | |
| 75 #include "SkBitmapDevice.h" | |
| 76 #if SK_SUPPORT_GPU | |
| 77 #include "SkGpuDevice.h" | |
| 78 #endif | |
| 79 | |
| 80 SkImageFilter::Proxy* SkSpecialImage::internal_getProxy() { | |
| 81 if (fProxy) { | |
| 82 SkASSERT(fDevice); | |
| 83 return fProxy; | |
| 84 } | |
| 85 | |
| 86 const SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16); | |
| 87 const SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); | |
| 88 | |
| 89 #if SK_SUPPORT_GPU | |
| 90 if (this->peekTexture()) { | |
| 91 GrContext* context = this->peekTexture()->getContext(); | |
| 92 | |
| 93 // Youch! | |
| 94 fDevice.reset(SkGpuDevice::Create(context, SkSurface::kYes_Budgeted, inf o, 0, &props, | |
|
Stephen White
2016/02/17 17:00:02
This doesn't make sense to me. Why do we need to h
robertphillips
2016/02/17 19:10:28
Done.
| |
| 95 SkGpuDevice::kUninit_InitContents)); | |
| 96 | |
| 97 fProxy = new SkImageFilter::DeviceProxy(fDevice); | |
| 98 return fProxy; | |
| 99 } | |
| 100 #endif | |
| 101 | |
| 102 fDevice.reset(SkBitmapDevice::Create(info, props)); | |
| 103 | |
| 104 fProxy = new SkImageFilter::DeviceProxy(fDevice); | |
| 105 return fProxy; | |
| 106 } | |
| 107 | |
| 51 /////////////////////////////////////////////////////////////////////////////// | 108 /////////////////////////////////////////////////////////////////////////////// |
| 52 #include "SkImage.h" | 109 #include "SkImage.h" |
| 53 #if SK_SUPPORT_GPU | 110 #if SK_SUPPORT_GPU |
| 54 #include "SkGr.h" | |
| 55 #include "SkGrPriv.h" | 111 #include "SkGrPriv.h" |
| 56 #endif | 112 #endif |
| 57 | 113 |
| 58 class SkSpecialImage_Image : public SkSpecialImage_Base { | 114 class SkSpecialImage_Image : public SkSpecialImage_Base { |
| 59 public: | 115 public: |
| 60 SkSpecialImage_Image(const SkIRect& subset, const SkImage* image) | 116 SkSpecialImage_Image(const SkIRect& subset, const SkImage* image) |
| 61 : INHERITED(subset) | 117 : INHERITED(subset, image->uniqueID()) |
| 62 , fImage(SkRef(image)) { | 118 , fImage(SkRef(image)) { |
| 63 } | 119 } |
| 64 | 120 |
| 65 ~SkSpecialImage_Image() override { } | 121 ~SkSpecialImage_Image() override { } |
| 66 | 122 |
| 123 bool isOpaque() const override { return fImage->isOpaque(); } | |
| 124 | |
| 125 size_t getSize() const override { | |
| 126 if (fImage->getTexture()) { | |
| 127 return fImage->getTexture()->gpuMemorySize(); | |
| 128 } else { | |
| 129 SkImageInfo info; | |
| 130 size_t rowBytes; | |
| 131 | |
| 132 if (fImage->peekPixels(&info, &rowBytes)) { | |
| 133 return info.height() * rowBytes; | |
| 134 } | |
| 135 } | |
| 136 return 0; | |
| 137 } | |
| 138 | |
| 67 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { | 139 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
| 68 SkRect dst = SkRect::MakeXYWH(x, y, this->subset().width(), this->subset ().height()); | 140 SkRect dst = SkRect::MakeXYWH(x, y, this->subset().width(), this->subset ().height()); |
| 69 | 141 |
| 70 canvas->drawImageRect(fImage, this->subset(), | 142 canvas->drawImageRect(fImage, this->subset(), |
| 71 dst, paint, SkCanvas::kStrict_SrcRectConstraint); | 143 dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 72 } | 144 } |
| 73 | 145 |
| 74 bool onPeekPixels(SkPixmap* pixmap) const override { | 146 bool onPeekPixels(SkPixmap* pixmap) const override { |
| 75 return fImage->peekPixels(pixmap); | 147 return fImage->peekPixels(pixmap); |
| 76 } | 148 } |
| 77 | 149 |
| 78 GrTexture* onPeekTexture() const override { return fImage->getTexture(); } | 150 GrTexture* onPeekTexture() const override { return fImage->getTexture(); } |
| 79 | 151 |
| 152 bool getBitmap(SkBitmap* result) const override { | |
| 153 return false; | |
| 154 } | |
| 155 | |
| 80 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { | 156 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
| 81 #if SK_SUPPORT_GPU | 157 #if SK_SUPPORT_GPU |
| 82 GrTexture* texture = fImage->getTexture(); | 158 GrTexture* texture = fImage->getTexture(); |
| 83 if (texture) { | 159 if (texture) { |
| 84 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); | 160 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); |
| 85 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 161 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 86 | 162 |
| 87 return SkSpecialSurface::NewRenderTarget(texture->getContext(), desc ); | 163 return SkSpecialSurface::NewRenderTarget(texture->getContext(), desc ); |
| 88 } | 164 } |
| 89 #endif | 165 #endif |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 111 } | 187 } |
| 112 | 188 |
| 113 /////////////////////////////////////////////////////////////////////////////// | 189 /////////////////////////////////////////////////////////////////////////////// |
| 114 #include "SkBitmap.h" | 190 #include "SkBitmap.h" |
| 115 #include "SkImageInfo.h" | 191 #include "SkImageInfo.h" |
| 116 #include "SkPixelRef.h" | 192 #include "SkPixelRef.h" |
| 117 | 193 |
| 118 class SkSpecialImage_Raster : public SkSpecialImage_Base { | 194 class SkSpecialImage_Raster : public SkSpecialImage_Base { |
| 119 public: | 195 public: |
| 120 SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm) | 196 SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm) |
| 121 : INHERITED(subset) | 197 : INHERITED(subset, bm.getGenerationID()) |
| 122 , fBitmap(bm) { | 198 , fBitmap(bm) { |
| 123 if (bm.pixelRef()->isPreLocked()) { | 199 if (bm.pixelRef()->isPreLocked()) { |
| 124 // we only preemptively lock if there is no chance of triggering som ething expensive | 200 // we only preemptively lock if there is no chance of triggering som ething expensive |
| 125 // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already. | 201 // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already. |
| 126 fBitmap.lockPixels(); | 202 fBitmap.lockPixels(); |
| 127 } | 203 } |
| 128 } | 204 } |
| 129 | 205 |
| 130 ~SkSpecialImage_Raster() override { } | 206 ~SkSpecialImage_Raster() override { } |
| 131 | 207 |
| 208 bool isOpaque() const override { return fBitmap.isOpaque(); } | |
| 209 | |
| 210 size_t getSize() const override { return fBitmap.getSize(); } | |
| 211 | |
| 132 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { | 212 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
| 133 SkRect dst = SkRect::MakeXYWH(x, y, | 213 SkRect dst = SkRect::MakeXYWH(x, y, |
| 134 this->subset().width(), this->subset().hei ght()); | 214 this->subset().width(), this->subset().hei ght()); |
| 135 | 215 |
| 136 canvas->drawBitmapRect(fBitmap, this->subset(), | 216 canvas->drawBitmapRect(fBitmap, this->subset(), |
| 137 dst, paint, SkCanvas::kStrict_SrcRectConstraint); | 217 dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 138 } | 218 } |
| 139 | 219 |
| 140 bool onPeekPixels(SkPixmap* pixmap) const override { | 220 bool onPeekPixels(SkPixmap* pixmap) const override { |
| 141 const SkImageInfo info = fBitmap.info(); | 221 const SkImageInfo info = fBitmap.info(); |
| 142 if ((kUnknown_SkColorType == info.colorType()) || !fBitmap.getPixels()) { | 222 if ((kUnknown_SkColorType == info.colorType()) || !fBitmap.getPixels()) { |
| 143 return false; | 223 return false; |
| 144 } | 224 } |
| 145 const void* pixels = fBitmap.getPixels(); | 225 const void* pixels = fBitmap.getPixels(); |
| 146 if (pixels) { | 226 if (pixels) { |
| 147 if (pixmap) { | 227 if (pixmap) { |
| 148 pixmap->reset(info, pixels, fBitmap.rowBytes()); | 228 pixmap->reset(info, pixels, fBitmap.rowBytes()); |
| 149 } | 229 } |
| 150 return true; | 230 return true; |
| 151 } | 231 } |
| 152 return false; | 232 return false; |
| 153 } | 233 } |
| 154 | 234 |
| 235 bool getBitmap(SkBitmap* result) const override { | |
| 236 *result = fBitmap; | |
| 237 return true; | |
| 238 } | |
| 239 | |
| 155 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { | 240 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
| 156 return SkSpecialSurface::NewRaster(info, nullptr); | 241 return SkSpecialSurface::NewRaster(info, nullptr); |
| 157 } | 242 } |
| 158 | 243 |
| 159 private: | 244 private: |
| 160 SkBitmap fBitmap; | 245 SkBitmap fBitmap; |
| 161 | 246 |
| 162 typedef SkSpecialImage_Base INHERITED; | 247 typedef SkSpecialImage_Base INHERITED; |
| 163 }; | 248 }; |
| 164 | 249 |
| 165 SkSpecialImage* SkSpecialImage::NewFromRaster(const SkIRect& subset, const SkBit map& bm) { | 250 SkSpecialImage* SkSpecialImage::NewFromRaster(const SkIRect& subset, const SkBit map& bm) { |
| 166 SkASSERT(nullptr == bm.getTexture()); | 251 SkASSERT(nullptr == bm.getTexture()); |
| 167 SkASSERT(rect_fits(subset, bm.width(), bm.height())); | 252 SkASSERT(rect_fits(subset, bm.width(), bm.height())); |
| 168 return new SkSpecialImage_Raster(subset, bm); | 253 return new SkSpecialImage_Raster(subset, bm); |
| 169 } | 254 } |
| 170 | 255 |
| 171 #if SK_SUPPORT_GPU | 256 #if SK_SUPPORT_GPU |
| 172 /////////////////////////////////////////////////////////////////////////////// | 257 /////////////////////////////////////////////////////////////////////////////// |
| 173 #include "GrTexture.h" | 258 #include "GrTexture.h" |
| 174 | 259 |
| 175 class SkSpecialImage_Gpu : public SkSpecialImage_Base { | 260 class SkSpecialImage_Gpu : public SkSpecialImage_Base { |
| 176 public: | 261 public: |
| 177 SkSpecialImage_Gpu(const SkIRect& subset, GrTexture* tex) | 262 SkSpecialImage_Gpu(const SkIRect& subset, uint32_t uniqueID, GrTexture* tex, SkAlphaType at) |
| 178 : INHERITED(subset) | 263 : INHERITED(subset, uniqueID) |
| 179 , fTexture(SkRef(tex)) { | 264 , fTexture(SkRef(tex)) |
| 265 , fAlphaType(at) { | |
| 180 } | 266 } |
| 181 | 267 |
| 182 ~SkSpecialImage_Gpu() override { } | 268 ~SkSpecialImage_Gpu() override { } |
| 183 | 269 |
| 270 bool isOpaque() const override { | |
| 271 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaqu e_SkAlphaType; | |
| 272 } | |
| 273 | |
| 274 size_t getSize() const override { return fTexture->gpuMemorySize(); } | |
| 275 | |
| 184 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { | 276 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
| 185 SkRect dst = SkRect::MakeXYWH(x, y, | 277 SkRect dst = SkRect::MakeXYWH(x, y, |
| 186 this->subset().width(), this->subset().hei ght()); | 278 this->subset().width(), this->subset().hei ght()); |
| 187 | 279 |
| 188 SkBitmap bm; | 280 SkBitmap bm; |
| 189 | 281 |
| 190 static const bool kUnknownOpacity = false; | |
| 191 GrWrapTextureInBitmap(fTexture, | 282 GrWrapTextureInBitmap(fTexture, |
| 192 fTexture->width(), fTexture->height(), kUnknownOpa city, &bm); | 283 fTexture->width(), fTexture->height(), this->isOpa que(), &bm); |
| 193 | 284 |
| 194 canvas->drawBitmapRect(bm, this->subset(), | 285 canvas->drawBitmapRect(bm, this->subset(), |
| 195 dst, paint, SkCanvas::kStrict_SrcRectConstraint); | 286 dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 196 } | 287 } |
| 197 | 288 |
| 198 GrTexture* onPeekTexture() const override { return fTexture; } | 289 GrTexture* onPeekTexture() const override { return fTexture; } |
| 199 | 290 |
| 291 bool getBitmap(SkBitmap* result) const override { | |
| 292 const SkImageInfo info = GrMakeInfoFromTexture(fTexture, | |
| 293 this->width(), this->heig ht(), | |
| 294 this->isOpaque()); | |
| 295 if (!result->setInfo(info)) { | |
| 296 return false; | |
| 297 } | |
| 298 | |
| 299 result->setPixelRef(new SkGrPixelRef(info, fTexture))->unref(); | |
| 300 return true; | |
| 301 } | |
| 302 | |
| 200 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { | 303 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
| 201 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); | 304 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); |
| 202 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 305 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 203 | 306 |
| 204 return SkSpecialSurface::NewRenderTarget(fTexture->getContext(), desc); | 307 return SkSpecialSurface::NewRenderTarget(fTexture->getContext(), desc); |
| 205 } | 308 } |
| 206 | 309 |
| 207 private: | 310 private: |
| 208 SkAutoTUnref<GrTexture> fTexture; | 311 SkAutoTUnref<GrTexture> fTexture; |
| 312 const SkAlphaType fAlphaType; | |
| 209 | 313 |
| 210 typedef SkSpecialImage_Base INHERITED; | 314 typedef SkSpecialImage_Base INHERITED; |
| 211 }; | 315 }; |
| 212 | 316 |
| 213 SkSpecialImage* SkSpecialImage::NewFromGpu(const SkIRect& subset, GrTexture* tex ) { | 317 SkSpecialImage* SkSpecialImage::NewFromGpu(const SkIRect& subset, |
| 318 uint32_t uniqueID, | |
| 319 GrTexture* tex, | |
| 320 SkAlphaType at) { | |
| 214 SkASSERT(rect_fits(subset, tex->width(), tex->height())); | 321 SkASSERT(rect_fits(subset, tex->width(), tex->height())); |
| 215 return new SkSpecialImage_Gpu(subset, tex); | 322 return new SkSpecialImage_Gpu(subset, uniqueID, tex, at); |
| 216 } | 323 } |
| 217 | 324 |
| 218 #else | 325 #else |
| 219 | 326 |
| 220 SkSpecialImage* SkSpecialImage::NewFromGpu(const SkIRect& subset, GrTexture* tex ) { | 327 SkSpecialImage* SkSpecialImage::NewFromGpu(const SkIRect& subset, |
| 328 uint32_t uniqueID, | |
| 329 GrTexture* tex, | |
| 330 SkAlphaType at) { | |
| 221 return nullptr; | 331 return nullptr; |
| 222 } | 332 } |
| 223 | 333 |
| 224 #endif | 334 #endif |
| OLD | NEW |