blob: 451ad072a6877b84a61b03553d60709067aa9b32 [file] [log] [blame]
reed@google.com5d4ba882012-07-31 15:45:27 +00001/*
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.com97b6b072012-10-31 14:48:39 +000012#include "GrContext.h"
13#include "GrTexture.h"
14#include "SkGrPixelRef.h"
reed@google.com5d4ba882012-07-31 15:45:27 +000015
16class SkImage_Gpu : public SkImage_Base {
17public:
robertphillips@google.com97b6b072012-10-31 14:48:39 +000018 SK_DECLARE_INST_COUNT(SkImage_Gpu)
reed@google.com5d4ba882012-07-31 15:45:27 +000019
robertphillips@google.com97b6b072012-10-31 14:48:39 +000020 SkImage_Gpu(GrTexture*);
reed@google.com5d4ba882012-07-31 15:45:27 +000021 virtual ~SkImage_Gpu();
22
robertphillips@google.com97b6b072012-10-31 14:48:39 +000023 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) SK_OVERRIDE;
junov@chromium.orgda904742013-05-01 22:38:16 +000024 virtual GrTexture* onGetTexture() SK_OVERRIDE;
robertphillips@google.com97b6b072012-10-31 14:48:39 +000025
26 GrTexture* getTexture() { return fTexture; }
27
28 void setTexture(GrTexture* texture);
reed@google.com5d4ba882012-07-31 15:45:27 +000029
30private:
robertphillips@google.com97b6b072012-10-31 14:48:39 +000031 GrTexture* fTexture;
reed@google.com5d4ba882012-07-31 15:45:27 +000032 SkBitmap fBitmap;
33
34 typedef SkImage_Base INHERITED;
35};
36
robertphillips@google.com97b6b072012-10-31 14:48:39 +000037SK_DEFINE_INST_COUNT(SkImage_Gpu)
38
reed@google.com5d4ba882012-07-31 15:45:27 +000039///////////////////////////////////////////////////////////////////////////////
40
robertphillips@google.com97b6b072012-10-31 14:48:39 +000041SkImage_Gpu::SkImage_Gpu(GrTexture* texture)
42 : INHERITED(texture->width(), texture->height())
43 , fTexture(texture) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000044
robertphillips@google.com97b6b072012-10-31 14:48:39 +000045 SkASSERT(NULL != fTexture);
46 fTexture->ref();
47 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, fTexture->width(), fTexture->height());
junov@chromium.orgc37589d2013-04-03 13:58:32 +000048 fBitmap.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (fTexture)))->unref();
reed@google.com5d4ba882012-07-31 15:45:27 +000049}
50
robertphillips@google.com97b6b072012-10-31 14:48:39 +000051SkImage_Gpu::~SkImage_Gpu() {
52 SkSafeUnref(fTexture);
53}
reed@google.com5d4ba882012-07-31 15:45:27 +000054
robertphillips@google.com97b6b072012-10-31 14:48:39 +000055void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
56 const SkPaint* paint) {
reed@google.com5d4ba882012-07-31 15:45:27 +000057 canvas->drawBitmap(fBitmap, x, y, paint);
58}
59
junov@chromium.orgda904742013-05-01 22:38:16 +000060GrTexture* SkImage_Gpu::onGetTexture() {
61 return fTexture;
62}
63
robertphillips@google.com97b6b072012-10-31 14:48:39 +000064void SkImage_Gpu::setTexture(GrTexture* texture) {
reed@google.com5d4ba882012-07-31 15:45:27 +000065
robertphillips@google.com97b6b072012-10-31 14:48:39 +000066 if (texture == fTexture) {
67 return;
reed@google.com5d4ba882012-07-31 15:45:27 +000068 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000069
robertphillips@google.com97b6b072012-10-31 14:48:39 +000070 SkRefCnt_SafeAssign(fTexture, texture);
71 fBitmap.setPixelRef(new SkGrPixelRef(texture))->unref();
reed@google.com5d4ba882012-07-31 15:45:27 +000072}
73
robertphillips@google.com97b6b072012-10-31 14:48:39 +000074///////////////////////////////////////////////////////////////////////////////
75
76SkImage* SkImage::NewTexture(GrTexture* texture) {
77 if (NULL == texture) {
78 return NULL;
79 }
80
81 return SkNEW_ARGS(SkImage_Gpu, (texture));
82}
83
84GrTexture* SkTextureImageGetTexture(SkImage* image) {
85 return ((SkImage_Gpu*)image)->getTexture();
86}
87
88void SkTextureImageSetTexture(SkImage* image, GrTexture* texture) {
89 ((SkImage_Gpu*)image)->setTexture(texture);
90}