| OLD | NEW | 
|    1 /* |    1 /* | 
|    2  * Copyright 2012 Google Inc. |    2  * Copyright 2012 Google Inc. | 
|    3  * |    3  * | 
|    4  * Use of this source code is governed by a BSD-style license that can be |    4  * Use of this source code is governed by a BSD-style license that can be | 
|    5  * found in the LICENSE file. |    5  * found in the LICENSE file. | 
|    6  */ |    6  */ | 
|    7  |    7  | 
|    8 #include "SkSurface_Base.h" |    8 #include "SkSurface_Base.h" | 
|    9 #include "SkImagePriv.h" |    9 #include "SkImagePriv.h" | 
|   10 #include "SkCanvas.h" |   10 #include "SkCanvas.h" | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
|   24  |   24  | 
|   25     SkCanvas* onNewCanvas() override; |   25     SkCanvas* onNewCanvas() override; | 
|   26     SkSurface* onNewSurface(const SkImageInfo&) override; |   26     SkSurface* onNewSurface(const SkImageInfo&) override; | 
|   27     SkImage* onNewImageSnapshot(Budgeted) override; |   27     SkImage* onNewImageSnapshot(Budgeted) override; | 
|   28     void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override; |   28     void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override; | 
|   29     void onCopyOnWrite(ContentChangeMode) override; |   29     void onCopyOnWrite(ContentChangeMode) override; | 
|   30     void onRestoreBackingMutability() override; |   30     void onRestoreBackingMutability() override; | 
|   31  |   31  | 
|   32 private: |   32 private: | 
|   33     SkBitmap    fBitmap; |   33     SkBitmap    fBitmap; | 
 |   34     size_t      fRowBytes; | 
|   34     bool        fWeOwnThePixels; |   35     bool        fWeOwnThePixels; | 
|   35  |   36  | 
|   36     typedef SkSurface_Base INHERITED; |   37     typedef SkSurface_Base INHERITED; | 
|   37 }; |   38 }; | 
|   38  |   39  | 
|   39 /////////////////////////////////////////////////////////////////////////////// |   40 /////////////////////////////////////////////////////////////////////////////// | 
|   40  |   41  | 
|   41 bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) { |   42 bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) { | 
|   42     if (info.isEmpty()) { |   43     if (info.isEmpty()) { | 
|   43         return false; |   44         return false; | 
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   81  |   82  | 
|   82     return true; |   83     return true; | 
|   83 } |   84 } | 
|   84  |   85  | 
|   85 SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t
      rb, |   86 SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t
      rb, | 
|   86                                    void (*releaseProc)(void* pixels, void* conte
     xt), void* context, |   87                                    void (*releaseProc)(void* pixels, void* conte
     xt), void* context, | 
|   87                                    const SkSurfaceProps* props) |   88                                    const SkSurfaceProps* props) | 
|   88     : INHERITED(info, props) |   89     : INHERITED(info, props) | 
|   89 { |   90 { | 
|   90     fBitmap.installPixels(info, pixels, rb, nullptr, releaseProc, context); |   91     fBitmap.installPixels(info, pixels, rb, nullptr, releaseProc, context); | 
 |   92     fRowBytes = 0;              // don't need to track the rowbytes | 
|   91     fWeOwnThePixels = false;    // We are "Direct" |   93     fWeOwnThePixels = false;    // We are "Direct" | 
|   92 } |   94 } | 
|   93  |   95  | 
|   94 SkSurface_Raster::SkSurface_Raster(SkPixelRef* pr, const SkSurfaceProps* props) |   96 SkSurface_Raster::SkSurface_Raster(SkPixelRef* pr, const SkSurfaceProps* props) | 
|   95     : INHERITED(pr->info().width(), pr->info().height(), props) |   97     : INHERITED(pr->info().width(), pr->info().height(), props) | 
|   96 { |   98 { | 
|   97     const SkImageInfo& info = pr->info(); |   99     const SkImageInfo& info = pr->info(); | 
|   98  |  100  | 
|   99     fBitmap.setInfo(info, info.minRowBytes()); |  101     fBitmap.setInfo(info, pr->rowBytes()); | 
|  100     fBitmap.setPixelRef(pr); |  102     fBitmap.setPixelRef(pr); | 
 |  103     fRowBytes = pr->rowBytes(); // we track this, so that subsequent re-allocs w
     ill match | 
|  101     fWeOwnThePixels = true; |  104     fWeOwnThePixels = true; | 
|  102 } |  105 } | 
|  103  |  106  | 
|  104 SkCanvas* SkSurface_Raster::onNewCanvas() { return new SkCanvas(fBitmap, this->p
     rops()); } |  107 SkCanvas* SkSurface_Raster::onNewCanvas() { return new SkCanvas(fBitmap, this->p
     rops()); } | 
