reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 "SkImage_Base.h" |
| 9 | #include "SkImagePriv.h" |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkCanvas.h" |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 12 | #include "SkSurface.h" |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 13 | #include "GrContext.h" |
| 14 | #include "GrTexture.h" |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 15 | |
| 16 | class SkImage_Gpu : public SkImage_Base { |
| 17 | public: |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 18 | SK_DECLARE_INST_COUNT(SkImage_Gpu) |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 19 | |
reed | 3f10b9d | 2014-11-21 10:27:53 -0800 | [diff] [blame] | 20 | SkImage_Gpu(const SkBitmap&, int sampleCountForNewSurfaces); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 21 | |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 22 | void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const SK_OVERRIDE; |
| 23 | void onDrawRect(SkCanvas*, const SkRect* src, const SkRect& dst, |
| 24 | const SkPaint*) const SK_OVERRIDE; |
| 25 | SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const SK_OVERRIDE; |
| 26 | GrTexture* onGetTexture() const SK_OVERRIDE; |
| 27 | bool getROPixels(SkBitmap*) const SK_OVERRIDE; |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 28 | |
reed | 8572fc0 | 2014-08-11 13:03:55 -0700 | [diff] [blame] | 29 | GrTexture* getTexture() const { return fBitmap.getTexture(); } |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 30 | |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 31 | SkShader* onNewShader(SkShader::TileMode, |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 32 | SkShader::TileMode, |
| 33 | const SkMatrix* localMatrix) const SK_OVERRIDE; |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 34 | |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 35 | bool isOpaque() const SK_OVERRIDE; |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 36 | |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 37 | private: |
| 38 | SkBitmap fBitmap; |
reed | 3f10b9d | 2014-11-21 10:27:53 -0800 | [diff] [blame] | 39 | const int fSampleCountForNewSurfaces; // 0 if we don't know |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 40 | |
| 41 | typedef SkImage_Base INHERITED; |
| 42 | }; |
| 43 | |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | |
reed | 3f10b9d | 2014-11-21 10:27:53 -0800 | [diff] [blame] | 46 | SkImage_Gpu::SkImage_Gpu(const SkBitmap& bitmap, int sampleCountForNewSurfaces) |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 47 | : INHERITED(bitmap.width(), bitmap.height(), NULL) |
| 48 | , fBitmap(bitmap) |
reed | 3f10b9d | 2014-11-21 10:27:53 -0800 | [diff] [blame] | 49 | , fSampleCountForNewSurfaces(sampleCountForNewSurfaces) |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 50 | { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 51 | SkASSERT(fBitmap.getTexture()); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 52 | } |
| 53 | |
piotaixr | 76d5b47 | 2014-07-22 15:02:05 -0700 | [diff] [blame] | 54 | SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX, |
| 55 | SkShader::TileMode tileY, |
| 56 | const SkMatrix* localMatrix) const |
| 57 | { |
| 58 | return SkShader::CreateBitmapShader(fBitmap, tileX, tileY, localMatrix); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 59 | } |
| 60 | |
reed | 8572fc0 | 2014-08-11 13:03:55 -0700 | [diff] [blame] | 61 | void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const { |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 62 | canvas->drawBitmap(fBitmap, x, y, paint); |
| 63 | } |
| 64 | |
piotaixr | 5ceff91 | 2014-09-26 07:36:26 -0700 | [diff] [blame] | 65 | void SkImage_Gpu::onDrawRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst, |
reed | 8572fc0 | 2014-08-11 13:03:55 -0700 | [diff] [blame] | 66 | const SkPaint* paint) const { |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 67 | canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); |
| 68 | } |
| 69 | |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 70 | SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) const { |
| 71 | GrContext* ctx = this->getTexture()->getContext(); |
reed | 3f10b9d | 2014-11-21 10:27:53 -0800 | [diff] [blame] | 72 | return SkSurface::NewRenderTarget(ctx, info, fSampleCountForNewSurfaces, &props); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 73 | } |
| 74 | |
reed | 8572fc0 | 2014-08-11 13:03:55 -0700 | [diff] [blame] | 75 | GrTexture* SkImage_Gpu::onGetTexture() const { |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 76 | return fBitmap.getTexture(); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 77 | } |
| 78 | |
reed@google.com | ace1354 | 2014-02-06 22:00:58 +0000 | [diff] [blame] | 79 | bool SkImage_Gpu::getROPixels(SkBitmap* dst) const { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 80 | return fBitmap.copyTo(dst, kN32_SkColorType); |
reed@google.com | ace1354 | 2014-02-06 22:00:58 +0000 | [diff] [blame] | 81 | } |
| 82 | |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 83 | bool SkImage_Gpu::isOpaque() const { |
| 84 | return fBitmap.isOpaque(); |
| 85 | } |
| 86 | |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 87 | /////////////////////////////////////////////////////////////////////////////// |
| 88 | |
reed | 3f10b9d | 2014-11-21 10:27:53 -0800 | [diff] [blame] | 89 | SkImage* SkNewImageFromBitmapTexture(const SkBitmap& bitmap, int sampleCountForNewSurfaces) { |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 90 | if (NULL == bitmap.getTexture()) { |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 91 | return NULL; |
| 92 | } |
reed | 3f10b9d | 2014-11-21 10:27:53 -0800 | [diff] [blame] | 93 | return SkNEW_ARGS(SkImage_Gpu, (bitmap, sampleCountForNewSurfaces)); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 94 | } |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 95 | |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 96 | GrTexture* SkTextureImageGetTexture(SkImage* image) { |
| 97 | return ((SkImage_Gpu*)image)->getTexture(); |
| 98 | } |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 99 | |