| 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 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 20 | SkImage_Gpu(GrTexture*); |
| 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; |
| junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 24 | virtual GrTexture* onGetTexture() SK_OVERRIDE; |
| reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame^] | 25 | virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE { |
| 26 | // TODO |
| 27 | return false; |
| 28 | } |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 29 | |
| 30 | GrTexture* getTexture() { return fTexture; } |
| 31 | |
| 32 | void setTexture(GrTexture* texture); |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 33 | |
| 34 | private: |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 35 | GrTexture* fTexture; |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 36 | SkBitmap fBitmap; |
| 37 | |
| 38 | typedef SkImage_Base INHERITED; |
| 39 | }; |
| 40 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 41 | SK_DEFINE_INST_COUNT(SkImage_Gpu) |
| 42 | |
| 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(GrTexture* texture) |
| 46 | : INHERITED(texture->width(), texture->height()) |
| 47 | , fTexture(texture) { |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 48 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 49 | SkASSERT(NULL != fTexture); |
| 50 | fTexture->ref(); |
| 51 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, fTexture->width(), fTexture->height()); |
| junov@chromium.org | c37589d | 2013-04-03 13:58:32 +0000 | [diff] [blame] | 52 | fBitmap.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (fTexture)))->unref(); |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 55 | SkImage_Gpu::~SkImage_Gpu() { |
| 56 | SkSafeUnref(fTexture); |
| 57 | } |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 58 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 59 | void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 60 | const SkPaint* paint) { |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 61 | canvas->drawBitmap(fBitmap, x, y, paint); |
| 62 | } |
| 63 | |
| junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 64 | GrTexture* SkImage_Gpu::onGetTexture() { |
| 65 | return fTexture; |
| 66 | } |
| 67 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 68 | void SkImage_Gpu::setTexture(GrTexture* texture) { |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 69 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 70 | if (texture == fTexture) { |
| 71 | return; |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 72 | } |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 73 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 74 | SkRefCnt_SafeAssign(fTexture, texture); |
| 75 | fBitmap.setPixelRef(new SkGrPixelRef(texture))->unref(); |
| reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 78 | /////////////////////////////////////////////////////////////////////////////// |
| 79 | |
| 80 | SkImage* SkImage::NewTexture(GrTexture* texture) { |
| 81 | if (NULL == texture) { |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | return SkNEW_ARGS(SkImage_Gpu, (texture)); |
| 86 | } |
| 87 | |
| 88 | GrTexture* SkTextureImageGetTexture(SkImage* image) { |
| 89 | return ((SkImage_Gpu*)image)->getTexture(); |
| 90 | } |
| 91 | |
| 92 | void SkTextureImageSetTexture(SkImage* image, GrTexture* texture) { |
| 93 | ((SkImage_Gpu*)image)->setTexture(texture); |
| 94 | } |