blob: 875f933b9c1f6faa530cba41103bc8b632b4492f [file] [log] [blame]
reed@google.comf6627b72012-07-27 18:02:50 +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 "SkDataPixelRef.h"
9#include "SkData.h"
reed@google.com9ddef9b2012-08-07 16:10:42 +000010#include "SkFlattenableBuffers.h"
reed@google.comf6627b72012-07-27 18:02:50 +000011
reed@google.com6965a0a2013-12-11 18:04:56 +000012SkDataPixelRef::SkDataPixelRef(const SkImageInfo& info,
13 SkData* data, size_t rowBytes)
14 : INHERITED(info)
15 , fData(data)
16 , fRB(rowBytes)
17{
reed@google.comf6627b72012-07-27 18:02:50 +000018 fData->ref();
reed@google.com6965a0a2013-12-11 18:04:56 +000019 this->setPreLocked(const_cast<void*>(fData->data()), rowBytes, NULL);
reed@google.comf6627b72012-07-27 18:02:50 +000020}
21
22SkDataPixelRef::~SkDataPixelRef() {
23 fData->unref();
24}
25
reed@google.com6965a0a2013-12-11 18:04:56 +000026bool SkDataPixelRef::onNewLockPixels(LockRec* rec) {
27 rec->fPixels = const_cast<void*>(fData->data());
28 rec->fColorTable = NULL;
29 rec->fRowBytes = fRB;
30 return true;
reed@google.comf6627b72012-07-27 18:02:50 +000031}
32
33void SkDataPixelRef::onUnlockPixels() {
34 // nothing to do
35}
36
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +000037size_t SkDataPixelRef::getAllocatedSizeInBytes() const {
38 return fData ? fData->size() : 0;
39}
40
reed@google.comf6627b72012-07-27 18:02:50 +000041void SkDataPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const {
42 this->INHERITED::flatten(buffer);
reed@google.com6965a0a2013-12-11 18:04:56 +000043
reed@google.comda300552013-10-14 14:33:11 +000044 buffer.writeDataAsByteArray(fData);
reed@google.com6965a0a2013-12-11 18:04:56 +000045 buffer.write32(fRB);
reed@google.comf6627b72012-07-27 18:02:50 +000046}
47
48SkDataPixelRef::SkDataPixelRef(SkFlattenableReadBuffer& buffer)
reed@google.com6965a0a2013-12-11 18:04:56 +000049 : INHERITED(buffer, NULL)
50{
reed@google.comda300552013-10-14 14:33:11 +000051 fData = buffer.readByteArrayAsData();
reed@google.com6965a0a2013-12-11 18:04:56 +000052 fRB = buffer.read32();
53 this->setPreLocked(const_cast<void*>(fData->data()), fRB, NULL);
reed@google.comf6627b72012-07-27 18:02:50 +000054}