SkSurface_Raster.cpp revision 02d91d187324cd0f014a78eab1ca09ec71a9b356
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 "SkDevice.h"
12#include "SkMallocPixelRef.h"
13
14static const size_t kIgnoreRowBytesValue = (size_t)~0;
15
16class SkSurface_Raster : public SkSurface_Base {
17public:
18    static bool Valid(const SkImageInfo&, size_t rb = kIgnoreRowBytesValue);
19
20    SkSurface_Raster(const SkImageInfo&, void*, size_t rb,
21                     void (*releaseProc)(void* pixels, void* context), void* context,
22                     const SkSurfaceProps*);
23    SkSurface_Raster(SkPixelRef*, const SkSurfaceProps*);
24
25    SkCanvas* onNewCanvas() override;
26    SkSurface* onNewSurface(const SkImageInfo&) override;
27    SkImage* onNewImageSnapshot(Budgeted) override;
28    void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override;
29    void onCopyOnWrite(ContentChangeMode) override;
30    void onRestoreBackingMutability() override;
31
32private:
33    SkBitmap    fBitmap;
34    bool        fWeOwnThePixels;
35
36    typedef SkSurface_Base INHERITED;
37};
38
39///////////////////////////////////////////////////////////////////////////////
40
41bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) {
42    if (info.isEmpty()) {
43        return false;
44    }
45
46    static const size_t kMaxTotalSize = SK_MaxS32;
47
48    int shift = 0;
49    switch (info.colorType()) {
50        case kAlpha_8_SkColorType:
51            shift = 0;
52            break;
53        case kRGB_565_SkColorType:
54            shift = 1;
55            break;
56        case kN32_SkColorType:
57            shift = 2;
58            break;
59        default:
60            return false;
61    }
62
63    if (kIgnoreRowBytesValue == rowBytes) {
64        return true;
65    }
66
67    uint64_t minRB = (uint64_t)info.width() << shift;
68    if (minRB > rowBytes) {
69        return false;
70    }
71
72    size_t alignedRowBytes = rowBytes >> shift << shift;
73    if (alignedRowBytes != rowBytes) {
74        return false;
75    }
76
77    uint64_t size = sk_64_mul(info.height(), rowBytes);
78    if (size > kMaxTotalSize) {
79        return false;
80    }
81
82    return true;
83}
84
85SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t rb,
86                                   void (*releaseProc)(void* pixels, void* context), void* context,
87                                   const SkSurfaceProps* props)
88    : INHERITED(info, props)
89{
90    fBitmap.installPixels(info, pixels, rb, NULL, releaseProc, context);
91    fWeOwnThePixels = false;    // We are "Direct"
92}
93
94SkSurface_Raster::SkSurface_Raster(SkPixelRef* pr, const SkSurfaceProps* props)
95    : INHERITED(pr->info().width(), pr->info().height(), props)
96{
97    const SkImageInfo& info = pr->info();
98
99    fBitmap.setInfo(info, info.minRowBytes());
100    fBitmap.setPixelRef(pr);
101    fWeOwnThePixels = true;
102
103    if (!info.isOpaque()) {
104        fBitmap.eraseColor(SK_ColorTRANSPARENT);
105    }
106}
107
108SkCanvas* SkSurface_Raster::onNewCanvas() {
109    return SkNEW_ARGS(SkCanvas, (fBitmap, this->props()));
110}
111
112SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) {
113    return SkSurface::NewRaster(info, &this->props());
114}
115
116void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
117                              const SkPaint* paint) {
118    canvas->drawBitmap(fBitmap, x, y, paint);
119}
120
121SkImage* SkSurface_Raster::onNewImageSnapshot(Budgeted) {
122    if (fWeOwnThePixels) {
123        // SkImage_raster requires these pixels are immutable for its full lifetime.
124        // We'll undo this via onRestoreBackingMutability() if we can avoid the COW.
125        if (SkPixelRef* pr = fBitmap.pixelRef()) {
126            pr->setTemporarilyImmutable();
127        }
128    }
129    // Our pixels are in memory, so read access on the snapshot SkImage could be cheap.
130    // Lock the shared pixel ref to ensure peekPixels() is usable.
131    return SkNewImageFromRasterBitmap(fBitmap, &this->props(),
132                                      fWeOwnThePixels ? kNo_ForceCopyMode : kYes_ForceCopyMode);
133}
134
135void SkSurface_Raster::onRestoreBackingMutability() {
136    SkASSERT(!this->hasCachedImage());  // Shouldn't be any snapshots out there.
137    if (SkPixelRef* pr = fBitmap.pixelRef()) {
138        pr->restoreMutability();
139    }
140}
141
142void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) {
143    // are we sharing pixelrefs with the image?
144    SkASSERT(this->getCachedImage(kNo_Budgeted));
145    if (SkBitmapImageGetPixelRef(this->getCachedImage(kNo_Budgeted)) == fBitmap.pixelRef()) {
146        SkASSERT(fWeOwnThePixels);
147        if (kDiscard_ContentChangeMode == mode) {
148            fBitmap.setPixelRef(NULL);
149            fBitmap.allocPixels();
150        } else {
151            SkBitmap prev(fBitmap);
152            prev.deepCopyTo(&fBitmap);
153        }
154        // Now fBitmap is a deep copy of itself (and therefore different from
155        // what is being used by the image. Next we update the canvas to use
156        // this as its backend, so we can't modify the image's pixels anymore.
157        SkASSERT(this->getCachedCanvas());
158        this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap);
159    }
160}
161
162///////////////////////////////////////////////////////////////////////////////
163
164SkSurface* SkSurface::NewRasterDirectReleaseProc(const SkImageInfo& info, void* pixels, size_t rb,
165                                                 void (*releaseProc)(void* pixels, void* context),
166                                                 void* context, const SkSurfaceProps* props) {
167    if (NULL == releaseProc) {
168        context = NULL;
169    }
170    if (!SkSurface_Raster::Valid(info, rb)) {
171        return NULL;
172    }
173    if (NULL == pixels) {
174        return NULL;
175    }
176
177    return SkNEW_ARGS(SkSurface_Raster, (info, pixels, rb, releaseProc, context, props));
178}
179
180SkSurface* SkSurface::NewRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes,
181                                      const SkSurfaceProps* props) {
182    return NewRasterDirectReleaseProc(info, pixels, rowBytes, NULL, NULL, props);
183}
184
185SkSurface* SkSurface::NewRaster(const SkImageInfo& info, const SkSurfaceProps* props) {
186    if (!SkSurface_Raster::Valid(info)) {
187        return NULL;
188    }
189
190    SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, 0, NULL));
191    if (NULL == pr.get()) {
192        return NULL;
193    }
194    return SkNEW_ARGS(SkSurface_Raster, (pr, props));
195}
196