Home | History | Annotate | Download | only in gpu
      1 /*
      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 "GrSurface.h"
      9 
     10 #include "SkBitmap.h"
     11 #include "SkGr.h"
     12 #include "SkImageEncoder.h"
     13 #include <stdio.h>
     14 
     15 SkImageInfo GrSurface::info() const {
     16     SkImageInfo info;
     17     if (!GrPixelConfig2ColorType(this->config(), &info.fColorType)) {
     18         sk_throw();
     19     }
     20     info.fWidth = this->width();
     21     info.fHeight = this->height();
     22     info.fAlphaType = kPremul_SkAlphaType;
     23     return info;
     24 }
     25 
     26 bool GrSurface::savePixels(const char* filename) {
     27     SkBitmap bm;
     28     if (!bm.allocPixels(SkImageInfo::MakeN32Premul(this->width(),
     29                                                    this->height()))) {
     30         return false;
     31     }
     32 
     33     bool result = readPixels(0, 0, this->width(), this->height(), kSkia8888_GrPixelConfig,
     34                              bm.getPixels());
     35     if (!result) {
     36         SkDebugf("------ failed to read pixels for %s\n", filename);
     37         return false;
     38     }
     39 
     40     // remove any previous version of this file
     41     remove(filename);
     42 
     43     if (!SkImageEncoder::EncodeFile(filename, bm, SkImageEncoder::kPNG_Type, 100)) {
     44         SkDebugf("------ failed to encode %s\n", filename);
     45         remove(filename);   // remove any partial file
     46         return false;
     47     }
     48 
     49     return true;
     50 }
     51