blob: 8afab6b01cf7b4dc03e7e18f8a6b12b1bc23e83d [file] [log] [blame]
reed@google.comc9062042012-07-30 18:06:00 +00001/*
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"
reed@google.com97af1a62012-08-28 12:19:02 +000011#include "SkDevice.h"
reed@google.comc9062042012-07-30 18:06:00 +000012#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 SkImage::Info&, SkColorSpace*, size_t rb = kIgnoreRowBytesValue);
19
20 SkSurface_Raster(const SkImage::Info&, SkColorSpace*, void*, size_t rb);
21 SkSurface_Raster(const SkImage::Info&, SkColorSpace*, SkPixelRef*, size_t rb);
22
23 virtual SkCanvas* onNewCanvas() SK_OVERRIDE;
24 virtual SkSurface* onNewSurface(const SkImage::Info&, SkColorSpace*) SK_OVERRIDE;
25 virtual SkImage* onNewImageShapshot() SK_OVERRIDE;
26 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y,
27 const SkPaint*) SK_OVERRIDE;
reed@google.com97af1a62012-08-28 12:19:02 +000028 virtual void onCopyOnWrite(SkImage*, SkCanvas*) SK_OVERRIDE;
reed@google.comc9062042012-07-30 18:06:00 +000029
30private:
31 SkBitmap fBitmap;
32 bool fWeOwnThePixels;
33
34 typedef SkSurface_Base INHERITED;
35};
36
37///////////////////////////////////////////////////////////////////////////////
38
39bool SkSurface_Raster::Valid(const SkImage::Info& info, SkColorSpace* cs,
40 size_t rowBytes) {
41 static const size_t kMaxTotalSize = SK_MaxS32;
42
43 bool isOpaque;
44 SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
45
46 int shift = 0;
47 switch (config) {
48 case SkBitmap::kA8_Config:
49 shift = 0;
50 break;
51 case SkBitmap::kRGB_565_Config:
52 shift = 1;
53 break;
54 case SkBitmap::kARGB_8888_Config:
55 shift = 2;
56 break;
57 default:
58 return false;
59 }
60
61 // TODO: examine colorspace
62
63 if (kIgnoreRowBytesValue == rowBytes) {
64 return true;
65 }
66
67 uint64_t minRB = (uint64_t)info.fWidth << 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 = (uint64_t)info.fHeight * rowBytes;
78 if (size > kMaxTotalSize) {
79 return false;
80 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000081
reed@google.comc9062042012-07-30 18:06:00 +000082 return true;
83}
84
85SkSurface_Raster::SkSurface_Raster(const SkImage::Info& info, SkColorSpace* cs,
86 void* pixels, size_t rb)
87 : INHERITED(info.fWidth, info.fHeight) {
88 bool isOpaque;
89 SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000090
reed@google.comc9062042012-07-30 18:06:00 +000091 fBitmap.setConfig(config, info.fWidth, info.fHeight, rb);
92 fBitmap.setPixels(pixels);
93 fBitmap.setIsOpaque(isOpaque);
reed@google.com97af1a62012-08-28 12:19:02 +000094 fWeOwnThePixels = false; // We are "Direct"
reed@google.comc9062042012-07-30 18:06:00 +000095}
96
97SkSurface_Raster::SkSurface_Raster(const SkImage::Info& info, SkColorSpace* cs,
98 SkPixelRef* pr, size_t rb)
99 : INHERITED(info.fWidth, info.fHeight) {
100 bool isOpaque;
101 SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
102
103 fBitmap.setConfig(config, info.fWidth, info.fHeight, rb);
104 fBitmap.setPixelRef(pr);
105 fBitmap.setIsOpaque(isOpaque);
106 fWeOwnThePixels = true;
107
108 if (!isOpaque) {
109 fBitmap.eraseColor(0);
110 }
111}
112
113SkCanvas* SkSurface_Raster::onNewCanvas() {
114 return SkNEW_ARGS(SkCanvas, (fBitmap));
115}
116
117SkSurface* SkSurface_Raster::onNewSurface(const SkImage::Info& info,
118 SkColorSpace* cs) {
119 return SkSurface::NewRaster(info, cs);
120}
121
reed@google.comc9062042012-07-30 18:06:00 +0000122void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
123 const SkPaint* paint) {
124 canvas->drawBitmap(fBitmap, x, y, paint);
125}
126
reed@google.com97af1a62012-08-28 12:19:02 +0000127SkImage* SkSurface_Raster::onNewImageShapshot() {
128 return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels);
129}
130
131void SkSurface_Raster::onCopyOnWrite(SkImage* image, SkCanvas* canvas) {
132 // are we sharing pixelrefs with the image?
133 if (SkBitmapImageGetPixelRef(image) == fBitmap.pixelRef()) {
134 SkASSERT(fWeOwnThePixels);
135 SkBitmap prev(fBitmap);
136 prev.deepCopyTo(&fBitmap, prev.config());
137 // Now fBitmap is a deep copy of itself (and therefore different from
138 // what is being used by the image. Next we update the canvas to use
139 // this as its backend, so we can't modify the image's pixels anymore.
140 canvas->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap);
141 }
142}
143
reed@google.comc9062042012-07-30 18:06:00 +0000144///////////////////////////////////////////////////////////////////////////////
145
146SkSurface* SkSurface::NewRasterDirect(const SkImage::Info& info,
147 SkColorSpace* cs,
148 void* pixels, size_t rowBytes) {
149 if (!SkSurface_Raster::Valid(info, cs, rowBytes)) {
150 return NULL;
151 }
152 if (NULL == pixels) {
153 return NULL;
154 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000155
reed@google.comc9062042012-07-30 18:06:00 +0000156 return SkNEW_ARGS(SkSurface_Raster, (info, cs, pixels, rowBytes));
157}
158
159SkSurface* SkSurface::NewRaster(const SkImage::Info& info, SkColorSpace* cs) {
160 if (!SkSurface_Raster::Valid(info, cs)) {
161 return NULL;
162 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000163
reed@google.comc9062042012-07-30 18:06:00 +0000164 static const size_t kMaxTotalSize = SK_MaxS32;
165 size_t rowBytes = SkImageMinRowBytes(info);
166 uint64_t size64 = (uint64_t)info.fHeight * rowBytes;
167 if (size64 > kMaxTotalSize) {
168 return NULL;
169 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000170
reed@google.comc9062042012-07-30 18:06:00 +0000171 size_t size = (size_t)size64;
172 void* pixels = sk_malloc_throw(size);
173 if (NULL == pixels) {
174 return NULL;
175 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000176
reed@google.comc9062042012-07-30 18:06:00 +0000177 SkAutoTUnref<SkPixelRef> pr(SkNEW_ARGS(SkMallocPixelRef, (pixels, size, NULL, true)));
178 return SkNEW_ARGS(SkSurface_Raster, (info, cs, pr, rowBytes));
179}
180