Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2016 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 "SkAutoPixmapStorage.h"
      9 #include "SkData.h"
     10 
     11 SkAutoPixmapStorage::SkAutoPixmapStorage() : fStorage(nullptr) {}
     12 
     13 SkAutoPixmapStorage::~SkAutoPixmapStorage() {
     14     this->freeStorage();
     15 }
     16 
     17 SkAutoPixmapStorage& SkAutoPixmapStorage::operator=(SkAutoPixmapStorage&& other) {
     18     this->fStorage = other.fStorage;
     19     this->INHERITED::reset(other.info(), this->fStorage, other.rowBytes());
     20 
     21     other.fStorage = nullptr;
     22     other.INHERITED::reset();
     23 
     24     return *this;
     25 }
     26 
     27 size_t SkAutoPixmapStorage::AllocSize(const SkImageInfo& info, size_t* rowBytes) {
     28     size_t rb = info.minRowBytes();
     29     if (rowBytes) {
     30         *rowBytes = rb;
     31     }
     32     return info.getSafeSize(rb);
     33 }
     34 
     35 bool SkAutoPixmapStorage::tryAlloc(const SkImageInfo& info) {
     36     this->freeStorage();
     37 
     38     size_t rb;
     39     size_t size = AllocSize(info, &rb);
     40     if (0 == size) {
     41         return false;
     42     }
     43     void* pixels = sk_malloc_flags(size, 0);
     44     if (nullptr == pixels) {
     45         return false;
     46     }
     47     this->reset(info, pixels, rb);
     48     fStorage = pixels;
     49     return true;
     50 }
     51 
     52 void SkAutoPixmapStorage::alloc(const SkImageInfo& info) {
     53     if (!this->tryAlloc(info)) {
     54         sk_throw();
     55     }
     56 }
     57 
     58 const SkData* SkAutoPixmapStorage::detachPixelsAsData() {
     59     if (!fStorage) {
     60         return nullptr;
     61     }
     62 
     63     auto data = SkData::MakeFromMalloc(fStorage, this->getSafeSize());
     64     fStorage = nullptr;
     65     this->INHERITED::reset();
     66 
     67     return data.release();
     68 }
     69