• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkSurface_Base.h"
9 #include "SkImagePriv.h"
10 #include "SkCanvas.h"
11 #include "SkGpuDevice.h"
12 
13 class SkSurface_Gpu : public SkSurface_Base {
14 public:
15     SK_DECLARE_INST_COUNT(SkSurface_Gpu)
16 
17     SkSurface_Gpu(GrContext*, const SkImage::Info&, int sampleCount);
18     SkSurface_Gpu(GrContext*, GrRenderTarget*);
19     virtual ~SkSurface_Gpu();
20 
21     virtual SkCanvas* onNewCanvas() SK_OVERRIDE;
22     virtual SkSurface* onNewSurface(const SkImage::Info&) SK_OVERRIDE;
23     virtual SkImage* onNewImageShapshot() SK_OVERRIDE;
24     virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y,
25                         const SkPaint*) SK_OVERRIDE;
26     virtual void onCopyOnWrite(SkImage*, SkCanvas*) SK_OVERRIDE;
27 
28 private:
29     SkGpuDevice* fDevice;
30 
31     typedef SkSurface_Base INHERITED;
32 };
33 
SK_DEFINE_INST_COUNT(SkSurface_Gpu)34 SK_DEFINE_INST_COUNT(SkSurface_Gpu)
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 SkSurface_Gpu::SkSurface_Gpu(GrContext* ctx, const SkImage::Info& info,
39                              int sampleCount)
40         : INHERITED(info.fWidth, info.fHeight) {
41     bool isOpaque;
42     SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
43 
44     fDevice = SkNEW_ARGS(SkGpuDevice, (ctx, config, info.fWidth, info.fHeight, sampleCount));
45 
46     if (!isOpaque) {
47         fDevice->clear(0x0);
48     }
49 }
50 
SkSurface_Gpu(GrContext * ctx,GrRenderTarget * renderTarget)51 SkSurface_Gpu::SkSurface_Gpu(GrContext* ctx, GrRenderTarget* renderTarget)
52         : INHERITED(renderTarget->width(), renderTarget->height()) {
53     fDevice = SkNEW_ARGS(SkGpuDevice, (ctx, renderTarget));
54 
55     if (kRGB_565_GrPixelConfig != renderTarget->config()) {
56         fDevice->clear(0x0);
57     }
58 }
59 
~SkSurface_Gpu()60 SkSurface_Gpu::~SkSurface_Gpu() {
61     SkSafeUnref(fDevice);
62 }
63 
onNewCanvas()64 SkCanvas* SkSurface_Gpu::onNewCanvas() {
65     return SkNEW_ARGS(SkCanvas, (fDevice));
66 }
67 
onNewSurface(const SkImage::Info & info)68 SkSurface* SkSurface_Gpu::onNewSurface(const SkImage::Info& info) {
69     GrRenderTarget* rt = (GrRenderTarget*) fDevice->accessRenderTarget();
70     int sampleCount = rt->numSamples();
71     return SkSurface::NewRenderTarget(fDevice->context(), info, sampleCount);
72 }
73 
onNewImageShapshot()74 SkImage* SkSurface_Gpu::onNewImageShapshot() {
75 
76     GrRenderTarget* rt = (GrRenderTarget*) fDevice->accessRenderTarget();
77 
78     return SkImage::NewTexture(rt->asTexture());
79 }
80 
onDraw(SkCanvas * canvas,SkScalar x,SkScalar y,const SkPaint * paint)81 void SkSurface_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
82                               const SkPaint* paint) {
83     canvas->drawBitmap(fDevice->accessBitmap(false), x, y, paint);
84 }
85 
86 // Copy the contents of the SkGpuDevice into a new texture and give that
87 // texture to the SkImage. Note that this flushes the SkGpuDevice but
88 // doesn't force an OpenGL flush.
onCopyOnWrite(SkImage * image,SkCanvas * canvas)89 void SkSurface_Gpu::onCopyOnWrite(SkImage* image, SkCanvas* canvas) {
90     GrRenderTarget* rt = (GrRenderTarget*) fDevice->accessRenderTarget();
91 
92     // are we sharing our render target with the image?
93     if (rt->asTexture() == SkTextureImageGetTexture(image)) {
94         GrTextureDesc desc;
95         // copyTexture requires a render target as the destination
96         desc.fFlags = kRenderTarget_GrTextureFlagBit;
97         desc.fWidth = fDevice->width();
98         desc.fHeight = fDevice->height();
99         desc.fConfig = SkBitmapConfig2GrPixelConfig(fDevice->config());
100         desc.fSampleCnt = 0;
101 
102         SkAutoTUnref<GrTexture> tex(fDevice->context()->createUncachedTexture(desc, NULL, 0));
103         if (NULL == tex) {
104             SkTextureImageSetTexture(image, NULL);
105             return;
106         }
107 
108         fDevice->context()->copyTexture(rt->asTexture(), tex->asRenderTarget());
109 
110         SkTextureImageSetTexture(image, tex);
111     }
112 }
113 
114 ///////////////////////////////////////////////////////////////////////////////
115 
NewRenderTargetDirect(GrContext * ctx,GrRenderTarget * target)116 SkSurface* SkSurface::NewRenderTargetDirect(GrContext* ctx,
117                                             GrRenderTarget* target) {
118     if (NULL == ctx || NULL == target) {
119         return NULL;
120     }
121 
122     return SkNEW_ARGS(SkSurface_Gpu, (ctx, target));
123 }
124 
NewRenderTarget(GrContext * ctx,const SkImage::Info & info,int sampleCount)125 SkSurface* SkSurface::NewRenderTarget(GrContext* ctx, const SkImage::Info& info, int sampleCount) {
126     if (NULL == ctx) {
127         return NULL;
128     }
129 
130     bool isOpaque;
131     SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
132 
133     GrTextureDesc desc;
134     desc.fFlags = kRenderTarget_GrTextureFlagBit;
135     desc.fWidth = info.fWidth;
136     desc.fHeight = info.fHeight;
137     desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
138     desc.fSampleCnt = sampleCount;
139 
140     SkAutoTUnref<GrTexture> tex(ctx->createUncachedTexture(desc, NULL, 0));
141     if (NULL == tex) {
142         return NULL;
143     }
144 
145     return SkNEW_ARGS(SkSurface_Gpu, (ctx, tex->asRenderTarget()));
146 }
147