blob: 28b972b90c52139b3481bb38987e2010ac511814 [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:
reed@google.com2bd8b812013-11-01 13:46:54 +000018 static bool Valid(const SkImageInfo&, size_t rb = kIgnoreRowBytesValue);
reed@google.comc9062042012-07-30 18:06:00 +000019
reed982542d2014-06-27 06:48:14 -070020 SkSurface_Raster(const SkImageInfo&, void*, size_t rb,
reed4a8126e2014-09-22 07:29:03 -070021 void (*releaseProc)(void* pixels, void* context), void* context,
22 const SkSurfaceProps*);
23 SkSurface_Raster(SkPixelRef*, const SkSurfaceProps*);
reed@google.comc9062042012-07-30 18:06:00 +000024
mtklein36352bf2015-03-25 18:17:31 -070025 SkCanvas* onNewCanvas() override;
26 SkSurface* onNewSurface(const SkImageInfo&) override;
27 SkImage* onNewImageSnapshot(Budgeted) override;
tfarina7831a4b2015-04-27 07:53:07 -070028 void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override;
mtklein36352bf2015-03-25 18:17:31 -070029 void onCopyOnWrite(ContentChangeMode) override;
reed@google.comc9062042012-07-30 18:06:00 +000030
31private:
32 SkBitmap fBitmap;
33 bool fWeOwnThePixels;
34
35 typedef SkSurface_Base INHERITED;
36};
37
38///////////////////////////////////////////////////////////////////////////////
39
reed@google.com2bd8b812013-11-01 13:46:54 +000040bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) {
reedb2497c22014-12-31 12:31:43 -080041 if (info.isEmpty()) {
42 return false;
43 }
44
reed@google.comc9062042012-07-30 18:06:00 +000045 static const size_t kMaxTotalSize = SK_MaxS32;
46
reed@google.comc9062042012-07-30 18:06:00 +000047 int shift = 0;
reede5ea5002014-09-03 11:54:58 -070048 switch (info.colorType()) {
reed@google.com1360c522014-01-08 21:25:26 +000049 case kAlpha_8_SkColorType:
reed@google.comc9062042012-07-30 18:06:00 +000050 shift = 0;
51 break;
reed@google.com1360c522014-01-08 21:25:26 +000052 case kRGB_565_SkColorType:
reed@google.comc9062042012-07-30 18:06:00 +000053 shift = 1;
54 break;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000055 case kN32_SkColorType:
reed@google.comc9062042012-07-30 18:06:00 +000056 shift = 2;
57 break;
58 default:
59 return false;
60 }
61
reed@google.comc9062042012-07-30 18:06:00 +000062 if (kIgnoreRowBytesValue == rowBytes) {
63 return true;
64 }
65
reede5ea5002014-09-03 11:54:58 -070066 uint64_t minRB = (uint64_t)info.width() << shift;
reed@google.comc9062042012-07-30 18:06:00 +000067 if (minRB > rowBytes) {
68 return false;
69 }
70
71 size_t alignedRowBytes = rowBytes >> shift << shift;
72 if (alignedRowBytes != rowBytes) {
73 return false;
74 }
75
reede5ea5002014-09-03 11:54:58 -070076 uint64_t size = sk_64_mul(info.height(), rowBytes);
reed@google.comc9062042012-07-30 18:06:00 +000077 if (size > kMaxTotalSize) {
78 return false;
79 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000080
reed@google.comc9062042012-07-30 18:06:00 +000081 return true;
82}
83
reed982542d2014-06-27 06:48:14 -070084SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t rb,
reed4a8126e2014-09-22 07:29:03 -070085 void (*releaseProc)(void* pixels, void* context), void* context,
86 const SkSurfaceProps* props)
87 : INHERITED(info, props)
reed@google.com1360c522014-01-08 21:25:26 +000088{
reed982542d2014-06-27 06:48:14 -070089 fBitmap.installPixels(info, pixels, rb, NULL, releaseProc, context);
reed@google.com97af1a62012-08-28 12:19:02 +000090 fWeOwnThePixels = false; // We are "Direct"
reed@google.comc9062042012-07-30 18:06:00 +000091}
92
reed4a8126e2014-09-22 07:29:03 -070093SkSurface_Raster::SkSurface_Raster(SkPixelRef* pr, const SkSurfaceProps* props)
94 : INHERITED(pr->info().width(), pr->info().height(), props)
reed@google.com1360c522014-01-08 21:25:26 +000095{
commit-bot@chromium.orge13af712014-01-13 20:39:14 +000096 const SkImageInfo& info = pr->info();
97
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000098 fBitmap.setInfo(info, info.minRowBytes());
commit-bot@chromium.orge13af712014-01-13 20:39:14 +000099 fBitmap.setPixelRef(pr);
reed@google.comc9062042012-07-30 18:06:00 +0000100 fWeOwnThePixels = true;
101
commit-bot@chromium.orge13af712014-01-13 20:39:14 +0000102 if (!info.isOpaque()) {
junov@google.comdbfac8a2012-12-06 21:47:40 +0000103 fBitmap.eraseColor(SK_ColorTRANSPARENT);
reed@google.comc9062042012-07-30 18:06:00 +0000104 }
105}
106
107SkCanvas* SkSurface_Raster::onNewCanvas() {
reed4a8126e2014-09-22 07:29:03 -0700108 return SkNEW_ARGS(SkCanvas, (fBitmap, this->props()));
reed@google.comc9062042012-07-30 18:06:00 +0000109}
110
reed@google.com2bd8b812013-11-01 13:46:54 +0000111SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) {
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000112 return SkSurface::NewRaster(info);
reed@google.comc9062042012-07-30 18:06:00 +0000113}
114
reed@google.comc9062042012-07-30 18:06:00 +0000115void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
116 const SkPaint* paint) {
117 canvas->drawBitmap(fBitmap, x, y, paint);
118}
119
bsalomoneaaaf0b2015-01-23 08:08:04 -0800120SkImage* SkSurface_Raster::onNewImageSnapshot(Budgeted) {
reed4af267b2014-11-21 08:46:37 -0800121 return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels, &this->props());
reed@google.com97af1a62012-08-28 12:19:02 +0000122}
123
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000124void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) {
reed@google.com97af1a62012-08-28 12:19:02 +0000125 // are we sharing pixelrefs with the image?
bsalomoneaaaf0b2015-01-23 08:08:04 -0800126 SkASSERT(this->getCachedImage(kNo_Budgeted));
127 if (SkBitmapImageGetPixelRef(this->getCachedImage(kNo_Budgeted)) == fBitmap.pixelRef()) {
reed@google.com97af1a62012-08-28 12:19:02 +0000128 SkASSERT(fWeOwnThePixels);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000129 if (kDiscard_ContentChangeMode == mode) {
reed@google.com672588b2014-01-08 15:42:01 +0000130 fBitmap.setPixelRef(NULL);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000131 fBitmap.allocPixels();
132 } else {
133 SkBitmap prev(fBitmap);
commit-bot@chromium.org6285f4f2014-02-20 19:08:07 +0000134 prev.deepCopyTo(&fBitmap);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000135 }
reed@google.com97af1a62012-08-28 12:19:02 +0000136 // 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.
bsalomon49f085d2014-09-05 13:34:00 -0700139 SkASSERT(this->getCachedCanvas());
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000140 this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap);
reed@google.com97af1a62012-08-28 12:19:02 +0000141 }
142}
143
reed@google.comc9062042012-07-30 18:06:00 +0000144///////////////////////////////////////////////////////////////////////////////
145
reed982542d2014-06-27 06:48:14 -0700146SkSurface* SkSurface::NewRasterDirectReleaseProc(const SkImageInfo& info, void* pixels, size_t rb,
147 void (*releaseProc)(void* pixels, void* context),
reed4a8126e2014-09-22 07:29:03 -0700148 void* context, const SkSurfaceProps* props) {
reed982542d2014-06-27 06:48:14 -0700149 if (NULL == releaseProc) {
150 context = NULL;
151 }
152 if (!SkSurface_Raster::Valid(info, rb)) {
reed@google.comc9062042012-07-30 18:06:00 +0000153 return NULL;
154 }
155 if (NULL == pixels) {
156 return NULL;
157 }
reed982542d2014-06-27 06:48:14 -0700158
reed4a8126e2014-09-22 07:29:03 -0700159 return SkNEW_ARGS(SkSurface_Raster, (info, pixels, rb, releaseProc, context, props));
reed982542d2014-06-27 06:48:14 -0700160}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000161
reed4a8126e2014-09-22 07:29:03 -0700162SkSurface* SkSurface::NewRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes,
163 const SkSurfaceProps* props) {
164 return NewRasterDirectReleaseProc(info, pixels, rowBytes, NULL, NULL, props);
reed@google.comc9062042012-07-30 18:06:00 +0000165}
166
reed4a8126e2014-09-22 07:29:03 -0700167SkSurface* SkSurface::NewRaster(const SkImageInfo& info, const SkSurfaceProps* props) {
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000168 if (!SkSurface_Raster::Valid(info)) {
reed@google.comc9062042012-07-30 18:06:00 +0000169 return NULL;
170 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000171
reed@google.combf790232013-12-13 19:45:58 +0000172 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, 0, NULL));
173 if (NULL == pr.get()) {
reed@google.comc9062042012-07-30 18:06:00 +0000174 return NULL;
175 }
reed4a8126e2014-09-22 07:29:03 -0700176 return SkNEW_ARGS(SkSurface_Raster, (pr, props));
reed@google.comc9062042012-07-30 18:06:00 +0000177}