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" 12 #include "GrContext.h" 13 #include "GrTexture.h" 14 #include "SkGrPixelRef.h" 15 16 class SkImage_Gpu : public SkImage_Base { 17 public: 18 SK_DECLARE_INST_COUNT(SkImage_Gpu) 19 20 SkImage_Gpu(GrTexture*); 21 virtual ~SkImage_Gpu(); 22 23 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) SK_OVERRIDE; 24 25 GrTexture* getTexture() { return fTexture; } 26 27 void setTexture(GrTexture* texture); 28 29 private: 30 GrTexture* fTexture; 31 SkBitmap fBitmap; 32 33 typedef SkImage_Base INHERITED; 34 }; 35 36 SK_DEFINE_INST_COUNT(SkImage_Gpu) 37 38 /////////////////////////////////////////////////////////////////////////////// 39 40 SkImage_Gpu::SkImage_Gpu(GrTexture* texture) 41 : INHERITED(texture->width(), texture->height()) 42 , fTexture(texture) { 43 44 SkASSERT(NULL != fTexture); 45 fTexture->ref(); 46 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, fTexture->width(), fTexture->height()); 47 fBitmap.setPixelRef(new SkGrPixelRef(fTexture))->unref(); 48 } 49 50 SkImage_Gpu::~SkImage_Gpu() { 51 SkSafeUnref(fTexture); 52 } 53 54 void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, 55 const SkPaint* paint) { 56 canvas->drawBitmap(fBitmap, x, y, paint); 57 } 58 59 void SkImage_Gpu::setTexture(GrTexture* texture) { 60 61 if (texture == fTexture) { 62 return; 63 } 64 65 SkRefCnt_SafeAssign(fTexture, texture); 66 fBitmap.setPixelRef(new SkGrPixelRef(texture))->unref(); 67 } 68 69 /////////////////////////////////////////////////////////////////////////////// 70 71 SkImage* SkImage::NewTexture(GrTexture* texture) { 72 if (NULL == texture) { 73 return NULL; 74 } 75 76 return SkNEW_ARGS(SkImage_Gpu, (texture)); 77 } 78 79 GrTexture* SkTextureImageGetTexture(SkImage* image) { 80 return ((SkImage_Gpu*)image)->getTexture(); 81 } 82 83 void SkTextureImageSetTexture(SkImage* image, GrTexture* texture) { 84 ((SkImage_Gpu*)image)->setTexture(texture); 85 } 86