• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SkAutoPixmapStorage_DEFINED
9 #define SkAutoPixmapStorage_DEFINED
10 
11 #include "include/core/SkPixmap.h"
12 #include "include/private/base/SkMalloc.h"
13 
14 class SkData;
15 
16 class SkAutoPixmapStorage : public SkPixmap {
17 public:
18     SkAutoPixmapStorage();
19     ~SkAutoPixmapStorage();
20 
21     SkAutoPixmapStorage(SkAutoPixmapStorage&& other);
22 
23     /**
24     * Leave the moved-from object in a free-but-valid state.
25     */
26     SkAutoPixmapStorage& operator=(SkAutoPixmapStorage&& other);
27 
28     /**
29     *  Try to allocate memory for the pixels needed to match the specified Info. On success
30     *  return true and fill out the pixmap to point to that memory. The storage will be freed
31     *  when this object is destroyed, or if another call to tryAlloc() or alloc() is made.
32     *
33     *  On failure, return false and reset() the pixmap to empty.
34     */
35     bool tryAlloc(const SkImageInfo&);
36 
37     /**
38     *  Allocate memory for the pixels needed to match the specified Info and fill out the pixmap
39     *  to point to that memory. The storage will be freed when this object is destroyed,
40     *  or if another call to tryAlloc() or alloc() is made.
41     *
42     *  If the memory cannot be allocated, calls SK_ABORT().
43     */
44     void alloc(const SkImageInfo&);
45 
46     /**
47     * Gets the size and optionally the rowBytes that would be allocated by SkAutoPixmapStorage if
48     * alloc/tryAlloc was called.
49     */
50     static size_t AllocSize(const SkImageInfo& info, size_t* rowBytes);
51 
52     /**
53     * Returns a void* of the allocated pixel memory and resets the pixmap. If the storage hasn't
54     * been allocated, the result is NULL. The caller is responsible for calling sk_free to free
55     * the returned memory.
56     */
57     void* SK_WARN_UNUSED_RESULT detachPixels();
58 
59     /**
60     *  Returns an SkData object wrapping the allocated pixels memory, and resets the pixmap.
61     *  If the storage hasn't been allocated, the result is NULL.
62     */
63     sk_sp<SkData> SK_WARN_UNUSED_RESULT detachPixelsAsData();
64 
65     // We wrap these so we can clear our internal storage
66 
reset()67     void reset() {
68         this->freeStorage();
69         this->INHERITED::reset();
70     }
reset(const SkImageInfo & info,const void * addr,size_t rb)71     void reset(const SkImageInfo& info, const void* addr, size_t rb) {
72         this->freeStorage();
73         this->INHERITED::reset(info, addr, rb);
74     }
75 
reset(const SkMask & mask)76     bool SK_WARN_UNUSED_RESULT reset(const SkMask& mask) {
77         this->freeStorage();
78         return this->INHERITED::reset(mask);
79     }
80 
81 private:
82     void*   fStorage;
83 
freeStorage()84     void freeStorage() {
85         sk_free(fStorage);
86         fStorage = nullptr;
87     }
88 
89     using INHERITED = SkPixmap;
90 };
91 
92 #endif
93