OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkSurface_Gpu.h" | 8 #include "SkSurface_Gpu.h" |
9 | 9 |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkGpuDevice.h" | 11 #include "SkGpuDevice.h" |
12 #include "SkImage_Base.h" | 12 #include "SkImage_Base.h" |
13 #include "SkImagePriv.h" | 13 #include "SkImagePriv.h" |
14 #include "SkSurface_Base.h" | 14 #include "SkSurface_Base.h" |
15 | 15 |
16 #if SK_SUPPORT_GPU | 16 #if SK_SUPPORT_GPU |
17 | 17 |
18 /////////////////////////////////////////////////////////////////////////////// | 18 /////////////////////////////////////////////////////////////////////////////// |
19 | 19 |
20 SkSurface_Gpu::SkSurface_Gpu(SkGpuDevice* device) | 20 SkSurface_Gpu::SkSurface_Gpu(SkGpuDevice* device) |
21 : INHERITED(device->width(), device->height(), &device->surfaceProps()) | 21 : INHERITED(device->width(), device->height(), &device->surfaceProps()) |
22 , fDevice(SkRef(device)) { | 22 , fDevice(SkRef(device)) { |
| 23 fDevice->setSurface(this); |
| 24 fGenerationID = 0; |
23 } | 25 } |
24 | 26 |
25 SkSurface_Gpu::~SkSurface_Gpu() { | 27 SkSurface_Gpu::~SkSurface_Gpu() { |
| 28 // In case the device outlives this instance, make sure device does not try
to access this. |
| 29 fDevice->setSurface(NULL); |
26 fDevice->unref(); | 30 fDevice->unref(); |
27 } | 31 } |
28 | 32 |
| 33 uint32_t SkSurface_Gpu::generationID() { |
| 34 if (0 == fGenerationID) { |
| 35 fGenerationID = this->newGenerationID(); |
| 36 } |
| 37 return fGenerationID; |
| 38 } |
| 39 |
29 SkCanvas* SkSurface_Gpu::onNewCanvas() { | 40 SkCanvas* SkSurface_Gpu::onNewCanvas() { |
30 SkCanvas::InitFlags flags = SkCanvas::kDefault_InitFlags; | 41 SkCanvas::InitFlags flags = SkCanvas::kDefault_InitFlags; |
31 // When we think this works... | 42 // When we think this works... |
32 // flags |= SkCanvas::kConservativeRasterClip_InitFlag; | 43 // flags |= SkCanvas::kConservativeRasterClip_InitFlag; |
33 | 44 |
34 return SkNEW_ARGS(SkCanvas, (fDevice, &this->props(), flags)); | 45 return SkNEW_ARGS(SkCanvas, (fDevice, &this->props(), flags)); |
35 } | 46 } |
36 | 47 |
37 SkSurface* SkSurface_Gpu::onNewSurface(const SkImageInfo& info) { | 48 SkSurface* SkSurface_Gpu::onNewSurface(const SkImageInfo& info) { |
38 GrRenderTarget* rt = fDevice->accessRenderTarget(); | 49 int sampleCount = fDevice->getBackendSampleCnt(); |
39 int sampleCount = rt->numSamples(); | |
40 // TODO: Make caller specify this (change virtual signature of onNewSurface)
. | 50 // TODO: Make caller specify this (change virtual signature of onNewSurface)
. |
41 static const Budgeted kBudgeted = kNo_Budgeted; | 51 static const Budgeted kBudgeted = kNo_Budgeted; |
42 return SkSurface::NewRenderTarget(fDevice->context(), kBudgeted, info, sampl
eCount, | 52 return SkSurface::NewRenderTarget(fDevice->context(), kBudgeted, info, sampl
eCount, |
43 &this->props()); | 53 &this->props()); |
44 } | 54 } |
45 | 55 |
46 SkImage* SkSurface_Gpu::onNewImageSnapshot(Budgeted budgeted) { | 56 SkImage* SkSurface_Gpu::onNewImageSnapshot(Budgeted budgeted) { |
47 const int sampleCount = fDevice->accessRenderTarget()->numSamples(); | 57 return fDevice->newImageSnapshot(&this->props(), budgeted); |
48 SkImage* image = SkNewImageFromBitmapTexture(fDevice->accessBitmap(false), s
ampleCount, | |
49 budgeted); | |
50 if (image) { | |
51 as_IB(image)->initWithProps(this->props()); | |
52 } | |
53 return image; | |
54 } | 58 } |
55 | 59 |
56 void SkSurface_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, | 60 bool SkSurface_Gpu::isBackendBudgeted() const { |
57 const SkPaint* paint) { | 61 return fDevice->isBackendBudgeted(); |
58 canvas->drawBitmap(fDevice->accessBitmap(false), x, y, paint); | |
59 } | |
60 | |
61 // Create a new render target and, if necessary, copy the contents of the old | |
62 // render target into it. Note that this flushes the SkGpuDevice but | |
63 // doesn't force an OpenGL flush. | |
64 void SkSurface_Gpu::onCopyOnWrite(ContentChangeMode mode) { | |
65 GrRenderTarget* rt = fDevice->accessRenderTarget(); | |
66 // are we sharing our render target with the image? Note this call should ne
ver create a new | |
67 // image because onCopyOnWrite is only called when there is a cached image. | |
68 SkImage* image = this->getCachedImage(kNo_Budgeted); | |
69 SkASSERT(image); | |
70 if (rt->asTexture() == SkTextureImageGetTexture(image)) { | |
71 this->fDevice->replaceRenderTarget(SkSurface::kRetain_ContentChangeMode
== mode); | |
72 SkTextureImageApplyBudgetedDecision(image); | |
73 } else if (kDiscard_ContentChangeMode == mode) { | |
74 this->SkSurface_Gpu::onDiscard(); | |
75 } | |
76 } | |
77 | |
78 void SkSurface_Gpu::onDiscard() { | |
79 fDevice->accessRenderTarget()->discard(); | |
80 } | 62 } |
81 | 63 |
82 /////////////////////////////////////////////////////////////////////////////// | 64 /////////////////////////////////////////////////////////////////////////////// |
83 | 65 |
84 SkSurface* SkSurface::NewRenderTargetDirect(GrRenderTarget* target, const SkSurf
aceProps* props) { | 66 SkSurface* SkSurface::NewRenderTargetDirect(GrRenderTarget* target, const SkSurf
aceProps* props) { |
85 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(target, props)); | 67 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(target, props)); |
86 if (!device) { | 68 if (!device) { |
87 return NULL; | 69 return NULL; |
88 } | 70 } |
89 return SkNEW_ARGS(SkSurface_Gpu, (device)); | 71 return SkNEW_ARGS(SkSurface_Gpu, (device)); |
90 } | 72 } |
91 | 73 |
92 SkSurface* SkSurface::NewRenderTarget(GrContext* ctx, Budgeted budgeted, const S
kImageInfo& info, | 74 SkSurface* SkSurface::NewRenderTarget(GrContext* ctx, Budgeted budgeted, const S
kImageInfo& info, |
93 int sampleCount, const SkSurfaceProps* pro
ps) { | 75 int sampleCount, const SkSurfaceProps* pro
ps) { |
94 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(ctx, budgeted, info, sa
mpleCount, props, | 76 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(ctx, budgeted, info, sa
mpleCount, props, |
95 SkGpuDevice::kNeedClear
_Flag)); | 77 SkGpuDevice::kNeedClear
_Flag)); |
96 if (!device) { | 78 if (!device) { |
97 return NULL; | 79 return NULL; |
98 } | 80 } |
99 return SkNEW_ARGS(SkSurface_Gpu, (device)); | 81 return SkNEW_ARGS(SkSurface_Gpu, (device)); |
100 } | 82 } |
101 | 83 |
102 #endif | 84 #endif |
OLD | NEW |