blob: a07fe67ae458dabd45e61305753dcf4cf9862aca [file] [log] [blame]
robertphillips@google.com7d501ab2012-06-21 21:09:06 +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 "GrSurface.h"
9
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000010#include "SkBitmap.h"
reed@google.combf790232013-12-13 19:45:58 +000011#include "SkGr.h"
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000012#include "SkImageEncoder.h"
bungeman@google.comfab44db2013-10-11 18:50:45 +000013#include <stdio.h>
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000014
reed6c225732014-06-09 19:52:07 -070015SkImageInfo GrSurface::info() const {
16 SkImageInfo info;
17 if (!GrPixelConfig2ColorType(this->config(), &info.fColorType)) {
reed@google.combf790232013-12-13 19:45:58 +000018 sk_throw();
19 }
reed6c225732014-06-09 19:52:07 -070020 info.fWidth = this->width();
21 info.fHeight = this->height();
22 info.fAlphaType = kPremul_SkAlphaType;
23 return info;
reed@google.combf790232013-12-13 19:45:58 +000024}
25
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000026bool GrSurface::savePixels(const char* filename) {
27 SkBitmap bm;
reed@google.com9ebcac52014-01-24 18:53:42 +000028 if (!bm.allocPixels(SkImageInfo::MakeN32Premul(this->width(),
29 this->height()))) {
30 return false;
31 }
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000032
skia.committer@gmail.com1d3bfdc2013-10-01 07:01:46 +000033 bool result = readPixels(0, 0, this->width(), this->height(), kSkia8888_GrPixelConfig,
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000034 bm.getPixels());
35 if (!result) {
36 SkDebugf("------ failed to read pixels for %s\n", filename);
37 return false;
38 }
skia.committer@gmail.com1d3bfdc2013-10-01 07:01:46 +000039
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000040 // 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}