aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/image/SkImage_Gpu.cpp
blob: b6e4211d28fc422501ed3c79449a98ec393c5c82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkImage_Base.h"
#include "SkImagePriv.h"
#include "SkBitmap.h"
#include "SkCanvas.h"
#include "GrContext.h"
#include "GrTexture.h"
#include "SkGrPixelRef.h"

class SkImage_Gpu : public SkImage_Base {
public:
    SK_DECLARE_INST_COUNT(SkImage_Gpu)

    SkImage_Gpu(GrTexture*);
    virtual ~SkImage_Gpu();

    virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) SK_OVERRIDE;
    virtual GrTexture* onGetTexture() SK_OVERRIDE;
    virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE {
        // TODO
        return false;
    }

    GrTexture* getTexture() { return fTexture; }

    void setTexture(GrTexture* texture);

private:
    GrTexture*  fTexture;
    SkBitmap    fBitmap;

    typedef SkImage_Base INHERITED;
};

SK_DEFINE_INST_COUNT(SkImage_Gpu)

///////////////////////////////////////////////////////////////////////////////

SkImage_Gpu::SkImage_Gpu(GrTexture* texture)
    : INHERITED(texture->width(), texture->height())
    , fTexture(texture) {

    SkASSERT(NULL != fTexture);
    fTexture->ref();
    fBitmap.setConfig(SkBitmap::kARGB_8888_Config, fTexture->width(), fTexture->height());
    fBitmap.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (fTexture)))->unref();
}

SkImage_Gpu::~SkImage_Gpu() {
    SkSafeUnref(fTexture);
}

void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
                         const SkPaint* paint) {
    canvas->drawBitmap(fBitmap, x, y, paint);
}

GrTexture* SkImage_Gpu::onGetTexture() {
    return fTexture;
}

void SkImage_Gpu::setTexture(GrTexture* texture) {

    if (texture == fTexture) {
        return;
    }

    SkRefCnt_SafeAssign(fTexture, texture);
    fBitmap.setPixelRef(new SkGrPixelRef(texture))->unref();
}

///////////////////////////////////////////////////////////////////////////////

SkImage* SkImage::NewTexture(GrTexture* texture) {
    if (NULL == texture) {
        return NULL;
    }

    return SkNEW_ARGS(SkImage_Gpu, (texture));
}

GrTexture* SkTextureImageGetTexture(SkImage* image) {
    return ((SkImage_Gpu*)image)->getTexture();
}

void SkTextureImageSetTexture(SkImage* image, GrTexture* texture) {
    ((SkImage_Gpu*)image)->setTexture(texture);
}