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