|  105  |  108  | 
|  106 SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { |  109 SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { | 
|  107     return SkSurface::NewRaster(info, &this->props()); |  110     return SkSurface::NewRaster(info, &this->props()); | 
|  108 } |  111 } | 
|  109  |  112  | 
|  110 void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |  113 void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, | 
| (...skipping 21 matching lines...) Expand all  Loading... | 
|  132         pr->restoreMutability(); |  135         pr->restoreMutability(); | 
|  133     } |  136     } | 
|  134 } |  137 } | 
|  135  |  138  | 
|  136 void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) { |  139 void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) { | 
|  137     // are we sharing pixelrefs with the image? |  140     // are we sharing pixelrefs with the image? | 
|  138     SkASSERT(this->getCachedImage(kNo_Budgeted)); |  141     SkASSERT(this->getCachedImage(kNo_Budgeted)); | 
|  139     if (SkBitmapImageGetPixelRef(this->getCachedImage(kNo_Budgeted)) == fBitmap.
     pixelRef()) { |  142     if (SkBitmapImageGetPixelRef(this->getCachedImage(kNo_Budgeted)) == fBitmap.
     pixelRef()) { | 
|  140         SkASSERT(fWeOwnThePixels); |  143         SkASSERT(fWeOwnThePixels); | 
|  141         if (kDiscard_ContentChangeMode == mode) { |  144         if (kDiscard_ContentChangeMode == mode) { | 
|  142             fBitmap.setPixelRef(nullptr); |  | 
|  143             fBitmap.allocPixels(); |  145             fBitmap.allocPixels(); | 
|  144         } else { |  146         } else { | 
|  145             SkBitmap prev(fBitmap); |  147             SkBitmap prev(fBitmap); | 
|  146             prev.deepCopyTo(&fBitmap); |  148             fBitmap.allocPixels(); | 
 |  149             prev.lockPixels(); | 
 |  150             SkASSERT(prev.info() == fBitmap.info()); | 
 |  151             SkASSERT(prev.rowBytes() == fBitmap.rowBytes()); | 
 |  152             memcpy(fBitmap.getPixels(), prev.getPixels(), fBitmap.getSafeSize())
     ; | 
|  147         } |  153         } | 
 |  154         SkASSERT(fBitmap.rowBytes() == fRowBytes);  // be sure we always use the
      same value | 
 |  155  | 
|  148         // Now fBitmap is a deep copy of itself (and therefore different from |  156         // Now fBitmap is a deep copy of itself (and therefore different from | 
|  149         // what is being used by the image. Next we update the canvas to use |  157         // what is being used by the image. Next we update the canvas to use | 
|  150         // this as its backend, so we can't modify the image's pixels anymore. |  158         // this as its backend, so we can't modify the image's pixels anymore. | 
|  151         SkASSERT(this->getCachedCanvas()); |  159         SkASSERT(this->getCachedCanvas()); | 
|  152         this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurfa
     ce(fBitmap); |  160         this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurfa
     ce(fBitmap); | 
|  153     } |  161     } | 
|  154 } |  162 } | 
|  155  |  163  | 
|  156 /////////////////////////////////////////////////////////////////////////////// |  164 /////////////////////////////////////////////////////////////////////////////// | 
|  157  |  165  | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
|  169     } |  177     } | 
|  170  |  178  | 
|  171     return new SkSurface_Raster(info, pixels, rb, releaseProc, context, props); |  179     return new SkSurface_Raster(info, pixels, rb, releaseProc, context, props); | 
|  172 } |  180 } | 
|  173  |  181  | 
|  174 SkSurface* SkSurface::NewRasterDirect(const SkImageInfo& info, void* pixels, siz
     e_t rowBytes, |  182 SkSurface* SkSurface::NewRasterDirect(const SkImageInfo& info, void* pixels, siz
     e_t rowBytes, | 
|  175                                       const SkSurfaceProps* props) { |  183                                       const SkSurfaceProps* props) { | 
|  176     return NewRasterDirectReleaseProc(info, pixels, rowBytes, nullptr, nullptr, 
     props); |  184     return NewRasterDirectReleaseProc(info, pixels, rowBytes, nullptr, nullptr, 
     props); | 
|  177 } |  185 } | 
|  178  |  186  | 
|  179 SkSurface* SkSurface::NewRaster(const SkImageInfo& info, const SkSurfaceProps* p
     rops) { |  187 SkSurface* SkSurface::NewRaster(const SkImageInfo& info, size_t rowBytes, | 
 |  188                                 const SkSurfaceProps* props) { | 
|  180     if (!SkSurface_Raster::Valid(info)) { |  189     if (!SkSurface_Raster::Valid(info)) { | 
|  181         return nullptr; |  190         return nullptr; | 
|  182     } |  191     } | 
|  183  |  192  | 
|  184     SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewZeroed(info, 0, nullptr)); |  193     SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewZeroed(info, rowBytes, null
     ptr)); | 
|  185     if (nullptr == pr.get()) { |  194     if (nullptr == pr.get()) { | 
|  186         return nullptr; |  195         return nullptr; | 
|  187     } |  196     } | 
 |  197     if (rowBytes) { | 
 |  198         SkASSERT(pr->rowBytes() == rowBytes); | 
 |  199     } | 
|  188     return new SkSurface_Raster(pr, props); |  200     return new SkSurface_Raster(pr, props); | 
|  189 } |  201 } | 
 |  202  | 
 |  203 SkSurface* SkSurface::NewRaster(const SkImageInfo& info, const SkSurfaceProps* p
     rops) { | 
 |  204     return NewRaster(info, 0, props); | 
 |  205 } | 
| OLD | NEW |