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" |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 12 | #include "GrContext.h" |
| 13 | #include "GrTexture.h" |
| 14 | #include "SkGrPixelRef.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 | |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 20 | explicit SkImage_Gpu(const SkBitmap&); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 21 | virtual ~SkImage_Gpu(); |
| 22 | |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 23 | virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) SK_OVERRIDE; |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 24 | virtual void onDrawRectToRect(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*) SK_OVERRIDE; |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 25 | virtual GrTexture* onGetTexture() SK_OVERRIDE; |
reed@google.com | ace1354 | 2014-02-06 22:00:58 +0000 | [diff] [blame] | 26 | virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE; |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 27 | |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 28 | GrTexture* getTexture() { return fBitmap.getTexture(); } |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 29 | |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame^] | 30 | virtual SkShader* onNewShader(SkShader::TileMode, SkShader::TileMode) const SK_OVERRIDE; |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 31 | private: |
| 32 | SkBitmap fBitmap; |
| 33 | |
| 34 | typedef SkImage_Base INHERITED; |
| 35 | }; |
| 36 | |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 39 | SkImage_Gpu::SkImage_Gpu(const SkBitmap& bitmap) |
| 40 | : INHERITED(bitmap.width(), bitmap.height()) |
| 41 | , fBitmap(bitmap) { |
| 42 | SkASSERT(NULL != fBitmap.getTexture()); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 43 | } |
| 44 | |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 45 | SkImage_Gpu::~SkImage_Gpu() { |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 46 | } |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 47 | |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame^] | 48 | SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX, SkShader::TileMode tileY) const { |
| 49 | return SkShader::CreateBitmapShader(fBitmap, tileX, tileY, NULL); |
| 50 | } |
| 51 | |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 52 | void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 53 | const SkPaint* paint) { |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 54 | canvas->drawBitmap(fBitmap, x, y, paint); |
| 55 | } |
| 56 | |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 57 | void SkImage_Gpu::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst, |
| 58 | const SkPaint* paint) { |
| 59 | canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); |
| 60 | } |
| 61 | |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 62 | GrTexture* SkImage_Gpu::onGetTexture() { |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 63 | return fBitmap.getTexture(); |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 64 | } |
| 65 | |
reed@google.com | ace1354 | 2014-02-06 22:00:58 +0000 | [diff] [blame] | 66 | bool SkImage_Gpu::getROPixels(SkBitmap* dst) const { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 67 | return fBitmap.copyTo(dst, kN32_SkColorType); |
reed@google.com | ace1354 | 2014-02-06 22:00:58 +0000 | [diff] [blame] | 68 | } |
| 69 | |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 72 | SkImage* SkImage::NewTexture(const SkBitmap& bitmap) { |
| 73 | if (NULL == bitmap.getTexture()) { |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 74 | return NULL; |
| 75 | } |
| 76 | |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 77 | return SkNEW_ARGS(SkImage_Gpu, (bitmap)); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | GrTexture* SkTextureImageGetTexture(SkImage* image) { |
| 81 | return ((SkImage_Gpu*)image)->getTexture(); |
| 82 | } |